Commit 39e2ed99 authored by Zack Tanner's avatar Zack Tanner

Backport Next.js changes to v15.2.7 (#26)

* 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>

* fix test

---------
Co-authored-by: 's avatarJosh Story <story@hey.com>
parent 25ba2c26
...@@ -659,5 +659,7 @@ ...@@ -659,5 +659,7 @@
"658": "Pass `Infinity` instead of `false` if you want to cache on the server forever without checking with the origin.", "658": "Pass `Infinity` instead of `false` if you want to cache on the server forever without checking with the origin.",
"659": "SSG should not return an image cache value", "659": "SSG should not return an image cache value",
"660": "Rspack support is only available in Next.js canary.", "660": "Rspack support is only available in Next.js canary.",
"661": "Build failed because of %s errors" "661": "Build failed because of %s errors",
"662": "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",
"663": "Failed to find Server Action%s. This request might be from an older or newer deployment. %s\\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'
) )
......
...@@ -801,7 +801,6 @@ describe('app-dir action handling', () => { ...@@ -801,7 +801,6 @@ describe('app-dir action handling', () => {
await next.fetch('/server', { await next.fetch('/server', {
method: 'POST', method: 'POST',
headers: { headers: {
'content-type': 'application/x-www-form-urlencoded',
'next-action': 'abc123', 'next-action': 'abc123',
}, },
body: 'foo=bar', body: 'foo=bar',
......
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 log when triggering a fetch action on an app with no server actions', async () => {
await next.fetch('/', {
method: 'POST',
headers: {
'Next-Action': 'abc123',
},
})
expect(next.cliOutput).toContain(
'Failed to find Server Action "abc123". This request might be from an older or newer deployment.'
)
})
it('should log when triggering an MPA action on an app with no server actions', async () => {
const formData = new FormData()
formData.append('test', 'value')
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(next.cliOutput).toContain(
'Failed to find Server Action. This request might be from an older or newer deployment.'
)
})
})
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