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

Prevent double uri decoding #9532

Merged
merged 1 commit into from
Dec 27, 2023
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
5 changes: 5 additions & 0 deletions .changeset/old-goats-occur.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Prevents unnecessary URI decoding when rendering a route
4 changes: 2 additions & 2 deletions packages/astro/src/core/routing/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ export function getParams(array: string[]) {
const params: Params = {};
array.forEach((key, i) => {
if (key.startsWith('...')) {
params[key.slice(3)] = match[i + 1] ? decodeURIComponent(match[i + 1]) : undefined;
params[key.slice(3)] = match[i + 1] ? match[i + 1] : undefined;
} else {
params[key] = decodeURIComponent(match[i + 1]);
params[key] = match[i + 1];
Comment on lines -15 to +17
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This getParams function is being called at

function getRouteParams(route: RouteData, pathname: string): Params | undefined {
if (route.params.length) {
// The RegExp pattern expects a decoded string, but the pathname is encoded
// when the URL contains non-English characters.
const paramsMatch = route.pattern.exec(decodeURIComponent(pathname));
if (paramsMatch) {
return getParams(route.params)(paramsMatch);
}
}
}

As shown, it already calls decodeURIComponent, so we don't have to call decodeURIComponent here too.

}
});
return params;
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/vite-plugin-astro-server/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export async function handleRequest({
if (config.trailingSlash === 'never' && !incomingRequest.url) {
pathname = '';
} else {
pathname = decodeURI(url.pathname);
pathname = url.pathname;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't look like we need to decode it here. It also doesn't work well with the line below:

// Add config.base back to url before passing it to SSR
url.pathname = removeTrailingForwardSlash(config.base) + url.pathname;

(URLs are always encoded)

Removing this decodeURI seems to not break things. It affects dev only. The prod SSR "app" also doesn't decode the url, so I think this makes it more consistent.

}

// Add config.base back to url before passing it to SSR
Expand Down
10 changes: 10 additions & 0 deletions packages/astro/test/ssr-params.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,14 @@ describe('Astro.params in SSR', () => {
expect($('.category').text()).to.equal('food');
});
});

it('No double URL decoding', async () => {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/users/houston/%25');
const response = await app.render(request);
expect(response.status).to.equal(200);
const html = await response.text();
const $ = cheerio.load(html);
expect($('.category').text()).to.equal('%');
});
});
Loading