-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Harden URL pathname normalization to collapse multiple leading slashes #15717
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| 'astro': patch | ||
| --- | ||
|
|
||
| Hardens URL pathname normalization in the SSR request pipeline to collapse multiple leading slashes before routing and middleware execution | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
162 changes: 162 additions & 0 deletions
162
packages/astro/test/units/app/double-slash-bypass.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| // @ts-check | ||
| import assert from 'node:assert/strict'; | ||
| import { describe, it } from 'node:test'; | ||
| import { App } from '../../../dist/core/app/app.js'; | ||
| import { parseRoute } from '../../../dist/core/routing/parse-route.js'; | ||
| import { createComponent, render } from '../../../dist/runtime/server/index.js'; | ||
| import { createManifest } from './test-helpers.js'; | ||
|
|
||
| /** | ||
| * Security tests for double-slash URL prefix middleware authorization bypass. | ||
| * | ||
| * Vulnerability: A normalization inconsistency between route matching and middleware | ||
| * URL construction allows bypassing middleware-based authorization by prepending an | ||
| * extra `/` to the URL path (e.g., `//admin` instead of `/admin`). | ||
| * | ||
| * - `removeBase("//admin")` strips one slash → router matches `/admin` | ||
| * - `context.url.pathname` preserves `//admin` → middleware `startsWith("/admin")` fails | ||
| * | ||
| * See: withastro/astro-security#5 | ||
| * CWE-647: Use of Non-Canonical URL Paths for Authorization Decisions | ||
| * CWE-285: Improper Authorization | ||
| */ | ||
|
|
||
| const routeOptions = /** @type {Parameters<typeof parseRoute>[1]} */ ( | ||
| /** @type {any} */ ({ | ||
| config: { base: '/', trailingSlash: 'ignore' }, | ||
| pageExtensions: [], | ||
| }) | ||
| ); | ||
|
|
||
| const adminRouteData = parseRoute('admin', routeOptions, { | ||
| component: 'src/pages/admin.astro', | ||
| }); | ||
|
|
||
| const dashboardRouteData = parseRoute('dashboard', routeOptions, { | ||
| component: 'src/pages/dashboard.astro', | ||
| }); | ||
|
|
||
| const publicRouteData = parseRoute('index.astro', routeOptions, { | ||
| component: 'src/pages/index.astro', | ||
| }); | ||
|
|
||
| const adminPage = createComponent(() => { | ||
| return render`<h1>Admin Panel</h1>`; | ||
| }); | ||
|
|
||
| const dashboardPage = createComponent(() => { | ||
| return render`<h1>Dashboard</h1>`; | ||
| }); | ||
|
|
||
| const publicPage = createComponent(() => { | ||
| return render`<h1>Public</h1>`; | ||
| }); | ||
|
|
||
| const pageMap = new Map([ | ||
| [ | ||
| adminRouteData.component, | ||
| async () => ({ | ||
| page: async () => ({ | ||
| default: adminPage, | ||
| }), | ||
| }), | ||
| ], | ||
| [ | ||
| dashboardRouteData.component, | ||
| async () => ({ | ||
| page: async () => ({ | ||
| default: dashboardPage, | ||
| }), | ||
| }), | ||
| ], | ||
| [ | ||
| publicRouteData.component, | ||
| async () => ({ | ||
| page: async () => ({ | ||
| default: publicPage, | ||
| }), | ||
| }), | ||
| ], | ||
| ]); | ||
|
|
||
| /** | ||
| * Middleware that blocks access to /admin and /dashboard routes, | ||
| * as recommended in the official Astro authentication docs. | ||
| * @returns {() => Promise<{onRequest: import('../../../dist/types/public/common.js').MiddlewareHandler}>} | ||
| */ | ||
| function createAuthMiddleware() { | ||
| return async () => ({ | ||
| onRequest: /** @type {import('../../../dist/types/public/common.js').MiddlewareHandler} */ ( | ||
| async (context, next) => { | ||
| const protectedPaths = ['/admin', '/dashboard']; | ||
| if (protectedPaths.some((p) => context.url.pathname.startsWith(p))) { | ||
| return new Response('Forbidden', { status: 403 }); | ||
| } | ||
| return next(); | ||
| } | ||
| ), | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * @param {ReturnType<typeof createAuthMiddleware>} middleware | ||
| */ | ||
| function createApp(middleware) { | ||
| return new App( | ||
| createManifest({ | ||
| routes: [ | ||
| { routeData: adminRouteData }, | ||
| { routeData: dashboardRouteData }, | ||
| { routeData: publicRouteData }, | ||
| ], | ||
| pageMap, | ||
| middleware, | ||
| }), | ||
| ); | ||
| } | ||
|
|
||
| describe('Security: double-slash URL prefix middleware bypass', () => { | ||
| it('middleware blocks /admin with normal request', async () => { | ||
| const app = createApp(createAuthMiddleware()); | ||
| const request = new Request('http://example.com/admin'); | ||
| const response = await app.render(request); | ||
| assert.equal(response.status, 403, '/admin should be blocked by middleware'); | ||
| }); | ||
|
|
||
| it('middleware blocks //admin (double-slash bypass attempt)', async () => { | ||
| const app = createApp(createAuthMiddleware()); | ||
| const request = new Request('http://example.com//admin'); | ||
| const response = await app.render(request); | ||
| assert.equal(response.status, 403, '//admin should also be blocked by middleware'); | ||
| }); | ||
|
|
||
| it('middleware blocks ///admin (triple-slash bypass attempt)', async () => { | ||
| const app = createApp(createAuthMiddleware()); | ||
| const request = new Request('http://example.com///admin'); | ||
| const response = await app.render(request); | ||
| assert.equal(response.status, 403, '///admin should also be blocked by middleware'); | ||
| }); | ||
|
|
||
| it('middleware blocks //dashboard (double-slash on another protected route)', async () => { | ||
| const app = createApp(createAuthMiddleware()); | ||
| const request = new Request('http://example.com//dashboard'); | ||
| const response = await app.render(request); | ||
| assert.equal(response.status, 403, '//dashboard should also be blocked by middleware'); | ||
| }); | ||
|
|
||
| it('middleware blocks //admin/ (double-slash with trailing slash)', async () => { | ||
| const app = createApp(createAuthMiddleware()); | ||
| const request = new Request('http://example.com//admin/'); | ||
| const response = await app.render(request); | ||
| assert.equal(response.status, 403, '//admin/ should also be blocked by middleware'); | ||
| }); | ||
|
|
||
| it('public route is still accessible', async () => { | ||
| const app = createApp(createAuthMiddleware()); | ||
| const request = new Request('http://example.com/'); | ||
| const response = await app.render(request); | ||
| assert.equal(response.status, 200, '/ should be accessible'); | ||
| const html = await response.text(); | ||
| assert.match(html, /Public/); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please update this changeset so that it's more use-facing. What we fixed by showing a possible use case
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to avoid this coming across as alarmist; users should be checking these things in their own middleware, but it's good if we can prevent cases where they do not.
Happy to reword but not sure how to do so.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll give it a try.