Unverified Commit c885d482 authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

ensure maxPostponedStateSize is always respected (#90060)

`maxPostponedStateSize` should be verified in all cases where the
postponed body is buffered. This fixes a branch in self-hosted setups
where that check was missing.
parent 8d418beb
...@@ -65,10 +65,12 @@ import type { CacheControl } from '../../server/lib/cache-control' ...@@ -65,10 +65,12 @@ import type { CacheControl } from '../../server/lib/cache-control'
import { ENCODED_TAGS } from '../../server/stream-utils/encoded-tags' import { ENCODED_TAGS } from '../../server/stream-utils/encoded-tags'
import { sendRenderResult } from '../../server/send-payload' import { sendRenderResult } from '../../server/send-payload'
import { NoFallbackError } from '../../shared/lib/no-fallback-error.external' import { NoFallbackError } from '../../shared/lib/no-fallback-error.external'
import { parseMaxPostponedStateSize } from '../../shared/lib/size-limit'
import { import {
DEFAULT_MAX_POSTPONED_STATE_SIZE, getMaxPostponedStateSize,
parseMaxPostponedStateSize, getPostponedStateExceededErrorMessage,
} from '../../shared/lib/size-limit' readBodyWithSizeLimit,
} from '../../server/lib/postponed-request-body'
// These are injected by the loader afterwards. // These are injected by the loader afterwards.
...@@ -243,23 +245,13 @@ export async function handler( ...@@ -243,23 +245,13 @@ export async function handler(
typeof resumeStateLengthHeader === 'string' typeof resumeStateLengthHeader === 'string'
) { ) {
const stateLength = parseInt(resumeStateLengthHeader, 10) const stateLength = parseInt(resumeStateLengthHeader, 10)
const maxPostponedStateSize = const { maxPostponedStateSize, maxPostponedStateSizeBytes } =
nextConfig.experimental.maxPostponedStateSize ?? getMaxPostponedStateSize(nextConfig.experimental.maxPostponedStateSize)
DEFAULT_MAX_POSTPONED_STATE_SIZE
const maxPostponedStateSizeBytes = parseMaxPostponedStateSize(
nextConfig.experimental.maxPostponedStateSize
)
if (!isNaN(stateLength) && stateLength > 0) { if (!isNaN(stateLength) && stateLength > 0) {
if ( if (stateLength > maxPostponedStateSizeBytes) {
maxPostponedStateSizeBytes === undefined ||
stateLength > maxPostponedStateSizeBytes
) {
res.statusCode = 413 res.statusCode = 413
res.end( res.end(getPostponedStateExceededErrorMessage(maxPostponedStateSize))
`Postponed state exceeded ${maxPostponedStateSize} limit. ` +
`To configure the limit, see: https://nextjs.org/docs/app/api-reference/config/next-config-js/max-postponed-state-size`
)
ctx.waitUntil?.(Promise.resolve()) ctx.waitUntil?.(Promise.resolve())
return null return null
} }
...@@ -280,24 +272,16 @@ export async function handler( ...@@ -280,24 +272,16 @@ export async function handler(
: 1024 * 1024 // 1 MB : 1024 * 1024 // 1 MB
const maxTotalBodySize = stateLength + actionBodySizeLimitBytes const maxTotalBodySize = stateLength + actionBodySizeLimitBytes
// Read the entire body, checking size as we go. const fullBody = await readBodyWithSizeLimit(req, maxTotalBodySize)
const bodyChunks: Array<Buffer> = [] if (fullBody === null) {
let size = 0 res.statusCode = 413
for await (const chunk of req) { res.end(
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk) `Request body exceeded limit. ` +
size += buffer.byteLength `To configure the body size limit for Server Actions, see: https://nextjs.org/docs/app/api-reference/next-config-js/serverActions#bodysizelimit`
if (size > maxTotalBodySize) { )
res.statusCode = 413 ctx.waitUntil?.(Promise.resolve())
res.end( return null
`Request body exceeded limit. ` +
`To configure the body size limit for Server Actions, see: https://nextjs.org/docs/app/api-reference/next-config-js/serverActions#bodysizelimit`
)
ctx.waitUntil?.(Promise.resolve())
return null
}
bodyChunks.push(buffer)
} }
const fullBody = Buffer.concat(bodyChunks)
if (fullBody.length >= stateLength) { if (fullBody.length >= stateLength) {
// Extract postponed state from the beginning // Extract postponed state from the beginning
...@@ -323,15 +307,20 @@ export async function handler( ...@@ -323,15 +307,20 @@ export async function handler(
req.headers[NEXT_RESUME_HEADER] === '1' && req.headers[NEXT_RESUME_HEADER] === '1' &&
req.method === 'POST' req.method === 'POST'
) { ) {
const { maxPostponedStateSize, maxPostponedStateSizeBytes } =
getMaxPostponedStateSize(nextConfig.experimental.maxPostponedStateSize)
// Decode the postponed state from the request body, it will come as // Decode the postponed state from the request body, it will come as
// an array of buffers, so collect them and then concat them to form // an array of buffers, so collect them and then concat them to form
// the string. // the string.
const body = await readBodyWithSizeLimit(req, maxPostponedStateSizeBytes)
const body: Array<Buffer> = [] if (body === null) {
for await (const chunk of req) { res.statusCode = 413
body.push(chunk) res.end(getPostponedStateExceededErrorMessage(maxPostponedStateSize))
ctx.waitUntil?.(Promise.resolve())
return null
} }
const postponed = Buffer.concat(body).toString('utf8') const postponed = body.toString('utf8')
addRequestMeta(req, 'postponed', postponed) addRequestMeta(req, 'postponed', postponed)
} }
......
...@@ -6,10 +6,7 @@ import type { ...@@ -6,10 +6,7 @@ import type {
import type { MiddlewareRouteMatch } from '../shared/lib/router/utils/middleware-route-matcher' import type { MiddlewareRouteMatch } from '../shared/lib/router/utils/middleware-route-matcher'
import type { Params } from './request/params' import type { Params } from './request/params'
import type { NextConfig, NextConfigRuntime } from './config-shared' import type { NextConfig, NextConfigRuntime } from './config-shared'
import { import { parseMaxPostponedStateSize } from './config-shared'
DEFAULT_MAX_POSTPONED_STATE_SIZE,
parseMaxPostponedStateSize,
} from './config-shared'
import type { import type {
NextParsedUrlQuery, NextParsedUrlQuery,
NextUrlWithParsedQuery, NextUrlWithParsedQuery,
...@@ -155,6 +152,11 @@ import type { PrerenderedRoute } from '../build/static-paths/types' ...@@ -155,6 +152,11 @@ import type { PrerenderedRoute } from '../build/static-paths/types'
import { createOpaqueFallbackRouteParams } from './request/fallback-params' import { createOpaqueFallbackRouteParams } from './request/fallback-params'
import { RouteKind } from './route-kind' import { RouteKind } from './route-kind'
import type { ErrorModule } from './load-default-error-components' import type { ErrorModule } from './load-default-error-components'
import {
getMaxPostponedStateSize,
getPostponedStateExceededErrorMessage,
readBodyWithSizeLimit,
} from './lib/postponed-request-body'
export type FindComponentsResult< export type FindComponentsResult<
NextModule extends GenericComponentMod = GenericComponentMod, NextModule extends GenericComponentMod = GenericComponentMod,
...@@ -1069,37 +1071,28 @@ export default abstract class Server< ...@@ -1069,37 +1071,28 @@ export default abstract class Server<
req.headers[NEXT_RESUME_HEADER] === '1' && req.headers[NEXT_RESUME_HEADER] === '1' &&
req.method === 'POST' req.method === 'POST'
) { ) {
// Get the configured max postponed state size. const { maxPostponedStateSize, maxPostponedStateSizeBytes } =
const maxPostponedStateSize = getMaxPostponedStateSize(
this.nextConfig.experimental.maxPostponedStateSize ?? this.nextConfig.experimental.maxPostponedStateSize
DEFAULT_MAX_POSTPONED_STATE_SIZE
const maxPostponedStateSizeBytes = parseMaxPostponedStateSize(
this.nextConfig.experimental.maxPostponedStateSize
)
if (maxPostponedStateSizeBytes === undefined) {
throw new Error(
'maxPostponedStateSize must be a valid number (bytes) or filesize format string (e.g., "5mb")'
) )
}
// Decode the postponed state from the request body, it will come as // Decode the postponed state from the request body, it will come as
// an array of buffers, so collect them and then concat them to form // an array of buffers, so collect them and then concat them to form
// the string. // the string.
const body: Array<Buffer> = [] const body = await readBodyWithSizeLimit(
let size = 0 req.body,
for await (const chunk of req.body) { maxPostponedStateSizeBytes
size += Buffer.byteLength(chunk) )
if (size > maxPostponedStateSizeBytes) { if (body === null) {
res.statusCode = 413 res.statusCode = 413
const errorMessage = res
`Postponed state exceeded ${maxPostponedStateSize} limit. ` + .body(
`To configure the limit, see: https://nextjs.org/docs/app/api-reference/config/next-config-js/max-postponed-state-size` getPostponedStateExceededErrorMessage(maxPostponedStateSize)
res.body(errorMessage).send() )
return .send()
} return
body.push(chunk)
} }
const postponed = Buffer.concat(body).toString('utf8') const postponed = body.toString('utf8')
addRequestMeta(req, 'postponed', postponed) addRequestMeta(req, 'postponed', postponed)
} }
......
import {
DEFAULT_MAX_POSTPONED_STATE_SIZE,
parseMaxPostponedStateSize,
} from '../../shared/lib/size-limit'
import type { SizeLimit } from '../../types'
const INVALID_MAX_POSTPONED_STATE_SIZE_ERROR_MESSAGE =
'maxPostponedStateSize must be a valid number (bytes) or filesize format string (e.g., "5mb")'
export type PostponedRequestBodyChunk = Buffer | Uint8Array | string
export function getMaxPostponedStateSize(
configuredMaxPostponedStateSize: SizeLimit | undefined
): {
maxPostponedStateSize: SizeLimit
maxPostponedStateSizeBytes: number
} {
const maxPostponedStateSize =
configuredMaxPostponedStateSize ?? DEFAULT_MAX_POSTPONED_STATE_SIZE
const maxPostponedStateSizeBytes = parseMaxPostponedStateSize(
configuredMaxPostponedStateSize
)
if (maxPostponedStateSizeBytes === undefined) {
throw new Error(INVALID_MAX_POSTPONED_STATE_SIZE_ERROR_MESSAGE)
}
return { maxPostponedStateSize, maxPostponedStateSizeBytes }
}
export function getPostponedStateExceededErrorMessage(
maxPostponedStateSize: SizeLimit
): string {
return (
`Postponed state exceeded ${maxPostponedStateSize} limit. ` +
`To configure the limit, see: https://nextjs.org/docs/app/api-reference/config/next-config-js/max-postponed-state-size`
)
}
function toBuffer(chunk: PostponedRequestBodyChunk): Buffer {
return Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)
}
export async function readBodyWithSizeLimit(
body: AsyncIterable<PostponedRequestBodyChunk>,
maxBodySizeBytes: number
): Promise<Buffer | null> {
const chunks: Array<Buffer> = []
let size = 0
for await (const chunk of body) {
const buffer = toBuffer(chunk)
size += buffer.byteLength
if (size > maxBodySizeBytes) {
return null
}
chunks.push(buffer)
}
return Buffer.concat(chunks)
}
export default function Layout({ children }: { children: React.ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export default function Page() {
return <p>Hello World</p>
}
import { nextTestSetup } from 'e2e-utils'
describe('app-dir - max postponed state size', () => {
const { next } = nextTestSetup({
files: __dirname,
})
it('should return 413 when next-resume request exceeds max postponed state size', async () => {
const res = await next.fetch('/', {
method: 'POST',
headers: {
'next-action': 'abc123',
'next-resume': '1',
},
body: Buffer.alloc(1025, 'x'),
})
expect(res.status).toBe(413)
expect(await res.text()).toContain('Postponed state exceeded 1 KB limit.')
})
})
module.exports = {
cacheComponents: true,
experimental: {
maxPostponedStateSize: '1 KB',
},
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment