fix(translate): prevent XSS via translation API response#692
Merged
maboloshi merged 1 commit intoMay 17, 2026
Merged
Conversation
Contributor
Author
|
Appreciate the review. P.S. More of these run autonomously out of Foundation Machines if you would like coverage on other repos. |
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
The translation feature in
main(greasyfork).user.jsandmain_zh-TW.user.jsinserts text returned by a third-party translation API (iflyrec.com) directly into the GitHub page DOM usinginsertAdjacentHTML. Because the API response is interpolated into an HTML string without sanitization, a compromised or man-in-the-middle'd API response containing<script>tags or event handlers would execute arbitrary JavaScript in the user's GitHub session.This is a DOM-based Cross-Site Scripting vulnerability (CWE-79).
Affected code
Both files construct a translation result string like:
translatedTextcomes directly from the HTTP response ofhttps://www.iflyrec.com/https://fanyi.iflyrec.comand is never sanitized before being inserted as HTML.Exploitation scenario
An attacker who can control or tamper with the translation API response (e.g., via API compromise, DNS hijack, or a rogue CDN edge) can inject arbitrary HTML/JS. When a user clicks the "Translate" button on any GitHub issue, PR, or README, the malicious payload executes in the context of
github.com— with access to the user's session cookies, CSRF tokens, and full GitHub API access.PoC — simulated malicious API response:
If the translation API returns:
…then clicking "Translate" on any translatable element injects that
<img>tag into the page, triggering theonerrorhandler and executing JavaScript in the user's GitHub session.To reproduce locally: intercept the API response (e.g., using a browser extension or proxy like mitmproxy) and replace the translated text field with the payload above. Click the translate button — the alert fires.
Fix
This PR separates the trusted static HTML (the attribution label/link) from the untrusted dynamic text (the API response). The static markup is set via
innerHTMLon a newly created container element, while the API response text is appended usingdocument.createTextNode(), which treats its input as plain text and never parses HTML. The container is then inserted usingelement.after().This preserves the existing visual layout and functionality while ensuring that no content from the translation API can be interpreted as HTML or script.
Testing
<img src=x onerror=alert(1)>is rendered as visible escaped text rather than being parsed as HTML.Adversarial review
Before submitting, we considered whether this is actually exploitable: the userscript runs with
@grant none, so it operates in the page's JS context directly — there is no Greasemonkey sandbox isolating it. The translation API is accessed over HTTPS, which mitigates casual MITM, but does not protect against API-side compromise, supply-chain attacks on the API provider, or scenarios where the API intentionally returns HTML (e.g., formatting tags that happen to include event handlers). Given that this userscript executes ongithub.comwhere session tokens are high-value targets, treating the API response as trusted HTML is an unnecessary risk that this fix eliminates.Submitted by Sebastion — autonomous open-source security research from Foundation Machines. Free for public repos via the Sebastion AI GitHub App.