0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

fix(redirects): escape Location header (#10410)

This commit is contained in:
Arsh 2024-03-13 14:57:39 +05:30 committed by GitHub
parent 1863727215
commit 055fe293c6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 24 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
"astro": patch
---
Fixes an issue where configured redirects could not include certain characters in the target path.

View file

@ -8,7 +8,7 @@ export async function renderRedirect(renderContext: RenderContext) {
const { redirect, redirectRoute } = routeData;
const status =
redirectRoute && typeof redirect === 'object' ? redirect.status : method === 'GET' ? 301 : 308;
const headers = { location: redirectRouteGenerate(renderContext) };
const headers = { location: encodeURI(redirectRouteGenerate(renderContext)) };
return new Response(null, { status, headers });
}

View file

@ -98,6 +98,16 @@ describe('Astro.redirect', () => {
const response = await app.render(request);
assert.equal(response.headers.get('Location'), '/not-verbatim/target3/x/y/z');
});
it('Forwards params to the target path - special characters', async () => {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/source/Las Vegas');
const response = await app.render(request);
assert.equal(
response.headers.get('Location'),
'/not-verbatim/target1/Las%20Vegas%E2%80%99'
);
});
});
});
@ -232,9 +242,17 @@ describe('Astro.redirect', () => {
it('performs dynamic redirects', async () => {
const response = await fixture.fetch('/more/old/hello', { redirect: 'manual' });
assert.equal(response.status, 301);
assert.equal(response.headers.get('Location'), '/more/hello');
});
it('performs dynamic redirects with special characters', async () => {
// encodeURI("/more/old/")
const response = await fixture.fetch("/more/old/%E2%80%99", { redirect: 'manual' });
assert.equal(response.status, 301);
assert.equal(response.headers.get('Location'), "/more/%E2%80%99");
});
it('performs dynamic redirects with multiple params', async () => {
const response = await fixture.fetch('/more/old/hello/world', { redirect: 'manual' });
assert.equal(response.headers.get('Location'), '/more/hello/world');