Commit eeb6db33 authored by Zack Tanner's avatar Zack Tanner

Backport Next.js changes to v15.3.7 (#25)

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

* test fix

---------
Co-authored-by: 's avatarJosh Story <story@hey.com>
parent 2f026aae
...@@ -670,5 +670,7 @@ ...@@ -670,5 +670,7 @@
"669": "Invariant: --turbopack is set but the build used Webpack", "669": "Invariant: --turbopack is set but the build used Webpack",
"670": "Invariant: --turbopack is not set but the build used Turbopack. Add --turbopack to \"next start\".", "670": "Invariant: --turbopack is not set but the build used Turbopack. Add --turbopack to \"next start\".",
"671": "Specified images.remotePatterns must have protocol \"http\" or \"https\" received \"%s\".", "671": "Specified images.remotePatterns must have protocol \"http\" or \"https\" received \"%s\".",
"672": "Expected `telemetry` to be set in globals" "672": "Expected `telemetry` to be set in globals",
"673": "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",
"674": "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'
) )
......
...@@ -802,10 +802,11 @@ describe('app-dir action handling', () => { ...@@ -802,10 +802,11 @@ 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', 'content-type': 'multipart/form-data',
'next-action': 'abc123', 'next-action': 'abc123',
}, },
body: 'foo=bar', // @ts-ignore
body: new FormData().append('foo', 'bar'),
}) })
await retry(async () => await retry(async () =>
......
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.\nRead more: https://nextjs.org/docs/messages/failed-to-find-server-action'
)
})
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.\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