Skip to content

Commit

Permalink
log error and ignore for external redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
lilnasy committed Dec 4, 2023
1 parent 729bdd3 commit adbe10d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/astro/src/core/logger/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
13 changes: 13 additions & 0 deletions packages/astro/src/core/routing/manifest/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions packages/astro/test/redirects.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe('Astro.redirect', () => {
adapter: testAdapter(),
redirects: {
'/api/redirect': '/test',
'/external/redirect': 'https://example.com/',
},
});
await fixture.build();
Expand All @@ -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');
Expand Down

0 comments on commit adbe10d

Please sign in to comment.