Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion lib/get-file-path-from-url.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const path = require('path');
const fs = require('fs');

/**
* Safely get the path for a file in the project directory, or reject by returning "dummy"
Expand Down Expand Up @@ -33,7 +34,21 @@ function getFilePathFromUrl(url, basePath, { pathLib = path, baseHref = '' } = {
}
}

const absolutePath = pathLib.join(basePath, relativePath);
let absolutePath = pathLib.join(basePath, relativePath);
if ( absolutePath.endsWith(pathLib.sep) ) {
// By convention, accessing a path routes to a default document in
// that directory if it exists.
// Commonly used default documents.
const defaultDocs = [ "index.html", "index.htm", "index.php" ];
// See if one of these exists.
for ( let d of defaultDocs ) {
if ( fs.existsSync( absolutePath + d ) ) {
absolutePath += d;
break;
}
}
}

if (
!absolutePath.startsWith(basePath) || // if the path has broken out of the basePath, it should be rejected
absolutePath.endsWith(pathLib.sep) // only files (not folders) can be served
Expand Down