-
Notifications
You must be signed in to change notification settings - Fork 29.9k
[middleware]: add upper bound to cloneBodyStream #84539
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
ijjk
merged 2 commits into
canary
from
10-05-_middleware_add_upper_bound_to_clonebodystream
Oct 8, 2025
Merged
Changes from all commits
Commits
Show all changes
2 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
Some comments aren't visible on the classic Files Changed page.
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import { NextRequest, NextResponse } from 'next/server' | ||
|
|
||
| export async function POST(request: NextRequest) { | ||
| return new NextResponse('Hello World', { status: 200 }) | ||
| } |
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,223 @@ | ||
| import { nextTestSetup } from 'e2e-utils' | ||
| import { fetchViaHTTP } from 'next-test-utils' | ||
|
|
||
| describe('client-max-body-size', () => { | ||
| describe('default 10MB limit', () => { | ||
| const { next, skipped } = nextTestSetup({ | ||
| files: __dirname, | ||
| // Deployed environment has it's own configured limits. | ||
| skipDeployment: true, | ||
| }) | ||
|
|
||
| if (skipped) return | ||
|
|
||
| it('should reject request body over 10MB by default', async () => { | ||
| const bodySize = 11 * 1024 * 1024 // 11MB | ||
| const body = 'x'.repeat(bodySize) | ||
|
|
||
| const res = await fetchViaHTTP( | ||
| next.url, | ||
| '/api/echo', | ||
| {}, | ||
| { | ||
| body, | ||
| method: 'POST', | ||
| } | ||
| ) | ||
|
|
||
| expect(res.status).toBe(400) | ||
| expect(next.cliOutput).toContain('Request body exceeded 10MB') | ||
| }) | ||
|
|
||
| it('should accept request body at exactly 10MB', async () => { | ||
| const bodySize = 10 * 1024 * 1024 // 10MB | ||
| const body = 'y'.repeat(bodySize) | ||
|
|
||
| const res = await fetchViaHTTP( | ||
| next.url, | ||
| '/api/echo', | ||
| {}, | ||
| { | ||
| body, | ||
| method: 'POST', | ||
| } | ||
| ) | ||
|
|
||
| expect(res.status).toBe(200) | ||
| const responseBody = await res.text() | ||
| expect(responseBody).toBe('Hello World') | ||
| }) | ||
|
|
||
| it('should accept request body under 10MB', async () => { | ||
| const bodySize = 5 * 1024 * 1024 // 5MB | ||
| const body = 'z'.repeat(bodySize) | ||
|
|
||
| const res = await fetchViaHTTP( | ||
| next.url, | ||
| '/api/echo', | ||
| {}, | ||
| { | ||
| body, | ||
| method: 'POST', | ||
| } | ||
| ) | ||
|
|
||
| expect(res.status).toBe(200) | ||
| const responseBody = await res.text() | ||
| expect(responseBody).toBe('Hello World') | ||
| }) | ||
| }) | ||
|
|
||
| describe('custom limit with string format', () => { | ||
| const { next, skipped } = nextTestSetup({ | ||
| files: __dirname, | ||
| skipDeployment: true, | ||
| nextConfig: { | ||
| experimental: { | ||
| middlewareClientMaxBodySize: '5mb', | ||
| }, | ||
| }, | ||
| }) | ||
|
|
||
| if (skipped) return | ||
|
|
||
| it('should reject request body over custom 5MB limit', async () => { | ||
| const bodySize = 6 * 1024 * 1024 // 6MB | ||
| const body = 'a'.repeat(bodySize) | ||
|
|
||
| const res = await fetchViaHTTP( | ||
| next.url, | ||
| '/api/echo', | ||
| {}, | ||
| { | ||
| body, | ||
| method: 'POST', | ||
| } | ||
| ) | ||
|
|
||
| expect(res.status).toBe(400) | ||
| expect(next.cliOutput).toContain('Request body exceeded 5MB') | ||
| }) | ||
|
|
||
| it('should accept request body under custom 5MB limit', async () => { | ||
| const bodySize = 4 * 1024 * 1024 // 4MB | ||
| const body = 'b'.repeat(bodySize) | ||
|
|
||
| const res = await fetchViaHTTP( | ||
| next.url, | ||
| '/api/echo', | ||
| {}, | ||
| { | ||
| body, | ||
| method: 'POST', | ||
| } | ||
| ) | ||
|
|
||
| expect(res.status).toBe(200) | ||
| const responseBody = await res.text() | ||
| expect(responseBody).toBe('Hello World') | ||
| }) | ||
| }) | ||
|
|
||
| describe('custom limit with number format', () => { | ||
| const { next, skipped } = nextTestSetup({ | ||
| files: __dirname, | ||
| skipDeployment: true, | ||
| nextConfig: { | ||
| experimental: { | ||
| middlewareClientMaxBodySize: 2 * 1024 * 1024, // 2MB in bytes | ||
| }, | ||
| }, | ||
| }) | ||
|
|
||
| if (skipped) return | ||
|
|
||
| it('should reject request body over custom 2MB limit', async () => { | ||
| const bodySize = 3 * 1024 * 1024 // 3MB | ||
| const body = 'c'.repeat(bodySize) | ||
|
|
||
| const res = await fetchViaHTTP( | ||
| next.url, | ||
| '/api/echo', | ||
| {}, | ||
| { | ||
| body, | ||
| method: 'POST', | ||
| } | ||
| ) | ||
|
|
||
| expect(res.status).toBe(400) | ||
| expect(next.cliOutput).toContain('Request body exceeded 2MB') | ||
| }) | ||
|
|
||
| it('should accept request body under custom 2MB limit', async () => { | ||
| const bodySize = 1 * 1024 * 1024 // 1MB | ||
| const body = 'd'.repeat(bodySize) | ||
|
|
||
| const res = await fetchViaHTTP( | ||
| next.url, | ||
| '/api/echo', | ||
| {}, | ||
| { | ||
| body, | ||
| method: 'POST', | ||
| } | ||
| ) | ||
|
|
||
| expect(res.status).toBe(200) | ||
| const responseBody = await res.text() | ||
| expect(responseBody).toBe('Hello World') | ||
| }) | ||
| }) | ||
|
|
||
| describe('large custom limit', () => { | ||
| const { next, skipped } = nextTestSetup({ | ||
| files: __dirname, | ||
| skipDeployment: true, | ||
| nextConfig: { | ||
| experimental: { | ||
| middlewareClientMaxBodySize: '50mb', | ||
| }, | ||
| }, | ||
| }) | ||
|
|
||
| if (skipped) return | ||
|
|
||
| it('should accept request body up to 50MB with custom limit', async () => { | ||
| const bodySize = 20 * 1024 * 1024 // 20MB | ||
| const body = 'e'.repeat(bodySize) | ||
|
|
||
| const res = await fetchViaHTTP( | ||
| next.url, | ||
| '/api/echo', | ||
| {}, | ||
| { | ||
| body, | ||
| method: 'POST', | ||
| } | ||
| ) | ||
|
|
||
| expect(res.status).toBe(200) | ||
| const responseBody = await res.text() | ||
| expect(responseBody).toBe('Hello World') | ||
| }) | ||
|
|
||
| it('should reject request body over custom 50MB limit', async () => { | ||
| const bodySize = 51 * 1024 * 1024 // 51MB | ||
| const body = 'f'.repeat(bodySize) | ||
|
|
||
| const res = await fetchViaHTTP( | ||
| next.url, | ||
| '/api/echo', | ||
| {}, | ||
| { | ||
| body, | ||
| method: 'POST', | ||
| } | ||
| ) | ||
|
|
||
| expect(res.status).toBe(400) | ||
| expect(next.cliOutput).toContain('Request body exceeded 50MB') | ||
| }) | ||
| }) | ||
| }) |
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,9 @@ | ||
| import { NextRequest, NextResponse } from 'next/server' | ||
|
|
||
| export async function middleware(request: NextRequest) { | ||
| return NextResponse.next() | ||
| } | ||
|
|
||
| export const config = { | ||
| matcher: '/api/:path*', | ||
| } |
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.