Skip to content

Commit 572f565

Browse files
bjohansebasalexander-akait
authored andcommitted
fix: update getFilenameFromUrl to return errorCode and filename
1 parent 063213e commit 572f565

3 files changed

Lines changed: 16 additions & 18 deletions

File tree

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ function wdm(compiler, options = {}) {
279279

280280
// API
281281
instance.getFilenameFromUrl = (url, extra) =>
282-
getFilenameFromUrl(filledContext, url, extra);
282+
getFilenameFromUrl(filledContext, url, extra)?.filename;
283283

284284
instance.waitUntilValid = (callback = noop) => {
285285
ready(filledContext, callback);

src/middleware.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -498,22 +498,20 @@ function wrapper(context) {
498498
*/
499499
async function processRequest() {
500500
// Pipe and SendFile
501-
/** @type {import("./utils/getFilenameFromUrl").Extra} */
502-
const extra = {};
503-
const filename = getFilenameFromUrl(
501+
const { filename, extra, errorCode } = getFilenameFromUrl(
504502
context,
505503
/** @type {string} */ (getRequestURL(req)),
506-
extra,
504+
{},
507505
);
508506

509-
if (extra.errorCode) {
510-
if (extra.errorCode === 403) {
507+
if (errorCode) {
508+
if (errorCode === 403) {
511509
context.logger.error(`Malicious path "${filename}".`);
512510
}
513511

514512
await sendError(
515-
extra.errorCode === 400 ? "Bad Request" : "Forbidden",
516-
extra.errorCode,
513+
errorCode === 400 ? "Bad Request" : "Forbidden",
514+
errorCode,
517515
{
518516
modifyResponseData: context.options.modifyResponseData,
519517
},

src/utils/getFilenameFromUrl.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ const UP_PATH_REGEXP = /(?:^|[\\/])\.\.(?:[\\/]|$)/;
3030
/**
3131
* @typedef {object} Extra
3232
* @property {import("fs").Stats=} stats stats
33-
* @property {number=} errorCode error code
3433
* @property {boolean=} immutable true when immutable, otherwise false
3534
*/
3635

@@ -42,30 +41,31 @@ const UP_PATH_REGEXP = /(?:^|[\\/])\.\.(?:[\\/]|$)/;
4241
* @returns {string}
4342
*/
4443

45-
// TODO refactor me in the next major release, this function should return `{ filename, stats, error }`
4644
// TODO fix redirect logic when `/` at the end, like https://github.com/pillarjs/send/blob/master/index.js#L586
4745
/**
4846
* @template {IncomingMessage} Request
4947
* @template {ServerResponse} Response
5048
* @param {import("../index.js").FilledContext<Request, Response>} context context
5149
* @param {string} url url
5250
* @param {Extra=} extra extra
53-
* @returns {string | undefined} filename
51+
* @returns {{ filename?: string, extra: Extra, errorCode?: number }} filename
5452
*/
5553
function getFilenameFromUrl(context, url, extra = {}) {
5654
const { options } = context;
5755
const paths = getPaths(context);
5856

5957
/** @type {string | undefined} */
6058
let foundFilename;
59+
/** @type {number | undefined} */
60+
let errorCode;
6161
/** @type {import("node:url").Url} */
6262
let urlObject;
6363

6464
try {
6565
// The `url` property of the `request` is contains only `pathname`, `search` and `hash`
6666
urlObject = memoizedParse(url, false, true);
6767
} catch {
68-
return;
68+
return { errorCode, filename: foundFilename, extra };
6969
}
7070

7171
for (const { publicPath, outputPath, assetsInfo } of paths) {
@@ -94,16 +94,16 @@ function getFilenameFromUrl(context, url, extra = {}) {
9494
) {
9595
// Null byte(s)
9696
if (pathname.includes("\0")) {
97-
extra.errorCode = 400;
97+
errorCode = 400;
9898

99-
return;
99+
return { errorCode, filename: foundFilename, extra };
100100
}
101101

102102
// ".." is malicious
103103
if (UP_PATH_REGEXP.test(path.normalize(`./${pathname}`))) {
104-
extra.errorCode = 403;
104+
errorCode = 403;
105105

106-
return;
106+
return { errorCode, filename: foundFilename, extra };
107107
}
108108

109109
// Strip the `pathname` property from the `publicPath` option from the start of requested url
@@ -161,7 +161,7 @@ function getFilenameFromUrl(context, url, extra = {}) {
161161
}
162162
}
163163

164-
return foundFilename;
164+
return { filename: foundFilename, extra, errorCode };
165165
}
166166

167167
module.exports = getFilenameFromUrl;

0 commit comments

Comments
 (0)