Repository navigation
Sandbox nested persona template rendering in evaluation prompts - #5266
petrmarinec wants to merge 1 commit into
Conversation
|
For clarity, I also reran the remaining failing full-suite tests on unmodified The only full-suite failures I saw were these 5
Those same 5 failures reproduce on unmodified |
|
Hi @petrmarinec , Thank you for your contribution! We appreciate you taking the time to submit this pull request. Your PR has been received by the team and is currently under review. We will provide feedback as soon as we have an update to share. |
|
Hi @wukath , can you please review this. |
c4e30e5 to
edc6364
Compare
Merge #5266 ### Link to Issue or Description of Change **1. Link to an existing issue (if applicable):** - Related: #5265 **2. Or, if no issue exists, describe the change:** **Problem:** Nested persona behavior strings were rendered through `render_string_filter`, which created a fresh Jinja template from persona-controlled content instead of consistently reusing a sandboxed environment. That allowed nested persona templates to execute outside the intended sandbox boundary in evaluation prompt construction. Affected files: - `src/google/adk/evaluation/simulation/llm_backed_user_simulator_prompts.py` - `src/google/adk/evaluation/simulation/per_turn_user_simulator_quality_prompts.py` **Solution:** Render nested persona strings through `SandboxedEnvironment` and use `SandboxedEnvironment` for the per-turn evaluator prompt builder as well. This keeps supported nested placeholders such as `{{ stop_signal }}` working while blocking unsafe nested template access. Added regression tests for both the safe interpolation path and blocked unsafe attribute traversal. ### Testing Plan **Unit Tests:** - [x] I have added or updated unit tests for my change. - [ ] All unit tests pass locally. Passed in clean Linux Docker (`python:3.11-bookworm`): - `uv sync --all-extras` - `pytest tests/unittests/evaluation/simulation` - Result: `70 passed` Additional repo-wide validation: - `pytest tests/unittests` - Result on patched branch: `5326 passed, 1 skipped, 5 failed` - The same 5 failures reproduce on unmodified `origin/main` - Those failures are unrelated `tests/unittests/tools/test_skill_toolset.py` integration timeouts **Manual End-to-End (E2E) Tests:** - [x] Ran a live `adk web` regression test against the eval API in Linux Docker using a local non-LLM root agent. - [x] Malicious nested persona template `{{ ''.__class__.__mro__ }}` was blocked during prompt construction with `jinja2.exceptions.SecurityError`. - [x] A safe persona using nested `{{ stop_signal }}` placeholders did not raise `TemplateSyntaxError` or `SecurityError` and progressed beyond prompt rendering into a real Gemini model call. - [x] The safe run did not fully complete because the test key hit `429 RESOURCE_EXHAUSTED`, but the absence of template errors and the subsequent model call confirm the sandboxed nested rendering path is functioning as intended. ### Checklist - [x] I have read the [CONTRIBUTING.md](https://github.com/google/adk-python/blob/main/CONTRIBUTING.md) document. - [x] I have performed a self-review of my own code. - [ ] I have commented my code, particularly in hard-to-understand areas. - [x] I have added tests that prove my fix is effective or that my feature works. - [ ] New and existing unit tests pass locally with my changes. - [x] I have manually tested my changes end-to-end. - [x] Any dependent changes have been merged and published in downstream modules. ### Additional context This is a small, focused fix intended to close the nested-template sandbox bypass path without changing the supported nested placeholder behavior used by existing personas. Co-authored-by: George Weale <gweale@google.com> COPYBARA_INTEGRATE_REVIEW=#5266 from petrmarinec:fix-jinja-ssti-sandbox edc6364 PiperOrigin-RevId: 933966100
|
Thank you @petrmarinec for your contribution! 🎉 Your changes have been successfully imported and merged via Copybara in commit 30493ba. Closing this PR as the changes are now in the main branch. |
|
Google OSS VRP #498357322 |
Merge google#5266 ### Link to Issue or Description of Change **1. Link to an existing issue (if applicable):** - Related: google#5265 **2. Or, if no issue exists, describe the change:** **Problem:** Nested persona behavior strings were rendered through `render_string_filter`, which created a fresh Jinja template from persona-controlled content instead of consistently reusing a sandboxed environment. That allowed nested persona templates to execute outside the intended sandbox boundary in evaluation prompt construction. Affected files: - `src/google/adk/evaluation/simulation/llm_backed_user_simulator_prompts.py` - `src/google/adk/evaluation/simulation/per_turn_user_simulator_quality_prompts.py` **Solution:** Render nested persona strings through `SandboxedEnvironment` and use `SandboxedEnvironment` for the per-turn evaluator prompt builder as well. This keeps supported nested placeholders such as `{{ stop_signal }}` working while blocking unsafe nested template access. Added regression tests for both the safe interpolation path and blocked unsafe attribute traversal. ### Testing Plan **Unit Tests:** - [x] I have added or updated unit tests for my change. - [ ] All unit tests pass locally. Passed in clean Linux Docker (`python:3.11-bookworm`): - `uv sync --all-extras` - `pytest tests/unittests/evaluation/simulation` - Result: `70 passed` Additional repo-wide validation: - `pytest tests/unittests` - Result on patched branch: `5326 passed, 1 skipped, 5 failed` - The same 5 failures reproduce on unmodified `origin/main` - Those failures are unrelated `tests/unittests/tools/test_skill_toolset.py` integration timeouts **Manual End-to-End (E2E) Tests:** - [x] Ran a live `adk web` regression test against the eval API in Linux Docker using a local non-LLM root agent. - [x] Malicious nested persona template `{{ ''.__class__.__mro__ }}` was blocked during prompt construction with `jinja2.exceptions.SecurityError`. - [x] A safe persona using nested `{{ stop_signal }}` placeholders did not raise `TemplateSyntaxError` or `SecurityError` and progressed beyond prompt rendering into a real Gemini model call. - [x] The safe run did not fully complete because the test key hit `429 RESOURCE_EXHAUSTED`, but the absence of template errors and the subsequent model call confirm the sandboxed nested rendering path is functioning as intended. ### Checklist - [x] I have read the [CONTRIBUTING.md](https://github.com/google/adk-python/blob/main/CONTRIBUTING.md) document. - [x] I have performed a self-review of my own code. - [ ] I have commented my code, particularly in hard-to-understand areas. - [x] I have added tests that prove my fix is effective or that my feature works. - [ ] New and existing unit tests pass locally with my changes. - [x] I have manually tested my changes end-to-end. - [x] Any dependent changes have been merged and published in downstream modules. ### Additional context This is a small, focused fix intended to close the nested-template sandbox bypass path without changing the supported nested placeholder behavior used by existing personas. Co-authored-by: George Weale <gweale@google.com> COPYBARA_INTEGRATE_REVIEW=google#5266 from petrmarinec:fix-jinja-ssti-sandbox edc6364 PiperOrigin-RevId: 933966100
Link to Issue or Description of Change
1. Link to an existing issue (if applicable):
2. Or, if no issue exists, describe the change:
Problem:
Nested persona behavior strings were rendered through
render_string_filter, which created a fresh Jinja template from persona-controlled content instead of consistently reusing a sandboxed environment. That allowed nested persona templates to execute outside the intended sandbox boundary in evaluation prompt construction.Affected files:
src/google/adk/evaluation/simulation/llm_backed_user_simulator_prompts.pysrc/google/adk/evaluation/simulation/per_turn_user_simulator_quality_prompts.pySolution:
Render nested persona strings through
SandboxedEnvironmentand useSandboxedEnvironmentfor the per-turn evaluator prompt builder as well. This keeps supported nested placeholders such as{{ stop_signal }}working while blocking unsafe nested template access. Added regression tests for both the safe interpolation path and blocked unsafe attribute traversal.Testing Plan
Unit Tests:
Passed in clean Linux Docker (
python:3.11-bookworm):uv sync --all-extraspytest tests/unittests/evaluation/simulation70 passedAdditional repo-wide validation:
pytest tests/unittests5326 passed, 1 skipped, 5 failedorigin/maintests/unittests/tools/test_skill_toolset.pyintegration timeoutsManual End-to-End (E2E) Tests:
adk webregression test against the eval API in Linux Docker using a local non-LLM root agent.{{ ''.__class__.__mro__ }}was blocked during prompt construction withjinja2.exceptions.SecurityError.{{ stop_signal }}placeholders did not raiseTemplateSyntaxErrororSecurityErrorand progressed beyond prompt rendering into a real Gemini model call.429 RESOURCE_EXHAUSTED, but the absence of template errors and the subsequent model call confirm the sandboxed nested rendering path is functioning as intended.Checklist
Additional context
This is a small, focused fix intended to close the nested-template sandbox bypass path without changing the supported nested placeholder behavior used by existing personas.