@@ -7,6 +7,19 @@ const multiparty = require( "multiparty" );
7
7
8
8
let cspLog = "" ;
9
9
10
+ /**
11
+ * Like `readFileSync`, but on error returns "ERROR"
12
+ * without crashing.
13
+ * @param path
14
+ */
15
+ function readFileSync ( path ) {
16
+ try {
17
+ return fs . readFileSync ( path ) ;
18
+ } catch ( e ) {
19
+ return "ERROR" ;
20
+ }
21
+ }
22
+
10
23
/**
11
24
* Keep in sync with /test/mock.php
12
25
*/
@@ -143,7 +156,7 @@ const mocks = {
143
156
} ,
144
157
xmlOverJsonp : function ( req , resp ) {
145
158
const callback = req . query . callback ;
146
- const body = fs . readFileSync ( `${ __dirname } /data/with_fries.xml` ) . toString ( ) ;
159
+ const body = readFileSync ( `${ __dirname } /data/with_fries.xml` ) . toString ( ) ;
147
160
resp . writeHead ( 200 ) ;
148
161
resp . end ( `${ cleanCallback ( callback ) } (${ JSON . stringify ( body ) } )\n` ) ;
149
162
} ,
@@ -238,8 +251,9 @@ const mocks = {
238
251
} ,
239
252
testHTML : function ( req , resp ) {
240
253
resp . writeHead ( 200 , { "Content-Type" : "text/html" } ) ;
241
- const body = fs
242
- . readFileSync ( `${ __dirname } /data/test.include.html` )
254
+ const body = readFileSync (
255
+ `${ __dirname } /data/test.include.html`
256
+ )
243
257
. toString ( )
244
258
. replace ( / { { baseURL} } / g, req . query . baseURL ) ;
245
259
resp . end ( body ) ;
@@ -250,17 +264,19 @@ const mocks = {
250
264
"Content-Security-Policy" : "default-src 'self'; require-trusted-types-for 'script'; " +
251
265
"report-uri /test/data/mock.php?action=cspLog"
252
266
} ) ;
253
- const body = fs . readFileSync ( `${ __dirname } /data/csp.include.html` ) . toString ( ) ;
267
+ const body = readFileSync ( `${ __dirname } /data/csp.include.html` ) . toString ( ) ;
254
268
resp . end ( body ) ;
255
269
} ,
256
270
cspNonce : function ( req , resp ) {
257
- const testParam = req . query . test ? `-${ req . query . test } ` : "" ;
271
+ const testParam = req . query . test ?
272
+ `-${ req . query . test . replace ( / [ ^ a - z 0 - 9 ] / gi, "" ) } ` :
273
+ "" ;
258
274
resp . writeHead ( 200 , {
259
275
"Content-Type" : "text/html" ,
260
276
"Content-Security-Policy" : "script-src 'nonce-jquery+hardcoded+nonce'; " +
261
277
"report-uri /test/data/mock.php?action=cspLog"
262
278
} ) ;
263
- const body = fs . readFileSync (
279
+ const body = readFileSync (
264
280
`${ __dirname } /data/csp-nonce${ testParam } .html` ) . toString ( ) ;
265
281
resp . end ( body ) ;
266
282
} ,
@@ -270,7 +286,7 @@ const mocks = {
270
286
"Content-Security-Policy" : "script-src 'self'; " +
271
287
"report-uri /test/data/mock.php?action=cspLog"
272
288
} ) ;
273
- const body = fs . readFileSync (
289
+ const body = readFileSync (
274
290
`${ __dirname } /data/csp-ajax-script.html` ) . toString ( ) ;
275
291
resp . end ( body ) ;
276
292
} ,
@@ -290,7 +306,7 @@ const mocks = {
290
306
"Content-Security-Policy" : "require-trusted-types-for 'script'; " +
291
307
"report-uri /test/data/mock.php?action=cspLog"
292
308
} ) ;
293
- const body = fs . readFileSync ( `${ __dirname } /data/trusted-html.html` ) . toString ( ) ;
309
+ const body = readFileSync ( `${ __dirname } /data/trusted-html.html` ) . toString ( ) ;
294
310
resp . end ( body ) ;
295
311
} ,
296
312
trustedTypesAttributes : function ( _req , resp ) {
@@ -299,7 +315,7 @@ const mocks = {
299
315
"Content-Security-Policy" : "require-trusted-types-for 'script'; " +
300
316
"report-uri /test/data/mock.php?action=cspLog"
301
317
} ) ;
302
- const body = fs . readFileSync (
318
+ const body = readFileSync (
303
319
`${ __dirname } /data/trusted-types-attributes.html` ) . toString ( ) ;
304
320
resp . end ( body ) ;
305
321
} ,
0 commit comments