fix: prevent SSTI by switching to SandboxedEnvironment#74
Merged
Conversation
Templates are rendered in an unsandboxed Jinja2 environment, so passing untrusted user input as template text allows arbitrary code execution. Document the safe pattern (variables) vs the unsafe one (raw user strings). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Switch from jinja2.Environment to jinja2.sandbox.SandboxedEnvironment. This blocks access to dunder attributes in templates, preventing SSTI payloads from reaching __builtins__ and executing arbitrary code. All existing tests pass unchanged. Add a regression test that verifies the canonical SSTI payload raises an exception. Also document that templates are trusted code and that user-supplied strings must never be passed as template text. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR aims to mitigate Server-Side Template Injection (SSTI) risks in Banks prompt rendering by moving the global Jinja2 environment to Jinja2’s sandbox, adding a regression test for a canonical SSTI payload, and documenting safe/unsafe template usage patterns.
Changes:
- Switch global Jinja environment from
jinja2.Environmenttojinja2.sandbox.SandboxedEnvironment. - Add a regression test intended to ensure a known SSTI/RCE-style payload is blocked.
- Add/extend documentation warning against rendering user-supplied template text.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
src/banks/env.py |
Uses SandboxedEnvironment for the global Jinja environment to reduce SSTI risk. |
tests/test_prompt.py |
Adds a regression test for an SSTI payload being blocked at render time. |
docs/prompt.md |
Adds a warning about treating templates as trusted code, but currently describes the environment incorrectly. |
docs/index.md |
Adds a “Security” section explaining safe vs unsafe patterns, but wording should reflect sandboxing. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…lect SandboxedEnvironment Agent-Logs-Url: https://github.com/masci/banks/sessions/de6273d5-80bf-468c-8853-feeb273a7294 Co-authored-by: masci <7241+masci@users.noreply.github.com>
e2e tests require an OpenAI API key that is no longer valid. Python 3.9 is EOL (Oct 2025) and newer virtualenv drops support for it. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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
jinja2.Environmenttojinja2.sandbox.SandboxedEnvironmentinsrc/banks/env.py— blocks dunder attribute traversal in templates, preventing SSTI/RCE payloads from reaching__builtins__Test plan
SandboxedEnvironmenttest_ssti_blockedconfirms the exploit payload raisesSecurityErrorhatch run docs build)🤖 Generated with Claude Code