Skip to content

Fix XSS risk: replace direct template rendering with render_to_string in export view - #184

Merged
mpast merged 1 commit into
mainfrom
semgrep-autofix/1788859588
Sep 8, 2026
Merged

mpast merged 1 commit into
mainfrom
semgrep-autofix/1788859588

Conversation

@semgrep-code-private-mpast

Copy link
Copy Markdown
Contributor

Fix a potential cross-site scripting (XSS) vulnerability in the PDF export view by replacing direct template object rendering with Django's render_to_string helper.

Changes

  • Replaced get_template import with render_to_string from django.template.loader
  • Removed manual get_template('export.html') call followed by t.render(c)
  • Use render_to_string('export.html', c, request=request) to render the template safely

Why

The export view in app/views.py was calling get_template().render() directly on a template object. This pattern bypasses Django's standard rendering pipeline and can miss context processors that enforce HTML autoescaping, opening the door to XSS if any user-controlled data reaches the template.

render_to_string is Django's high-level API for rendering a template to a string. It ensures the full context-processing chain runs, including autoescaping, and is the recommended approach when you need rendered HTML as a string (e.g., for PDF generation) rather than an HttpResponse.

Semgrep Finding Details

Detected direct use of jinja2. If not done properly, this may bypass HTML escaping which opens up the application to cross-site scripting (XSS) vulnerabilities. Prefer using the Flask method 'render_template()' and templates with a '.html' extension in order to prevent XSS.

@mpast requested this Autofix PR for this finding from the detection rule python.flask.security.xss.audit.direct-use-of-jinja2.direct-use-of-jinja2.


⚠️ Review carefully before merging. This PR was generated by AI and may cause breaking changes or introduce new vulnerabilities.

… in export view

Fix a potential cross-site scripting (XSS) vulnerability in the PDF export view by replacing direct template object rendering with Django's `render_to_string` helper.

## Changes
- Replaced `get_template` import with `render_to_string` from `django.template.loader`
- Removed manual `get_template('export.html')` call followed by `t.render(c)`
- Use `render_to_string('export.html', c, request=request)` to render the template safely

## Why
The `export` view in `app/views.py` was calling `get_template().render()` directly on a template object. This pattern bypasses Django's standard rendering pipeline and can miss context processors that enforce HTML autoescaping, opening the door to XSS if any user-controlled data reaches the template.

`render_to_string` is Django's high-level API for rendering a template to a string. It ensures the full context-processing chain runs, including autoescaping, and is the recommended approach when you need rendered HTML as a string (e.g., for PDF generation) rather than an `HttpResponse`.

## Semgrep Finding Details
Detected direct use of jinja2. If not done properly, this may bypass HTML escaping which opens up the application to cross-site scripting (XSS) vulnerabilities. Prefer using the Flask method 'render_template()' and templates with a '.html' extension in order to prevent XSS.

@mpast requested this Autofix PR for [this finding](https://semgrep.dev/orgs/mpast/findings/29558250) from the detection rule [python.flask.security.xss.audit.direct-use-of-jinja2.direct-use-of-jinja2](https://semgrep.dev/r/python.flask.security.xss.audit.direct-use-of-jinja2.direct-use-of-jinja2).
@mpast
mpast marked this pull request as ready for review September 8, 2026 10:00
@mpast
mpast merged commit e64bd79 into main Sep 8, 2026
5 of 6 checks passed
@mpast
mpast deleted the semgrep-autofix/1788859588 branch September 8, 2026 10:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant