transform-directives-for-browser.js
1.83 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
function createFirefoxPreCSP10Directives (directives, basePolicy) {
var result = Object.assign({}, basePolicy)
Object.keys(directives).forEach(function (key) {
var value = directives[key]
if (key === 'connectSrc') {
result.xhrSrc = value
} else {
result[key] = value
}
if (key === 'scriptSrc') {
var optionsValues = []
if (value.indexOf("'unsafe-inline'") !== -1) {
optionsValues.push('inline-script')
}
if (value.indexOf("'unsafe-eval'") !== -1) {
optionsValues.push('eval-script')
}
if (optionsValues.length !== 0) {
result.options = optionsValues
}
}
})
return result
}
var handlers = {
Firefox: function (browser, directives) {
var version = parseFloat(browser.version)
if (version >= 4 && version < 23) {
var basePolicy = {}
if (version < 5) {
basePolicy.allow = ['*']
if (directives.defaultSrc) {
basePolicy.allow = directives.defaultSrc
delete directives.defaultSrc
}
} else {
basePolicy.defaultSrc = ['*']
}
return createFirefoxPreCSP10Directives(directives, basePolicy)
} else {
return directives
}
},
'Firefox Mobile': function (browser, directives) {
// Handles both Firefox for Android and Firefox OS
var family = browser.os.family
var version = parseFloat(browser.version)
if ((family === 'Firefox OS' && version < 32) || (family === 'Android' && version < 25)) {
return createFirefoxPreCSP10Directives(directives, { defaultSrc: ['*'] })
} else {
return directives
}
}
}
module.exports = function transformDirectivesForBrowser (browser, directives) {
var handler = handlers[browser.name]
if (handler) {
return handler(browser, directives)
} else {
return directives
}
}