fix: escape user-controlled query in HTML title to prevent injection#8
Open
AmSach wants to merge 1 commit into
Open
fix: escape user-controlled query in HTML title to prevent injection#8AmSach wants to merge 1 commit into
AmSach wants to merge 1 commit into
Conversation
The presentResults() function embeds the user's search query directly into an HTML <title> tag using template literal interpolation. A query string such as </title><script>alert(1)</script> was rendered verbatim into the result page that is opened in the user's browser, allowing arbitrary HTML/JavaScript injection. Add an escapeHtml() helper and apply it to the query when building the title so untrusted characters are HTML-encoded before being written to the page.
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.
Summary
presentResults()embeds the user's search query directly into an HTML<title>element using template literal interpolation. Because the generated HTML file is then opened in the user's default browser, a query string containing characters such as</title><script>..., attribute-breaking quotes, or<img onerror=...>payloads is rendered verbatim. This is a self-XSS at minimum (since the file is opened on the user's own machine) but is still a real correctness/security bug: any code embedded this way executes with the privileges of the page that is being opened.Reproduction
Run any successful search:
The HTML file written to
os.tmpdir()and opened in the browser contains a<script>tag that breaks out of the title.Fix
Add a small
escapeHtml()helper that HTML-encodes&,<,>,"and', and apply it to the query when building the title insrc/search.ts. Filenames are already passed throughpathToFileURL(), which percent-encodes special characters, so they remain safe inside the existinghref/srcattributes.Diff
Found while browsing the codebase — happy to iterate if you'd prefer a different approach. — Sent via @AmSach bot