import path from "node:path"; import querystring from "node:querystring"; import getPaths from "./getPaths.js"; /** @typedef {import("../index.js").IncomingMessage} IncomingMessage */ /** @typedef {import("../index.js").OutputFileSystem} OutputFileSystem */ /** @typedef {import("../index.js").ServerResponse} ServerResponse */ /** @typedef {import("fs").Stats} FSStats */ /** * @param {string} url url * @returns {string} decoded pathname */ function getPathnameFromUrl(url) { return querystring.unescape(new URL(url, "http://localhost").pathname); } const UP_PATH_REGEXP = /(?:^|[\\/])\.\.(?:[\\/]|$)/; /** * @typedef {object} Extra * @property {FSStats} stats stats * @property {boolean=} immutable true when immutable, otherwise false * @property {OutputFileSystem} outputFileSystem output file system */ /** * decodeURIComponent. * * Allows V8 to only deoptimize this fn instead of all of send(). * @param {string} input * @returns {string} */ class FilenameError extends Error { /** * @param {string} message message * @param {number=} code error code */ constructor(message, code) { super(message); this.name = "FilenameError"; this.statusCode = code; } } /** @typedef {{ filename: string, extra: Extra }} FilenameWithExtra */ /** * @param {unknown} error error * @returns {boolean} true when error is like not found, otherwise false */ function isNotFoundError(error) { switch (/** @type {NodeJS.ErrnoException} */ (error).code) { case "ENAMETOOLONG": case "ENOENT": case "ENOTDIR": return true; default: return false; } } // TODO fix redirect logic when `/` at the end, like https://github.com/pillarjs/send/blob/master/index.js#L586 /** * @template {IncomingMessage} Request * @template {ServerResponse} Response * @param {import("../index.js").FilledContext} context context * @param {string} url url * @returns {FilenameWithExtra | undefined} result of get filename from url */ function getFilenameFromUrl(context, url) { const { options } = context; const paths = getPaths(context); const index = options.index === false ? /** @type {string[]} */ ([]) : typeof options.index === "undefined" || options.index === true ? ["index.html"] : [options.index]; /** @type {string} */ let pathname; try { pathname = getPathnameFromUrl(url); } catch { return; } for (const { compilation, publicPath, outputPath, outputFileSystem, } of paths) { /** @type {string | undefined} */ let filename; /** @type {string} */ let publicPathPathname; try { publicPathPathname = getPathnameFromUrl( publicPath !== "auto" && publicPath ? publicPath : "/", ); } catch { continue; } if ( pathname && publicPathPathname && pathname.startsWith(publicPathPathname) ) { // Null byte(s) if (pathname.includes("\0")) { throw new FilenameError("Bad Request", 400); } // Strip the public path prefix from the decoded pathname: // `/complex/foo.js` => `foo.js`. const relativePathname = pathname.slice(publicPathPathname.length); // Only decoded paths containing two consecutive dots can produce a // parent-directory segment through `path.join`. const mayHaveUpPath = relativePathname.includes(".."); // Stripping a public path without a trailing slash can turn // `/assets../secret` into `../secret`. Check the remainder before joining. if ( mayHaveUpPath && UP_PATH_REGEXP.test(path.normalize(`./${relativePathname}`)) ) { throw new FilenameError("Forbidden", 403); } // Add outputPath: `foo.js` => `/home/user/my-project/dist/foo.js`. filename = path.join(outputPath, relativePathname); if (mayHaveUpPath) { // Keep the final containment check as a backstop for platform-specific // path behavior, without adding work for ordinary requests. const relative = path.relative(outputPath, filename); if (relative === ".." || relative.startsWith(`..${path.sep}`)) { throw new FilenameError("Forbidden", 403); } } /** @type {boolean | undefined} */ let immutable = undefined; /** * @param {FSStats} stats stats * @returns {Extra} extra */ const createExtra = (stats) => ({ // Lazy evaluate immutable because it may be expensive to get asset info get immutable() { if (immutable === undefined) { immutable = Boolean( compilation.getAsset(relativePathname)?.info?.immutable, ); } return immutable; }, outputFileSystem, stats, }); /** * @param {string} filename filename * @param {Set=} visited visited filenames * @returns {FilenameWithExtra | undefined} filename when found, otherwise undefined */ const resolveIndex = (filename, visited = new Set()) => { if (index.length === 0) { return; } const nextFilename = path.join(filename, index[0]); // Guard against index values like "" or "." that never advance // the path, and against longer cycles such as "..". if (visited.has(nextFilename)) { return; } visited.add(nextFilename); filename = nextFilename; let stats; try { stats = outputFileSystem.statSync(filename); } catch (error) { if (isNotFoundError(error)) { return; } throw error; } if (/** @type {FSStats} */ (stats).isDirectory()) { return resolveIndex(filename, visited); } return { filename, extra: createExtra(/** @type {FSStats} */ (stats)), }; }; /** * @param {string} filename filename * @returns {FilenameWithExtra | undefined} filename when found, otherwise undefined */ const resolveFile = (filename) => { let stats; try { stats = outputFileSystem.statSync(filename); } catch (error) { if (isNotFoundError(error)) { return; } throw error; } if (/** @type {FSStats} */ (stats).isDirectory()) { // Different from send: we resolve the index file instead of issuing a redirect. return resolveIndex(filename); } if (filename.endsWith(path.sep)) { return; } return { filename, extra: createExtra(/** @type {FSStats} */ (stats)), }; }; if (index.length > 0 && pathname.endsWith("/")) { const result = resolveIndex(filename); if (!result) { continue; } return result; } const result = resolveFile(filename); if (!result) { continue; } return result; } } } export { FilenameError }; export default getFilenameFromUrl;