Fix JS code injection in browser_hover via selector escaping (CWE-94)#45
Merged
Fix JS code injection in browser_hover via selector escaping (CWE-94)#45
Conversation
…ctor Co-authored-by: cfc4n <709947+cfc4n@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] [gojue/moling#4] Fix security vulnerability: code injection
Fix JS code injection in browser_hover via json.Marshal selector escaping
Mar 15, 2026
Co-authored-by: cfc4n <709947+cfc4n@users.noreply.github.com>
Copilot
AI
changed the title
Fix JS code injection in browser_hover via json.Marshal selector escaping
Fix JS code injection in browser_hover via selector escaping (CWE-94)
Mar 15, 2026
There was a problem hiding this comment.
Pull request overview
This PR hardens the browser hover tool implementation by safely embedding the user-provided CSS selector into the evaluated JavaScript to prevent selector-based JS injection.
Changes:
- JSON-encode the CSS selector before inserting it into the
chromedp.EvaluateJS expression forbrowser_hover. - Add a unit test intended to validate selector escaping behavior.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| pkg/services/browser/browser.go | Escapes the selector via JSON encoding before building the hover JS expression. |
| pkg/services/browser/browser_test.go | Adds a test for selector JSON-encoding / JS string construction behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
Comment on lines
+86
to
+91
| selectorJSON, err := json.Marshal(tc.selector) | ||
| if err != nil { | ||
| t.Fatalf("json.Marshal failed: %v", err) | ||
| } | ||
| js := `document.querySelector(` + string(selectorJSON) + `).dispatchEvent(new Event('mouseover'))` | ||
|
|
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.
handleHoverconcatenated the caller-suppliedselectordirectly into achromedp.EvaluateJS string, allowing single-quote breakout to execute arbitrary JavaScript in the chromedp-controlled browser — enabling theft of cookies,localStorage, and DOM content.Root cause (
browser.go)json.Marshalencodes the selector as a double-quoted, fully-escaped JS string literal; single quotes, semicolons, and other breakout characters are inert inside itencoding/jsonwas already imported; no new dependencies.Tests (
browser_test.go)TestHoverSelectorEscapingcovering: normal selector, comma-operator injection ('PWNED'), IIFE-with-semicolons injection, and double-quote escaping — each asserting the JSON-encoded value is always a self-contained double-quoted string with no executable breakout.Original prompt
This section details on the original issue you should resolve
<issue_title>Security Vulnerability: Code Injection</issue_title>
<issue_description>This vulnerability is found by Songwu security researcher,Zeyu Luo security researcher, Dr. CAO Yinfeng, Kevin(The Hong Kong Polytechnic University / HKCT Institute of Higher Education)
vulnerability description
browser.go:435 directly interpolates selector into a JavaScript string. By crafting
selector = "body'); (function(){ /* exfiltration code */ })(); document.querySelector('body"
the entire expression remains syntactically valid while the injected code is executed.
Impact | Arbitrary JavaScript execution in any page opened by the browser controlled via chromedp, allowing theft of cookies, localStorage, and form contents.
POC