diff --git a/lib/get-file-path-from-url.js b/lib/get-file-path-from-url.js index 22dc157..8d9f28d 100644 --- a/lib/get-file-path-from-url.js +++ b/lib/get-file-path-from-url.js @@ -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" @@ -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