feat: add Jinja2 template safety utilities - #76
Open
kocaemre wants to merge 1 commit into
Open
Conversation
Signed-off-by: Emre K <110906681+kocaemre@users.noreply.github.com>
kocaemre
force-pushed
the
fix/jinja-template-safety
branch
from
July 26, 2026 17:38
2c79fd2 to
a061d02
Compare
There was a problem hiding this comment.
Pull request overview
Adds a Jinja2 safety utility layer to help prevent template-injection when user-controlled text may be interpolated into templates, controlled by a new ENABLE_JINJA2_SECURITY feature flag (default True).
Changes:
- Introduces
template_mcp_server.utils.jinja2_securitywith delimiter escaping, delimiter-based validation, and a helper to build a “secure by default” Jinja2Environment. - Adds
ENABLE_JINJA2_SECURITYto application settings, plus documentation inREADME.mdand.env.example. - Adds unit tests covering delimiter escaping, injection detection/logging, env autoescape default behavior, and feature-flag behavior.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| tests/test_jinja2_security.py | Adds unit/regression tests for new Jinja2 security utilities and the feature flag default. |
| template_mcp_server/utils/jinja2_security.py | Implements delimiter escaping/validation and a helper to create a Jinja2 environment with autoescape enabled by default. |
| template_mcp_server/src/settings.py | Adds ENABLE_JINJA2_SECURITY setting (default True). |
| README.md | Documents the new ENABLE_JINJA2_SECURITY environment variable in the config table. |
| .env.example | Adds ENABLE_JINJA2_SECURITY=True with a short description comment. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+54
to
+56
| if _jinja2_security_enabled(enabled): | ||
| kwargs.setdefault("autoescape", True) | ||
| return Environment(**kwargs) |
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.
Summary
ENABLE_JINJA2_SECURITY(defaultTrue) to control template-injection safeguards.template_mcp_server.utils.jinja2_securityhelpers:escape_jinja2_delimiters(text)validate_user_query_for_jinja2(query)get_secure_jinja2_env()withautoescape=Trueby default.env.exampleandREADME.md.Why
This provides the Jinja2 safety utility layer requested in #50 for template code that needs to handle user-controlled input safely.
Closes #50.
Test plan
ModuleNotFoundError: No module named 'template_mcp_server.utils.jinja2_security').uv run --python 3.12 python -m pytest tests/test_jinja2_security.py -q— 15 passeduv run --python 3.12 python -m pytest tests/test_jinja2_security.py tests/test_settings.py tests/test_utils.py -q— 73 passeduv run --python 3.12 python -m pytest tests -q— 320 passed, 1 skipped, 1 existing warninguv run --python 3.12 ruff check template_mcp_server tests/test_jinja2_security.py tests/test_settings.py— passeduv run --python 3.12 python -m py_compile template_mcp_server/utils/jinja2_security.py template_mcp_server/src/settings.py tests/test_jinja2_security.pygit diff --check