Repository navigation
Respect path-segment boundaries when stripping the configured base #17701
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
+186
−58
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8071dd2
Respect path-segment boundaries when stripping the configured base
matthewp 875491c
Merge remote-tracking branch 'origin/main' into fix/base-path-segment…
matthewp 4c3e068
Use stripRequestBase in matchRequest and cover the app.match path
matthewp c69b9d7
Merge branch 'main' into fix/base-path-segment-boundary
matthewp 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,6 @@ | ||
| --- | ||
| 'astro': patch | ||
| '@astrojs/internal-helpers': patch | ||
| --- | ||
|
|
||
| Fixes base path stripping to respect path-segment boundaries. With a configured `base` such as `/docs`, a request like `/docs-archive/page` is no longer treated as being under the base, so routing and `context.url.pathname` now agree on the same pathname. |
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
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
146 changes: 146 additions & 0 deletions
146
packages/astro/test/units/app/base-prefix-boundary.test.ts
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,146 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import { describe, it } from 'node:test'; | ||
| import type { MiddlewareHandler } from '../../../dist/types/public/common.js'; | ||
| 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, createRouteInfo } from './test-helpers.ts'; | ||
|
|
||
| /** | ||
| * Tests that base stripping respects path-segment boundaries. | ||
| * | ||
| * With `base: '/app'`, only `/app` and paths under `/app/` belong to the base. | ||
| * Stripping the base with a plain prefix check treats `/appX/admin` as under the | ||
| * base and slices it to `/admin`, so the router matches `/admin` while | ||
| * `context.url.pathname` still reads `/appX/admin`. Middleware keyed on the | ||
| * base-prefixed pathname then sees a path it does not recognize. | ||
| */ | ||
|
|
||
| const routeOptions: Parameters<typeof parseRoute>[1] = { | ||
| config: { base: '/app', trailingSlash: 'ignore' }, | ||
| pageExtensions: [], | ||
| } as any; | ||
|
|
||
| const adminRouteData = parseRoute('admin', routeOptions, { | ||
| component: 'src/pages/admin.astro', | ||
| }); | ||
|
|
||
| const publicRouteData = parseRoute('index.astro', routeOptions, { | ||
| component: 'src/pages/index.astro', | ||
| }); | ||
|
|
||
| const adminPage = createComponent((_result: any, _props: any, _slots: any) => { | ||
| return render`<h1>Admin Panel</h1>`; | ||
| }); | ||
|
|
||
| const publicPage = createComponent((_result: any, _props: any, _slots: any) => { | ||
| return render`<h1>Public</h1>`; | ||
| }); | ||
|
|
||
| const pageMap = new Map([ | ||
| [ | ||
| adminRouteData.component, | ||
| async () => ({ | ||
| page: async () => ({ | ||
| default: adminPage, | ||
| }), | ||
| }), | ||
| ], | ||
| [ | ||
| publicRouteData.component, | ||
| async () => ({ | ||
| page: async () => ({ | ||
| default: publicPage, | ||
| }), | ||
| }), | ||
| ], | ||
| ]); | ||
|
|
||
| /** | ||
| * Middleware that blocks access to the base-prefixed `/app/admin` route, | ||
| * as recommended in the official Astro authentication docs. | ||
| */ | ||
| function createAuthMiddleware() { | ||
| return (async () => ({ | ||
| onRequest: (async (context, next) => { | ||
| const pathname = context.url.pathname; | ||
| if (pathname === '/app/admin' || pathname.startsWith('/app/admin/')) { | ||
| return new Response('Forbidden', { status: 403 }); | ||
| } | ||
| return next(); | ||
| }) satisfies MiddlewareHandler, | ||
| })) as () => Promise<{ onRequest: MiddlewareHandler }>; | ||
| } | ||
|
|
||
| function createApp(middleware: ReturnType<typeof createAuthMiddleware>) { | ||
| return new App( | ||
| createManifest({ | ||
| base: '/app', | ||
| routes: [createRouteInfo(adminRouteData), createRouteInfo(publicRouteData)], | ||
| pageMap: pageMap as any, | ||
| middleware: middleware as any, | ||
| }) as any, | ||
| ); | ||
| } | ||
|
|
||
| describe('base stripping respects path-segment boundaries', () => { | ||
| it('middleware blocks /app/admin with normal request', async () => { | ||
| const app = createApp(createAuthMiddleware()); | ||
| const request = new Request('http://example.com/app/admin'); | ||
| const response = await app.render(request); | ||
| assert.equal(response.status, 403, '/app/admin should be blocked by middleware'); | ||
| }); | ||
|
|
||
| it('middleware blocks /appX/admin (single-character prefix extension)', async () => { | ||
| const app = createApp(createAuthMiddleware()); | ||
| const request = new Request('http://example.com/appX/admin'); | ||
| const response = await app.render(request); | ||
| assert.notEqual( | ||
| response.status, | ||
| 200, | ||
| '/appX/admin should not resolve to the protected /admin route', | ||
| ); | ||
| }); | ||
|
|
||
| it('middleware blocks /app2/admin (digit prefix extension)', async () => { | ||
| const app = createApp(createAuthMiddleware()); | ||
| const request = new Request('http://example.com/app2/admin'); | ||
| const response = await app.render(request); | ||
| assert.notEqual( | ||
| response.status, | ||
| 200, | ||
| '/app2/admin should not resolve to the protected /admin route', | ||
| ); | ||
| }); | ||
|
|
||
| it('middleware blocks /app-/admin (punctuation prefix extension)', async () => { | ||
| const app = createApp(createAuthMiddleware()); | ||
| const request = new Request('http://example.com/app-/admin'); | ||
| const response = await app.render(request); | ||
| assert.notEqual( | ||
| response.status, | ||
| 200, | ||
| '/app-/admin should not resolve to the protected /admin route', | ||
| ); | ||
| }); | ||
|
|
||
| it('app.match does not resolve /appX/admin to the protected route', () => { | ||
| const app = createApp(createAuthMiddleware()); | ||
| const request = new Request('http://example.com/appX/admin'); | ||
| const routeData = app.match(request); | ||
| assert.equal(routeData, undefined, '/appX/admin should not match a route under the base'); | ||
| }); | ||
|
|
||
| it('middleware blocks /appX/admin when the adapter supplies routeData', async () => { | ||
| const app = createApp(createAuthMiddleware()); | ||
| const request = new Request('http://example.com/appX/admin'); | ||
| // Adapters may call app.match() and thread the result back into render(). | ||
| const routeData = app.match(request); | ||
| const response = await app.render(request, { routeData }); | ||
| assert.notEqual( | ||
| response.status, | ||
| 200, | ||
| '/appX/admin should not render the protected /admin route via the routeData render path', | ||
| ); | ||
| }); | ||
| }); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.