Skip to content

fix: redirect trailing slash normalization relatively instead of against root #13719

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions .changeset/honest-pandas-itch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@sveltejs/adapter-node': patch
'@sveltejs/kit': patch
---

fix: redirect trailing slash normalization relatively instead of against root
14 changes: 11 additions & 3 deletions packages/adapter-node/src/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import process from 'node:process';
import sirv from 'sirv';
import { fileURLToPath } from 'node:url';
import { parse as polka_url_parser } from '@polka/url';
import { getRequest, setResponse, createReadableStream } from '@sveltejs/kit/node';
import {
getRequest,
setResponse,
createReadableStream,
relative_pathname
} from '@sveltejs/kit/node';
import { Server } from 'SERVER';
import { manifest, prerendered, base } from 'MANIFEST';
import { env } from 'ENV';
Expand Down Expand Up @@ -81,8 +86,11 @@ function serve_prerendered() {
}

// remove or add trailing slash as appropriate
let location = pathname.at(-1) === '/' ? pathname.slice(0, -1) : pathname + '/';
if (prerendered.has(location)) {
const inverted_trailing_slash =
pathname.at(-1) === '/' ? pathname.slice(0, -1) : pathname + '/';
if (prerendered.has(inverted_trailing_slash)) {
// ensure preservation of (possibly invisible) path prefixes
let location = relative_pathname(pathname, inverted_trailing_slash);
if (query) location += search;
res.writeHead(308, { location }).end();
} else {
Expand Down
1 change: 1 addition & 0 deletions packages/kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"cookie": "^0.6.0",
"devalue": "^5.1.0",
"esm-env": "^1.2.2",
"get-relative-path": "^1.0.2",
"import-meta-resolve": "^4.1.0",
"kleur": "^4.1.5",
"magic-string": "^0.30.5",
Expand Down
2 changes: 2 additions & 0 deletions packages/kit/src/exports/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,5 @@ export async function setResponse(res, response) {
export function createReadableStream(file) {
return /** @type {ReadableStream} */ (Readable.toWeb(createReadStream(file)));
}

export { relative_pathname } from '../../utils/url.js';
13 changes: 9 additions & 4 deletions packages/kit/src/runtime/server/respond.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ import {
method_not_allowed,
redirect_response
} from './utils.js';
import { decode_pathname, decode_params, disable_search, normalize_path } from '../../utils/url.js';
import {
decode_pathname,
decode_params,
disable_search,
normalize_path,
relative_pathname
} from '../../utils/url.js';
import { exec } from '../../utils/routing.js';
import { redirect_json_response, render_data } from './data/index.js';
import { add_cookies_to_headers, get_cookies } from './cookie.js';
Expand Down Expand Up @@ -321,9 +327,8 @@ export async function respond(request, options, manifest, state) {
headers: {
'x-sveltekit-normalize': '1',
location:
// ensure paths starting with '//' are not treated as protocol-relative
(normalized.startsWith('//') ? url.origin + normalized : normalized) +
(url.search === '?' ? '' : url.search)
// ensure preservation of (possibly invisible) path prefixes
relative_pathname(url.pathname, normalized) + (url.search === '?' ? '' : url.search)
}
});
}
Expand Down
13 changes: 13 additions & 0 deletions packages/kit/src/utils/url.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BROWSER, DEV } from 'esm-env';
import getRelativePath from 'get-relative-path';

/**
* Matches a URI scheme. See https://www.rfc-editor.org/rfc/rfc3986#section-3.1
Expand Down Expand Up @@ -77,6 +78,18 @@ export function decode_uri(uri) {
}
}

/**
* Calculates the relative path between two URL pathnames.
* Note that `relative` from `node:path` works with file system paths which are subtly diffent.
* For example: it ignores trailing slashes, which are significant in URLs.
* @param {string} from
* @param {string} to
*/
export function relative_pathname(from, to) {
// TODO inline
return getRelativePath(from, to);
}

/**
* Returns everything up to the first `#` in a URL
* @param {{href: string}} url_like
Expand Down
6 changes: 6 additions & 0 deletions packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2145,6 +2145,12 @@ declare module '@sveltejs/kit/node' {
* @since 2.4.0
*/
export function createReadableStream(file: string): ReadableStream;
/**
* Calculates the relative path between two URL pathnames.
* Note that `relative` from `node:path` works with file system paths which are subtly diffent.
* For example: it ignores trailing slashes, which are significant in URLs.
* */
export function relative_pathname(from: string, to: string): string;

export {};
}
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading