Fix Jinja2 autoescape disabled XSS vulnerability#7217
Open
WiamSkakri wants to merge 1 commit intohotosm:developfrom
Open
Fix Jinja2 autoescape disabled XSS vulnerability#7217WiamSkakri wants to merge 1 commit intohotosm:developfrom
WiamSkakri wants to merge 1 commit intohotosm:developfrom
Conversation
Enable autoescape=True in the Jinja2 Environment to prevent cross-site scripting via user-controlled template variables. Flagged by Bandit (B701) and Semgrep.
4b4fb79 to
468b85b
Compare
|
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.



Enable autoescape=True in the Jinja2 Environment to prevent cross-site scripting via user-controlled template variables. Flagged by Bandit (B701) and Semgrep.
What type of PR is this? (check all applicable)
Related Issue
No existing issue — vulnerability identified via static analysis scanning using Bandit and Semgrep.
Describe this PR
The Jinja2 Environment in backend/services/messaging/template_service.py was initialized without autoescape=True, which is Jinja2's default insecure configuration. This means any user-controlled data rendered in email templates (e.g. usernames, project names, comment content) could contain malicious HTML or JavaScript that would be rendered unescaped, leading to Cross-Site Scripting (XSS).
This PR adds autoescape=True to the Environment constructor, ensuring all template variables are HTML-escaped by default. All existing templates were reviewed and none use the | safe filter or Markup, so this change does not affect any existing rendering behaviour.
Flagged as High severity, High confidence by Bandit (B701: jinja2_autoescape_false) and as a Blocking finding by Semgrep (direct-use-of-jinja2).
Screenshots
N/A — backend-only change with no visual impact.
Alternative Approaches Considered
An alternative would be to use Jinja2's select_autoescape() helper to enable autoescape only for .html files. However, since all templates in this project are HTML email templates, enabling autoescape=True globally is simpler and equally correct.
Review Guide
Start the app with docker-compose up
Checklist before requesting a review
[optional] What gif best describes this PR or how it makes you feel?