| |
| |
| |
| |
| @@ -1,9 +1,10 @@ |
| import { nextTestSetup } from 'e2e-utils' |
| import { createRequestTracker } from 'e2e-utils/request-tracker' |
| import { retry } from 'next-test-utils' |
| -import { outdent } from 'outdent' |
| |
| describe('unrecognized server actions', () => { |
| + const unrecognizedActionId = '0'.repeat(42) |
| + |
| const { next, isNextDeploy, isNextDev } = nextTestSetup({ |
| files: __dirname, |
| }) |
| @@ -36,50 +37,72 @@ describe('unrecognized server actions', () => { |
| expect(res.status).toBe(404) |
| }) |
| |
| - it.each([ |
| + describe.each([ |
| { |
| - // encodeReply encodes simple args as plaintext. |
| - name: 'plaintext', |
| - request: { |
| - contentType: 'text/plain;charset=UTF-8', |
| - body: '{}', |
| - }, |
| + idType: 'malformed', |
| + actionId: '123', |
| + expectedClassification: 'invalid', |
| }, |
| { |
| - // 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(), |
| - }, |
| + idType: 'plausible but missing', |
| + actionId: unrecognizedActionId, |
| + expectedClassification: 'missing', |
| }, |
| - ])( |
| - 'should 404 when POSTing a server action with an unrecognized id to a nonexistent page: $name', |
| - async ({ request: { contentType, body } }) => { |
| - const res = await next.fetch('/non-existent-route', { |
| - method: 'POST', |
| - headers: { |
| - 'next-action': '123', |
| - ...(contentType ? { 'content-type': contentType } : undefined), |
| + { |
| + // Object reflection names must be handled as malformed client input, |
| + // rather than being resolved through Object.prototype. |
| + idType: 'object reflection property name', |
| + actionId: 'toString', |
| + expectedClassification: 'invalid', |
| + }, |
| + ])('with a $idType id', ({ actionId, expectedClassification }) => { |
| + 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, |
| - }) |
| + }, |
| + { |
| + // Complex args are encoded as FormData. Validation should happen |
| + // before decoding this intentionally empty payload. |
| + name: 'form-data/multipart', |
| + request: { |
| + body: new FormData(), |
| + }, |
| + }, |
| + ])( |
| + 'should reject a server action request 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() |
| - expect(cliOutput).not.toContain('TypeError') |
| - expect(cliOutput).not.toContain( |
| - 'Missing `origin` header from a forwarded Server Actions request' |
| - ) |
| - expect(cliOutput).toInclude(outdent` |
| - 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 |
| - `) |
| - } |
| - ) |
| + const cliOutput = getLogs() |
| + expect(cliOutput).not.toContain('TypeError') |
| + if (expectedClassification === 'invalid') { |
| + expect(cliOutput).toMatch(/server reference id/i) |
| + expect(cliOutput).toMatch(/invalid|malformed|format/i) |
| + } else { |
| + expect(cliOutput).toMatch(/server action/i) |
| + expect(cliOutput).toMatch(/not found|missing|failed to find/i) |
| + expect(cliOutput).not.toMatch( |
| + /server reference id.{0,100}(invalid|malformed|format)/i |
| + ) |
| + } |
| + } |
| + ) |
| + }) |
| } |
| |
| it('should error when POSTing a urlencoded action to a nonexistent page', async () => { |
| @@ -144,12 +167,14 @@ describe('unrecognized server actions', () => { |
| ) |
| |
| if (!isNextDeploy) { |
| - await retry(async () => |
| - expect(getLogs()).toInclude(outdent` |
| - Failed to find Server Action "decafc0ffeebad01". This request might be from an older or newer deployment. |
| - Read more: https://nextjs.org/docs/messages/failed-to-find-server-action |
| - `) |
| - ) |
| + await retry(async () => { |
| + const output = getLogs() |
| + expect(output).toMatch(/server action/i) |
| + expect(output).toMatch(/not found|missing|failed to find/i) |
| + expect(output).not.toMatch( |
| + /server reference id.{0,100}(invalid|malformed|format)/i |
| + ) |
| + }) |
| } |
| } else { |
| // An MPA action, sent without JS. |
| @@ -180,11 +205,14 @@ describe('unrecognized server actions', () => { |
| } |
| |
| if (!isNextDeploy) { |
| - await retry(async () => |
| - expect(getLogs()).toInclude( |
| - `Error: Failed to find Server Action. This request might be from an older or newer deployment` |
| + await retry(async () => { |
| + const output = getLogs() |
| + expect(output).toMatch(/server action/i) |
| + expect(output).toMatch(/not found|missing|failed to find/i) |
| + expect(output).not.toMatch( |
| + /server reference id.{0,100}(invalid|malformed|format)/i |
| ) |
| - ) |
| + }) |
| } |
| } |
| } |
| |
| |
| |
| |
| @@ -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 |
| -setServerActionId(action, 'decafc0ffeebad01') |
| +setServerActionId(action, '0'.repeat(42)) |
| |
| export default function Page() { |
| return ( |
| |
| |
| |
| |
| @@ -1,28 +1,53 @@ |
| import { nextTestSetup } from 'e2e-utils' |
| |
| describe('app-dir - no server actions', () => { |
| + const missingActionId = '0'.repeat(42) |
| + |
| const { next, isNextDeploy } = nextTestSetup({ |
| files: __dirname, |
| }) |
| |
| - it('should error when triggering a fetch action on an app with no server actions', async () => { |
| - const res = await next.fetch('/', { |
| - method: 'POST', |
| - headers: { |
| - 'Next-Action': 'abc123', |
| - }, |
| - }) |
| + it.each([ |
| + { |
| + description: 'a malformed id', |
| + actionId: 'abc123', |
| + expectedClassification: 'invalid', |
| + }, |
| + { |
| + description: 'a plausible but missing id', |
| + actionId: missingActionId, |
| + expectedClassification: 'missing', |
| + }, |
| + ])( |
| + 'should reject a fetch action with $description on an app with no server actions', |
| + async ({ actionId, expectedClassification }) => { |
| + const outputPosition = next.cliOutput.length |
| + const res = await next.fetch('/', { |
| + method: 'POST', |
| + headers: { |
| + 'Next-Action': actionId, |
| + }, |
| + }) |
| |
| - expect(res.status).toBe(404) |
| - expect(res.headers.get('x-nextjs-action-not-found')).toBe('1') |
| + expect(res.status).toBe(404) |
| + expect(res.headers.get('x-nextjs-action-not-found')).toBe('1') |
| |
| - // Runtime logs and custom headers are not forwarded to the client when deployed. |
| - if (!isNextDeploy) { |
| - 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' |
| - ) |
| + // Runtime logs are not available when deployed. |
| + if (!isNextDeploy) { |
| + const output = next.cliOutput.slice(outputPosition) |
| + if (expectedClassification === 'invalid') { |
| + expect(output).toMatch(/server reference id/i) |
| + expect(output).toMatch(/invalid|malformed|format/i) |
| + } else { |
| + expect(output).toMatch(/server action/i) |
| + expect(output).toMatch(/not found|missing|failed to find/i) |
| + expect(output).not.toMatch( |
| + /server reference id.{0,100}(invalid|malformed|format)/i |
| + ) |
| + } |
| + } |
| } |
| - }) |
| + ) |
| |
| it('should error when triggering an MPA action on an app with no server actions', async () => { |
| const formData = new FormData() |
|
|