Skip to content

Fix #50: Add Jinja2 security utilities for template injection prevention - #74

Open
TensorDevLJ wants to merge 5 commits into
redhat-data-and-ai:deep-agentfrom
TensorDevLJ:feat/jinja2-security-utilities
Open

Fix #50: Add Jinja2 security utilities for template injection prevention#74
TensorDevLJ wants to merge 5 commits into
redhat-data-and-ai:deep-agentfrom
TensorDevLJ:feat/jinja2-security-utilities

Conversation

@TensorDevLJ

@TensorDevLJ TensorDevLJ commented Jul 15, 2026

Copy link
Copy Markdown

Summary

  • Add template_mcp_server.utils.jinja2_security with three utilities for Jinja2 SSTI defense-in-depth:
    • escape_jinja2_delimiters() — neutralize {{, }}, {%, %}, {#, #} in untrusted text
    • validate_user_query_for_jinja2() — reject delimiter injection with ValueError and WARNING log
    • get_secure_jinja2_env() — return Environment(autoescape=True) when security is enabled
  • Add ENABLE_JINJA2_SECURITY feature flag (default: True)
  • Document security implications and safe usage in docs/development.md

Fixes #50

Scope note

This repository currently has no Jinja2 usage. This PR intentionally adds reusable security utilities for future tools and does not integrate them into existing code. Current server behavior is unchanged.

Security model

These utilities help prevent accidental template injection when handling untrusted input. They do not make it safe to construct Jinja2 template source from user-controlled data. Callers should validate input before rendering and avoid building templates from user input.

Test plan

  • 330 tests passing (25 new)
  • All 6 delimiter patterns tested for validation and escaping
  • autoescape=True enforced when security enabled (kwargs cannot override)
  • WARNING logged on blocked injection
  • Feature flag disable path tested
  • Pre-commit hooks pass (ruff, mypy, pydocstyle, bandit)

abhiskum and others added 4 commits June 1, 2026 10:31
…t-data-and-ai#22)

Signed-off-by: Abhishek Kumar <abhishek68@gmail.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
… injection prevention

Introduce reusable delimiter validation, escaping, and secure Environment
creation behind ENABLE_JINJA2_SECURITY so future Jinja2 tools can handle
untrusted input safely without changing current server behavior.

Co-authored-by: Cursor <cursoragent@cursor.com>
@TensorDevLJ
TensorDevLJ changed the base branch from main to deep-agent July 15, 2026 06:56
Signed-off-by: Likhitha J <143345274+TensorDevLJ@users.noreply.github.com>
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.

Add Jinja2 security utilities for template injection prevention

2 participants