Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

fix(edge-runtime): match path with URI-encoded chars #2873

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 3 commits into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions 14 edge-runtime/lib/routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,14 +424,24 @@ export interface MiddlewareMatcher {
missing?: RouteHas[]
}

const decodeMaybeEncodedPath = (path: string): string => {
try {
return decodeURIComponent(path)
} catch {
return path
}
}

export function getMiddlewareRouteMatcher(matchers: MiddlewareMatcher[]): MiddlewareRouteMatch {
return (
pathname: string | null | undefined,
unsafePathname: string | null | undefined,
req: Pick<Request, 'headers' | 'url'>,
query: Params,
) => {
const pathname = decodeMaybeEncodedPath(unsafePathname ?? '')

for (const matcher of matchers) {
const routeMatch = new RegExp(matcher.regexp).exec(pathname!)
const routeMatch = new RegExp(matcher.regexp).exec(pathname)
if (!routeMatch) {
continue
}
Expand Down
9 changes: 9 additions & 0 deletions 9 tests/e2e/edge-middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,15 @@ test("requests with x-middleware-subrequest don't skip middleware (GHSA-f82v-jwr
expect(response.headers.get('x-test-used-next-version')).toBe('15.2.2')
})

test('requests with different encoding than matcher match anyway', async ({
middlewareStaticAssetMatcher,
}) => {
const response = await fetch(`${middlewareStaticAssetMatcher.url}/hello%2Fworld.txt`)

// middleware was not skipped
expect(await response.text()).toBe('hello from middleware')
})

test.describe('RSC cache poisoning', () => {
test('Middleware rewrite', async ({ page, middleware }) => {
const prefetchResponsePromise = new Promise<Response>((resolve) => {
Expand Down
2 changes: 0 additions & 2 deletions 2 tests/fixtures/middleware-og/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
"build": "next build"
},
"dependencies": {
"@aws-amplify/adapter-nextjs": "^1.0.18",
"aws-amplify": "^6.0.18",
"next": "latest",
"react": "18.2.0",
"react-dom": "18.2.0"
Expand Down
12 changes: 12 additions & 0 deletions 12 tests/fixtures/middleware-static-asset-matcher/app/layout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const metadata = {
title: 'Simple Next App',
description: 'Description for Simple Next App',
}

export default function RootLayout({ children }) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
7 changes: 7 additions & 0 deletions 7 tests/fixtures/middleware-static-asset-matcher/app/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function Home() {
return (
<main>
<h1>Home</h1>
</main>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function middleware() {
return new Response('hello from middleware')
}

export const config = {
matcher: '/hello/world.txt',
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
output: 'standalone',
eslint: {
ignoreDuringBuilds: true,
},
}

module.exports = nextConfig
15 changes: 15 additions & 0 deletions 15 tests/fixtures/middleware-static-asset-matcher/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "middleware",
"version": "0.1.0",
"private": true,
"scripts": {
"postinstall": "next build",
"dev": "next dev",
"build": "next build"
},
"dependencies": {
"next": "latest",
"react": "18.2.0",
"react-dom": "18.2.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello from a static asset
3 changes: 2 additions & 1 deletion 3 tests/utils/create-e2e-fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,10 +333,11 @@ export const fixtureFactories = {
pnpm: () => createE2EFixture('pnpm', { packageManger: 'pnpm' }),
bun: () => createE2EFixture('simple', { packageManger: 'bun' }),
middleware: () => createE2EFixture('middleware'),
middlewareSubrequestVuln: () => createE2EFixture('middleware-subrequest-vuln'),
middlewareI18nExcludedPaths: () => createE2EFixture('middleware-i18n-excluded-paths'),
middlewareOg: () => createE2EFixture('middleware-og'),
middlewarePages: () => createE2EFixture('middleware-pages'),
middlewareStaticAssetMatcher: () => createE2EFixture('middleware-static-asset-matcher'),
middlewareSubrequestVuln: () => createE2EFixture('middleware-subrequest-vuln'),
pageRouter: () => createE2EFixture('page-router'),
pageRouterBasePathI18n: () => createE2EFixture('page-router-base-path-i18n'),
turborepo: () =>
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.