fix(untrusted): neutralize fuzzy closing-tag variants in wrap_untrusted - #46
Open
viistaproducts wants to merge 1 commit into
Open
fix(untrusted): neutralize fuzzy closing-tag variants in wrap_untrusted#46viistaproducts wants to merge 1 commit into
viistaproducts wants to merge 1 commit into
Conversation
wrap_untrusted only escaped the exact byte string `</event_content>`, so an attacker could break out of the tagged data region with a whitespace- or case-varied closing tag (`</event_content >`, `</EVENT_CONTENT>`, `< / event_content >`, tab-separated). LLMs do not parse XML strictly, so a fuzzy closing tag can still read as the delimiter's end, letting subsequent text be interpreted as instructions. Replace the literal str.replace with a case-insensitive, whitespace-tolerant regex over closing-tag variants only (requires the slash), preserving the re-wrap idempotency contract for inner opening tags. Add tests pinning the fuzzy variants. Co-Authored-By: Claude Opus 4.8 <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.
What
wrap_untrustedonly escaped the exact byte string</event_content>. Whitespace- or case-varied closing tags slip through unescaped:Since LLMs don't parse XML strictly, a fuzzy closing tag can still read as the delimiter's end to the model, letting an attacker's subsequent text be interpreted as instructions rather than data — the exact break-out the wrapper exists to prevent. The docstring also claimed an attacker "cannot inject a fake
</event_content>", which overstated the guarantee.Change
str.replacewith a case-insensitive, whitespace-tolerant regex over closing-tag variants only (requires the slash). Being close-only preserves the existing re-wrap idempotency contract (an inner opening tag is never escaped), whichtest_wrap_untrusted_idempotent_in_content_does_not_corruptpins.test_wrap_untrusted_neutralizes_fuzzy_closing_tagscovering the four variants above.Defense-in-depth: the
UNTRUSTED_CONTENT_DIRECTIVEstill instructs the model to ignore embedded directives; this closes the delimiter-escape gap that sat behind it.Tests
tests/test_untrusted.py(12 passed), plus theUNTRUSTED-referencing suitestest_schema_evolver.py+test_self_improvement_hardening.py(62 passed total).ruff check,ruff format --check, andmypyall clean.🤖 Generated with Claude Code