Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -813,15 +813,15 @@ describe('createServeRequestHandler', () => {
).resolves.toBe('500 Internal Server Error');
});

// TODO fix: answer 400, like other ".." segments going above the root
it('redirects ".." segments that reach the name of the fake root', async () => {
// Like other ".." segments going above the root
it('answers 400 to ".." segments that reach the name of the fake root', async () => {
await expect(
serveRequest(
{baseUrl: '/', trailingSlash: undefined},
'/../__docusaurus_root__/file.html',
{asIs: true},
),
).resolves.toBe('301 /file → 200 file.html');
).resolves.toBe('400 Bad Request');
});

// Browsers send the baseUrl percent-encoded: "/caf%C3%A9/"
Expand Down
15 changes: 14 additions & 1 deletion packages/docusaurus/src/commands/serve/requestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,20 @@ async function getRedirectLocation(
// Like parseURLPath(), but undefined when ".." segments go above the root
// It also keeps "//host/path" as a path instead of parsing another host
function parseRootURLPath(url: string): URLPath | undefined {
const fakeRoot = '/__docusaurus_root__';
// ".." segments going above the root can reach the name of a fake root,
// like "/../__docusaurus_root__/file", but not the names of 2 fake roots
const urlPath = parseFakeRootURLPath(url, '/__docusaurus_root__');
const otherUrlPath = parseFakeRootURLPath(url, '/__docusaurus_other_root__');
if (urlPath === undefined || otherUrlPath === undefined) {
return undefined;
}
return urlPath;
}

function parseFakeRootURLPath(
url: string,
fakeRoot: string,
): URLPath | undefined {
const {pathname, search} = parseURLPath(`.${url}`, `${fakeRoot}/`);
if (!pathname.startsWith(`${fakeRoot}/`)) {
return undefined;
Expand Down
Loading