-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
64 lines (53 loc) · 1.56 KB
/
index.js
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import parse from 'content-security-policy-parser'
/**
* Remove duplicated values from an array.
*
* @param array The array to be handled.
* @returns {*} The array without duplicated values.
*/
function arrayUnique(array) {
const a = array.concat()
for (let i = 0; i < a.length; ++i) {
for (let j = i + 1; j < a.length; ++j) {
if (a[i] === a[j]) a.splice(j--, 1)
}
}
return a
}
/**
* Merges two Content-Security-Policy directives.
*
* @param {string|Object} csp1 The first CSP directives list.
* @param {string|Object} csp2 The second CSP directives list.
* @returns {string} The string of the merged Content-Security-Policy header.
*/
function merge(csp1, csp2) {
if (typeof csp1 === 'string') {
csp1 = parse(csp1)
}
if (typeof csp2 === 'string') {
csp2 = parse(csp2)
}
const csp = {}
// Sets a list of CSP directives from csp1 and csp2 list.
const cspDirectives = arrayUnique(Object.keys(csp1).concat(Object.keys(csp2)))
// Merges directives.
cspDirectives.forEach(function (directive) {
csp[directive] = []
if (typeof csp2[directive] !== 'undefined') {
csp[directive] = csp2[directive]
}
if (typeof csp1[directive] !== 'undefined') {
csp[directive] = arrayUnique(csp[directive].concat(csp1[directive]))
}
csp[directive] = csp[directive].sort()
})
// Generates CSP header string.
let cspString = ''
Object.keys(csp).map(
(directive) =>
(cspString += directive + ' ' + csp[directive].join(' ') + '; '),
)
return cspString.trim().replace(/;*$/, '')
}
export { merge }