Build: Make middleware-mockserver not crash on reading nonexistent files
Some checks failed
Browserstack / ${{ matrix.BROWSER }} (Chrome_latest) (push) Has been cancelled
Browserstack / ${{ matrix.BROWSER }} (Chrome_latest-1) (push) Has been cancelled
Browserstack / ${{ matrix.BROWSER }} (Edge_latest) (push) Has been cancelled
Browserstack / ${{ matrix.BROWSER }} (Edge_latest-1) (push) Has been cancelled
Browserstack / ${{ matrix.BROWSER }} (Firefox_latest) (push) Has been cancelled
Browserstack / ${{ matrix.BROWSER }} (Firefox_latest-1) (push) Has been cancelled
Browserstack / ${{ matrix.BROWSER }} (IE_11) (push) Has been cancelled
Browserstack / ${{ matrix.BROWSER }} (Opera_latest) (push) Has been cancelled
Browserstack / ${{ matrix.BROWSER }} (Safari_latest) (push) Has been cancelled
Browserstack / ${{ matrix.BROWSER }} (Safari_latest-1) (push) Has been cancelled
Browserstack / ${{ matrix.BROWSER }} (__iOS_15) (push) Has been cancelled
Browserstack / ${{ matrix.BROWSER }} (__iOS_16) (push) Has been cancelled
Browserstack / ${{ matrix.BROWSER }} (__iOS_17) (push) Has been cancelled
Code scanning - action / CodeQL-Build (push) Has been cancelled
Filestash / Update Filestash (push) Has been cancelled
Node / ${{ matrix.NPM_SCRIPT }} - ${{ matrix.NAME }} (${{ matrix.NODE_VERSION }}) (Chrome, 20.x, test:esm) (push) Has been cancelled
Node / ${{ matrix.NPM_SCRIPT }} - ${{ matrix.NAME }} (${{ matrix.NODE_VERSION }}) (Chrome, 20.x, test:no-deprecated) (push) Has been cancelled
Node / ${{ matrix.NPM_SCRIPT }} - ${{ matrix.NAME }} (${{ matrix.NODE_VERSION }}) (Chrome, 20.x, test:selector-native) (push) Has been cancelled
Node / ${{ matrix.NPM_SCRIPT }} - ${{ matrix.NAME }} (${{ matrix.NODE_VERSION }}) (Chrome, 20.x, test:slim) (push) Has been cancelled
Node / ${{ matrix.NPM_SCRIPT }} - ${{ matrix.NAME }} (${{ matrix.NODE_VERSION }}) (Chrome/Firefox, 20.x, test:browser) (push) Has been cancelled
Node / ${{ matrix.NPM_SCRIPT }} - ${{ matrix.NAME }} (${{ matrix.NODE_VERSION }}) (Firefox ESR (new), 20.x, test:firefox) (push) Has been cancelled
Node / ${{ matrix.NPM_SCRIPT }} - ${{ matrix.NAME }} (${{ matrix.NODE_VERSION }}) (Firefox ESR (old), 20.x, test:firefox) (push) Has been cancelled
Node / ${{ matrix.NPM_SCRIPT }} - ${{ matrix.NAME }} (${{ matrix.NODE_VERSION }}) (Node, 18.x, test:browserless) (push) Has been cancelled
Node / ${{ matrix.NPM_SCRIPT }} - ${{ matrix.NAME }} (${{ matrix.NODE_VERSION }}) (Node, 20.x, lint) (push) Has been cancelled
Node / ${{ matrix.NPM_SCRIPT }} - ${{ matrix.NAME }} (${{ matrix.NODE_VERSION }}) (Node, 20.x, test:browserless) (push) Has been cancelled
Node / ${{ matrix.NPM_SCRIPT }} - ${{ matrix.NAME }} (${{ matrix.NODE_VERSION }}) (Node, 22.x, test:browserless) (push) Has been cancelled
Node / ${{ matrix.NPM_SCRIPT }} - ${{ matrix.NAME }} (${{ matrix.NODE_VERSION }}) (Node, 23.x, test:browserless) (push) Has been cancelled
Node / test:ie - IE (push) Has been cancelled
Node / test:safari - Safari (push) Has been cancelled

`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 gh-5579
This commit is contained in:
Michał Gołębiowski-Owczarek 2024-11-05 22:54:34 +01:00 committed by GitHub
parent 329661fd53
commit d5ebb464de
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -7,6 +7,19 @@ const multiparty = require( "multiparty" );
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
*/
@ -143,7 +156,7 @@ const mocks = {
},
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` );
},
@ -238,8 +251,9 @@ const mocks = {
},
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 );
@ -250,17 +264,19 @@ const mocks = {
"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 );
},
@ -270,7 +286,7 @@ const mocks = {
"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 );
},
@ -290,7 +306,7 @@ const mocks = {
"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 ) {
@ -299,7 +315,7 @@ const mocks = {
"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 );
},