Skip to content

Commit 352d6e7

Browse files
authored
Merge pull request #6 from milo526/master
Remove "'none'" value for merged directives with multiple declarations
2 parents 6e0d6bb + 4c7b0f3 commit 352d6e7

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

packages/csp-header/src/index.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import {
22
ALLOWED_DIRECTIVES,
33
} from './constants/directives';
4+
import {
5+
NONE
6+
} from './constants/values';
47
import {
58
CSPHeaderParams,
69
CSPDirectives,
@@ -138,10 +141,18 @@ function mergeDirectiveRules(directiveValue1: CSPDirectiveValue = '', directiveV
138141
}
139142

140143
if (Array.isArray(directiveValue1) && Array.isArray(directiveValue2)) {
141-
return getUniqRules([
144+
const uniqRules = getUniqRules([
142145
...directiveValue1,
143146
...directiveValue2
144147
]);
148+
149+
const noneIndex = uniqRules.indexOf(NONE);
150+
// Remove "'none'" if there are other rules
151+
if(noneIndex >= 0 && uniqRules.length > 1) {
152+
uniqRules.splice(noneIndex, 1);
153+
}
154+
155+
return uniqRules;
145156
}
146157

147158
return directiveValue2;

packages/csp-header/tests/index.test.ts

+53-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getCSP, CSPDirectiveName, CSPHeaderParams, nonce, SELF } from '../src';
1+
import {CSPDirectiveName, CSPHeaderParams, getCSP, nonce, NONE, SELF} from '../src';
22

33
describe('CSP building', () => {
44
test('should correctly make policy with the only rule', () => {
@@ -107,6 +107,32 @@ describe('Presets', () => {
107107
})).toBe('script-src domain1.com domain2.com;')
108108
});
109109

110+
test('should remove \'none\' directive when merging with well-defined directive', () => {
111+
expect(getCSP({
112+
directives: {
113+
'script-src': [ 'domain1.com' ]
114+
},
115+
presets: [
116+
{
117+
'script-src': [ NONE ]
118+
}
119+
]
120+
})).toBe('script-src domain1.com;')
121+
});
122+
123+
test('should remove \'none\' directive when merging with well-defined preset', () => {
124+
expect(getCSP({
125+
directives: {
126+
'script-src': [ NONE ]
127+
},
128+
presets: [
129+
{
130+
'script-src': [ 'domain2.com' ]
131+
}
132+
]
133+
})).toBe('script-src domain2.com;')
134+
});
135+
110136
test('should work with empty policies', () => {
111137
expect(getCSP({
112138
directives: {},
@@ -206,6 +232,32 @@ describe('Presets', () => {
206232
})).toBe('script-src domain1.com domain2.com;')
207233
});
208234

235+
test('should remove \'none\' directive when merging with well-defined directive', () => {
236+
expect(getCSP({
237+
directives: {
238+
'script-src': [ 'domain1.com' ]
239+
},
240+
presets: {
241+
myPreset: {
242+
'script-src': [ NONE ]
243+
}
244+
}
245+
})).toBe('script-src domain1.com;')
246+
});
247+
248+
test('should remove \'none\' directive when merging with well-defined preset', () => {
249+
expect(getCSP({
250+
directives: {
251+
'script-src': [ NONE ]
252+
},
253+
presets: {
254+
myPreset: {
255+
'script-src': [ 'domain2.com' ]
256+
}
257+
}
258+
})).toBe('script-src domain2.com;')
259+
});
260+
209261
test('should work with empty policies', () => {
210262
expect(getCSP({
211263
directives: {},

0 commit comments

Comments
 (0)