The vulnerability, identified as GHSA-w37m-7fhw-fmv9, allows for the exposure of Server Actions source code in Next.js. The root cause lies in the insufficient validation of incoming HTTP requests for server actions, particularly those that are URL-encoded. The primary function responsible for this is handleAction in packages/next/src/server/app-render/action-handler.ts.
In vulnerable versions, this function would attempt to process URL-encoded action requests. An attacker could craft a malicious request that, upon being processed, would cause an error during the action decoding phase. The resulting error handling could then improperly expose the compiled source code of the server function.
The patch addresses this by introducing several key changes:
-
Rejection of URL-encoded Actions: The code now explicitly checks for and rejects application/x-www-form-urlencoded content types for server actions, which was a primary vector for the attack.
-
Pre-validation of Action IDs: New functions, such as areAllActionIdsValid, isInvalidStringActionDescriptor, and isInvalidActionIdFieldName, have been added. These functions perform checks on the action IDs and their descriptors within the request payload before the main decoding logic is invoked. This ensures that only requests referencing valid and well-formed server actions are processed, preventing the decoding of malicious payloads.
-
Early Exit for Apps without Server Actions: A check (hasServerActions) was added to quickly return a 'not-found' response if an application does not have any server actions defined, reducing the attack surface.
By analyzing the patch, it's clear that the handleAction function is the main entry point for the vulnerability. During exploitation, this function would be present in any runtime profile or stack trace. The addition of the validation functions highlights the lack of input sanitization that was the core of the vulnerability.