Skip to content

fix(prompt): stop HTML-escaping prompt values - #583

Open
roydonsequeira wants to merge 1 commit into
OpenBMB:mainfrom
roydonsequeira:fix/prompt-html-escaping
Open

roydonsequeira wants to merge 1 commit into
OpenBMB:mainfrom
roydonsequeira:fix/prompt-html-escaping

Conversation

@roydonsequeira

Copy link
Copy Markdown

Fixes #582.

_safe_render ran every value through markupsafe.escape and the sandboxed environment had autoescape=True, so questions and passages reached the model as HTML (team's became team's). search_r1_gen and r1_searcher_gen render the previous prompt back in as history, 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_render pass 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. The AGENTS.md line that described the escaping is updated to match.

tests/servers/prompt/test_prompt_rendering.py checks 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 raises SecurityError. 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), using qa_rag_boxed and 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 &amp; 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_page still renders docs_text as a Python list (now ['...', '...'] rather than [Markup(&#39;...&#39;)]). Joining it like the other prompt tools do seemed like a separate change, so I left it.

_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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Prompt server sends HTML-escaped questions and passages to the model

1 participant