Skip to content

Commit 209d50b

Browse files
committed
Simplified CSP HTTP header injection, avoiding report-to until actually supported by browsers.
1 parent c9c7b7a commit 209d50b

2 files changed

Lines changed: 25 additions & 29 deletions

File tree

src/bg/ReportingCSP.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
"use strict";
2-
2+
33
function ReportingCSP(reportURI, reportGroup) {
4+
const REPORT_TO_SUPPORTED = false;
5+
// TODO: figure out if we're running on a browser supporting the report-to
6+
// CSP directive, breaking report-uri, see
7+
// 1. https://www.w3.org/TR/CSP3/#directive-report-uri
8+
// 2. https://bugs.chromium.org/p/chromium/issues/detail?id=726634
9+
// 3. https://bugzilla.mozilla.org/show_bug.cgi?id=1391243
10+
411
const REPORT_TO = {
512
name: "Report-To",
613
value: JSON.stringify({ "url": reportURI,
@@ -9,39 +16,40 @@ function ReportingCSP(reportURI, reportGroup) {
916
};
1017
return Object.assign(
1118
new CapsCSP(new NetCSP(
12-
`report-uri ${reportURI};`,
13-
`;report-to ${reportGroup};`
14-
)),
19+
REPORT_TO_SUPPORTED ? `;report-to ${reportGroup};`
20+
: `report-uri ${reportURI};`
21+
)),
1522
{
1623
reportURI,
1724
reportGroup,
1825
patchHeaders(responseHeaders, capabilities) {
1926
let header = null;
20-
let hasReportTo = false;
27+
let needsReportTo = REPORT_TO_SUPPORTED;
2128
for (let h of responseHeaders) {
2229
if (this.isMine(h)) {
2330
header = h;
24-
h.value = this.inject(h.value, "");
25-
} else if (h.name === REPORT_TO.name && h.value === REPORT_TO.value) {
26-
hasReportTo = true;
31+
h.value = "";
32+
} else if (needsReportTo &&
33+
h.name === REPORT_TO.name && h.value === REPORT_TO.value) {
34+
needsReportTo = false;
2735
}
2836
}
2937

3038
let blocker = capabilities && this.buildFromCapabilities(capabilities);
3139
if (blocker) {
32-
if (!hasReportTo) {
40+
if (needsReportTo) {
3341
responseHeaders.push(REPORT_TO);
3442
}
3543
if (header) {
36-
header.value = this.inject(header.value, blocker);
44+
header.value = blocker;
3745
} else {
3846
header = this.asHeader(blocker);
3947
responseHeaders.push(header);
4048
}
4149
}
42-
50+
4351
return header;
4452
}
4553
}
4654
);
47-
}
55+
}

src/lib/NetCSP.js

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,20 @@
11
"use strict";
22

33
class NetCSP extends CSP {
4-
constructor(start, end) {
4+
constructor(start) {
55
super();
66
this.start = start;
7-
this.end = end;
87
}
9-
8+
109
isMine(header) {
1110
let {name, value} = header;
12-
if (name.toLowerCase() !== CSP.headerName) return false;
13-
let startIdx = value.indexOf(this.start);
14-
return startIdx > -1 && startIdx < value.lastIndexOf(this.end);
11+
return name.toLowerCase() === CSP.headerName && value.startsWith(this.start);
1512
}
16-
17-
inject(headerValue, mine) {
18-
let startIdx = headerValue.indexOf(this.start);
19-
if (startIdx < 0) return `${headerValue};${mine}`;
20-
let endIdx = headerValue.lastIndexOf(this.end);
21-
let retValue = `${headerValue.substring(0, startIdx)}${mine}`;
2213

23-
return endIdx < 0 ? retValue : `${retValue}${headerValue.substring(endIdx + this.end.length + 1)}`;
24-
}
25-
2614
build(...directives) {
27-
return `${this.start}${super.build(...directives)}${this.end}`;
15+
return `${this.start}${super.build(...directives)}`;
2816
}
29-
17+
3018
cleanup(headers) {
3119
}
3220
}

0 commit comments

Comments
 (0)