@@ -43,11 +43,6 @@ const allowGoogleTranslate = ({ 'connect-src':connectSrc, 'img-src':imgSrc, ...o
4343const contentSecurityPolicies = {
4444 'backend-strict' : {
4545 block : {
46- 'default-src' : 'NOTE:FROM-BACKEND:block' ,
47- 'form-action' : 'NOTE:FROM-BACKEND:block' ,
48- 'frame-ancestors' : 'NOTE:FROM-BACKEND:block' ,
49- } ,
50- reportOnly : {
5146 'default-src' : [
5247 reportSample ,
5348 none ,
@@ -59,16 +54,8 @@ const contentSecurityPolicies = {
5954 } ,
6055 } ,
6156 'backend-unmodified' : {
62- block : {
63- 'default-src' : 'NOTE:FROM-BACKEND:block' ,
64- 'form-action' : 'NOTE:FROM-BACKEND:block' ,
65- 'frame-ancestors' : 'NOTE:FROM-BACKEND:block' ,
66- } ,
67- reportOnly : {
68- 'default-src' : 'NOTE:FROM-BACKEND:reportOnly' ,
69- 'form-action' : 'NOTE:FROM-BACKEND:reportOnly' ,
70- 'frame-ancestors' : 'NOTE:FROM-BACKEND:reportOnly' ,
71- } ,
57+ block : 'NOTE:FROM-BACKEND:block' ,
58+ reportOnly : 'NOTE:FROM-BACKEND:reportOnly' ,
7259 } ,
7360 'blank-html' : {
7461 block : allowGoogleTranslate ( {
@@ -122,11 +109,7 @@ const contentSecurityPolicies = {
122109 } ) ,
123110 } ,
124111 enketo : {
125- block : {
126- 'default-src' : 'NOTE:FROM-BACKEND:block' ,
127- 'form-action' : 'NOTE:FROM-BACKEND:block' ,
128- 'frame-ancestors' : 'NOTE:FROM-BACKEND:block' ,
129- } ,
112+ block : 'NOTE:FROM-BACKEND:block' ,
130113 reportOnly : allowGoogleTranslate ( {
131114 'default-src' : [
132115 reportSample ,
@@ -262,36 +245,39 @@ describe('Content-Security-Policy definitions', () => {
262245 if ( ! policy ) continue ;
263246
264247 describe ( `header: ${ headerNames [ headerType ] } ` , ( ) => {
265- it ( `should have required directives: ${ requiredDirectives } ` , ( ) => {
266- assert . containsAllKeys ( policy , requiredDirectives ) ;
267- } ) ;
268-
269- Object . entries ( policy )
270- . map ( ( [ key , directive ] ) => [ key , asArray ( directive ) ] )
271- . filter ( ( [ key , directive ] ) => ! ( directive . length === 1 && directive [ 0 ] === `NOTE:FROM-BACKEND:${ headerType } ` ) ) // eslint-disable-line no-unused-vars
272- . forEach ( ( [ key , directive ] ) => {
273- describe ( `directive: ${ key } ` , ( ) => {
274- if ( supportsReportSample . includes ( key ) ) {
275- if ( key . startsWith ( 'style-src' ) && directive . includes ( `'unsafe-inline'` ) ) {
276- // For style-* directives, report-sample will only provide a sample of inline violations.
277- it ( `should not include 'report-sample' in directive '${ key } ' when 'unsafe-inline' is allowed` , ( ) => {
278- // expect
279- assert . notInclude ( directive , "'report-sample'" ) ;
280- } ) ;
248+ if ( typeof policy === 'string' ) {
249+ if ( ! policy . startsWith ( 'NOTE:FROM-BACKEND:' ) ) throw new Error ( `Unexpected policy string: '${ policy } '` ) ;
250+ } else {
251+ it ( `should have required directives: ${ requiredDirectives } ` , ( ) => {
252+ assert . containsAllKeys ( policy , requiredDirectives ) ;
253+ } ) ;
254+
255+ Object . entries ( policy )
256+ . map ( ( [ key , directive ] ) => [ key , asArray ( directive ) ] )
257+ . forEach ( ( [ key , directive ] ) => {
258+ describe ( `directive: ${ key } ` , ( ) => {
259+ if ( supportsReportSample . includes ( key ) ) {
260+ if ( key . startsWith ( 'style-src' ) && directive . includes ( `'unsafe-inline'` ) ) {
261+ // For style-* directives, report-sample will only provide a sample of inline violations.
262+ it ( `should not include 'report-sample' in directive '${ key } ' when 'unsafe-inline' is allowed` , ( ) => {
263+ // expect
264+ assert . notInclude ( directive , "'report-sample'" ) ;
265+ } ) ;
266+ } else {
267+ it ( `should include 'report-sample' in directive '${ key } '` , ( ) => {
268+ // expect
269+ assert . include ( directive , "'report-sample'" ) ;
270+ } ) ;
271+ }
281272 } else {
282- it ( `should include 'report-sample' in directive '${ key } '` , ( ) => {
273+ it ( `should not include 'report-sample' in directive '${ key } '` , ( ) => {
283274 // expect
284- assert . include ( directive , "'report-sample'" ) ;
275+ assert . notInclude ( directive , "'report-sample'" ) ;
285276 } ) ;
286277 }
287- } else {
288- it ( `should not include 'report-sample' in directive '${ key } '` , ( ) => {
289- // expect
290- assert . notInclude ( directive , "'report-sample'" ) ;
291- } ) ;
292- }
278+ } ) ;
293279 } ) ;
294- } ) ;
280+ }
295281 } ) ;
296282 }
297283 } ) ;
@@ -1182,9 +1168,13 @@ function assertSecurityHeaders(res, { csp }) {
11821168function assertCsp ( actual , expected ) {
11831169 if ( ! expected ) return assert . isNull ( actual ) ;
11841170
1185- assert . deepEqualInAnyOrder (
1186- actual ?. split ( '; ' ) ,
1187- Object . entries ( expected )
1188- . map ( ( [ k , v ] ) => `${ k } ${ asArray ( v ) . join ( ' ' ) } ` ) ,
1189- ) ;
1171+ if ( typeof expected === 'string' ) {
1172+ assert . equal ( actual , expected ) ;
1173+ } else {
1174+ assert . deepEqualInAnyOrder (
1175+ actual ?. split ( '; ' ) ,
1176+ Object . entries ( expected )
1177+ . map ( ( [ k , v ] ) => `${ k } ${ asArray ( v ) . join ( ' ' ) } ` ) ,
1178+ ) ;
1179+ }
11901180}
0 commit comments