Fix XSS risk: replace direct template rendering with render_to_string in export view - #184
Merged
Merged
Conversation
… 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
approved these changes
Sep 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix a potential cross-site scripting (XSS) vulnerability in the PDF export view by replacing direct template object rendering with Django's
render_to_stringhelper.Changes
get_templateimport withrender_to_stringfromdjango.template.loaderget_template('export.html')call followed byt.render(c)render_to_string('export.html', c, request=request)to render the template safelyWhy
The
exportview inapp/views.pywas callingget_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_stringis 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 anHttpResponse.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.