Skip to content

Commit be4f9ec

Browse files
committed
Build: Make middleware-mockserver not crash on reading nonexistent files
`fs.readFileSync` crashes when a non-existing file is passed to it. Some APIs of `middleware-mockserver` read a file the path of which depends on query parameters, making it possible to crash it by providing such a parameter. The old PHP server doesn't have these issues. To fix this, wrap all `fs.readFileSync` occurrences with a function that falls back to the string `"ERROR"`. Closes jquerygh-5579 (cherry picked from commit d5ebb46)
1 parent 7dad5cb commit be4f9ec

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

test/middleware-mockserver.cjs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@ const getRawBody = require( "raw-body" );
66

77
let cspLog = "";
88

9+
/**
10+
* Like `readFileSync`, but on error returns "ERROR"
11+
* without crashing.
12+
* @param path
13+
*/
14+
function readFileSync( path ) {
15+
try {
16+
return fs.readFileSync( path );
17+
} catch ( _ ) {
18+
return "ERROR";
19+
}
20+
}
21+
922
/**
1023
* Keep in sync with /test/mock.php
1124
*/
@@ -142,7 +155,7 @@ const mocks = {
142155
},
143156
xmlOverJsonp: function( req, resp ) {
144157
const callback = req.query.callback;
145-
const body = fs.readFileSync( `${ __dirname }/data/with_fries.xml` ).toString();
158+
const body = readFileSync( `${ __dirname }/data/with_fries.xml` ).toString();
146159
resp.writeHead( 200 );
147160
resp.end( `${ cleanCallback( callback ) }(${ JSON.stringify( body ) })\n` );
148161
},
@@ -224,8 +237,9 @@ const mocks = {
224237
},
225238
testHTML: function( req, resp ) {
226239
resp.writeHead( 200, { "Content-Type": "text/html" } );
227-
const body = fs
228-
.readFileSync( `${ __dirname }/data/test.include.html` )
240+
const body = readFileSync(
241+
`${ __dirname }/data/test.include.html`
242+
)
229243
.toString()
230244
.replace( /{{baseURL}}/g, req.query.baseURL );
231245
resp.end( body );
@@ -236,17 +250,19 @@ const mocks = {
236250
"Content-Security-Policy": "default-src 'self'; " +
237251
"report-uri /test/data/mock.php?action=cspLog"
238252
} );
239-
const body = fs.readFileSync( `${ __dirname }/data/csp.include.html` ).toString();
253+
const body = readFileSync( `${ __dirname }/data/csp.include.html` ).toString();
240254
resp.end( body );
241255
},
242256
cspNonce: function( req, resp ) {
243-
const testParam = req.query.test ? `-${ req.query.test }` : "";
257+
const testParam = req.query.test ?
258+
`-${ req.query.test.replace( /[^a-z0-9]/gi, "" ) }` :
259+
"";
244260
resp.writeHead( 200, {
245261
"Content-Type": "text/html",
246262
"Content-Security-Policy": "script-src 'nonce-jquery+hardcoded+nonce'; " +
247263
"report-uri /test/data/mock.php?action=cspLog"
248264
} );
249-
const body = fs.readFileSync(
265+
const body = readFileSync(
250266
`${ __dirname }/data/csp-nonce${ testParam }.html` ).toString();
251267
resp.end( body );
252268
},

0 commit comments

Comments
 (0)