forked from helmetjs/csp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
209 lines (181 loc) · 5.41 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
var _ = require("lodash");
var camelize = require("camelize");
var platform = require("platform");
var ALL_HEADERS = [
"X-Content-Security-Policy",
"Content-Security-Policy",
"X-WebKit-CSP"
];
var DIRECTIVES = [
"default-src",
"script-src",
"object-src",
"img-src",
"media-src",
"frame-src",
"frame-ancestors",
"font-src",
"connect-src",
"style-src",
"report-uri",
"sandbox"
];
var MUST_BE_QUOTED = [
"none",
"self",
"unsafe-inline",
"unsafe-eval"
];
module.exports = function csp(options) {
options = _.clone(options) || { "default-src": ["'self'"] };
var reportOnly = options.reportOnly || false;
var setAllHeaders = options.setAllHeaders || false;
var safari5 = options.safari5 || false;
DIRECTIVES.forEach(function (directive) {
// Normalize camelCase to spinal-case
var cameledKey = camelize(directive);
var cameledValue = options[cameledKey];
if (cameledValue && (cameledKey !== directive)) {
if (options[directive]) {
throw new Error(directive + " and " + cameledKey + " specified. Specify just one.");
}
options[directive] = cameledValue;
}
var value = options[directive];
if (!value) {
return;
}
// Normalize to array
if (!Array.isArray(value)) {
if (directive === "sandbox" && value === true) {
options[directive] = [];
} else if (_.isString(value)) {
options[directive] = value.split(/\s/g);
} else {
throw new Error("Invalid directive: " + directive + " " + value);
}
value = options[directive];
}
// Check quoted source
MUST_BE_QUOTED.forEach(function (must) {
if (value.indexOf(must) !== -1) {
throw new Error(value + " must be quoted");
}
});
});
if (reportOnly && !options["report-uri"]) {
throw new Error("Please remove reportOnly or add a report-uri.");
}
return function csp(req, res, next) {
var headers = [];
var policy = {};
var browser = platform.parse(req.headers["user-agent"]);
var version = parseFloat(browser.version);
var unknownBrowser = false;
DIRECTIVES.forEach(function (directive) {
var value = options[directive];
if (value) {
// Clone the array so we don"t later mutate `options` by mistake
policy[directive] = value.slice();
}
});
switch (browser.name) {
case "IE":
if (version >= 10) {
headers.push("X-Content-Security-Policy");
if (!setAllHeaders) {
if (policy.sandbox) {
policy = { sandbox: policy.sandbox };
} else {
policy = {};
}
}
}
break;
case "Firefox":
if (version >= 23) {
headers.push("Content-Security-Policy");
} else if ((version >= 4) && (version < 23)) {
headers.push("X-Content-Security-Policy");
policy["default-src"] = policy["default-src"] || ["*"];
Object.keys(policy).forEach(function (key) {
var value = policy[key];
if (key === "connect-src") {
policy["xhr-src"] = value;
} else if (key === "default-src") {
if (version < 5) {
policy.allow = value;
} else {
policy["default-src"] = value;
}
} else if (key !== "sandbox") {
policy[key] = value;
}
var index;
if ((index = policy[key].indexOf("'unsafe-inline'")) !== -1) {
if (key === "script-src") {
policy[key][index] = "'inline-script'";
} else {
policy[key].splice(index, 1);
}
}
if ((index = policy[key].indexOf("'unsafe-eval'")) !== -1) {
if (key === "script-src") {
policy[key][index] = "'eval-script'";
} else {
policy[key].splice(index, 1);
}
}
});
}
break;
case "Chrome":
if ((version >= 14) && (version < 25)) {
headers.push("X-WebKit-CSP");
} else if (version >= 25) {
headers.push("Content-Security-Policy");
}
// older versions of Chrome don't support frame-ancestors,
// and will crash the tab if we send the header
if (version <= 40 && policy['frame-ancestors']) {
delete policy['frame-ancestors'];
}
break;
case "Safari":
if (version >= 7) {
headers.push("Content-Security-Policy");
} else if ((version >= 6) || ((version >= 5.1) && safari5)) {
headers.push("X-WebKit-CSP");
}
break;
case "Opera":
if (version >= 15) {
headers.push("Content-Security-Policy");
}
break;
case "Chrome Mobile":
if (version >= 14) {
headers.push("Content-Security-Policy");
}
break;
default:
unknownBrowser = true;
}
var policyString = _.map(policy, function (value, key) {
return [key].concat(value).join(" ");
}).join(";");
if (setAllHeaders || unknownBrowser) {
headers = ALL_HEADERS;
}
if (policyString) {
headers.forEach(function (header) {
var headerName = header;
if (reportOnly) {
headerName += "-Report-Only";
}
res.setHeader(headerName, policyString);
});
}
next();
};
};