Skip to content

Commit 5aff2e1

Browse files
committed
Prevent ANY redirection to data: URIs in documents.
1 parent 9b3a12f commit 5aff2e1

3 files changed

Lines changed: 16 additions & 5 deletions

File tree

src/bg/ReportingCSP.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ function ReportingCSP(reportURI, reportGroup) {
3535
h.name === REPORT_TO.name && h.value === REPORT_TO.value) {
3636
needsReportTo = false;
3737
} else if (blocker && /^(Location|Refresh)$/i.test(h.name)) {
38+
// neutralize any HTTP redirection to data: URLs, like Chromium
3839
let url = /^R/i.test(h.name)
3940
? h.value.replace(/^[^,;]*[,;]url[^\w=]*=\s*/i, "") : h.value;
40-
let patched = CSP.patchDataURI(url, blocker);
41-
if (patched !== url) {
42-
h.value = h.value.slice(0, -url.length) + patched;
41+
if (/^data:/i.test(url)) {
42+
h.value = h.value.slice(0, -url.length) + "data:";
4343
}
4444
}
4545
}

src/content/content.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,14 @@ ns.on("capabilities", () => {
114114

115115
ns.fetchPolicy();
116116
notifyPage();
117+
118+
addEventListener("DOMContentLoaded", e => {
119+
if (ns.canScript) return;
120+
for (let m of document.querySelectorAll("meta[http-equiv=refresh]")) {
121+
if (/^[^,;]*[,;]url[^\w=]*=\s*data:/.test(m.getAttribute("content"))) {
122+
let url = m.getAttribute("content").replace(/.*?(?=data:)/, "");
123+
log(`Blocking refresh to ${url}`);
124+
window.stop();
125+
}
126+
}
127+
});

src/lib/CSP.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class CSP {
2222
CSP.isEmbedType = type => /\b(?:application|video|audio)\b/.test(type) && type !== "application/xhtml+xml";
2323
CSP.headerName = "content-security-policy";
2424
CSP.patchDataURI = (uri, blocker) => {
25-
let parts = /^data:(?:[^,;]*ml)(;[^,]*)?,/i.exec(uri);
25+
let parts = /^data:(?:[^,;]*ml|unknown-content-type)(;[^,]*)?,/i.exec(uri);
2626
if (!(blocker && parts)) {
2727
// not an interesting data: URI, return as it is
2828
return uri;
@@ -33,6 +33,6 @@ CSP.patchDataURI = (uri, blocker) => {
3333
}
3434
// It's a HTML/XML page, let's prepend our CSP blocker to the document
3535
let patch = parts[0] + encodeURIComponent(
36-
`<meta http-equiv="${CSP.headerName}" content="${blocker}">`);
36+
`<meta http-equiv="${CSP.headerName}" content="${blocker}"/>`);
3737
return uri.startsWith(patch) ? uri : patch + uri.substring(parts[0].length);
3838
}

0 commit comments

Comments
 (0)