Skip to content

fix(server-actions): catch TypeError for malformed Origin header to prevent DoS#92747

Open
sleitor wants to merge 1 commit intovercel:canaryfrom
sleitor:fix-92703
Open

fix(server-actions): catch TypeError for malformed Origin header to prevent DoS#92747
sleitor wants to merge 1 commit intovercel:canaryfrom
sleitor:fix-92703

Conversation

@sleitor
Copy link
Copy Markdown
Contributor

@sleitor sleitor commented Apr 13, 2026

Fixes #92703

Wrap new URL(originHeader).host in try/catch so a malformed Origin header (e.g. http://, ftp://, not-a-url) does not throw an unhandled TypeError and cause a 500 response.

Problem

Origin: http://

new URL('http://') throws TypeError: Invalid URL, which propagates as an unhandled exception and returns a 500.

Fix

Wrap in try/catch and return a sentinel value '__invalid_origin__' on parse failure. The sentinel is guaranteed to fail the subsequent host comparison, so CSRF protection is preserved — malformed origins are treated as cross-origin requests, not bypasses.

Behavior

Origin header Before After
http://valid.com ✅ 200 ✅ 200
null ✅ handled ✅ handled
http:// ❌ 500 TypeError ✅ CSRF-rejected
not-a-url ❌ 500 TypeError ✅ CSRF-rejected

…revent DoS

Fixes vercel#92703

Wrap `new URL(originHeader).host` in try/catch so a malformed Origin header
(e.g. 'http://') does not throw an unhandled TypeError and cause a 500 response.
Return a sentinel '__invalid_origin__' that fails host comparison, preserving CSRF protection.
@nextjs-bot
Copy link
Copy Markdown
Collaborator

Allow CI Workflow Run

  • approve CI run for commit: 61e736c

Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer

Comment on lines +627 to +636
: (() => {
try {
return new URL(originHeader).host
} catch {
// A malformed Origin header (e.g. 'http://') must NOT fall through
// to undefined/null — that would skip CSRF validation entirely.
// Return a sentinel that is guaranteed to fail host comparison.
return '__invalid_origin__'
}
})()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code should be refactored to avoid allocating an IIFE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Malformed Origin header in Next.js Server Actions returns HTTP 500 instead of 400

3 participants