Unverified Commit 409edf93 authored by Josh Story's avatar Josh Story Committed by GitHub

Stop compressing resume data cache

## Summary

Serialize the Resume Data Cache in postponed state as raw JSON instead of a zlib-compressed base64 string. This lets cache persistence implementations decide whether and how to compress postponed state, and removes the decompression path and zip-bomb exposure from Next.js.

Remove the temporary experimental.disableResumeDataCacheCompression rollout flag together with the deflate and inflate branches, making raw JSON the only persisted representation.

Retain serializePostponedState as the single construction path for the postponed-state wire format so HTML and data serialization cannot drift.

The existing postponed-state request-body size limit remains in place. Parsing diagnostics and tests cover truncated or malformed JSON cache tails.

## Verification

- pnpm build-all
- pnpm exec jest packages/next/src/server/resume-data-cache/resume-data-cache.test.ts packages/next/src/server/app-render/postponed-state.test.ts --runInBand
- pnpm --filter=next types

<!-- NEXT_JS_LLM -->
parent fef4c28b
...@@ -953,9 +953,6 @@ export function createAppPageEntrypoint({ ...@@ -953,9 +953,6 @@ export function createAppPageEntrypoint({
maxPostponedStateSizeBytes: parseMaxPostponedStateSize( maxPostponedStateSizeBytes: parseMaxPostponedStateSize(
nextConfig.experimental.maxPostponedStateSize nextConfig.experimental.maxPostponedStateSize
), ),
disableResumeDataCacheCompression:
nextConfig.experimental.disableResumeDataCacheCompression ??
false,
exposeTestingApi, exposeTestingApi,
}, },
......
...@@ -184,8 +184,6 @@ async function requestHandler( ...@@ -184,8 +184,6 @@ async function requestHandler(
maxPostponedStateSizeBytes: parseMaxPostponedStateSize( maxPostponedStateSizeBytes: parseMaxPostponedStateSize(
nextConfig.experimental.maxPostponedStateSize nextConfig.experimental.maxPostponedStateSize
), ),
disableResumeDataCacheCompression:
nextConfig.experimental.disableResumeDataCacheCompression ?? false,
exposeTestingApi: exposeTestingApi:
nextConfig.cacheComponents === true && nextConfig.cacheComponents === true &&
(pageRouteModule.isDev === true || (pageRouteModule.isDev === true ||
......
...@@ -521,8 +521,6 @@ async function exportAppImpl( ...@@ -521,8 +521,6 @@ async function exportAppImpl(
maxPostponedStateSizeBytes: parseMaxPostponedStateSize( maxPostponedStateSizeBytes: parseMaxPostponedStateSize(
nextConfig.experimental.maxPostponedStateSize nextConfig.experimental.maxPostponedStateSize
), ),
disableResumeDataCacheCompression:
nextConfig.experimental.disableResumeDataCacheCompression ?? false,
exposeTestingApi: exposeTestingApi:
nextConfig.experimental.exposeTestingApiInProductionBuild === true, nextConfig.experimental.exposeTestingApiInProductionBuild === true,
}, },
......
...@@ -28,10 +28,7 @@ import { AfterRunner } from '../../server/after/run-with-after' ...@@ -28,10 +28,7 @@ import { AfterRunner } from '../../server/after/run-with-after'
import type { RequestLifecycleOpts } from '../../server/base-server' import type { RequestLifecycleOpts } from '../../server/base-server'
import type { AppSharedContext } from '../../server/app-render/app-render' import type { AppSharedContext } from '../../server/app-render/app-render'
import type { MultiFileWriter } from '../../lib/multi-file-writer' import type { MultiFileWriter } from '../../lib/multi-file-writer'
import { import { stringifyResumeDataCache } from '../../server/resume-data-cache/resume-data-cache'
deflateResumeDataCache,
stringifyResumeDataCache,
} from '../../server/resume-data-cache/resume-data-cache'
import { import {
UNDERSCORE_GLOBAL_ERROR_ROUTE_ENTRY, UNDERSCORE_GLOBAL_ERROR_ROUTE_ENTRY,
UNDERSCORE_NOT_FOUND_ROUTE_ENTRY, UNDERSCORE_NOT_FOUND_ROUTE_ENTRY,
...@@ -231,19 +228,6 @@ export async function exportAppPage( ...@@ -231,19 +228,6 @@ export async function exportAppPage(
JSON.stringify(meta, null, 2) JSON.stringify(meta, null, 2)
) )
let serializedRenderResumeDataCache: string | undefined
if (renderResumeDataCache) {
serializedRenderResumeDataCache = await stringifyResumeDataCache(
renderResumeDataCache,
renderOpts.cacheComponents
)
if (!renderOpts.experimental.disableResumeDataCacheCompression) {
serializedRenderResumeDataCache = deflateResumeDataCache(
serializedRenderResumeDataCache
)
}
}
return { return {
// Filter the metadata if the environment does not have next support. // Filter the metadata if the environment does not have next support.
metadata: hasNextSupport metadata: hasNextSupport
...@@ -259,7 +243,12 @@ export async function exportAppPage( ...@@ -259,7 +243,12 @@ export async function exportAppPage(
hasStaticRsc, hasStaticRsc,
cacheControl, cacheControl,
fetchMetrics, fetchMetrics,
renderResumeDataCache: serializedRenderResumeDataCache, renderResumeDataCache: renderResumeDataCache
? await stringifyResumeDataCache(
renderResumeDataCache,
renderOpts.cacheComponents
)
: undefined,
} }
} catch (err) { } catch (err) {
if (!isDynamicUsageError(err)) { if (!isDynamicUsageError(err)) {
......
...@@ -413,11 +413,7 @@ export async function exportPages( ...@@ -413,11 +413,7 @@ export async function exportPages(
process.env.NODE_OPTIONS?.includes('--inspect') process.env.NODE_OPTIONS?.includes('--inspect')
const renderResumeDataCache = renderResumeDataCachesByPage[pageKey] const renderResumeDataCache = renderResumeDataCachesByPage[pageKey]
? createRenderResumeDataCache( ? createRenderResumeDataCache(renderResumeDataCachesByPage[pageKey])
renderResumeDataCachesByPage[pageKey],
renderOpts.experimental.maxPostponedStateSizeBytes,
renderOpts.experimental.disableResumeDataCacheCompression
)
: undefined : undefined
while (attempt < maxAttempts) { while (attempt < maxAttempts) {
......
...@@ -3310,17 +3310,13 @@ function prepareAppPage( ...@@ -3310,17 +3310,13 @@ function prepareAppPage(
postponedState = { postponedState = {
type: DynamicState.DATA, type: DynamicState.DATA,
renderResumeDataCache: parseResumeDataCacheFromPostponedState( renderResumeDataCache: parseResumeDataCacheFromPostponedState(
renderOpts.postponed, renderOpts.postponed
renderOpts.experimental.maxPostponedStateSizeBytes,
renderOpts.experimental.disableResumeDataCacheCompression
), ),
} }
} else { } else {
postponedState = parsePostponedState( postponedState = parsePostponedState(
renderOpts.postponed, renderOpts.postponed,
interpolatedParams, interpolatedParams
renderOpts.experimental.maxPostponedStateSizeBytes,
renderOpts.experimental.disableResumeDataCacheCompression
) )
} }
} }
...@@ -9425,16 +9421,12 @@ async function prerenderToStream( ...@@ -9425,16 +9421,12 @@ async function prerenderToStream(
: DynamicHTMLPreludeState.Full, : DynamicHTMLPreludeState.Full,
fallbackRouteParams, fallbackRouteParams,
resumeDataCache, resumeDataCache,
cacheComponents, cacheComponents
renderOpts.experimental.maxPostponedStateSizeBytes,
renderOpts.experimental.disableResumeDataCacheCompression
) )
} else { } else {
metadata.postponed = await getDynamicDataPostponedState( metadata.postponed = await getDynamicDataPostponedState(
resumeDataCache, resumeDataCache,
cacheComponents, cacheComponents
renderOpts.experimental.maxPostponedStateSizeBytes,
renderOpts.experimental.disableResumeDataCacheCompression
) )
} }
reactServerResult.consume() reactServerResult.consume()
...@@ -9958,9 +9950,7 @@ async function prerenderToStream( ...@@ -9958,9 +9950,7 @@ async function prerenderToStream(
if (originalFlightPrerenderResultIsDynamic) { if (originalFlightPrerenderResultIsDynamic) {
metadata.postponed = await getDynamicDataPostponedState( metadata.postponed = await getDynamicDataPostponedState(
originalResumeDataCache, originalResumeDataCache,
cacheComponents, cacheComponents
renderOpts.experimental.maxPostponedStateSizeBytes,
renderOpts.experimental.disableResumeDataCacheCompression
) )
originalFlightPrerenderResult.consume() originalFlightPrerenderResult.consume()
errorServerResult.consume() errorServerResult.consume()
......
import { import { createPrerenderResumeDataCache } from '../resume-data-cache/resume-data-cache'
createPrerenderResumeDataCache,
stringifyResumeDataCache,
} from '../resume-data-cache/resume-data-cache'
import { import {
streamFromString, streamFromString,
streamToString, streamToString,
...@@ -18,7 +15,6 @@ import type { ...@@ -18,7 +15,6 @@ import type {
OpaqueFallbackRouteParams, OpaqueFallbackRouteParams,
OpaqueFallbackRouteParamValue, OpaqueFallbackRouteParamValue,
} from '../request/fallback-params' } from '../request/fallback-params'
import { CachedRouteKind } from '../response-cache/types'
export function createMockOpaqueFallbackRouteParams( export function createMockOpaqueFallbackRouteParams(
params: Record<string, OpaqueFallbackRouteParamValue> params: Record<string, OpaqueFallbackRouteParamValue>
...@@ -62,7 +58,7 @@ describe('getDynamicHTMLPostponedState', () => { ...@@ -62,7 +58,7 @@ describe('getDynamicHTMLPostponedState', () => {
isCacheComponentsEnabled isCacheComponentsEnabled
) )
const parsed = parsePostponedState(state, { slug: '123' }, undefined) const parsed = parsePostponedState(state, { slug: '123' })
expect(parsed).toMatchInlineSnapshot(` expect(parsed).toMatchInlineSnapshot(`
{ {
...@@ -122,7 +118,7 @@ describe('getDynamicHTMLPostponedState', () => { ...@@ -122,7 +118,7 @@ describe('getDynamicHTMLPostponedState', () => {
const value = 'hello' const value = 'hello'
const params = { slug: value } const params = { slug: value }
const parsed = parsePostponedState(state, params, undefined) const parsed = parsePostponedState(state, params)
expect(parsed).toEqual({ expect(parsed).toEqual({
type: DynamicState.HTML, type: DynamicState.HTML,
data: [1, { [value]: value }], data: [1, { [value]: value }],
...@@ -149,79 +145,6 @@ describe('getDynamicDataPostponedState', () => { ...@@ -149,79 +145,6 @@ describe('getDynamicDataPostponedState', () => {
) )
expect(state).toMatchInlineSnapshot(`"4:nullnull"`) expect(state).toMatchInlineSnapshot(`"4:nullnull"`)
}) })
it('serializes and parses an uncompressed cache when compression is disabled', async () => {
const resumeDataCache = createPrerenderResumeDataCache()
resumeDataCache.fetch.set('cache-key', {
kind: CachedRouteKind.FETCH,
data: {
headers: {},
body: 'cached body',
url: 'https://example.com',
},
revalidate: 60,
})
const serializedResumeDataCache = await stringifyResumeDataCache(
resumeDataCache,
isCacheComponentsEnabled
)
const state = await getDynamicDataPostponedState(
resumeDataCache,
isCacheComponentsEnabled,
undefined,
true
)
expect(state).toBe(`4:null${serializedResumeDataCache}`)
const parsed = parsePostponedState(state, {}, undefined, true)
expect(parsed.renderResumeDataCache.fetch.get('cache-key')).toEqual(
resumeDataCache.fetch.get('cache-key')
)
})
it('warns when the uncompressed state would exceed the size limit', async () => {
const resumeDataCache = createPrerenderResumeDataCache()
resumeDataCache.fetch.set('cache-key', {
kind: CachedRouteKind.FETCH,
data: {
headers: {},
body: '💥'.repeat(2048),
url: 'https://example.com',
},
revalidate: 60,
})
const serializedResumeDataCache = await stringifyResumeDataCache(
resumeDataCache,
isCacheComponentsEnabled
)
const uncompressedStateByteLength = Buffer.byteLength(
`4:null${serializedResumeDataCache}`
)
const warn = jest.spyOn(console, 'warn').mockImplementation(() => {})
await getDynamicDataPostponedState(
resumeDataCache,
isCacheComponentsEnabled,
uncompressedStateByteLength
)
expect(warn).not.toHaveBeenCalled()
await getDynamicDataPostponedState(
resumeDataCache,
isCacheComponentsEnabled,
uncompressedStateByteLength - 1
)
expect(warn).toHaveBeenCalledWith(
expect.stringContaining(
`The uncompressed postponed state is ${uncompressedStateByteLength} bytes`
)
)
warn.mockRestore()
})
}) })
describe('parseResumeDataCacheFromPostponedState', () => { describe('parseResumeDataCacheFromPostponedState', () => {
...@@ -258,10 +181,7 @@ describe('parseResumeDataCacheFromPostponedState', () => { ...@@ -258,10 +181,7 @@ describe('parseResumeDataCacheFromPostponedState', () => {
isCacheComponentsEnabled isCacheComponentsEnabled
) )
const resumeDataCache = parseResumeDataCacheFromPostponedState( const resumeDataCache = parseResumeDataCacheFromPostponedState(state)
state,
undefined
)
const value = await resumeDataCache.cache.get('cache-key') const value = await resumeDataCache.cache.get('cache-key')
expect(value).toBeDefined() expect(value).toBeDefined()
...@@ -277,7 +197,7 @@ describe('parsePostponedState', () => { ...@@ -277,7 +197,7 @@ describe('parsePostponedState', () => {
const params = { const params = {
slug: Math.random().toString(16).slice(3), slug: Math.random().toString(16).slice(3),
} }
const parsed = parsePostponedState(state, params, undefined) const parsed = parsePostponedState(state, params)
// Ensure that it parsed it correctly. // Ensure that it parsed it correctly.
expect(parsed).toEqual({ expect(parsed).toEqual({
...@@ -300,7 +220,7 @@ describe('parsePostponedState', () => { ...@@ -300,7 +220,7 @@ describe('parsePostponedState', () => {
it('parses a HTML postponed state without fallback params', () => { it('parses a HTML postponed state without fallback params', () => {
const state = `2:{}null` const state = `2:{}null`
const params = {} const params = {}
const parsed = parsePostponedState(state, params, undefined) const parsed = parsePostponedState(state, params)
// Ensure that it parsed it correctly. // Ensure that it parsed it correctly.
expect(parsed).toEqual({ expect(parsed).toEqual({
...@@ -319,7 +239,7 @@ describe('parsePostponedState', () => { ...@@ -319,7 +239,7 @@ describe('parsePostponedState', () => {
it('parses a data postponed state', () => { it('parses a data postponed state', () => {
const state = '4:nullnull' const state = '4:nullnull'
const parsed = parsePostponedState(state, {}, undefined) const parsed = parsePostponedState(state, {})
// Ensure that it parsed it correctly. // Ensure that it parsed it correctly.
expect(parsed).toEqual({ expect(parsed).toEqual({
......
...@@ -7,7 +7,6 @@ import type { Params } from '../request/params' ...@@ -7,7 +7,6 @@ import type { Params } from '../request/params'
import { import {
createPrerenderResumeDataCache, createPrerenderResumeDataCache,
createRenderResumeDataCache, createRenderResumeDataCache,
deflateResumeDataCache,
stringifyResumeDataCache, stringifyResumeDataCache,
type PrerenderResumeDataCache, type PrerenderResumeDataCache,
type RenderResumeDataCache, type RenderResumeDataCache,
...@@ -79,34 +78,14 @@ export type PostponedState = ...@@ -79,34 +78,14 @@ export type PostponedState =
async function serializePostponedState( async function serializePostponedState(
postponedString: string, postponedString: string,
resumeDataCache: PrerenderResumeDataCache | RenderResumeDataCache, resumeDataCache: PrerenderResumeDataCache | RenderResumeDataCache,
isCacheComponentsEnabled: boolean, isCacheComponentsEnabled: boolean
maxPostponedStateSizeBytes: number | undefined,
disableResumeDataCacheCompression: boolean
): Promise<string> { ): Promise<string> {
const prefix = `${postponedString.length}:${postponedString}` const serializedResumeDataCache = await stringifyResumeDataCache(
let serializedResumeDataCache = await stringifyResumeDataCache(
resumeDataCache, resumeDataCache,
isCacheComponentsEnabled isCacheComponentsEnabled
) )
if (!disableResumeDataCacheCompression) { return `${postponedString.length}:${postponedString}${serializedResumeDataCache}`
if (maxPostponedStateSizeBytes !== undefined) {
const uncompressedPostponedStateByteLength =
Buffer.byteLength(prefix) + Buffer.byteLength(serializedResumeDataCache)
if (uncompressedPostponedStateByteLength > maxPostponedStateSizeBytes) {
console.warn(
`The uncompressed postponed state is ${uncompressedPostponedStateByteLength} bytes, which exceeds the configured experimental.maxPostponedStateSize limit of ${maxPostponedStateSizeBytes} bytes. Next.js currently compresses the Resume Data Cache before persisting the postponed state, but this compression will be removed in a future release. Increase experimental.maxPostponedStateSize to ensure this route can still be resumed after that change.`
)
}
}
serializedResumeDataCache = deflateResumeDataCache(
serializedResumeDataCache
)
}
return prefix + serializedResumeDataCache
} }
export async function getDynamicHTMLPostponedState( export async function getDynamicHTMLPostponedState(
...@@ -114,9 +93,7 @@ export async function getDynamicHTMLPostponedState( ...@@ -114,9 +93,7 @@ export async function getDynamicHTMLPostponedState(
preludeState: DynamicHTMLPreludeState, preludeState: DynamicHTMLPreludeState,
fallbackRouteParams: OpaqueFallbackRouteParams | null, fallbackRouteParams: OpaqueFallbackRouteParams | null,
resumeDataCache: PrerenderResumeDataCache | RenderResumeDataCache, resumeDataCache: PrerenderResumeDataCache | RenderResumeDataCache,
isCacheComponentsEnabled: boolean, isCacheComponentsEnabled: boolean
maxPostponedStateSizeBytes?: number,
disableResumeDataCacheCompression = false
): Promise<string> { ): Promise<string> {
const data: DynamicHTMLPostponedState['data'] = [preludeState, postponed] const data: DynamicHTMLPostponedState['data'] = [preludeState, postponed]
const dataString = JSON.stringify(data) const dataString = JSON.stringify(data)
...@@ -128,9 +105,7 @@ export async function getDynamicHTMLPostponedState( ...@@ -128,9 +105,7 @@ export async function getDynamicHTMLPostponedState(
return serializePostponedState( return serializePostponedState(
dataString, dataString,
resumeDataCache, resumeDataCache,
isCacheComponentsEnabled, isCacheComponentsEnabled
maxPostponedStateSizeBytes,
disableResumeDataCacheCompression
) )
} }
...@@ -146,32 +121,22 @@ export async function getDynamicHTMLPostponedState( ...@@ -146,32 +121,22 @@ export async function getDynamicHTMLPostponedState(
return serializePostponedState( return serializePostponedState(
postponedString, postponedString,
resumeDataCache, resumeDataCache,
isCacheComponentsEnabled, isCacheComponentsEnabled
maxPostponedStateSizeBytes,
disableResumeDataCacheCompression
) )
} }
export async function getDynamicDataPostponedState( export async function getDynamicDataPostponedState(
resumeDataCache: PrerenderResumeDataCache | RenderResumeDataCache, resumeDataCache: PrerenderResumeDataCache | RenderResumeDataCache,
isCacheComponentsEnabled: boolean, isCacheComponentsEnabled: boolean
maxPostponedStateSizeBytes?: number,
disableResumeDataCacheCompression = false
): Promise<string> { ): Promise<string> {
return serializePostponedState( return serializePostponedState(
'null', 'null',
resumeDataCache, resumeDataCache,
isCacheComponentsEnabled, isCacheComponentsEnabled
maxPostponedStateSizeBytes,
disableResumeDataCacheCompression
) )
} }
function parsePostponedStateParts( function parsePostponedStateParts(state: string): {
state: string,
maxPostponedStateSizeBytes: number | undefined,
disableResumeDataCacheCompression: boolean
): {
postponedString: string postponedString: string
renderResumeDataCache: RenderResumeDataCache renderResumeDataCache: RenderResumeDataCache
} { } {
...@@ -191,25 +156,15 @@ function parsePostponedStateParts( ...@@ -191,25 +156,15 @@ function parsePostponedStateParts(
postponedStringLengthMatch.length + 1, postponedStringLengthMatch.length + 1,
tailStart tailStart
), ),
renderResumeDataCache: createRenderResumeDataCache( renderResumeDataCache: createRenderResumeDataCache(state.slice(tailStart)),
state.slice(tailStart),
maxPostponedStateSizeBytes,
disableResumeDataCacheCompression
),
} }
} }
export function parseResumeDataCacheFromPostponedState( export function parseResumeDataCacheFromPostponedState(
state: string, state: string
maxPostponedStateSizeBytes: number | undefined,
disableResumeDataCacheCompression = false
): RenderResumeDataCache { ): RenderResumeDataCache {
try { try {
return parsePostponedStateParts( return parsePostponedStateParts(state).renderResumeDataCache
state,
maxPostponedStateSizeBytes,
disableResumeDataCacheCompression
).renderResumeDataCache
} catch (err) { } catch (err) {
console.error( console.error(
'Failed to parse postponed state', 'Failed to parse postponed state',
...@@ -221,16 +176,11 @@ export function parseResumeDataCacheFromPostponedState( ...@@ -221,16 +176,11 @@ export function parseResumeDataCacheFromPostponedState(
export function parsePostponedState( export function parsePostponedState(
state: string, state: string,
interpolatedParams: Params, interpolatedParams: Params
maxPostponedStateSizeBytes: number | undefined,
disableResumeDataCacheCompression = false
): PostponedState { ): PostponedState {
try { try {
const { postponedString, renderResumeDataCache } = parsePostponedStateParts( const { postponedString, renderResumeDataCache } =
state, parsePostponedStateParts(state)
maxPostponedStateSizeBytes,
disableResumeDataCacheCompression
)
try { try {
if (postponedString === 'null') { if (postponedString === 'null') {
...@@ -318,14 +268,12 @@ export function parsePostponedState( ...@@ -318,14 +268,12 @@ export function parsePostponedState(
* sensitive) serialized contents. Every field is a size, a structural flag, or * sensitive) serialized contents. Every field is a size, a structural flag, or
* an error code, never the state bytes themselves. * an error code, never the state bytes themselves.
* *
* The serialized layout is `<N>:<postponedString><cache>`. The cache is a * The serialized layout is `<N>:<postponedString><JSON cache>`, so
* base64-deflate string by default and raw JSON when RDC compression is * these fields distinguish the failure shapes:
* disabled, so these fields distinguish the failure shapes:
* - `postponedStringComplete: false`: the declared length `N` exceeds what * - `postponedStringComplete: false`: the declared length `N` exceeds what
* actually arrived, i.e. the postponed string itself was truncated. * actually arrived, i.e. the postponed string itself was truncated.
* - `errorCode: 'Z_BUF_ERROR'` with an empty or short tail: the * - `errorName: 'SyntaxError'` with an empty or short tail: the
* resume-data-cache tail was truncated (ran out of input while inflating). * resume-data-cache tail was truncated or malformed.
* - `errorCode: 'Z_DATA_ERROR'`: the tail bytes are corrupt, not merely short.
* - `hasLengthPrefix: false`: the body had no `<N>:` prefix at all (e.g. empty * - `hasLengthPrefix: false`: the body had no `<N>:` prefix at all (e.g. empty
* or otherwise malformed input). * or otherwise malformed input).
*/ */
......
...@@ -172,15 +172,10 @@ export interface RenderOptsPartial { ...@@ -172,15 +172,10 @@ export interface RenderOptsPartial {
/** /**
* The maximum size (in bytes) of the postponed state body for PPR resume * The maximum size (in bytes) of the postponed state body for PPR resume
* requests. Used to calculate decompression limits (5x this value). * requests.
*/ */
maxPostponedStateSizeBytes: number | undefined maxPostponedStateSizeBytes: number | undefined
/**
* Whether the Resume Data Cache should be persisted without compression.
*/
disableResumeDataCacheCompression: boolean
/** /**
* Whether the Instant Navigation Testing API is exposed (dev mode or the * Whether the Instant Navigation Testing API is exposed (dev mode or the
* `exposeTestingApiInProductionBuild` flag). When true, the prerendered * `exposeTestingApiInProductionBuild` flag). When true, the prerendered
......
...@@ -611,9 +611,6 @@ export default abstract class Server< ...@@ -611,9 +611,6 @@ export default abstract class Server<
maxPostponedStateSizeBytes: parseMaxPostponedStateSize( maxPostponedStateSizeBytes: parseMaxPostponedStateSize(
this.nextConfig.experimental.maxPostponedStateSize this.nextConfig.experimental.maxPostponedStateSize
), ),
disableResumeDataCacheCompression:
this.nextConfig.experimental.disableResumeDataCacheCompression ??
false,
exposeTestingApi: exposeTestingApi:
this.nextConfig.cacheComponents === true && this.nextConfig.cacheComponents === true &&
(this.dev === true || (this.dev === true ||
......
...@@ -251,7 +251,6 @@ export const experimentalSchema = { ...@@ -251,7 +251,6 @@ export const experimentalSchema = {
}) })
.optional(), .optional(),
maxPostponedStateSize: zSizeLimit.optional(), maxPostponedStateSize: zSizeLimit.optional(),
disableResumeDataCacheCompression: z.boolean().optional(),
// The original type was Record<string, any> // The original type was Record<string, any>
extensionAlias: z.record(z.string(), z.any()).optional(), extensionAlias: z.record(z.string(), z.any()).optional(),
externalDir: z.boolean().optional(), externalDir: z.boolean().optional(),
......
...@@ -1182,13 +1182,6 @@ export interface ExperimentalConfig { ...@@ -1182,13 +1182,6 @@ export interface ExperimentalConfig {
*/ */
maxPostponedStateSize?: SizeLimit maxPostponedStateSize?: SizeLimit
/**
* Disables compression of the Resume Data Cache (RDC) when persisting
* postponed state.
* @default false
*/
disableResumeDataCacheCompression?: boolean
/** /**
* enables the minification of server code. * enables the minification of server code.
* *
...@@ -2306,7 +2299,6 @@ export const defaultConfig = Object.freeze({ ...@@ -2306,7 +2299,6 @@ export const defaultConfig = Object.freeze({
globalNotFound: false, globalNotFound: false,
browserDebugInfoInTerminal: 'warn', browserDebugInfoInTerminal: 'warn',
lockDistDir: true, lockDistDir: true,
disableResumeDataCacheCompression: false,
proxyClientMaxBodySize: 10_485_760, // 10MB proxyClientMaxBodySize: 10_485_760, // 10MB
hideLogsAfterAbort: false, hideLogsAfterAbort: false,
mcpServer: true, mcpServer: true,
...@@ -2418,7 +2410,6 @@ export interface NextConfigRuntime { ...@@ -2418,7 +2410,6 @@ export interface NextConfigRuntime {
| 'testProxy' | 'testProxy'
| 'runtimeServerDeploymentId' | 'runtimeServerDeploymentId'
| 'maxPostponedStateSize' | 'maxPostponedStateSize'
| 'disableResumeDataCacheCompression'
| 'cachedNavigations' | 'cachedNavigations'
| 'exposeTestingApiInProductionBuild' | 'exposeTestingApiInProductionBuild'
| 'instantInsights' | 'instantInsights'
...@@ -2486,7 +2477,6 @@ export function getNextConfigRuntime( ...@@ -2486,7 +2477,6 @@ export function getNextConfigRuntime(
testProxy: ex.testProxy, testProxy: ex.testProxy,
runtimeServerDeploymentId: ex.runtimeServerDeploymentId, runtimeServerDeploymentId: ex.runtimeServerDeploymentId,
maxPostponedStateSize: ex.maxPostponedStateSize, maxPostponedStateSize: ex.maxPostponedStateSize,
disableResumeDataCacheCompression: ex.disableResumeDataCacheCompression,
cachedNavigations: ex.cachedNavigations, cachedNavigations: ex.cachedNavigations,
exposeTestingApiInProductionBuild: ex.exposeTestingApiInProductionBuild, exposeTestingApiInProductionBuild: ex.exposeTestingApiInProductionBuild,
instantInsights: ex.instantInsights, instantInsights: ex.instantInsights,
......
import { import {
deflateResumeDataCache,
stringifyResumeDataCache, stringifyResumeDataCache,
createRenderResumeDataCache, createRenderResumeDataCache,
} from './resume-data-cache' } from './resume-data-cache'
...@@ -150,14 +149,12 @@ describe('stringifyResumeDataCache', () => { ...@@ -150,14 +149,12 @@ describe('stringifyResumeDataCache', () => {
}) })
describe('parseResumeDataCache', () => { describe('parseResumeDataCache', () => {
it('throws in the edge runtime before handling an uncompressed cache', () => { it('throws in the edge runtime before parsing an empty cache', () => {
const nextRuntime = process.env.NEXT_RUNTIME const nextRuntime = process.env.NEXT_RUNTIME
process.env.NEXT_RUNTIME = 'edge' process.env.NEXT_RUNTIME = 'edge'
try { try {
expect(() => expect(() => createRenderResumeDataCache('null')).toThrow(
createRenderResumeDataCache('null', undefined, true)
).toThrow(
'`createRenderResumeDataCache` should not be called in edge runtime.' '`createRenderResumeDataCache` should not be called in edge runtime.'
) )
} finally { } finally {
...@@ -170,7 +167,7 @@ describe('parseResumeDataCache', () => { ...@@ -170,7 +167,7 @@ describe('parseResumeDataCache', () => {
}) })
it('parses an empty cache', () => { it('parses an empty cache', () => {
const parsed = createRenderResumeDataCache('null', undefined) const parsed = createRenderResumeDataCache('null')
expect(parsed.cache).toEqual(new Map()) expect(parsed.cache).toEqual(new Map())
expect(parsed.fetch).toEqual(new Map()) expect(parsed.fetch).toEqual(new Map())
expect(parsed.encryptedBoundArgs).toEqual(new Map()) expect(parsed.encryptedBoundArgs).toEqual(new Map())
...@@ -178,26 +175,16 @@ describe('parseResumeDataCache', () => { ...@@ -178,26 +175,16 @@ describe('parseResumeDataCache', () => {
expect(parsed.dynamicCacheKeys).toBeUndefined() expect(parsed.dynamicCacheKeys).toBeUndefined()
}) })
it.each([false, true])( it('parses a filled cache', async () => {
'parses a filled cache with compression disabled: %s', const cache = createMockedCache()
async (disableResumeDataCacheCompression) => { const serialized = await stringifyResumeDataCache(
const cache = createMockedCache() cache,
const serialized = await stringifyResumeDataCache( isCacheComponentsEnabled
cache, )
isCacheComponentsEnabled
)
const persisted = disableResumeDataCacheCompression const parsed = createRenderResumeDataCache(serialized)
? serialized
: deflateResumeDataCache(serialized)
const parsed = createRenderResumeDataCache(
persisted,
undefined,
disableResumeDataCacheCompression
)
expect(parsed.cache.size).toBe(isCacheComponentsEnabled ? 1 : 3) expect(parsed.cache.size).toBe(isCacheComponentsEnabled ? 1 : 3)
expect(parsed.fetch.size).toBe(0) expect(parsed.fetch.size).toBe(0)
} })
)
}) })
...@@ -199,25 +199,6 @@ export async function stringifyResumeDataCache( ...@@ -199,25 +199,6 @@ export async function stringifyResumeDataCache(
return JSON.stringify(json) return JSON.stringify(json)
} }
export function deflateResumeDataCache(
serializedResumeDataCache: string
): string {
if (process.env.NEXT_RUNTIME === 'edge') {
throw new InvariantError(
'`deflateResumeDataCache` should not be called in edge runtime.'
)
} else {
if (serializedResumeDataCache === 'null') {
return serializedResumeDataCache
}
// Compress the JSON string using zlib. As the data is already in memory,
// we use the synchronous deflateSync function.
const { deflateSync } = require('node:zlib') as typeof import('node:zlib')
return deflateSync(serializedResumeDataCache).toString('base64')
}
}
/** /**
* Creates a new empty mutable resume data cache for pre-rendering. * Creates a new empty mutable resume data cache for pre-rendering.
* Initializes fresh Map instances for both the 'use cache' and fetch caches. * Initializes fresh Map instances for both the 'use cache' and fetch caches.
...@@ -261,96 +242,55 @@ export function createPrerenderResumeDataCache( ...@@ -261,96 +242,55 @@ export function createPrerenderResumeDataCache(
* @param renderResumeDataCache - A RenderResumeDataCache instance to be used directly * @param renderResumeDataCache - A RenderResumeDataCache instance to be used directly
* @param prerenderResumeDataCache - A PrerenderResumeDataCache instance to convert to immutable * @param prerenderResumeDataCache - A PrerenderResumeDataCache instance to convert to immutable
* @param persistedCache - A serialized cache string to parse * @param persistedCache - A serialized cache string to parse
* @param maxPostponedStateSizeBytes - The max compressed size limit in bytes (used to calculate 5x decompression limit)
* @param disableResumeDataCacheCompression - Whether the persisted cache is raw JSON instead of compressed
* @returns An immutable RenderResumeDataCache instance * @returns An immutable RenderResumeDataCache instance
*/ */
export function createRenderResumeDataCache( export function createRenderResumeDataCache(
resumeDataCache: ResumeDataCache resumeDataCache: ResumeDataCache
): RenderResumeDataCache ): RenderResumeDataCache
export function createRenderResumeDataCache( export function createRenderResumeDataCache(
persistedCache: string, persistedCache: string
maxPostponedStateSizeBytes: number | undefined,
disableResumeDataCacheCompression?: boolean
): RenderResumeDataCache ): RenderResumeDataCache
export function createRenderResumeDataCache( export function createRenderResumeDataCache(
resumeDataCacheOrPersistedCache: ResumeDataCache | string, resumeDataCacheOrPersistedCache: ResumeDataCache | string
maxPostponedStateSizeBytes?: number | undefined,
disableResumeDataCacheCompression = false
): RenderResumeDataCache { ): RenderResumeDataCache {
if (process.env.NEXT_RUNTIME === 'edge') { if (process.env.NEXT_RUNTIME === 'edge') {
throw new InvariantError( throw new InvariantError(
'`createRenderResumeDataCache` should not be called in edge runtime.' '`createRenderResumeDataCache` should not be called in edge runtime.'
) )
} else { }
if (typeof resumeDataCacheOrPersistedCache !== 'string') {
// If the cache is already read-only, return it directly. Otherwise we
// perform a type change by overriding the discriminator — the underlying
// Map references are still shared, but callers should treat the result
// as immutable.
if (!resumeDataCacheOrPersistedCache.mutable) {
return resumeDataCacheOrPersistedCache
}
return { ...resumeDataCacheOrPersistedCache, mutable: false }
}
if (resumeDataCacheOrPersistedCache === 'null') {
return {
mutable: false,
cache: new Map(),
fetch: new Map(),
encryptedBoundArgs: new Map(),
decryptedBoundArgs: new Map(),
imageResponses: new Map(),
}
}
let serializedResumeDataCache = resumeDataCacheOrPersistedCache
if (!disableResumeDataCacheCompression) {
// This should be a compressed string. Let's decompress it using zlib.
// As the data we already want to decompress is in memory, we use the
// synchronous inflateSync function.
const { inflateSync } = require('node:zlib') as typeof import('node:zlib')
// Limit decompressed size to prevent zipbomb attacks. This is 5x the
// configured maxPostponedStateSize, allowing reasonable compression
// ratios while preventing extreme decompression bombs.
// Default is 500MB (5x the default 100MB compressed limit).
const maxDecompressedSize = maxPostponedStateSizeBytes
? maxPostponedStateSizeBytes * 5
: 500 * 1024 * 1024
try { if (typeof resumeDataCacheOrPersistedCache !== 'string') {
serializedResumeDataCache = inflateSync( // If the cache is already read-only, return it directly. Otherwise we
Buffer.from(serializedResumeDataCache, 'base64'), // perform a type change by overriding the discriminator — the underlying
{ // Map references are still shared, but callers should treat the result
maxOutputLength: maxDecompressedSize, // as immutable.
} if (!resumeDataCacheOrPersistedCache.mutable) {
).toString('utf-8') return resumeDataCacheOrPersistedCache
} catch (err: unknown) {
if (
err instanceof RangeError &&
(err as NodeJS.ErrnoException).code === 'ERR_BUFFER_TOO_LARGE'
) {
throw new Error(
`Decompressed resume data cache exceeded ${maxDecompressedSize} byte limit`
)
}
throw err
}
} }
return { ...resumeDataCacheOrPersistedCache, mutable: false }
}
const json = JSON.parse(serializedResumeDataCache) as ResumeStoreSerialized if (resumeDataCacheOrPersistedCache === 'null') {
return { return {
mutable: false, mutable: false,
cache: parseUseCacheCacheStore(Object.entries(json.store.cache)), cache: new Map(),
fetch: new Map(Object.entries(json.store.fetch)), fetch: new Map(),
encryptedBoundArgs: new Map( encryptedBoundArgs: new Map(),
Object.entries(json.store.encryptedBoundArgs)
),
decryptedBoundArgs: new Map(), decryptedBoundArgs: new Map(),
imageResponses: new Map(), imageResponses: new Map(),
} }
} }
const json = JSON.parse(
resumeDataCacheOrPersistedCache
) as ResumeStoreSerialized
return {
mutable: false,
cache: parseUseCacheCacheStore(Object.entries(json.store.cache)),
fetch: new Map(Object.entries(json.store.fetch)),
encryptedBoundArgs: new Map(Object.entries(json.store.encryptedBoundArgs)),
decryptedBoundArgs: new Map(),
imageResponses: new Map(),
}
} }
...@@ -1828,7 +1828,6 @@ function extractResumeDataCacheFromPostponedState( ...@@ -1828,7 +1828,6 @@ function extractResumeDataCacheFromPostponedState(
const postponedStringLength = parseInt(postponedStringLengthMatch) const postponedStringLength = parseInt(postponedStringLengthMatch)
return createRenderResumeDataCache( return createRenderResumeDataCache(
state.slice(postponedStringLengthMatch.length + postponedStringLength + 1), state.slice(postponedStringLengthMatch.length + postponedStringLength + 1)
undefined
) )
} }
import { nextTestSetup } from 'e2e-utils' import { nextTestSetup } from 'e2e-utils'
import { createNowRouteMatches, retry } from 'next-test-utils' import { createNowRouteMatches, retry } from 'next-test-utils'
import zlib from 'node:zlib'
const MATCHED_PATH = '/dynamic/[slug]' const MATCHED_PATH = '/dynamic/[slug]'
const PARSE_ERROR = 'Failed to parse postponed state' const PARSE_ERROR = 'Failed to parse postponed state'
// A real deflate stream (base64) of a representative resume-data-cache payload. // A representative serialized resume-data-cache payload. Slicing this produces
// Slicing this produces the truncated/corrupt tails used by the cases below. // the truncated/malformed tails used by the cases below.
function validResumeDataCacheTail(): string { function validResumeDataCacheTail(): string {
return zlib return JSON.stringify({
.deflateSync( store: { cache: {}, fetch: { a: 1 }, encryptedBoundArgs: {} },
JSON.stringify({ })
store: { cache: {}, fetch: { a: 1 }, encryptedBoundArgs: {} },
})
)
.toString('base64')
} }
// These tests exercise the real minimal-mode resume path: the body of a // These tests exercise the real minimal-mode resume path: the body of a
...@@ -51,7 +46,7 @@ describe('postponed resume - parse failure diagnostics', () => { ...@@ -51,7 +46,7 @@ describe('postponed resume - parse failure diagnostics', () => {
return { response, outputIndex } return { response, outputIndex }
} }
it('reports a truncated postponed string (incomplete delivery) as Z_BUF', async () => { it('reports a truncated postponed string (incomplete delivery)', async () => {
// Declares a 100-char postponed string but delivers far fewer bytes, so the // Declares a 100-char postponed string but delivers far fewer bytes, so the
// postponed string itself is short and the resume-data-cache tail is empty. // postponed string itself is short and the resume-data-cache tail is empty.
const { response, outputIndex } = await postResume('a', '100:short') const { response, outputIndex } = await postResume('a', '100:short')
...@@ -63,11 +58,11 @@ describe('postponed resume - parse failure diagnostics', () => { ...@@ -63,11 +58,11 @@ describe('postponed resume - parse failure diagnostics', () => {
const cliOutput = next.cliOutput.slice(outputIndex) const cliOutput = next.cliOutput.slice(outputIndex)
expect(cliOutput).toContain(PARSE_ERROR) expect(cliOutput).toContain(PARSE_ERROR)
expect(cliOutput).toContain('postponedStringComplete: false') expect(cliOutput).toContain('postponedStringComplete: false')
expect(cliOutput).toContain('Z_BUF_ERROR') expect(cliOutput).toContain("errorName: 'SyntaxError'")
}) })
}) })
it('reports a truncated resume-data-cache tail (complete string) as Z_BUF', async () => { it('reports a truncated resume-data-cache tail (complete string)', async () => {
const tail = validResumeDataCacheTail() const tail = validResumeDataCacheTail()
const truncatedTail = tail.slice(0, tail.length - 12) const truncatedTail = tail.slice(0, tail.length - 12)
const { response, outputIndex } = await postResume( const { response, outputIndex } = await postResume(
...@@ -81,14 +76,13 @@ describe('postponed resume - parse failure diagnostics', () => { ...@@ -81,14 +76,13 @@ describe('postponed resume - parse failure diagnostics', () => {
const cliOutput = next.cliOutput.slice(outputIndex) const cliOutput = next.cliOutput.slice(outputIndex)
expect(cliOutput).toContain(PARSE_ERROR) expect(cliOutput).toContain(PARSE_ERROR)
expect(cliOutput).toContain('postponedStringComplete: true') expect(cliOutput).toContain('postponedStringComplete: true')
expect(cliOutput).toContain('Z_BUF_ERROR') expect(cliOutput).toContain("errorName: 'SyntaxError'")
}) })
}) })
it('reports a corrupt resume-data-cache tail as Z_DATA (a real bug to surface)', async () => { it('reports a malformed resume-data-cache tail', async () => {
const tail = validResumeDataCacheTail() const tail = validResumeDataCacheTail()
// Dropping the leading bytes corrupts the deflate header, which is a // Dropping the leading bytes makes the JSON malformed rather than short.
// content corruption rather than a short read.
const corruptTail = tail.slice(6) const corruptTail = tail.slice(6)
const { response, outputIndex } = await postResume('c', `1:x${corruptTail}`) const { response, outputIndex } = await postResume('c', `1:x${corruptTail}`)
...@@ -97,7 +91,7 @@ describe('postponed resume - parse failure diagnostics', () => { ...@@ -97,7 +91,7 @@ describe('postponed resume - parse failure diagnostics', () => {
await retry(async () => { await retry(async () => {
const cliOutput = next.cliOutput.slice(outputIndex) const cliOutput = next.cliOutput.slice(outputIndex)
expect(cliOutput).toContain(PARSE_ERROR) expect(cliOutput).toContain(PARSE_ERROR)
expect(cliOutput).toContain('Z_DATA_ERROR') expect(cliOutput).toContain("errorName: 'SyntaxError'")
}) })
}) })
......
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