let cspLog = "";
+/**
+ * Like `readFileSync`, but on error returns "ERROR"
+ * without crashing.
+ * @param path
+ */
+function readFileSync( path ) {
+ try {
+ return fs.readFileSync( path );
+ } catch ( e ) {
+ return "ERROR";
+ }
+}
+
/**
* Keep in sync with /test/mock.php
*/
},
xmlOverJsonp: function( req, resp ) {
const callback = req.query.callback;
- const body = fs.readFileSync( `${ __dirname }/data/with_fries.xml` ).toString();
+ const body = readFileSync( `${ __dirname }/data/with_fries.xml` ).toString();
resp.writeHead( 200 );
resp.end( `${ cleanCallback( callback ) }(${ JSON.stringify( body ) })\n` );
},
},
testHTML: function( req, resp ) {
resp.writeHead( 200, { "Content-Type": "text/html" } );
- const body = fs
- .readFileSync( `${ __dirname }/data/test.include.html` )
+ const body = readFileSync(
+ `${ __dirname }/data/test.include.html`
+ )
.toString()
.replace( /{{baseURL}}/g, req.query.baseURL );
resp.end( body );
"Content-Security-Policy": "default-src 'self'; require-trusted-types-for 'script'; " +
"report-uri /test/data/mock.php?action=cspLog"
} );
- const body = fs.readFileSync( `${ __dirname }/data/csp.include.html` ).toString();
+ const body = readFileSync( `${ __dirname }/data/csp.include.html` ).toString();
resp.end( body );
},
cspNonce: function( req, resp ) {
- const testParam = req.query.test ? `-${ req.query.test }` : "";
+ const testParam = req.query.test ?
+ `-${ req.query.test.replace( /[^a-z0-9]/gi, "" ) }` :
+ "";
resp.writeHead( 200, {
"Content-Type": "text/html",
"Content-Security-Policy": "script-src 'nonce-jquery+hardcoded+nonce'; " +
"report-uri /test/data/mock.php?action=cspLog"
} );
- const body = fs.readFileSync(
+ const body = readFileSync(
`${ __dirname }/data/csp-nonce${ testParam }.html` ).toString();
resp.end( body );
},
"Content-Security-Policy": "script-src 'self'; " +
"report-uri /test/data/mock.php?action=cspLog"
} );
- const body = fs.readFileSync(
+ const body = readFileSync(
`${ __dirname }/data/csp-ajax-script.html` ).toString();
resp.end( body );
},
"Content-Security-Policy": "require-trusted-types-for 'script'; " +
"report-uri /test/data/mock.php?action=cspLog"
} );
- const body = fs.readFileSync( `${ __dirname }/data/trusted-html.html` ).toString();
+ const body = readFileSync( `${ __dirname }/data/trusted-html.html` ).toString();
resp.end( body );
},
trustedTypesAttributes: function( _req, resp ) {
"Content-Security-Policy": "require-trusted-types-for 'script'; " +
"report-uri /test/data/mock.php?action=cspLog"
} );
- const body = fs.readFileSync(
+ const body = readFileSync(
`${ __dirname }/data/trusted-types-attributes.html` ).toString();
resp.end( body );
},