Skip to content

Commit 70c510a

Browse files
authored
Merge pull request #112 from linkRace/master
Nonce typo and match store
2 parents fbebe64 + 462de9c commit 70c510a

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

lib/csp.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,23 @@ module.exports = function (options) {
3030

3131
return function csp(req, res, next) {
3232
if (styleNonce) {
33-
if (value.match(/style-src 'nonce-.{48}'/)) {
34-
value = value.replace(value.match(/'style-src nonce-.{48}'/), 'style-src \'nonce-' + res.locals.nonce + '\'');
35-
}
33+
var styleMatch = value.match(/style-src 'nonce-.{48}'/);
34+
if (styleMatch) {
35+
value = value.replace(styleMatch[0], 'style-src \'nonce-' + res.locals.nonce + '\'');
36+
}
3637
else {
3738
value = value.replace('style-src', 'style-src \'nonce-' + res.locals.nonce + '\'');
3839
}
39-
}
40+
}
4041
if (scriptNonce) {
41-
if (value.match(/script-src 'nonce-.{48}'/)) {
42-
value = value.replace(value.match(/script-src 'nonce-.{48}'/)[0], 'script-src \'nonce-' + res.locals.nonce + '\'');
42+
var scriptMatch = value.match(/script-src 'nonce-.{48}'/);
43+
if (scriptMatch) {
44+
value = value.replace(scriptMatch[0], 'script-src \'nonce-' + res.locals.nonce + '\'');
4345
}
4446
else {
4547
value = value.replace('script-src', 'script-src \'nonce-' + res.locals.nonce + '\'');
4648
}
47-
}
49+
}
4850
res.header(name, value);
4951
next();
5052
};

0 commit comments

Comments
 (0)