Unverified Commit d43d57af authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

Remove urlencoded action handling & harden action ID checks (#87082)

- Removes support for urlencoded actions
- Errors when a server action request is received in apps that have no
server actions
- validate action ID in MPA actions before proceeding

---------
Co-authored-by: 's avatarJosh Story <story@hey.com>
parent 8479550b
...@@ -970,5 +970,8 @@ ...@@ -970,5 +970,8 @@
"969": "Invariant: nextConfig couldn't be loaded", "969": "Invariant: nextConfig couldn't be loaded",
"970": "process.env.NEXT_DEPLOYMENT_ID is missing but runtimeServerDeploymentId is enabled", "970": "process.env.NEXT_DEPLOYMENT_ID is missing but runtimeServerDeploymentId is enabled",
"971": "The NEXT_DEPLOYMENT_ID environment variable value \"%s\" does not match the provided deploymentId \"%s\" in the config.", "971": "The NEXT_DEPLOYMENT_ID environment variable value \"%s\" does not match the provided deploymentId \"%s\" in the config.",
"972": "Failed to resolve pattern \"%s\": %s" "972": "Failed to resolve pattern \"%s\": %s",
"973": "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",
"974": "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",
"975": "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"
} }
...@@ -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