From adbe10dc8f9377544d9a89e749b281cc1f87bfed Mon Sep 17 00:00:00 2001 From: lilnasy <69170106+lilnasy@users.noreply.github.com> Date: Mon, 4 Dec 2023 22:18:09 +0000 Subject: [PATCH] log error and ignore for external redirects --- packages/astro/src/core/logger/core.ts | 1 + packages/astro/src/core/routing/manifest/create.ts | 13 +++++++++++++ packages/astro/test/redirects.test.js | 9 +++++++++ 3 files changed, 23 insertions(+) diff --git a/packages/astro/src/core/logger/core.ts b/packages/astro/src/core/logger/core.ts index b3f3b725673eb..db571d8f14da4 100644 --- a/packages/astro/src/core/logger/core.ts +++ b/packages/astro/src/core/logger/core.ts @@ -26,6 +26,7 @@ export type LoggerLabel = | 'watch' | 'middleware' | 'preferences' + | 'redirects' // SKIP_FORMAT: A special label that tells the logger not to apply any formatting. // Useful for messages that are already formatted, like the server start message. | 'SKIP_FORMAT'; diff --git a/packages/astro/src/core/routing/manifest/create.ts b/packages/astro/src/core/routing/manifest/create.ts index c9c0bb071685f..7e5d530823115 100644 --- a/packages/astro/src/core/routing/manifest/create.ts +++ b/packages/astro/src/core/routing/manifest/create.ts @@ -451,6 +451,19 @@ export function createRouteManifest( .map(([{ dynamic, content }]) => (dynamic ? `[${content}]` : content)) .join('/')}`.toLowerCase(); + { + let destination: string + if (typeof to === "string") { + destination = to + } + else { + destination = to.destination + } + if (destination.startsWith("http")) { + return logger.error('redirects', `Redirecting to an external URLs is not supported: ${from} -> ${to}`); + } + } + const routeData: RouteData = { type: 'redirect', route, diff --git a/packages/astro/test/redirects.test.js b/packages/astro/test/redirects.test.js index bf9ad15f56f3a..b9417643d059d 100644 --- a/packages/astro/test/redirects.test.js +++ b/packages/astro/test/redirects.test.js @@ -14,6 +14,7 @@ describe('Astro.redirect', () => { adapter: testAdapter(), redirects: { '/api/redirect': '/test', + '/external/redirect': 'https://example.com/', }, }); await fixture.build(); @@ -27,6 +28,14 @@ describe('Astro.redirect', () => { expect(response.headers.get('location')).to.equal('/login'); }); + it('Ignores external redirect', async () => { + const app = await fixture.loadTestAdapterApp(); + const request = new Request('http://example.com/external/redirect'); + const response = await app.render(request); + expect(response.status).to.equal(404); + expect(response.headers.get('location')).to.equal(null); + }); + it('Warns when used inside a component', async () => { const app = await fixture.loadTestAdapterApp(); const request = new Request('http://example.com/late');