Unverified Commit c021d10f authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

[backport] Encode non-ASCII characters in cache tags at construction (#93918)

Backports:

- #93617
- #93601

---------
Co-authored-by: 's avatarSwarnava Sengupta <swarnava.sengupta@vercel.com>
Co-authored-by: 's avatarOr Nakash <ornakash@gmail.com>
parent 9184ddb1
/**
* Percent-encode every character outside printable ASCII so a tag value can be
* safely serialized as part of the `x-next-cache-tags` HTTP header.
*
* Node's `validateHeaderValue` rejects any code unit outside `\t\x20-\x7e`, so
* a matched route path or user-supplied tag containing a non-ASCII character
* (Hebrew, Arabic, Chinese, emoji, …) would otherwise throw `ERR_INVALID_CHAR`
* and crash ISR on every affected request.
*
* This is applied at the public boundaries — tag construction
* (`getImplicitTags`, `validateTags`) and invalidation input (`revalidatePath`,
* `revalidateTag`, `updateTag`) — so storage, comparison, and the wire all see
* the same canonical ASCII-safe form.
*
* The character class `[\t\x20-\x7e]` mirrors Node's `validHdrChars` table —
* `\t` plus printable ASCII through `~`. Anything outside that is rejected
* by `validateHeaderValue`, so we encode runs of those characters and leave
* everything else (`,`, `/`, `%`, `[`, `]`, `_`, `-`, `\t`, …) byte-for-byte
* unchanged. This preserves the comma-separated header format and the
* dynamic-segment markers in derived tags (`_N_T_/[slug]/page`).
*
* Properties:
* - Fast-path: input that already fits the validation class is returned
* unchanged. This makes the encoder idempotent on already-encoded `%xx`
* sequences.
* - Matches *runs* of out-of-class code units so surrogate pairs (e.g. an
* emoji) are handed to `encodeURIComponent` as a complete code point — a
* per-code-unit regex would split the pair and throw `URIError`.
*/
const OUT_OF_CLASS_CHAR = /[^\t\x20-\x7e]/
const OUT_OF_CLASS_RUN = /[^\t\x20-\x7e]+/g
export function encodeCacheTag(tag: string): string {
return OUT_OF_CLASS_CHAR.test(tag)
? tag.replace(OUT_OF_CLASS_RUN, (run) => encodeURIComponent(run))
: tag
}
...@@ -73,6 +73,34 @@ describe('getImplicitTags()', () => { ...@@ -73,6 +73,34 @@ describe('getImplicitTags()', () => {
'_N_T_/foo/bar/baz', '_N_T_/foo/bar/baz',
], ],
}, },
{
// Non-ASCII pathname must be percent-encoded so it can be safely
// serialized into the `x-next-cache-tags` HTTP header. Surrogate-pair
// emoji exercises run-based replacement (a per-code-unit regex would
// throw `URIError`).
page: '/[slug]/page',
pathname: '/🎉',
fallbackRouteParams: null,
expectedTags: [
'_N_T_/layout',
'_N_T_/[slug]/layout',
'_N_T_/[slug]/page',
'_N_T_/%F0%9F%8E%89',
],
},
{
// Already-encoded pathname must not be double-encoded. The encoder
// is idempotent on ASCII input including `%xx` sequences.
page: '/[slug]/page',
pathname: '/%F0%9F%8E%89',
fallbackRouteParams: null,
expectedTags: [
'_N_T_/layout',
'_N_T_/[slug]/layout',
'_N_T_/[slug]/page',
'_N_T_/%F0%9F%8E%89',
],
},
])( ])(
'for page $page with pathname $pathname', 'for page $page with pathname $pathname',
async ({ page, pathname, fallbackRouteParams, expectedTags }) => { async ({ page, pathname, fallbackRouteParams, expectedTags }) => {
......
import { NEXT_CACHE_IMPLICIT_TAG_ID } from '../../lib/constants' import { NEXT_CACHE_IMPLICIT_TAG_ID } from '../../lib/constants'
import type { OpaqueFallbackRouteParams } from '../request/fallback-params' import type { OpaqueFallbackRouteParams } from '../request/fallback-params'
import { getCacheHandlerEntries } from '../use-cache/handlers' import { getCacheHandlerEntries } from '../use-cache/handlers'
import { encodeCacheTag } from './encode-cache-tag'
import { createLazyResult, type LazyResult } from './lazy-result' import { createLazyResult, type LazyResult } from './lazy-result'
export interface ImplicitTags { export interface ImplicitTags {
...@@ -78,17 +79,19 @@ export async function getImplicitTags( ...@@ -78,17 +79,19 @@ export async function getImplicitTags(
): Promise<ImplicitTags> { ): Promise<ImplicitTags> {
const tags = new Set<string>() const tags = new Set<string>()
// Add the derived tags from the page. // Add the derived tags from the page. Encode each tag so a non-ASCII
// pathname doesn't trip header validation when written to
// `x-next-cache-tags`. Idempotent on already-ASCII input.
const derivedTags = getDerivedTags(page) const derivedTags = getDerivedTags(page)
for (let tag of derivedTags) { for (let tag of derivedTags) {
tag = `${NEXT_CACHE_IMPLICIT_TAG_ID}${tag}` tag = encodeCacheTag(`${NEXT_CACHE_IMPLICIT_TAG_ID}${tag}`)
tags.add(tag) tags.add(tag)
} }
// Add the tags from the pathname. If the route has unknown params, we don't // Add the tags from the pathname. If the route has unknown params, we don't
// want to add the pathname as a tag, as it will be invalid. // want to add the pathname as a tag, as it will be invalid.
if (pathname && (!fallbackRouteParams || fallbackRouteParams.size === 0)) { if (pathname && (!fallbackRouteParams || fallbackRouteParams.size === 0)) {
const tag = `${NEXT_CACHE_IMPLICIT_TAG_ID}${pathname}` const tag = encodeCacheTag(`${NEXT_CACHE_IMPLICIT_TAG_ID}${pathname}`)
tags.add(tag) tags.add(tag)
} }
......
...@@ -30,6 +30,7 @@ import { ...@@ -30,6 +30,7 @@ import {
import { cloneResponse } from './clone-response' import { cloneResponse } from './clone-response'
import type { IncrementalCache } from './incremental-cache' import type { IncrementalCache } from './incremental-cache'
import { RenderStage } from '../app-render/staged-rendering' import { RenderStage } from '../app-render/staged-rendering'
import { encodeCacheTag } from './encode-cache-tag'
const isEdgeRuntime = process.env.NEXT_RUNTIME === 'edge' const isEdgeRuntime = process.env.NEXT_RUNTIME === 'edge'
...@@ -95,7 +96,10 @@ export function validateTags(tags: any[], description: string) { ...@@ -95,7 +96,10 @@ export function validateTags(tags: any[], description: string) {
reason: `exceeded max length of ${NEXT_CACHE_TAG_MAX_LENGTH}`, reason: `exceeded max length of ${NEXT_CACHE_TAG_MAX_LENGTH}`,
}) })
} else { } else {
validTags.push(tag) // Encode so a non-ASCII tag can be safely serialized into the
// `x-next-cache-tags` HTTP header without tripping Node's header
// validation. Length is checked on the raw input above.
validTags.push(encodeCacheTag(tag))
} }
if (validTags.length > NEXT_CACHE_TAG_MAX_ITEMS) { if (validTags.length > NEXT_CACHE_TAG_MAX_ITEMS) {
......
...@@ -16,6 +16,7 @@ import { ...@@ -16,6 +16,7 @@ import {
ActionDidRevalidateStaticAndDynamic as ActionDidRevalidate, ActionDidRevalidateStaticAndDynamic as ActionDidRevalidate,
} from '../../../shared/lib/action-revalidation-kind' } from '../../../shared/lib/action-revalidation-kind'
import { removeTrailingSlash } from '../../../shared/lib/router/utils/remove-trailing-slash' import { removeTrailingSlash } from '../../../shared/lib/router/utils/remove-trailing-slash'
import { encodeCacheTag } from '../../lib/encode-cache-tag'
type CacheLifeConfig = { type CacheLifeConfig = {
expire?: number expire?: number
...@@ -32,7 +33,7 @@ export function revalidateTag(tag: string, profile: string | CacheLifeConfig) { ...@@ -32,7 +33,7 @@ export function revalidateTag(tag: string, profile: string | CacheLifeConfig) {
'"revalidateTag" without the second argument is now deprecated, add second argument of "max" or use "updateTag". See more info here: https://nextjs.org/docs/messages/revalidate-tag-single-arg' '"revalidateTag" without the second argument is now deprecated, add second argument of "max" or use "updateTag". See more info here: https://nextjs.org/docs/messages/revalidate-tag-single-arg'
) )
} }
return revalidate([tag], `revalidateTag ${tag}`, profile) return revalidate([encodeCacheTag(tag)], `revalidateTag ${tag}`, profile)
} }
/** /**
...@@ -54,7 +55,7 @@ export function updateTag(tag: string) { ...@@ -54,7 +55,7 @@ export function updateTag(tag: string) {
) )
} }
// updateTag uses immediate expiration (no profile) without deprecation warning // updateTag uses immediate expiration (no profile) without deprecation warning
return revalidate([tag], `updateTag ${tag}`, undefined) return revalidate([encodeCacheTag(tag)], `updateTag ${tag}`, undefined)
} }
/** /**
...@@ -97,7 +98,7 @@ export function revalidatePath(originalPath: string, type?: 'layout' | 'page') { ...@@ -97,7 +98,7 @@ export function revalidatePath(originalPath: string, type?: 'layout' | 'page') {
return return
} }
let normalizedPath = `${NEXT_CACHE_IMPLICIT_TAG_ID}${removeTrailingSlash(originalPath)}` let normalizedPath = `${NEXT_CACHE_IMPLICIT_TAG_ID}${encodeCacheTag(removeTrailingSlash(originalPath))}`
if (type) { if (type) {
normalizedPath += `${normalizedPath.endsWith('/') ? '' : '/'}${type}` normalizedPath += `${normalizedPath.endsWith('/') ? '' : '/'}${type}`
......
...@@ -288,8 +288,12 @@ export function unstable_cache<T extends Callback>( ...@@ -288,8 +288,12 @@ export function unstable_cache<T extends Callback>(
// Check if we need to do foreground revalidation // Check if we need to do foreground revalidation
if (workStore.isStaticGeneration) { if (workStore.isStaticGeneration) {
// When the page is revalidating and the cache entry is stale, // When the page is revalidating and the cache entry is stale,
// we need to wait for fresh data (blocking revalidate) // we need to wait for fresh data (blocking revalidate). The
return workStore.pendingRevalidates[invocationKey] // `await` here keeps `cacheSignal.endRead` (in the outer
// `finally`) suspended until the recompute + cacheNewResult
// actually complete, so the prospective prerender's
// `cacheSignal` doesn't resolve `cacheReady` prematurely.
return await workStore.pendingRevalidates[invocationKey]
} }
// Otherwise, we're doing background revalidation - return stale immediately // Otherwise, we're doing background revalidation - return stale immediately
} }
......
import { cacheTag, unstable_cache, updateTag } from 'next/cache'
import { connection } from 'next/server'
import { Suspense } from 'react'
export function generateStaticParams() {
return [{ slug: '🎉' }]
}
async function Cached({ params }: { params: Promise<{ slug: string }> }) {
'use cache'
const { slug } = await params
cacheTag('🎂')
return (
<>
<p id="slug">{slug}</p>
<p>
Cached: <span id="cached-time">{new Date().toISOString()}</span>
</p>
</>
)
}
async function Dynamic() {
await connection()
return (
<p>
Dynamic: <span id="dynamic-time">{new Date().toISOString()}</span>
</p>
)
}
const getUnstableCached = unstable_cache(
async () => new Date().toISOString(),
['unstable-cache-time'],
{ tags: ['🌶'], revalidate: false }
)
export default async function Page({
params,
}: {
params: Promise<{ slug: string }>
}) {
const fetched = await fetch(
'https://next-data-api-endpoint.vercel.app/api/random',
{ next: { tags: ['🌮'], revalidate: false } }
).then((r) => r.text())
const unstableCached = await getUnstableCached()
return (
<main>
<Cached params={params} />
<Suspense fallback={<p>Loading...</p>}>
<Dynamic />
</Suspense>
<p>
Fetched: <span id="fetched">{fetched}</span>
</p>
<p>
Unstable cached: <span id="unstable-cached-time">{unstableCached}</span>
</p>
<form>
<button
id="update-tag"
formAction={async () => {
'use server'
updateTag('🎂')
}}
>
updateTag
</button>
</form>
</main>
)
}
import { revalidatePath, revalidateTag } from 'next/cache'
export async function POST(request: Request) {
const { searchParams } = new URL(request.url)
const path = searchParams.get('path')
const tag = searchParams.get('tag')
if (path) {
revalidatePath(path)
}
if (tag) {
revalidateTag(tag, 'max')
}
return Response.json({ ok: true, path, tag })
}
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
cacheComponents: true,
}
module.exports = nextConfig
import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'
// Regression test for https://github.com/vercel/next.js/issues/93142
//
// Any non-ASCII character (Hebrew, Arabic, CJK, emoji, …) in a cache tag —
// whether it's a path-derived implicit tag or a user-supplied tag from
// `cacheTag()`, `unstable_cache({tags})`, or `fetch({next:{tags}})` — gets
// written into the internal `x-next-cache-tags` HTTP header on ISR responses.
// Node's `validateHeaderValue` rejects any byte outside `\t\x20-\x7e`, so the
// response crashes with `ERR_INVALID_CHAR`.
//
// On Vercel deploy stale-if-error masks the 500 from clients, but revalidation
// itself keeps failing and the cache stops refreshing for affected routes. The
// revalidate / updateTag cases here cover that round-trip: a cached entry keyed
// under a non-ASCII path / tag must actually be invalidated by
// `revalidatePath`, `revalidateTag`, or `updateTag` against each cache backend.
describe('non-ASCII cache tags', () => {
const { next, isNextDeploy } = nextTestSetup({
files: __dirname,
})
const SLUG = '🎉'
const TAG = '🎂'
const FETCH_TAG = '🌮'
const UNSTABLE_TAG = '🌶'
const PATH = `/${encodeURIComponent(SLUG)}`
it('serves a non-ASCII slug ISR page without ERR_INVALID_CHAR', async () => {
const res = await next.fetch(PATH)
expect(res.status).toBe(200)
if (isNextDeploy) {
const tags = res.headers.get('x-next-cache-tags')
if (tags !== null) {
// Anything outside the validation character class would have
// crashed `setHeader` on the way out, so reaching the client at
// all is itself a signal — but assert explicitly to guard format.
expect(tags).toMatch(/^[\t\x20-\x7e]+$/)
}
}
})
it('invalidates a cached entry via revalidatePath with a non-ASCII path', async () => {
const initial = (await next.render$(PATH))('#cached-time').text()
const res = await next.fetch(
`/api/revalidate?path=${encodeURIComponent(`/${SLUG}`)}`,
{ method: 'POST' }
)
expect(res.status).toBe(200)
// Revalidation may take a moment to propagate.
await retry(async () => {
const after = (await next.render$(PATH))('#cached-time').text()
expect(after).not.toBe(initial)
})
})
it('invalidates a cached entry via revalidateTag with a non-ASCII tag', async () => {
const initial = (await next.render$(PATH))('#cached-time').text()
const res = await next.fetch(
`/api/revalidate?tag=${encodeURIComponent(TAG)}`,
{ method: 'POST' }
)
expect(res.status).toBe(200)
await retry(async () => {
const after = (await next.render$(PATH))('#cached-time').text()
expect(after).not.toBe(initial)
})
})
it('invalidates a fetch entry tagged with a non-ASCII tag via revalidateTag', async () => {
const initial = (await next.render$(PATH))('#fetched').text()
const res = await next.fetch(
`/api/revalidate?tag=${encodeURIComponent(FETCH_TAG)}`,
{ method: 'POST' }
)
expect(res.status).toBe(200)
await retry(async () => {
const after = (await next.render$(PATH))('#fetched').text()
expect(after).not.toBe(initial)
})
})
it('invalidates an `unstable_cache` entry tagged with a non-ASCII tag via revalidateTag', async () => {
const initial = (await next.render$(PATH))('#unstable-cached-time').text()
const res = await next.fetch(
`/api/revalidate?tag=${encodeURIComponent(UNSTABLE_TAG)}`,
{ method: 'POST' }
)
expect(res.status).toBe(200)
await retry(async () => {
const after = (await next.render$(PATH))('#unstable-cached-time').text()
expect(after).not.toBe(initial)
})
})
it('invalidates a cached entry via updateTag with a non-ASCII tag (Server Action)', async () => {
const browser = await next.browser(PATH)
const initial = await browser.elementByCss('#cached-time').text()
await browser.elementByCss('#update-tag').click()
await retry(async () => {
const after = await browser.elementByCss('#cached-time').text()
expect(after).not.toBe(initial)
})
})
})
import { revalidateTag, unstable_cache } from 'next/cache'
import { setTimeout } from 'node:timers/promises'
const getUnstableTime = unstable_cache(
async () => {
// Small artificial delay so the foreground-await is actually in flight when
// the prospective prerender decides whether `cacheSignal` is ready.
await setTimeout(100)
return new Date().toISOString()
},
['blocked-by-unstable-cache'],
{ tags: ['blocked-by-unstable-cache-tag'], revalidate: false }
)
async function Cached() {
'use cache'
return (
<p>
Cached: <span id="cached-time">{new Date().toISOString()}</span>
</p>
)
}
// Awaiting an `unstable_cache` BEFORE rendering a `'use cache'` component.
// After the tag is revalidated, the next render runs a background revalidation
// prerender. In its prospective phase, the `unstable_cache` lookup hits a stale
// entry and foreground-awaits the recompute. The downstream `<Cached />` must
// still be reached during prospective so its RDC entry is populated for the
// final phase — otherwise the final phase throws "Unexpected cache miss after
// cache warming phase during prerendering".
export default async function Page() {
const time = await getUnstableTime()
return (
<main>
<p>
Unstable: <span id="unstable-time">{time}</span>
</p>
<Cached />
<form
action={async () => {
'use server'
revalidateTag('blocked-by-unstable-cache-tag', 'max')
}}
>
<button id="revalidate">Revalidate</button>
</form>
</main>
)
}
...@@ -350,6 +350,35 @@ describe('use-cache', () => { ...@@ -350,6 +350,35 @@ describe('use-cache', () => {
expect(finalValueB).toBe(finalValueB) expect(finalValueB).toBe(finalValueB)
}) })
it('should reach a "use cache" rendered after a stale unstable_cache', async () => {
// Regression test: when an `unstable_cache` lookup hits a stale entry and
// foreground-awaits its recompute, a downstream `'use cache'` invocation
// rendered after it must still be reached during the prospective prerender
// phase so its RDC entry is populated. Otherwise the final phase throws
// "Unexpected cache miss after cache warming phase during prerendering" and
// the response cache fails to write a fresh APP_PAGE entry.
const browser = await next.browser('/blocked-by-unstable-cache')
const initialUnstable = await browser.elementByCss('#unstable-time').text()
const initialCached = await browser.elementByCss('#cached-time').text()
expect(initialUnstable).toBeDateString()
expect(initialCached).toBeDateString()
// Revalidate the unstable_cache entry so the next render foreground-awaits
// the recompute.
await browser.elementByCss('#revalidate').click()
// After revalidation, the next render must succeed and produce a fresh
// unstable-time. If the prospective prerender's `cacheSignal` resolves
// `cacheReady` before `<Cached />` is reached, the final phase throws and
// the background revalidation never writes a new APP_PAGE entry, so the
// unstable-time stays at its initial value forever.
await retry(async () => {
await browser.refresh()
const after = await browser.elementByCss('#unstable-time').text()
expect(after).not.toBe(initialUnstable)
})
})
it('should revalidate caches nested in unstable_cache', async () => { it('should revalidate caches nested in unstable_cache', async () => {
const browser = await next.browser('/nested-in-unstable-cache') const browser = await next.browser('/nested-in-unstable-cache')
const initial = await browser.elementByCss('p').text() const initial = await browser.elementByCss('p').text()
...@@ -482,6 +511,7 @@ describe('use-cache', () => { ...@@ -482,6 +511,7 @@ describe('use-cache', () => {
expect.stringMatching(/\/api\/\d/), expect.stringMatching(/\/api\/\d/),
// [id] route, second entry in generateStaticParams // [id] route, second entry in generateStaticParams
expect.stringMatching(/\/b\d/), expect.stringMatching(/\/b\d/),
'/blocked-by-unstable-cache',
'/cache-fetch', '/cache-fetch',
'/cache-fetch-no-store', '/cache-fetch-no-store',
'/cache-life', '/cache-life',
......
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