Unverified Commit 025e8535 authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

Validate server reference IDs during manifest lookup (#96007)

https://github.com/vercel/next.js/security/advisories/GHSA-955p-x3mx-jcvpCo-authored-by: 's avatarJosh Story <story@hey.com>
parent 93cb9089
--- ---
title: Failed to find Server Action title: Failed to find Server Action
description: Learn why Next.js could not resolve a Server Action or received a Server Action ID in an unexpected format.
--- ---
## Why This Message Occurred ## Why This Message Occurred
For security purposes, Next.js creates encrypted, non-deterministic keys (IDs) to allow for the client to reference and call the [Server Action](/docs/app/getting-started/mutating-data). These keys are periodically recalculated between builds for enhanced security. For security purposes, Next.js creates non-deterministic IDs that allow the client to reference and call [Server Actions](/docs/app/getting-started/mutating-data). These IDs can change between builds.
When self-hosting your Next.js application across multiple servers, each server instance may end up with a different encryption key, leading to potential inconsistencies. You may see one of the following messages:
### Failed to find Server Action
This message means that the Server Action ID had the expected format, but no matching Server Action was found in the current build.
This commonly happens when the client and server are using different builds. For example, a browser may still have assets from a previous deployment that reference a Server Action which is not present in the current deployment.
### Server Reference ID did not match the expected format
This message means that the request contained a Server Action ID that was not in the format generated by the current version of Next.js.
This can happen if the client and server are running versions of Next.js that use different ID formats. It can also be caused by intentionally malformed requests, including traffic generated by automated security scanning. If neither explanation applies, a bug in Next.js may be causing a valid Server Action ID to be rejected as having an invalid format.
## Possible Ways to Fix It ## Possible Ways to Fix It
To mitigate this, you can overwrite the encryption key using the `process.env.NEXT_SERVER_ACTIONS_ENCRYPTION_KEY` environment variable. Specifying this variable ensures that your encryption keys are persistent across builds, and all server instances use the same key. Ensure that your client assets and Server Actions come from the same deployment and use compatible versions of Next.js.
When self-hosting your Next.js application across multiple servers, ensure that all server instances use the same build output and encryption key. You can provide a consistent key using the `process.env.NEXT_SERVER_ACTIONS_ENCRYPTION_KEY` environment variable.
The key must be a base64-encoded value with a valid AES key length (16, 24, or 32 bytes). Set this variable at **build time**—the key is embedded in the build output and used automatically at runtime. The key must be a base64-encoded value with a valid AES key length (16, 24, or 32 bytes). Set this variable at **build time**—the key is embedded in the build output and used automatically at runtime.
If you are deploying your Next.js application to Vercel, you can use the feature [Skew Protection](https://vercel.com/docs/deployments/skew-protection) to ensure assets and functions from the previous version are still available, even after a new version is deployed. If you are deploying your Next.js application to Vercel, you can use the feature [Skew Protection](https://vercel.com/docs/deployments/skew-protection) to ensure assets and Server Actions from the previous version are still available, even after a new version is deployed.
If unexpected-format errors occur repeatedly, confirm that the client and server use compatible versions of Next.js and check whether the requests were intentionally malformed, such as traffic generated by automated security scanning. If neither applies and you can reproduce the error with a single version of Next.js, report the issue to the Next.js project.
## Useful Links ## Useful Links
......
...@@ -1439,5 +1439,6 @@ ...@@ -1439,5 +1439,6 @@
"1438": "Route \"%s\": Next.js encountered uncached data in \\`generateViewport()\\`.\\n\\n\\`fetch(...)\\` or \\`connection()\\` in \\`generateViewport()\\` prevents the page from being prerendered, leading to a slower user experience.\\n\\nWays to fix this:\\n - [cache] Cache the viewport data with \\`\"use cache\"\\` in \\`generateViewport()\\` (does not apply to \\`connection()\\`)\\n - [block] Set \\`export const instant = false\\` to allow a blocking route\\n\\nLearn more: https://nextjs.org/docs/messages/blocking-prerender-viewport-dynamic", "1438": "Route \"%s\": Next.js encountered uncached data in \\`generateViewport()\\`.\\n\\n\\`fetch(...)\\` or \\`connection()\\` in \\`generateViewport()\\` prevents the page from being prerendered, leading to a slower user experience.\\n\\nWays to fix this:\\n - [cache] Cache the viewport data with \\`\"use cache\"\\` in \\`generateViewport()\\` (does not apply to \\`connection()\\`)\\n - [block] Set \\`export const instant = false\\` to allow a blocking route\\n\\nLearn more: https://nextjs.org/docs/messages/blocking-prerender-viewport-dynamic",
"1439": "Route \"%s\": Next.js encountered URL data during prerendering or a navigation.\\n\\n\\`params\\` or \\`searchParams\\` accessed outside of \\`<Suspense>\\` may prevent the navigation from being instant, leading to a slower user experience.\\n\\nWays to fix this:\\n - [stream] Provide a placeholder with \\`<Suspense fallback={...}>\\` around the data access\\n - [block] Set \\`export const instant = false\\` to allow a blocking route\\n\\nLearn more: https://nextjs.org/docs/messages/instant-shell-url-data", "1439": "Route \"%s\": Next.js encountered URL data during prerendering or a navigation.\\n\\n\\`params\\` or \\`searchParams\\` accessed outside of \\`<Suspense>\\` may prevent the navigation from being instant, leading to a slower user experience.\\n\\nWays to fix this:\\n - [stream] Provide a placeholder with \\`<Suspense fallback={...}>\\` around the data access\\n - [block] Set \\`export const instant = false\\` to allow a blocking route\\n\\nLearn more: https://nextjs.org/docs/messages/instant-shell-url-data",
"1440": "Route \"%s\": Next.js encountered uncached data during prerendering.\\n\\n\\`fetch(...)\\` or \\`connection()\\` accessed outside of \\`<Suspense>\\` prevents the route from being prerendered, blocking the page load and leading to a slower user experience.\\n\\nWays to fix this:\\n - [stream] Provide a placeholder with \\`<Suspense fallback={...}>\\` around the data access\\n - [cache] Cache the data access with \\`\"use cache\"\\` (does not apply to \\`connection()\\`)\\n - [block] Set \\`export const instant = false\\` to allow a blocking route\\n\\nLearn more: https://nextjs.org/docs/messages/blocking-prerender-dynamic", "1440": "Route \"%s\": Next.js encountered uncached data during prerendering.\\n\\n\\`fetch(...)\\` or \\`connection()\\` accessed outside of \\`<Suspense>\\` prevents the route from being prerendered, blocking the page load and leading to a slower user experience.\\n\\nWays to fix this:\\n - [stream] Provide a placeholder with \\`<Suspense fallback={...}>\\` around the data access\\n - [cache] Cache the data access with \\`\"use cache\"\\` (does not apply to \\`connection()\\`)\\n - [block] Set \\`export const instant = false\\` to allow a blocking route\\n\\nLearn more: https://nextjs.org/docs/messages/blocking-prerender-dynamic",
"1441": "DevValidationScheduler requires at least one active validation" "1441": "DevValidationScheduler requires at least one active validation",
"1442": "The Server Reference ID did not match the expected format. Received %s.\\nRead more: https://nextjs.org/docs/messages/failed-to-find-server-action"
} }
import { parseHostHeader } from './action-handler' import { parseHostHeader } from './action-handler'
import {
getServerModuleMap,
setManifestsSingleton,
} from './manifests-singleton'
import type { ClientReferenceManifest } from '../../build/webpack/plugins/flight-manifest-plugin'
describe('server module map', () => {
const actionId = '00' + 'a'.repeat(40)
const missingActionId = '00' + 'b'.repeat(40)
beforeAll(() => {
setManifestsSingleton({
page: '/test',
clientReferenceManifest: {} as ClientReferenceManifest,
serverActionsManifest: {
encryptionKey: '',
node: {
[actionId]: {
workers: {
'app/test/page': {
moduleId: 'test-module',
async: false,
},
},
},
},
edge: {},
},
})
})
it('resolves valid server reference IDs', () => {
expect(getServerModuleMap()[actionId]).toEqual({
id: 'test-module',
name: actionId,
chunks: [],
async: false,
})
})
it('rejects plausible server reference IDs that are missing', () => {
expect(() => getServerModuleMap()[missingActionId]).toThrow(
`Failed to find Server Action "${missingActionId}".`
)
})
it.each([actionId.slice(1), actionId + 'a'])(
'rejects server reference IDs with an invalid length',
(invalidActionId) => {
expect(() => getServerModuleMap()[invalidActionId]).toThrow(
'The Server Reference ID did not match the expected format.'
)
}
)
it.each(['toJSON', '_debugInfo', '@@iterator'])(
'does not treat framework reflection probe "%s" as a server reference ID',
(prop) => {
expect(getServerModuleMap()[prop]).toBeUndefined()
}
)
it('does not treat symbol probes as server reference IDs', () => {
expect(Reflect.get(getServerModuleMap(), Symbol.iterator)).toBeUndefined()
})
})
describe('parseHostHeader', () => { describe('parseHostHeader', () => {
it('should return correct host', () => { it('should return correct host', () => {
......
...@@ -56,10 +56,15 @@ import { ...@@ -56,10 +56,15 @@ import {
type ServerModuleMap, type ServerModuleMap,
getServerActionsManifest, getServerActionsManifest,
getServerModuleMap, getServerModuleMap,
getActionNotFoundError,
getInvalidServerReferenceIdError,
} from './manifests-singleton' } from './manifests-singleton'
import { isNodeNextRequest, isWebNextRequest } from '../base-http/helpers' import { isNodeNextRequest, isWebNextRequest } from '../base-http/helpers'
import { normalizeFilePath } from './segment-explorer-path' import { normalizeFilePath } from './segment-explorer-path'
import { extractInfoFromServerReferenceId } from '../../shared/lib/server-reference-info' import {
extractInfoFromServerReferenceId,
mightBeServerReferenceId,
} from '../../shared/lib/server-reference-info'
import type { ServerActionLogInfo } from '../dev/server-action-logger' import type { ServerActionLogInfo } from '../dev/server-action-logger'
import { RedirectStatusCode } from '../../client/components/redirect-status-code' import { RedirectStatusCode } from '../../client/components/redirect-status-code'
import { synchronizeMutableCookies } from '../async-storage/request-store' import { synchronizeMutableCookies } from '../async-storage/request-store'
...@@ -608,7 +613,11 @@ export async function handleAction({ ...@@ -608,7 +613,11 @@ export async function handleAction({
// If the app has no server actions at all, we can 404 early. // If the app has no server actions at all, we can 404 early.
if (!hasServerActions()) { if (!hasServerActions()) {
return handleUnrecognizedFetchAction(getActionNotFoundError(actionId)) const error =
actionId !== null && !mightBeServerReferenceId(actionId)
? getInvalidServerReferenceIdError(actionId)
: getActionNotFoundError(actionId)
return handleUnrecognizedFetchAction(error)
} }
if (workStore.isStaticGeneration) { if (workStore.isStaticGeneration) {
...@@ -1369,25 +1378,27 @@ function getActionModIdOrError( ...@@ -1369,25 +1378,27 @@ function getActionModIdOrError(
throw new InvariantError("Missing 'next-action' header.") throw new InvariantError("Missing 'next-action' header.")
} }
const actionModId = serverModuleMap[actionId]?.id const entry = serverModuleMap[actionId]
if (!actionModId) { if (entry == null) {
throw getActionNotFoundError(actionId) // The proxy throws for malformed IDs and IDs that are missing from the
// manifest. It only returns undefined when the ID collides with a
// well-known property name (e.g. `toString`) that the proxy excludes from
// server reference validation so that framework reflection probes don't
// throw. Next.js never produces such an ID, so this is almost certainly a
// probe with a known-bad ID. Repeat the manifest's throw logic here so the
// caller gets a diagnosable error instead of a `TypeError`.
throw mightBeServerReferenceId(actionId)
? getActionNotFoundError(actionId)
: getInvalidServerReferenceIdError(actionId)
} }
return actionModId return entry.id
}
function getActionNotFoundError(actionId: string | null): Error {
return new Error(
`Failed to find Server Action${actionId ? ` "${actionId}"` : ''}. This request might be from an older or newer deployment.\nRead more: https://nextjs.org/docs/messages/failed-to-find-server-action`
)
} }
const $ACTION_ = '$ACTION_' const $ACTION_ = '$ACTION_'
const $ACTION_REF_ = '$ACTION_REF_' const $ACTION_REF_ = '$ACTION_REF_'
const $ACTION_ID_ = '$ACTION_ID_' const $ACTION_ID_ = '$ACTION_ID_'
const ACTION_ID_EXPECTED_LENGTH = 42
/** /**
* This function mirrors logic inside React's decodeAction and should be kept in sync with that. * This function mirrors logic inside React's decodeAction and should be kept in sync with that.
...@@ -1446,14 +1457,14 @@ function isInvalidStringActionDescriptor( ...@@ -1446,14 +1457,14 @@ function isInvalidStringActionDescriptor(
} }
const from = ACTION_DESCRIPTOR_ID_PREFIX.length const from = ACTION_DESCRIPTOR_ID_PREFIX.length
const to = from + ACTION_ID_EXPECTED_LENGTH const to = actionDescriptor.indexOf('"', from)
if (to === -1) {
return true
}
// We expect actionDescriptor to be '{"id":"<actionId>",...}' // We expect actionDescriptor to be '{"id":"<actionId>",...}'
const actionId = actionDescriptor.slice(from, to) const actionId = actionDescriptor.slice(from, to)
if ( if (!mightBeServerReferenceId(actionId)) {
actionId.length !== ACTION_ID_EXPECTED_LENGTH ||
actionDescriptor[to] !== '"'
) {
return true return true
} }
...@@ -1473,15 +1484,13 @@ function isInvalidActionIdFieldName( ...@@ -1473,15 +1484,13 @@ function isInvalidActionIdFieldName(
// The field name must always start with $ACTION_ID_ but since it is // The field name must always start with $ACTION_ID_ but since it is
// the id is extracted from the key of the field we have already validated // the id is extracted from the key of the field we have already validated
// this before entering this function // this before entering this function
if ( const actionId = actionIdFieldName.slice($ACTION_ID_.length)
actionIdFieldName.length !== if (!mightBeServerReferenceId(actionId)) {
$ACTION_ID_.length + ACTION_ID_EXPECTED_LENGTH
) {
// this field name has too few or too many characters // this field name has too few or too many characters
// or it is otherwise in the wrong format
return true return true
} }
const actionId = actionIdFieldName.slice($ACTION_ID_.length)
const entry = serverModuleMap[actionId] const entry = serverModuleMap[actionId]
if (entry == null) { if (entry == null) {
......
...@@ -5,6 +5,8 @@ import { InvariantError } from '../../shared/lib/invariant-error' ...@@ -5,6 +5,8 @@ import { InvariantError } from '../../shared/lib/invariant-error'
import { normalizeAppPath } from '../../shared/lib/router/utils/app-paths' import { normalizeAppPath } from '../../shared/lib/router/utils/app-paths'
import { pathHasPrefix } from '../../shared/lib/router/utils/path-has-prefix' import { pathHasPrefix } from '../../shared/lib/router/utils/path-has-prefix'
import { removePathPrefix } from '../../shared/lib/router/utils/remove-path-prefix' import { removePathPrefix } from '../../shared/lib/router/utils/remove-path-prefix'
import { mightBeServerReferenceId } from '../../shared/lib/server-reference-info'
import { wellKnownProperties } from '../../shared/lib/utils/reflect-utils'
import { workAsyncStorage } from './work-async-storage.external' import { workAsyncStorage } from './work-async-storage.external'
export interface ServerModuleMap { export interface ServerModuleMap {
...@@ -16,6 +18,37 @@ export interface ServerModuleMap { ...@@ -16,6 +18,37 @@ export interface ServerModuleMap {
} }
} }
export function getActionNotFoundError(actionId: string | null): Error {
return new Error(
`Failed to find Server Action${actionId ? ` "${actionId}"` : ''}. This request might be from an older or newer deployment.\nRead more: https://nextjs.org/docs/messages/failed-to-find-server-action`
)
}
export function getInvalidServerReferenceIdError(id: string): Error {
// `id` is arbitrary client-provided input. Unlike the not-found case, it has
// not passed the length gate and can reach this error via a malformed server
// reference in an action payload, so it may be of any length and contain
// control characters. `JSON.stringify` escapes newlines and quotes so it
// can't forge log lines, and truncating overly long ids prevents log
// flooding. Ids at or below the cap are logged in full so that we only add an
// ellipsis to ids that are meaningfully longer than the truncated length.
const encoded = JSON.stringify(
id.length > MAX_LOGGED_SERVER_REFERENCE_ID_LENGTH
? id.slice(0, TRUNCATED_SERVER_REFERENCE_ID_LENGTH) + '…'
: id
)
return new Error(
`The Server Reference ID did not match the expected format. Received ${encoded}.\nRead more: https://nextjs.org/docs/messages/failed-to-find-server-action`
)
}
// Ids at or below the cap are logged in full. Longer ids are truncated to the
// shorter length and marked with an ellipsis, so the cap leaves headroom over
// the truncated length rather than ellipsizing ids that are barely too long.
const MAX_LOGGED_SERVER_REFERENCE_ID_LENGTH = 100
const TRUNCATED_SERVER_REFERENCE_ID_LENGTH = 90
// This is a global singleton that is, among other things, also used to // This is a global singleton that is, among other things, also used to
// encode/decode bound args of server function closures. This can't be using a // encode/decode bound args of server function closures. This can't be using a
// AsyncLocalStorage as it might happen at the module level. // AsyncLocalStorage as it might happen at the module level.
...@@ -179,48 +212,57 @@ function createProxiedClientReferenceManifest( ...@@ -179,48 +212,57 @@ function createProxiedClientReferenceManifest(
* runtime, workers, etc. that React doesn't need to know. * runtime, workers, etc. that React doesn't need to know.
*/ */
function createServerModuleMap(): ServerModuleMap { function createServerModuleMap(): ServerModuleMap {
return new Proxy( return new Proxy(Object.create(null) as ServerModuleMap, {
{}, get: (target, id: string | symbol, receiver) => {
{ // React's debug serialization can probe the module map like a plain object.
get: (_, id: string) => { // These probes are not server reference lookups.
const workers = if (typeof id !== 'string') {
getServerActionsManifest()[ return Reflect.get(target, id, receiver)
process.env.NEXT_RUNTIME === 'edge' ? 'edge' : 'node' }
]?.[id]?.workers
if (!workers) { if (wellKnownProperties.has(id)) {
return undefined return Reflect.get(target, id, receiver)
} }
const workStore = workAsyncStorage.getStore() if (!mightBeServerReferenceId(id)) {
throw getInvalidServerReferenceIdError(id)
}
let workerEntry: const workers =
| { moduleId: string | number; async: boolean } getServerActionsManifest()[
| undefined process.env.NEXT_RUNTIME === 'edge' ? 'edge' : 'node'
]?.[id]?.workers
if (workStore) {
workerEntry = workers[normalizeWorkerPageName(workStore.page)]
} else {
// If there's no work store defined, we can assume that a server
// module map is needed during module evaluation, e.g. to create a
// server action using a higher-order function. Therefore it should be
// safe to return any entry from the manifest that matches the action
// ID. They all refer to the same module ID, which must also exist in
// the current page bundle. TODO: This is currently not guaranteed in
// Turbopack, and needs to be fixed.
workerEntry = Object.values(workers).at(0)
}
if (!workerEntry) { if (!workers) {
return undefined throw getActionNotFoundError(id)
} }
const { moduleId, async } = workerEntry const workStore = workAsyncStorage.getStore()
let workerEntry: { moduleId: string | number; async: boolean } | undefined
if (workStore) {
workerEntry = workers[normalizeWorkerPageName(workStore.page)]
} else {
// If there's no work store defined, we can assume that a server
// module map is needed during module evaluation, e.g. to create a
// server action using a higher-order function. Therefore it should be
// safe to return any entry from the manifest that matches the action
// ID. They all refer to the same module ID, which must also exist in
// the current page bundle. TODO: This is currently not guaranteed in
// Turbopack, and needs to be fixed.
workerEntry = Object.values(workers).at(0)
}
return { id: moduleId, name: id, chunks: [], async } if (!workerEntry) {
}, throw getActionNotFoundError(id)
} }
)
const { moduleId, async } = workerEntry
return { id: moduleId, name: id, chunks: [], async }
},
})
} }
/** /**
......
import { import {
SERVER_REFERENCE_ID_LENGTH,
type ServerReferenceInfo, type ServerReferenceInfo,
extractInfoFromServerReferenceId, extractInfoFromServerReferenceId,
mightBeServerReferenceId,
omitUnusedArgs, omitUnusedArgs,
} from './server-reference-info' } from './server-reference-info'
describe('mightBeServerReferenceId', () => {
test('should identify IDs with the expected length', () => {
expect(
mightBeServerReferenceId('a'.repeat(SERVER_REFERENCE_ID_LENGTH))
).toBe(true)
expect(
mightBeServerReferenceId('a'.repeat(SERVER_REFERENCE_ID_LENGTH - 1))
).toBe(false)
expect(
mightBeServerReferenceId('a'.repeat(SERVER_REFERENCE_ID_LENGTH + 1))
).toBe(false)
})
})
describe('extractInfoFromServerReferenceId', () => { describe('extractInfoFromServerReferenceId', () => {
test('should parse id with typeBit 0, no args used, no restArgs', () => { test('should parse id with typeBit 0, no args used, no restArgs', () => {
const id = '00' // 0b00000000 const id = '00' // 0b00000000
......
...@@ -4,6 +4,12 @@ export interface ServerReferenceInfo { ...@@ -4,6 +4,12 @@ export interface ServerReferenceInfo {
hasRestArgs: boolean hasRestArgs: boolean
} }
export const SERVER_REFERENCE_ID_LENGTH = 42
export function mightBeServerReferenceId(id: string): boolean {
return id.length === SERVER_REFERENCE_ID_LENGTH
}
/** /**
* Extracts info about the server reference for the given server reference ID by * Extracts info about the server reference for the given server reference ID by
* parsing the first byte of the hex-encoded ID. * parsing the first byte of the hex-encoded ID.
......
...@@ -4,6 +4,8 @@ import { retry } from 'next-test-utils' ...@@ -4,6 +4,8 @@ import { retry } from 'next-test-utils'
import { outdent } from 'outdent' import { outdent } from 'outdent'
describe('unrecognized server actions', () => { describe('unrecognized server actions', () => {
const unrecognizedActionId = '0'.repeat(42)
const { next, isNextDeploy, isNextDev } = nextTestSetup({ const { next, isNextDeploy, isNextDev } = nextTestSetup({
files: __dirname, files: __dirname,
}) })
...@@ -36,50 +38,78 @@ describe('unrecognized server actions', () => { ...@@ -36,50 +38,78 @@ describe('unrecognized server actions', () => {
expect(res.status).toBe(404) expect(res.status).toBe(404)
}) })
it.each([ describe.each([
{ {
// encodeReply encodes simple args as plaintext. idType: 'malformed',
name: 'plaintext', actionId: '123',
request: { expectedError: outdent`
contentType: 'text/plain;charset=UTF-8', The Server Reference ID did not match the expected format. Received "123".
body: '{}', Read more: https://nextjs.org/docs/messages/failed-to-find-server-action
}, `,
}, },
{ {
// encodeReply encodes complex args as FormData. idType: 'plausible but missing',
// this body is empty and wouldn't match how react encodes an action, but it should be rejected actionId: unrecognizedActionId,
// before we even get to parsing the FormData, so it doesn't really matter. expectedError: outdent`
name: 'form-data/multipart', Failed to find Server Action "${unrecognizedActionId}". This request might be from an older or newer deployment.
request: { Read more: https://nextjs.org/docs/messages/failed-to-find-server-action
body: new FormData(), `,
},
}, },
])( {
'should 404 when POSTing a server action with an unrecognized id to a nonexistent page: $name', // A well-known property name is excluded from server reference
async ({ request: { contentType, body } }) => { // validation in the module map (so framework reflection probes don't
const res = await next.fetch('/non-existent-route', { // throw), which means the lookup returns undefined rather than throwing.
method: 'POST', // We should still surface a diagnosable error instead of a TypeError.
headers: { idType: 'well-known property name',
'next-action': '123', actionId: 'toString',
...(contentType ? { 'content-type': contentType } : undefined), expectedError: outdent`
The Server Reference ID did not match the expected format. Received "toString".
Read more: https://nextjs.org/docs/messages/failed-to-find-server-action
`,
},
])('with a $idType id', ({ actionId, expectedError }) => {
it.each([
{
// encodeReply encodes simple args as plaintext.
name: 'plaintext',
request: {
contentType: 'text/plain;charset=UTF-8',
body: '{}',
}, },
// @ts-expect-error: node-fetch types don't seem to like FormData },
body, {
}) // encodeReply encodes complex args as FormData.
// this body is empty and wouldn't match how react encodes an action, but it should be rejected
// before we even get to parsing the FormData, so it doesn't really matter.
name: 'form-data/multipart',
request: {
body: new FormData(),
},
},
])(
'should 404 when POSTing a server action to a nonexistent page: $name',
async ({ request: { contentType, body } }) => {
const res = await next.fetch('/non-existent-route', {
method: 'POST',
headers: {
'next-action': actionId,
...(contentType ? { 'content-type': contentType } : undefined),
},
// @ts-expect-error: node-fetch types don't seem to like FormData
body,
})
expect(res.status).toBe(404) expect(res.status).toBe(404)
const cliOutput = getLogs() const cliOutput = getLogs()
expect(cliOutput).not.toContain('TypeError') expect(cliOutput).not.toContain('TypeError')
expect(cliOutput).not.toContain( expect(cliOutput).not.toContain(
'Missing `origin` header from a forwarded Server Actions request' 'Missing `origin` header from a forwarded Server Actions request'
) )
expect(cliOutput).toInclude(outdent` expect(cliOutput).toInclude(expectedError)
Failed to find Server Action "123". This request might be from an older or newer deployment. }
Read more: https://nextjs.org/docs/messages/failed-to-find-server-action )
`) })
}
)
} }
it('should error when POSTing a urlencoded action to a nonexistent page', async () => { it('should error when POSTing a urlencoded action to a nonexistent page', async () => {
...@@ -146,7 +176,7 @@ describe('unrecognized server actions', () => { ...@@ -146,7 +176,7 @@ describe('unrecognized server actions', () => {
if (!isNextDeploy) { if (!isNextDeploy) {
await retry(async () => await retry(async () =>
expect(getLogs()).toInclude(outdent` expect(getLogs()).toInclude(outdent`
Failed to find Server Action "decafc0ffeebad01". This request might be from an older or newer deployment. Failed to find Server Action "${unrecognizedActionId}". This request might be from an older or newer deployment.
Read more: https://nextjs.org/docs/messages/failed-to-find-server-action Read more: https://nextjs.org/docs/messages/failed-to-find-server-action
`) `)
) )
...@@ -182,7 +212,7 @@ describe('unrecognized server actions', () => { ...@@ -182,7 +212,7 @@ describe('unrecognized server actions', () => {
if (!isNextDeploy) { if (!isNextDeploy) {
await retry(async () => await retry(async () =>
expect(getLogs()).toInclude( expect(getLogs()).toInclude(
`Error: Failed to find Server Action. This request might be from an older or newer deployment` `Error: Failed to find Server Action "${unrecognizedActionId}". This request might be from an older or newer deployment`
) )
) )
} }
......
...@@ -8,7 +8,7 @@ const action = async (...args: any[]) => { ...@@ -8,7 +8,7 @@ const action = async (...args: any[]) => {
} }
// simulate client-side version skew by changing the action ID to something the server won't recognize // simulate client-side version skew by changing the action ID to something the server won't recognize
setServerActionId(action, 'decafc0ffeebad01') setServerActionId(action, '0'.repeat(42))
export default function Page() { export default function Page() {
return ( return (
......
import { nextTestSetup } from 'e2e-utils' import { nextTestSetup } from 'e2e-utils'
describe('app-dir - no server actions', () => { describe('app-dir - no server actions', () => {
const missingActionId = '0'.repeat(42)
const { next, isNextDeploy } = nextTestSetup({ const { next, isNextDeploy } = nextTestSetup({
files: __dirname, files: __dirname,
}) })
it('should error when triggering a fetch action on an app with no server actions', async () => { it.each([
const res = await next.fetch('/', { {
method: 'POST', description: 'a malformed id',
headers: { actionId: 'abc123',
'Next-Action': 'abc123', expectedError:
}, 'The Server Reference ID did not match the expected format. Received "abc123".\nRead more: https://nextjs.org/docs/messages/failed-to-find-server-action',
}) },
{
description: 'a plausible but missing id',
actionId: missingActionId,
expectedError: `Failed to find Server Action "${missingActionId}". This request might be from an older or newer deployment.\nRead more: https://nextjs.org/docs/messages/failed-to-find-server-action`,
},
])(
'should error when triggering a fetch action with $description on an app with no server actions',
async ({ actionId, expectedError }) => {
const res = await next.fetch('/', {
method: 'POST',
headers: {
'Next-Action': actionId,
},
})
expect(res.status).toBe(404) expect(res.status).toBe(404)
expect(res.headers.get('x-nextjs-action-not-found')).toBe('1') expect(res.headers.get('x-nextjs-action-not-found')).toBe('1')
// Runtime logs and custom headers are not forwarded to the client when deployed. // Runtime logs and custom headers are not forwarded to the client when deployed.
if (!isNextDeploy) { if (!isNextDeploy) {
expect(next.cliOutput).toContain( expect(next.cliOutput).toContain(expectedError)
'Failed to find Server Action "abc123". This request might be from an older or newer deployment.\nRead more: https://nextjs.org/docs/messages/failed-to-find-server-action' }
)
} }
}) )
it('should error when triggering an MPA action on an app with no server actions', async () => { it('should error when triggering an MPA action on an app with no server actions', async () => {
const formData = new FormData() const formData = new FormData()
......
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