Repository navigation
fix(prompt): stop HTML-escaping prompt values - #583
Open
roydonsequeira wants to merge 1 commit into
Open
roydonsequeira wants to merge 1 commit into
roydonsequeira wants to merge 1 commit into
Conversation
_safe_render ran markupsafe.escape on every value and the sandboxed
environment had autoescape=True, so questions and passages reached the
model as HTML entities ("What's" became "What&OpenBMB#39;s"). The Search-R1 and
R1-Searcher append prompts render the previous prompt back in as history,
which escaped it again on every turn.
The sandbox is what guards template source, and render() never parses
values as templates, so the escaping protected nothing. Keep the sandbox,
turn autoescape off and pass values through unchanged.
This branch has not been deployed
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.
Fixes #582.
_safe_renderran every value throughmarkupsafe.escapeand the sandboxed environment hadautoescape=True, so questions and passages reached the model as HTML (team'sbecameteam's).search_r1_genandr1_searcher_genrender the previous prompt back in ashistory, which escaped it again on every turn: by the third turn the Search-R1 instructions read<think>.This keeps
SandboxedEnvironment, turns autoescape off, and has_safe_renderpass values through unchanged. Template source is still sandboxed and values are never parsed as templates, so this doesn't reopen the template injection that #272 addressed. The UI sanitizes its own HTML. TheAGENTS.mdline that described the escaping is updated to match.tests/servers/prompt/test_prompt_rendering.pychecks that a question and passages containing',",&,<and>come back verbatim, runs the Search-R1 and R1-Searcher append prompts for three turns and checks that each turn starts with the previous prompt unchanged, and checks that an unsafe template still raisesSecurityError. On main the verbatim test and both loop cases fail; the sandbox test passes on both. Tested on Windows 11 with Python 3.12: the CI test command gives 206 passed, 2 skipped and 3 failed, against 202, 2 and the same 3 on main (symlink tests that need Developer Mode on Windows). The sayhello build and run work with and without MCP.I also compared answers on 100 HotpotQA rows from UltraRAG_Benchmark (all 52 whose gold answer contains
&,'or", plus 48 others), usingqa_rag_boxedand qwen2.5:7b (Q4_K_M, Ollama 0.40.16) at temperature 0. 43 predictions differ between main and this branch, while repeating this branch's run changed 0 of 25. EM goes from 30.0 to 33.0 and F1 from 47.9 to 46.9, which 100 rows can't separate from chance (exact McNemar test on EM, p = 0.51), so I'm not claiming an accuracy gain. Five of main's answers contain an HTML entity, and one fails EM on that alone (The Suite Life of Zack & Cody); none on this branch do. The eval script uses the repo's own template, generation backend, answer extraction and metrics; I can share it with the per-row results.webnote_fill_pagestill rendersdocs_textas a Python list (now['...', '...']rather than[Markup('...')]). Joining it like the other prompt tools do seemed like a separate change, so I left it.