summaryrefslogtreecommitdiff
path: root/src/bg/ReportingCSP.js
blob: 03926c2cc8a1d8a26494384e17ee3b1847103f62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"use strict";
  
function ReportingCSP(reportURI, reportGroup) {
  const REPORT_TO = {
    name: "Report-To",
    value: JSON.stringify({ "url": reportURI,
             "group": reportGroup,
             "max-age": 10886400 }),
  };
  return Object.assign(
    new CapsCSP(new NetCSP(
      `report-uri ${reportURI};`,
      `;report-to ${reportGroup};`
    )), 
    {
      reportURI,
      reportGroup,
      patchHeaders(responseHeaders, capabilities) {
        let header = null;
        let hasReportTo = false;
        for (let h of responseHeaders) {
          if (this.isMine(h)) {
            header = h;
            h.value = this.inject(h.value, "");
          } else if (h.name === REPORT_TO.name && h.value === REPORT_TO.value) {
            hasReportTo = true;
          }
        }

        let blocker = capabilities && this.buildFromCapabilities(capabilities);
        if (blocker) {
          if (!hasReportTo) {
            responseHeaders.push(REPORT_TO);
          }
          if (header) {
            header.value = this.inject(header.value, blocker);
          } else {
            header = this.asHeader(blocker);
            responseHeaders.push(header);
          }
        }
        
        return header;
      }
    }
  );
}