summaryrefslogtreecommitdiff
path: root/src/lib/NetCSP.js
blob: 90ef8adc8246f96237e6a6effa21d193f25c42cb (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
"use strict";

class NetCSP extends CSP {
  constructor(start, end) {
    super();
    this.start = start;
    this.end = end;
  }
  
  isMine(header) {
    let {name, value} = header;
    if (name.toLowerCase() !== CSP.headerName) return false;
    let startIdx = value.indexOf(this.start);
    return startIdx > -1 && startIdx < value.lastIndexOf(this.end);
  }
  
  inject(headerValue, mine) {
    let startIdx = headerValue.indexOf(this.start);
    if (startIdx < 0) return `${headerValue};${mine}`;
    let endIdx = headerValue.lastIndexOf(this.end);
    let retValue = `${headerValue.substring(0, startIdx)}${mine}`;

    return endIdx < 0 ? retValue : `${retValue}${headerValue.substring(endIdx + this.end.length + 1)}`;
  }
  
  build(...directives) {
    return `${this.start}${super.build(...directives)}${this.end}`;
  }
  
  cleanup(headers) {
  }
}