Actions return 500 on a malformed JSON body or an undecodable action name
Astro Info
Astro v7.3.5
Vite v8.3.1
Node v24.15.0
System Linux (x64)
Package Manager npm
Output static
Adapter none
Integrations noneIf this issue only occurs in one browser, which browser is a problem?
No response
Describe the Bug
An action request with a malformed JSON body, or with an action name that can't be URL-decoded, returns a 500 instead of an action error. This happens on both the RPC route (/_actions/<name>) and the form action path (?_action=<name> on a page).
We noticed it in production from scanners, which post Content-Type: application/json with a body like a=b to form actions. Each request logs SyntaxError: Unexpected token 'a', "a=b" is not valid JSON and counts as a server error. A request to ?_action=%25 fails the same way with URIError: URI malformed.
Both errors escape from the action handler in getActionContext:
- The
parseRequestBodycatch atruntime/server.ts#L204-L212turns onlyActionErrorandTypeErrorinto an action error.JSON.parseandRequest.json()throw aSyntaxErroron a malformed body, so it gets rethrown. - The
getActioncatch atruntime/server.ts#L185-L198turns onlyActionNotFoundErrorintoNOT_FOUND. ThedecodeURIComponentcall atload.ts#L32throws aURIErrorfor a lone%, so that gets rethrown too.
A JSON body skips checkOrigin, so none of these requests need an Origin header.
For comparison, valid JSON sent to the same accept: 'form' action returns 415, and an unknown action name returns 404. Only the malformed cases leak out as a 500.
What's the expected result?
An action error, not a 500. For example, BAD_REQUEST for a malformed JSON body and NOT_FOUND for an action name that can't be decoded, the same answer an unknown name already gets.
I have a PR up with the fix and unit tests: #18141
Link to Minimal Reproducible Example
https://github.com/skezo/astro-actions-500
Participation
- I am willing to submit a pull request for this issue.
Source: withastro/astro