Unverified Commit 8c96fec0 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

backport: Strengthen _rsc cache-busting param (#92755) (#93105)

x-ref: #92755

---------
Co-authored-by: 's avatarZack Tanner <1939140+ztanner@users.noreply.github.com>
parent b15a3f62
......@@ -294,7 +294,7 @@ export async function createFetch(
// search param to it. This should not leak outside of this function, so we
// track them separately.
let fetchUrl = new URL(url)
setCacheBustingSearchParam(fetchUrl, headers)
await setCacheBustingSearchParam(fetchUrl, headers)
let browserResponse = await fetch(fetchUrl, fetchOptions)
// If the server responds with a redirect (e.g. 307), and the redirected
......@@ -349,7 +349,7 @@ export async function createFetch(
// Append the cache busting search param to the redirected URL and
// fetch again.
fetchUrl = new URL(responseUrl)
setCacheBustingSearchParam(fetchUrl, headers)
await setCacheBustingSearchParam(fetchUrl, headers)
browserResponse = await fetch(fetchUrl, fetchOptions)
// We just performed a manual redirect, so this is now true.
redirected = true
......
import {
NEXT_ROUTER_PREFETCH_HEADER,
NEXT_ROUTER_SEGMENT_PREFETCH_HEADER,
NEXT_ROUTER_STATE_TREE_HEADER,
NEXT_RSC_UNION_QUERY,
NEXT_URL,
} from '../app-router-headers'
import { setCacheBustingSearchParam } from './set-cache-busting-search-param'
import { computeLegacyCacheBustingSearchParam } from '../../../shared/lib/router/utils/cache-busting-search-param'
describe('setCacheBustingSearchParam', () => {
const originalCryptoDescriptor = Object.getOwnPropertyDescriptor(
globalThis,
'crypto'
)
afterEach(() => {
if (originalCryptoDescriptor) {
Object.defineProperty(globalThis, 'crypto', originalCryptoDescriptor)
}
})
it('falls back to the legacy hash when Web Crypto is unavailable', async () => {
Object.defineProperty(globalThis, 'crypto', {
configurable: true,
value: {},
})
const headers = {
[NEXT_ROUTER_PREFETCH_HEADER]: '1',
[NEXT_ROUTER_SEGMENT_PREFETCH_HEADER]: '/_tree',
[NEXT_ROUTER_STATE_TREE_HEADER]: '%5B%22%22%2C%7B%7D%5D',
[NEXT_URL]: '/pcsta0',
} as const
const url = new URL('https://example.com/')
await setCacheBustingSearchParam(url, headers)
expect(url.searchParams.get(NEXT_RSC_UNION_QUERY)).toBe(
computeLegacyCacheBustingSearchParam(
headers[NEXT_ROUTER_PREFETCH_HEADER],
headers[NEXT_ROUTER_SEGMENT_PREFETCH_HEADER],
headers[NEXT_ROUTER_STATE_TREE_HEADER],
headers[NEXT_URL]
)
)
})
})
'use client'
import { computeCacheBustingSearchParam } from '../../../shared/lib/router/utils/cache-busting-search-param'
import {
computeCacheBustingSearchParam,
computeLegacyCacheBustingSearchParam,
} from '../../../shared/lib/router/utils/cache-busting-search-param'
import {
NEXT_ROUTER_PREFETCH_HEADER,
NEXT_ROUTER_SEGMENT_PREFETCH_HEADER,
......@@ -10,6 +13,26 @@ import {
} from '../app-router-headers'
import type { RequestHeaders } from './fetch-server-response'
async function computeClientCacheBustingSearchParam(
headers: RequestHeaders
): Promise<string> {
if (typeof globalThis.crypto?.subtle?.digest === 'function') {
return computeCacheBustingSearchParam(
headers[NEXT_ROUTER_PREFETCH_HEADER],
headers[NEXT_ROUTER_SEGMENT_PREFETCH_HEADER],
headers[NEXT_ROUTER_STATE_TREE_HEADER],
headers[NEXT_URL]
)
}
return computeLegacyCacheBustingSearchParam(
headers[NEXT_ROUTER_PREFETCH_HEADER],
headers[NEXT_ROUTER_SEGMENT_PREFETCH_HEADER],
headers[NEXT_ROUTER_STATE_TREE_HEADER],
headers[NEXT_URL]
)
}
/**
* Mutates the provided URL by adding a cache-busting search parameter for CDNs that don't
* support custom headers. This helps avoid caching conflicts by making each request unique.
......@@ -21,21 +44,17 @@ import type { RequestHeaders } from './fetch-server-response'
* URL before: https://example.com/path?query=1
* URL after: https://example.com/path?query=1&_rsc=abc123
*
* Note: This function mutates the input URL directly and does not return anything.
* Note: This function mutates the input URL directly and resolves once the
* cache-busting value has been computed and applied.
*
* TODO: Since we need to use a search param anyway, we could simplify by removing the custom
* headers approach entirely and just use search params.
*/
export const setCacheBustingSearchParam = (
export const setCacheBustingSearchParam = async (
url: URL,
headers: RequestHeaders
): void => {
const uniqueCacheKey = computeCacheBustingSearchParam(
headers[NEXT_ROUTER_PREFETCH_HEADER],
headers[NEXT_ROUTER_SEGMENT_PREFETCH_HEADER],
headers[NEXT_ROUTER_STATE_TREE_HEADER],
headers[NEXT_URL]
)
): Promise<void> => {
const uniqueCacheKey = await computeClientCacheBustingSearchParam(headers)
setCacheBustingSearchParamWithHash(url, uniqueCacheKey)
}
......@@ -50,7 +69,7 @@ export const setCacheBustingSearchParam = (
* hash: "abc123"
* URL after: https://example.com/path?query=1&_rsc=abc123
*
* If the hash is null, we will set `_rsc` search param without a value.
* If the hash is empty, we will set `_rsc` search param without a value.
* Like this: https://example.com/path?query=1&_rsc
*
* Note: This function mutates the input URL directly and does not return anything.
......
......@@ -59,7 +59,8 @@ import { workUnitAsyncStorage } from '../app-render/work-unit-async-storage.exte
import { InvariantError } from '../../shared/lib/invariant-error'
import { executeRevalidates } from '../revalidation-utils'
import { getRequestMeta } from '../request-meta'
import { setCacheBustingSearchParam } from '../../client/components/router-reducer/set-cache-busting-search-param'
import { setCacheBustingSearchParamWithHash } from '../../client/components/router-reducer/set-cache-busting-search-param'
import { computeCacheBustingSearchParam } from '../../shared/lib/router/utils/cache-busting-search-param'
/**
* Checks if the app has any server actions defined in any runtime.
......@@ -346,19 +347,15 @@ async function createRedirectRenderResult(
forwardedHeaders.delete(ACTION_HEADER)
try {
setCacheBustingSearchParam(fetchUrl, {
[NEXT_ROUTER_PREFETCH_HEADER]: forwardedHeaders.get(
NEXT_ROUTER_PREFETCH_HEADER
)
const cacheBustingSearchParam = await computeCacheBustingSearchParam(
forwardedHeaders.get(NEXT_ROUTER_PREFETCH_HEADER)
? ('1' as const)
: undefined,
[NEXT_ROUTER_SEGMENT_PREFETCH_HEADER]:
forwardedHeaders.get(NEXT_ROUTER_SEGMENT_PREFETCH_HEADER) ??
undefined,
[NEXT_ROUTER_STATE_TREE_HEADER]:
forwardedHeaders.get(NEXT_ROUTER_STATE_TREE_HEADER) ?? undefined,
[NEXT_URL]: forwardedHeaders.get(NEXT_URL) ?? undefined,
})
forwardedHeaders.get(NEXT_ROUTER_SEGMENT_PREFETCH_HEADER) ?? undefined,
forwardedHeaders.get(NEXT_ROUTER_STATE_TREE_HEADER) ?? undefined,
forwardedHeaders.get(NEXT_URL) ?? undefined
)
setCacheBustingSearchParamWithHash(fetchUrl, cacheBustingSearchParam)
const response = await fetch(fetchUrl, {
method: 'GET',
......
......@@ -143,10 +143,13 @@ import { decodeQueryPathParameter } from './lib/decode-query-path-parameter'
import { NoFallbackError } from '../shared/lib/no-fallback-error.external'
import { getCacheHandlers } from './use-cache/handlers'
import { fixMojibake } from './lib/fix-mojibake'
import { computeCacheBustingSearchParam } from '../shared/lib/router/utils/cache-busting-search-param'
import { setCacheBustingSearchParamWithHash } from '../client/components/router-reducer/set-cache-busting-search-param'
import type { CacheControl } from './lib/cache-control'
import type { PrerenderedRoute } from '../build/static-paths/types'
import {
computeCacheBustingSearchParam,
computeLegacyCacheBustingSearchParam,
} from '../shared/lib/router/utils/cache-busting-search-param'
export type FindComponentsResult = {
components: LoadComponentsReturnType
......@@ -2050,7 +2053,7 @@ export default abstract class Server<
headers[NEXT_ROUTER_SEGMENT_PREFETCH_HEADER] ||
getRequestMeta(req, 'segmentPrefetchRSCRequest')
const expectedHash = computeCacheBustingSearchParam(
const expectedHash = await computeCacheBustingSearchParam(
routerPrefetch,
segmentPrefetchRSCRequest,
headers[NEXT_ROUTER_STATE_TREE_HEADER],
......@@ -2062,10 +2065,24 @@ export default abstract class Server<
NEXT_RSC_UNION_QUERY
)
if (expectedHash !== actualHash) {
let matchesHash = expectedHash === actualHash
if (!matchesHash && actualHash !== null) {
// We'll fallback to checking the legacy hash format to support clients that do not have a secure context
matchesHash =
computeLegacyCacheBustingSearchParam(
routerPrefetch,
segmentPrefetchRSCRequest,
headers[NEXT_ROUTER_STATE_TREE_HEADER],
headers[NEXT_URL]
) === actualHash
}
if (!matchesHash) {
// The hash sent by the client does not match the expected value.
// Redirect to the URL with the correct cache-busting search param.
// This prevents cache poisoning attacks on CDNs that don't respect Vary headers.
// We continue to accept the legacy short hash for clients that still
// generate the 5-character `_rsc` form.
// Note: When no headers are present, expectedHash is empty string and client
// must send `_rsc` param, otherwise actualHash is null and hash check fails.
const url = new URL(req.url || '', 'http://localhost')
......
import { hexHash } from '../../hash'
export function computeCacheBustingSearchParam(
const CACHE_BUSTING_SEARCH_PARAM_DIGEST_BYTES = 12
const textEncoder = new TextEncoder()
function encodeCacheBustingSearchParam(bytes: Uint8Array): string {
let binary = ''
for (let i = 0; i < bytes.length; i++) {
binary += String.fromCharCode(bytes[i])
}
return btoa(binary).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '')
}
function normalizeCacheBustingInput(
value: string | string[] | undefined
): string {
if (value === undefined) {
return '0'
}
return Array.isArray(value) ? value.join(',') : value
}
function createCacheBustingSearchParamInput(
prefetchHeader: '1' | '2' | '0' | undefined,
segmentPrefetchHeader: string | string[] | undefined,
stateTreeHeader: string | string[] | undefined,
nextUrlHeader: string | string[] | undefined
): string {
): string | null {
if (
(prefetchHeader === undefined || prefetchHeader === '0') &&
segmentPrefetchHeader === undefined &&
stateTreeHeader === undefined &&
nextUrlHeader === undefined
) {
return null
}
return [
prefetchHeader ?? '0',
normalizeCacheBustingInput(segmentPrefetchHeader),
normalizeCacheBustingInput(stateTreeHeader),
normalizeCacheBustingInput(nextUrlHeader),
].join(',')
}
async function computeCacheBustingSearchParamFromInput(
input: string
): Promise<string> {
// Truncate SHA-256 to 96 bits to keep `_rsc` compact
const digest = await globalThis.crypto.subtle.digest(
'SHA-256',
textEncoder.encode(input)
)
return encodeCacheBustingSearchParam(
new Uint8Array(digest).subarray(0, CACHE_BUSTING_SEARCH_PARAM_DIGEST_BYTES)
)
}
export async function computeCacheBustingSearchParam(
prefetchHeader: '1' | '2' | '0' | undefined,
segmentPrefetchHeader: string | string[] | undefined,
stateTreeHeader: string | string[] | undefined,
nextUrlHeader: string | string[] | undefined
): Promise<string> {
const input = createCacheBustingSearchParamInput(
prefetchHeader,
segmentPrefetchHeader,
stateTreeHeader,
nextUrlHeader
)
if (input === null) {
return ''
}
return hexHash(
[
prefetchHeader || '0',
segmentPrefetchHeader || '0',
stateTreeHeader || '0',
nextUrlHeader || '0',
].join(',')
return computeCacheBustingSearchParamFromInput(input)
}
export function computeLegacyCacheBustingSearchParam(
prefetchHeader: '1' | '2' | '0' | undefined,
segmentPrefetchHeader: string | string[] | undefined,
stateTreeHeader: string | string[] | undefined,
nextUrlHeader: string | string[] | undefined
): string {
const input = createCacheBustingSearchParamInput(
prefetchHeader,
segmentPrefetchHeader,
stateTreeHeader,
nextUrlHeader
)
if (input === null) {
return ''
}
return hexHash(input)
}
......@@ -260,14 +260,24 @@ describe('app dir - prefetching', () => {
true,
])
)
const response = await next.fetch(`/prefetch-auto/justputit?_rsc=dcqtr`, {
headers: {
rsc: '1',
'next-router-prefetch': '1',
'next-router-state-tree': stateTree,
'next-url': '/prefetch-auto/justputit',
},
})
const headers = {
rsc: '1',
'next-router-prefetch': '1',
'next-router-state-tree': stateTree,
'next-url': '/prefetch-auto/justputit',
} as const
const cacheBustingParam = await computeCacheBustingSearchParam(
headers['next-router-prefetch'],
undefined,
headers['next-router-state-tree'],
headers['next-url']
)
const response = await next.fetch(
`/prefetch-auto/justputit?_rsc=${cacheBustingParam}`,
{
headers,
}
)
const prefetchResponse = await response.text()
expect(prefetchResponse).not.toContain('Page Data!')
......@@ -304,7 +314,7 @@ describe('app dir - prefetching', () => {
}
const url = new URL('/prefetch-auto/justputit', 'http://localhost')
const cacheBustingParam = computeCacheBustingSearchParam(
const cacheBustingParam = await computeCacheBustingSearchParam(
headers['next-router-prefetch'] ? '1' : '0',
undefined,
headers['next-router-state-tree'],
......
import { nextTestSetup } from 'e2e-utils'
import { computeCacheBustingSearchParam } from 'next/dist/shared/lib/router/utils/cache-busting-search-param'
import {
computeCacheBustingSearchParam,
computeLegacyCacheBustingSearchParam,
} from 'next/dist/shared/lib/router/utils/cache-busting-search-param'
describe('app dir - validation', () => {
const { next, skipped } = nextTestSetup({
......@@ -29,13 +32,13 @@ describe('app dir - validation', () => {
const url2 = new URL('/', 'http://localhost')
// Add cache busting search param for both requests
const cacheBustingParam1 = computeCacheBustingSearchParam(
const cacheBustingParam1 = await computeCacheBustingSearchParam(
undefined,
undefined,
stateTree1,
undefined
)
const cacheBustingParam2 = computeCacheBustingSearchParam(
const cacheBustingParam2 = await computeCacheBustingSearchParam(
undefined,
undefined,
stateTree2,
......@@ -55,4 +58,51 @@ describe('app dir - validation', () => {
const res2 = await next.fetch(url2.toString(), { headers: headers2 })
expect(res2.status).toBe(200)
})
it('should generate distinct cache-busting params for known colliding RSC variants', async () => {
const stateTree = '%5B%22%22%2C%7B%7D%5D'
const fullRequestHash = await computeCacheBustingSearchParam(
undefined,
undefined,
stateTree,
undefined
)
const prefetchRequestHash = await computeCacheBustingSearchParam(
'1',
'/_tree',
stateTree,
'/pcsta0'
)
expect(fullRequestHash).toHaveLength(16)
expect(prefetchRequestHash).toHaveLength(16)
expect(fullRequestHash).not.toBe(prefetchRequestHash)
})
it('should accept legacy cache-busting params on plain HTTP requests', async () => {
const stateTree = '%5B%22%22%2C%7B%7D%5D'
const url = new URL('/', 'http://localhost')
const headers = {
rsc: '1',
'next-router-state-tree': stateTree,
}
url.searchParams.set(
'_rsc',
computeLegacyCacheBustingSearchParam(
undefined,
undefined,
stateTree,
undefined
)
)
const res = await next.fetch(url.toString(), {
headers,
redirect: 'manual',
})
expect(res.status).toBe(200)
})
})
......@@ -47,18 +47,18 @@ const pages: Page[] = [
},
]
const addCacheBustingSearchParam = (
const addCacheBustingSearchParam = async (
pathname: string,
headers: Record<string, string | string[] | undefined>
) => {
const cacheKey = computeCacheBustingSearchParam(
const cacheKey = await computeCacheBustingSearchParam(
headers['next-router-prefetch'] ? '1' : '0',
headers['next-router-segment-prefetch'],
headers['next-router-state-tree'],
headers['next-url']
)
if (cacheKey === null) {
if (cacheKey.length === 0) {
return pathname
}
......@@ -538,7 +538,7 @@ describe('ppr-full', () => {
rsc: '1',
'next-router-prefetch': '1',
}
const urlWithCacheBusting = addCacheBustingSearchParam(
const urlWithCacheBusting = await addCacheBustingSearchParam(
pathname,
headers
)
......@@ -577,7 +577,7 @@ describe('ppr-full', () => {
'next-router-prefetch': '1',
'X-Test-Input': unexpected,
}
const urlWithCacheBusting = addCacheBustingSearchParam(
const urlWithCacheBusting = await addCacheBustingSearchParam(
pathname,
headers
)
......@@ -597,7 +597,7 @@ describe('ppr-full', () => {
describe.each(pages)('for $pathname', ({ pathname, dynamic }) => {
it('should have correct headers', async () => {
const headers = { rsc: '1' }
const urlWithCacheBusting = addCacheBustingSearchParam(
const urlWithCacheBusting = await addCacheBustingSearchParam(
pathname,
headers
)
......@@ -624,7 +624,7 @@ describe('ppr-full', () => {
rsc: '1',
'X-Test-Input': expected,
}
const urlWithCacheBusting = addCacheBustingSearchParam(
const urlWithCacheBusting = await addCacheBustingSearchParam(
pathname,
headers
)
......@@ -644,7 +644,7 @@ describe('ppr-full', () => {
rsc: '1',
'X-Test-Input': unexpected,
}
const urlWithCacheBusting = addCacheBustingSearchParam(
const urlWithCacheBusting = await addCacheBustingSearchParam(
pathname,
headers
)
......
......@@ -417,9 +417,9 @@ describe('rewrite-headers', () => {
// Add cache busting param for RSC requests
if (headers.rsc === '1') {
const cacheBustingParam = computeCacheBustingSearchParam(
headers['next-router-prefetch'] ? '1' : '0',
undefined,
const cacheBustingParam = await computeCacheBustingSearchParam(
headers['next-router-prefetch'] ? '1' : undefined,
headers['next-router-segment-prefetch'],
headers['next-router-state-tree'],
undefined
)
......
......@@ -18,10 +18,13 @@ describe('conflicting routes', () => {
async function segmentPrefetch(url: string, segmentPath: string) {
const fetchUrl = new URL(url, 'http://localhost')
const searchParams = new URLSearchParams(fetchUrl.search)
searchParams.set(
'_rsc',
computeCacheBustingSearchParam('1', segmentPath, undefined, undefined)
const cacheBustingParam = await computeCacheBustingSearchParam(
'1',
segmentPath,
undefined,
undefined
)
searchParams.set('_rsc', cacheBustingParam)
fetchUrl.search = searchParams.toString()
return await next.fetch(fetchUrl.pathname + fetchUrl.search, {
headers: {
......
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