Skip to content

Fix Jinja2 autoescape disabled XSS vulnerability#7217

Open
WiamSkakri wants to merge 1 commit intohotosm:developfrom
WiamSkakri:bugfix/fix-jinja2-autoescape-xss
Open

Fix Jinja2 autoescape disabled XSS vulnerability#7217
WiamSkakri wants to merge 1 commit intohotosm:developfrom
WiamSkakri:bugfix/fix-jinja2-autoescape-xss

Conversation

@WiamSkakri
Copy link
Copy Markdown

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)

  • 🍕 Feature
  • [ x ] 🐛 Bug Fix
  • 📝 Documentation
  • 🧑‍💻 Refactor
  • ✅ Test
  • 🤖 Build or CI
  • ❓ Other (please specify)

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

  1. Check that autoescape=True is set in the Environment constructor in backend/services/messaging/template_service.py
    Start the app with docker-compose up
  2. Trigger an email notification (e.g. send a message between two users) and verify it renders correctly
  3. Confirm the backend container remains healthy: docker-compose ps

Checklist before requesting a review

[optional] What gif best describes this PR or how it makes you feel?

Enable autoescape=True in the Jinja2 Environment to prevent cross-site scripting via user-controlled template variables.
Flagged by Bandit (B701) and Semgrep.
@WiamSkakri WiamSkakri force-pushed the bugfix/fix-jinja2-autoescape-xss branch from 4b4fb79 to 468b85b Compare April 6, 2026 04:27
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud bot commented Apr 6, 2026

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant