@@ -7,6 +7,19 @@ const multiparty = require( "multiparty" );
77
88let cspLog = "" ;
99
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+
1023/**
1124 * Keep in sync with /test/mock.php
1225 */
@@ -143,7 +156,7 @@ const mocks = {
143156} ,
144157xmlOverJsonp :function ( req , resp ) {
145158const 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 ( ) ;
147160resp . writeHead ( 200 ) ;
148161resp . end ( `${ cleanCallback ( callback ) } (${ JSON . stringify ( body ) } )\n` ) ;
149162} ,
@@ -238,8 +251,9 @@ const mocks = {
238251} ,
239252testHTML :function ( req , resp ) {
240253resp . 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+ )
243257. toString ( )
244258. replace ( / { { baseURL} } / g, req . query . baseURL ) ;
245259resp . end ( body ) ;
@@ -250,17 +264,19 @@ const mocks = {
250264"Content-Security-Policy" :"default-src 'self'; require-trusted-types-for 'script'; " +
251265"report-uri /test/data/mock.php?action=cspLog"
252266} ) ;
253- const body = fs . readFileSync ( `${ __dirname } /data/csp.include.html` ) . toString ( ) ;
267+ const body = readFileSync ( `${ __dirname } /data/csp.include.html` ) . toString ( ) ;
254268resp . end ( body ) ;
255269} ,
256270cspNonce :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+ "" ;
258274resp . writeHead ( 200 , {
259275"Content-Type" :"text/html" ,
260276"Content-Security-Policy" :"script-src 'nonce-jquery+hardcoded+nonce'; " +
261277"report-uri /test/data/mock.php?action=cspLog"
262278} ) ;
263- const body = fs . readFileSync (
279+ const body = readFileSync (
264280`${ __dirname } /data/csp-nonce${ testParam } .html` ) . toString ( ) ;
265281resp . end ( body ) ;
266282} ,
@@ -270,7 +286,7 @@ const mocks = {
270286"Content-Security-Policy" :"script-src 'self'; " +
271287"report-uri /test/data/mock.php?action=cspLog"
272288} ) ;
273- const body = fs . readFileSync (
289+ const body = readFileSync (
274290`${ __dirname } /data/csp-ajax-script.html` ) . toString ( ) ;
275291resp . end ( body ) ;
276292} ,
@@ -290,7 +306,7 @@ const mocks = {
290306"Content-Security-Policy" :"require-trusted-types-for 'script'; " +
291307"report-uri /test/data/mock.php?action=cspLog"
292308} ) ;
293- const body = fs . readFileSync ( `${ __dirname } /data/trusted-html.html` ) . toString ( ) ;
309+ const body = readFileSync ( `${ __dirname } /data/trusted-html.html` ) . toString ( ) ;
294310resp . end ( body ) ;
295311} ,
296312trustedTypesAttributes :function ( _req , resp ) {
@@ -299,7 +315,7 @@ const mocks = {
299315"Content-Security-Policy" :"require-trusted-types-for 'script'; " +
300316"report-uri /test/data/mock.php?action=cspLog"
301317} ) ;
302- const body = fs . readFileSync (
318+ const body = readFileSync (
303319`${ __dirname } /data/trusted-types-attributes.html` ) . toString ( ) ;
304320resp . end ( body ) ;
305321} ,