Skip to content
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

fix(core): double encoding during match of routes #13303

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .changeset/stale-oranges-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes an issue where the dev server was applying second decoding of the URL of the incoming request, causing issues for certain URLs.
2 changes: 1 addition & 1 deletion packages/astro/src/core/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export class App {
if (!pathname) {
pathname = prependForwardSlash(this.removeBase(url.pathname));
}
let routeData = matchRoute(pathname, this.#manifestData);
let routeData = matchRoute(decodeURI(pathname), this.#manifestData);

// missing routes fall-through, pre rendered are handled by static layer
if (!routeData || routeData.prerender) return undefined;
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ async function getPathsForRoute(
// NOTE: The same URL may match multiple routes in the manifest.
// Routing priority needs to be verified here for any duplicate
// paths to ensure routing priority rules are enforced in the final build.
const matchedRoute = matchRoute(staticPath, options.routesList);
const matchedRoute = matchRoute(decodeURI(staticPath), options.routesList);
return matchedRoute === route;
});

Expand Down
7 changes: 3 additions & 4 deletions packages/astro/src/core/routing/match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@ import { SERVER_ISLAND_BASE_PREFIX, SERVER_ISLAND_COMPONENT } from '../server-is

/** Find matching route from pathname */
export function matchRoute(pathname: string, manifest: RoutesList): RouteData | undefined {
const decodedPathname = decodeURI(pathname);
return manifest.routes.find((route) => {
return (
route.pattern.test(decodedPathname) ||
route.fallbackRoutes.some((fallbackRoute) => fallbackRoute.pattern.test(decodedPathname))
route.pattern.test(pathname) ||
route.fallbackRoutes.some((fallbackRoute) => fallbackRoute.pattern.test(pathname))
);
});
}

/** Finds all matching routes from pathname */
export function matchAllRoutes(pathname: string, manifest: RoutesList): RouteData[] {
return manifest.routes.filter((route) => route.pattern.test(decodeURI(pathname)));
return manifest.routes.filter((route) => route.pattern.test(pathname));
}

const ROUTE404_RE = /^\/404\/?$/;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
export const prerender = false;
export function getStaticPaths() {
return [
{ params: { slug: `_'"()&%` } },
]
}
---
<h1>Hello {Astro.url} </h1>
14 changes: 14 additions & 0 deletions packages/astro/test/params.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,20 @@
const $ = cheerio.load(html);
assert.equal($('.category').text(), '你好');
});

it('should not decode twice', async () => {
try {
await fixture
.fetch(
"/d/_'%22()&%25",
)
.then((res) => res.text());
assert.ok(true);
} catch (e) {
console.log(e);

Check warning on line 126 in packages/astro/test/params.test.js

View workflow job for this annotation

GitHub Actions / Lint

lint/suspicious/noConsoleLog

Don't use console.log
assert.fail('Should not throw an error: ' + e.message);
}
});
});

describe('Astro.params in static mode', () => {
Expand Down
Loading