Merge pull request #19096 from Rob--W/test-server-hardening
Fix path traversal issue in createTemporaryNodeServer
This commit is contained in:
commit
8ae5b4e442
@ -127,9 +127,23 @@ function createTemporaryNodeServer() {
|
|||||||
|
|
||||||
const fs = process.getBuiltinModule("fs"),
|
const fs = process.getBuiltinModule("fs"),
|
||||||
http = process.getBuiltinModule("http");
|
http = process.getBuiltinModule("http");
|
||||||
|
function isAcceptablePath(requestUrl) {
|
||||||
|
try {
|
||||||
|
// Reject unnormalized paths, to protect against path traversal attacks.
|
||||||
|
const url = new URL(requestUrl, "https://localhost/");
|
||||||
|
return url.pathname === requestUrl;
|
||||||
|
} catch {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
// Create http server to serve pdf data for tests.
|
// Create http server to serve pdf data for tests.
|
||||||
const server = http
|
const server = http
|
||||||
.createServer((request, response) => {
|
.createServer((request, response) => {
|
||||||
|
if (!isAcceptablePath(request.url)) {
|
||||||
|
response.writeHead(400);
|
||||||
|
response.end("Invalid path");
|
||||||
|
return;
|
||||||
|
}
|
||||||
const filePath = process.cwd() + "/test/pdfs" + request.url;
|
const filePath = process.cwd() + "/test/pdfs" + request.url;
|
||||||
fs.promises.lstat(filePath).then(
|
fs.promises.lstat(filePath).then(
|
||||||
stat => {
|
stat => {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user