Skip to content

Commit ea106d1

Browse files
authored
fix: update redirect logic for URLs ending with '/' in getFilenameFromUrl
1 parent 063213e commit ea106d1

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

.github/workflows/nodejs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ on:
55
branches:
66
- main
77
- next
8+
- send-fix
89
pull_request:
910
branches:
1011
- main

src/middleware.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,6 @@ function wrapper(context) {
505505
/** @type {string} */ (getRequestURL(req)),
506506
extra,
507507
);
508-
509508
if (extra.errorCode) {
510509
if (extra.errorCode === 403) {
511510
context.logger.error(`Malicious path "${filename}".`);

src/utils/getFilenameFromUrl.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const UP_PATH_REGEXP = /(?:^|[\\/])\.\.(?:[\\/]|$)/;
4343
*/
4444

4545
// TODO refactor me in the next major release, this function should return `{ filename, stats, error }`
46-
// TODO fix redirect logic when `/` at the end, like https://github.com/pillarjs/send/blob/master/index.js#L586
46+
// TODO fix redirect logic when `/` at the end, like https://github.com/pillarjs/send/blob/121dda811bae6420ce08171b10685d19c4e53059/index.js#L426
4747
/**
4848
* @template {IncomingMessage} Request
4949
* @template {ServerResponse} Response
@@ -64,6 +64,13 @@ function getFilenameFromUrl(context, url, extra = {}) {
6464
try {
6565
// The `url` property of the `request` is contains only `pathname`, `search` and `hash`
6666
urlObject = memoizedParse(url, false, true);
67+
68+
if (
69+
urlObject?.pathname &&
70+
urlObject.pathname[urlObject.pathname.length - 1] === "/"
71+
) {
72+
urlObject.pathname = path.join(urlObject.pathname, "index.html");
73+
}
6774
} catch {
6875
return;
6976
}
@@ -110,10 +117,7 @@ function getFilenameFromUrl(context, url, extra = {}) {
110117
// `/complex/foo.js` => `foo.js`
111118
// and add outputPath
112119
// `foo.js` => `/home/user/my-project/dist/foo.js`
113-
filename = path.join(
114-
outputPath,
115-
pathname.slice(publicPathPathname.length),
116-
);
120+
filename = path.join(outputPath, pathname);
117121

118122
try {
119123
extra.stats = context.outputFileSystem.statSync(filename);

0 commit comments

Comments
 (0)