Detect and neutralize Jinja2 server-side template injection (SSTI) payloads
Project description
jinja2-ssti-sanitizer
Detect and neutralize Jinja2 server-side template injection (SSTI) payloads before they reach your templates.
Installation
pip install jinja2-ssti-sanitizer
For safe rendering helpers that use Jinja2's SandboxedEnvironment:
pip install jinja2-ssti-sanitizer[render]
Quick start
from jinja2_ssti_sanitizer import (
is_jinja2_payload,
sanitize_jinja2_input,
render_safely,
)
# Detect SSTI attempts in user input
if is_jinja2_payload(user_input):
raise ValueError("blocked")
# Escape delimiters and strip gadget fragments
result = sanitize_jinja2_input(user_input)
safe_text = result.sanitized
# Render a fixed template with a validated context
html = render_safely("Hello {{ name }}!", {"name": user_name})
API
| Function | Description |
|---|---|
detect_threats(value) |
Returns threat level and matched rule names |
is_jinja2_payload(value) |
True when input matches SSTI patterns |
sanitize_jinja2_input(value, mode="escape") |
Neutralize input (escape, strip, or reject) |
validate_template_variable(name, value) |
Validate a single context value |
validate_template_context(context) |
Validate all string values in a render context |
create_safe_environment() |
SandboxedEnvironment with safe defaults |
render_safely(template_source, context) |
Render with validated context |
Sanitization modes
escape(default) — HTML-entity-encode{{,}},{%,%},{#,#}and strip gadget fragmentsstrip— remove delimiter tokens and dangerous expressionsreject— raiseValueErroron HIGH or CRITICAL threats
Critical payloads (dunder chains, mro, subclasses, import/include tags, etc.) are always rejected when reject_critical=True (the default).
What it protects against
- Jinja2 variable, block, and comment delimiters
- Common gadget chains (
__class__,mro,subclasses,config,request,attrfilter, …) - Unicode homoglyphs and zero-width bypass characters
Defense in depth
This library is a input-validation layer, not a substitute for secure design:
- Never build template source from user-controlled input.
- Use
SandboxedEnvironmentwith autoescape enabled. - Sanitize or reject untrusted strings before they enter template context.
Development
git clone https://github.com/kjsxz/sanitizer-library.git
cd sanitizer-library
python -m venv .venv
source .venv/bin/activate
pip install -e ".[dev]"
pytest
python -m build
Publishing to PyPI
Releases are published automatically when a GitHub Release is created.
One-time setup
- Create a PyPI project at pypi.org (or test.pypi.org first).
- Configure trusted publishing on PyPI:
- Publisher: GitHub Actions
- Repository:
kjsxz/sanitizer-library - Workflow:
publish.yml - Environment:
pypi
- In GitHub repo settings, create an environment named
pypi(optional approval gate).
Release flow
# Bump version in pyproject.toml and jinja2_ssti_sanitizer/__init__.py
git tag v0.1.0
git push origin v0.1.0
Then create a GitHub Release from the tag. The publish workflow uploads to PyPI.
Manual publish (maintainers):
pip install build twine
python -m build
twine upload dist/*
License
MIT — see LICENSE.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file jinja2_ssti_sanitizer-0.1.0.tar.gz.
File metadata
- Download URL: jinja2_ssti_sanitizer-0.1.0.tar.gz
- Upload date:
- Size: 7.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
838d64752a3c461a8d0916693d0a88cac2d89af3f61b500eb7aed555e8eb253e
|
|
| MD5 |
a72011cecd14571385b26115b65fe1bd
|
|
| BLAKE2b-256 |
ff842efc246efee0e09153d8787e4c17529f01e2b164c5886fb81c3002df4c4e
|
Provenance
The following attestation bundles were made for jinja2_ssti_sanitizer-0.1.0.tar.gz:
Publisher:
publish.yml on kjsxz/sanitizer-library
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
jinja2_ssti_sanitizer-0.1.0.tar.gz -
Subject digest:
838d64752a3c461a8d0916693d0a88cac2d89af3f61b500eb7aed555e8eb253e - Sigstore transparency entry: 1909897800
- Sigstore integration time:
-
Permalink:
kjsxz/sanitizer-library@ee3b01690978ff36ff1a90916619ac96da25eb9b -
Branch / Tag:
refs/heads/main - Owner: https://github.com/kjsxz
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@ee3b01690978ff36ff1a90916619ac96da25eb9b -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file jinja2_ssti_sanitizer-0.1.0-py3-none-any.whl.
File metadata
- Download URL: jinja2_ssti_sanitizer-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e11ab710562e6a76e3088432249bd399bead923afeba65eef5c1084529196e5c
|
|
| MD5 |
f8ab5544e0495cbf6636cb43aae46da5
|
|
| BLAKE2b-256 |
9bc7f227be79390c531db503213970772f92e7eaa70c2cdb847d31375f46c6d8
|
Provenance
The following attestation bundles were made for jinja2_ssti_sanitizer-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on kjsxz/sanitizer-library
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
jinja2_ssti_sanitizer-0.1.0-py3-none-any.whl -
Subject digest:
e11ab710562e6a76e3088432249bd399bead923afeba65eef5c1084529196e5c - Sigstore transparency entry: 1909897910
- Sigstore integration time:
-
Permalink:
kjsxz/sanitizer-library@ee3b01690978ff36ff1a90916619ac96da25eb9b -
Branch / Tag:
refs/heads/main - Owner: https://github.com/kjsxz
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@ee3b01690978ff36ff1a90916619ac96da25eb9b -
Trigger Event:
workflow_dispatch
-
Statement type: