Commit fa858489 authored by Zack Tanner's avatar Zack Tanner

Backport Next.js changes to v16.0.9 (#20)

* remove urlencoded server action handling (#14)

* remove urlencoded server action handling

* urlencoded POSTs should still flow through action handler

* bailout early for urlencoded

* tweak handling to 404 in feetch action case

* error when there are no server actions (#15)

* remove custom error

* remove dev gate

* error when there are no server actions

* gate against dev

* Check whether action ids are valid before parsing (#16)

* Check whether action ids are valid before parsing

In an effort to narrow access to parsing methods Next.js now prequalifies MPA form parsing by ensuring there are only valid action IDs as part of the submission. This is not meant to be a strong line of defense against malicious payloads since action IDs are not private and are for many apps relatively easy to acquire. It does however provide some limited narrowing of code paths so that action decoding only happens for plausible actions

Additionally all other branches that use next-action header now consistently check the header before proceeding with parsing. This is also a perf improvement.

* fix test assertion

---------
Co-authored-by: 's avatarZack Tanner <1939140+ztanner@users.noreply.github.com>

---------
Co-authored-by: 's avatarJosh Story <story@hey.com>
parent 817ee56d
...@@ -907,5 +907,8 @@ ...@@ -907,5 +907,8 @@
"906": "Bindings not loaded yet, but they are being loaded, did you forget to await?", "906": "Bindings not loaded yet, but they are being loaded, did you forget to await?",
"907": "bindings not loaded yet. Either call `loadBindings` to wait for them to be available or ensure that `installBindings` has already been called.", "907": "bindings not loaded yet. Either call `loadBindings` to wait for them to be available or ensure that `installBindings` has already been called.",
"908": "Invalid flags should be run as node detached-flush dev ./path-to/project [eventsFile]", "908": "Invalid flags should be run as node detached-flush dev ./path-to/project [eventsFile]",
"909": "Failed to load SWC binary for %s/%s, see more info here: https://nextjs.org/docs/messages/failed-loading-swc" "909": "Failed to load SWC binary for %s/%s, see more info here: https://nextjs.org/docs/messages/failed-loading-swc",
"910": "Server Actions are not enabled for this application. This request might be from an older or newer deployment.\nRead more: https://nextjs.org/docs/messages/failed-to-find-server-action",
"911": "Failed to find Server Action. This request might be from an older or newer deployment.\\nRead more: https://nextjs.org/docs/messages/failed-to-find-server-action",
"912": "Failed to find Server Action%s. This request might be from an older or newer deployment.\\nRead more: https://nextjs.org/docs/messages/failed-to-find-server-action"
} }
...@@ -23,6 +23,9 @@ export function getServerActionRequestMetadata( ...@@ -23,6 +23,9 @@ export function getServerActionRequestMetadata(
contentType = req.headers['content-type'] ?? null contentType = req.headers['content-type'] ?? null
} }
// We don't actually support URL encoded actions, and the action handler will bail out if it sees one.
// But we still want it to flow through to the action handler, to prevent changes in behavior when a regular
// page component tries to handle a POST.
const isURLEncodedAction = Boolean( const isURLEncodedAction = Boolean(
req.method === 'POST' && contentType === 'application/x-www-form-urlencoded' req.method === 'POST' && contentType === 'application/x-www-form-urlencoded'
) )
......
...@@ -54,15 +54,6 @@ describe('unrecognized server actions', () => { ...@@ -54,15 +54,6 @@ describe('unrecognized server actions', () => {
body: new FormData(), body: new FormData(),
}, },
}, },
{
// we never use urlencoded actions, but we currently have codepaths for it in `handleAction`,
// so might as well test them.
name: 'urlencoded',
request: {
contentType: 'application/x-www-form-urlencoded',
body: 'foo=bar',
},
},
])( ])(
'should 404 when POSTing a server action with an unrecognized id to a nonexistent page: $name', 'should 404 when POSTing a server action with an unrecognized id to a nonexistent page: $name',
async ({ request: { contentType, body } }) => { async ({ request: { contentType, body } }) => {
...@@ -91,6 +82,19 @@ describe('unrecognized server actions', () => { ...@@ -91,6 +82,19 @@ describe('unrecognized server actions', () => {
) )
} }
it('should 404 when POSTing a urlencoded action to a nonexistent page', async () => {
const res = await next.fetch('/non-existent-route', {
method: 'POST',
headers: {
'next-action': '123',
'content-type': 'application/x-www-form-urlencoded',
},
body: 'foo=bar',
})
expect(res.status).toBe(404)
})
describe.each(['nodejs', 'edge'])( describe.each(['nodejs', 'edge'])(
'should error and log a warning when submitting a server action with an unrecognized ID - %s', 'should error and log a warning when submitting a server action with an unrecognized ID - %s',
(runtime) => { (runtime) => {
...@@ -178,10 +182,9 @@ describe('unrecognized server actions', () => { ...@@ -178,10 +182,9 @@ describe('unrecognized server actions', () => {
} }
if (!isNextDeploy) { if (!isNextDeploy) {
// FIXME: For an MPA action, the logs currently show the error thrown by React instead of our custom message with a link to a docs page.
await retry(async () => await retry(async () =>
expect(getLogs()).toInclude( expect(getLogs()).toInclude(
`Error: Could not find the module "decafc0ffeebad01" in the React Server Manifest. This is probably a bug in the React Server Components bundler` `Error: Failed to find Server Action. This request might be from an older or newer deployment`
) )
) )
} }
......
export default function Layout({ children }: { children: React.ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export default function Page() {
return <p>Hello World</p>
}
import { nextTestSetup } from 'e2e-utils'
describe('app-dir - no server actions', () => {
const { next } = nextTestSetup({
files: __dirname,
})
it('should 404 when triggering a fetch action on an app with no server actions', async () => {
const res = await next.fetch('/', {
method: 'POST',
headers: {
'Next-Action': 'abc123',
},
})
expect(res.status).toBe(404)
expect(res.headers.get('x-nextjs-action-not-found')).toBe('1')
expect(next.cliOutput).toContain(
'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 404 when triggering an MPA action on an app with no server actions', async () => {
const formData = new FormData()
formData.append('test', 'value')
const res = await next.fetch('/', {
method: 'POST',
headers: {
'Content-Type': 'multipart/form-data',
},
// @ts-expect-error: node-fetch types don't seem to like FormData
body: formData,
})
expect(res.status).toBe(404)
expect(res.headers.get('x-nextjs-action-not-found')).toBe('1')
expect(next.cliOutput).toContain(
'Failed to find Server Action. This request might be from an older or newer deployment.\nRead more: https://nextjs.org/docs/messages/failed-to-find-server-action'
)
})
})
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