fix: sanitize page-provided tool strings before they reach model context#2247
Open
serhiizghama wants to merge 2 commits into
Open
fix: sanitize page-provided tool strings before they reach model context#2247serhiizghama wants to merge 2 commits into
serhiizghama wants to merge 2 commits into
Conversation
Tool group and tool name/description strings returned by page JavaScript through the devtoolstooldiscovery event were interpolated into the list_3p_developer_tools response with only a typeof check. A page could respond with newlines, C0/C1 control characters, or Unicode bidirectional overrides to inject fake sections or reorder text in the model's context. Strip control and bidi characters and collapse whitespace on the Node side, after the values cross the Puppeteer boundary, since in-page code is untrusted.
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.
Problem
Fixes #2242.
When
list_3p_developer_toolsruns,McpResponse.getToolGroups()collects tool groups from page JavaScript via thedevtoolstooldiscoveryevent. Thename/descriptionof each group and tool are page-provided strings, and they are interpolated directly into the response text the model reads with no sanitization beyond atypeof === 'string'check.A page being browsed can respond with strings containing:
## Systemsection into the structured response,This is gated behind
categoryExperimentalThirdParty, but for any user who has enabled that category, a visited page can inject content into the LLM context.Solution
Add
sanitizePageString()and apply it togroup.name,group.description,tool.name, andtool.descriptioningetToolGroups(), in the Node-side loop that already runs after the values cross the Puppeteer boundary (in-page code is untrusted, so sanitizing there rather than insideevaluate()is deliberate). It:U+200E/200F,U+202A–202E,U+2066–2069), andSanitizing at the boundary means both the rendered text and
structuredContent.thirdPartyDeveloperToolsare clean, so the fix covers every downstream consumer.Testing
Added a test in
tests/tools/thirdPartyDeveloper.test.tsthat has a page respond with a tool group whose strings contain a right-to-left override, a control character, and a newline-led## Systemblock, then asserts those characters are removed and the injected section is collapsed onto a single line in bothstructuredContentand the response text. Verified it fails onmainand passes with the fix; the existing third-party andMcpResponsesuites stay green.