Skip to content

Commit

Permalink
fix(redirects): escape Location header (#10410)
Browse files Browse the repository at this point in the history
  • Loading branch information
lilnasy committed Mar 13, 2024
1 parent 1863727 commit 055fe29
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/khaki-bears-enjoy.md
@@ -0,0 +1,5 @@
---
"astro": patch
---

Fixes an issue where configured redirects could not include certain characters in the target path.
2 changes: 1 addition & 1 deletion packages/astro/src/core/redirects/render.ts
Expand Up @@ -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 });
}

Expand Down
18 changes: 18 additions & 0 deletions packages/astro/test/redirects.test.js
Expand Up @@ -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'
);
});
});
});

Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit 055fe29

Please sign in to comment.