The vulnerability is a Cross-Site Request Forgery (CSRF) in Mercurius, identified as GHSA-v66j-6wwf-jc57. It arises from the incorrect handling of the Content-Type header for incoming requests to the GraphQL endpoint. The application would process requests with 'simple' Content-Types (e.g., text/plain, application/x-www-form-urlencoded) as if they were application/json, without requiring a CORS preflight check. This allowed attackers to craft malicious requests on other websites that, when visited by an authenticated user, would be sent to the GraphQL endpoint with the user's credentials, leading to unauthorized actions.
The patch addresses this by introducing a CSRF prevention mechanism. A new file, lib/csrf.js, is added, which contains the core logic for the fix in the checkCSRFPrevention function. This function validates incoming requests based on their Content-Type and the presence of specific headers.
The fix is applied in lib/routes.js by adding an onRequest hook to the Fastify route definitions for both GET and POST methods. This hook calls checkCSRFPrevention for every request to the GraphQL endpoint. If a request does not meet the new security criteria (i.e., it has a 'simple' Content-Type and lacks a required preflight-triggering header), it is blocked before it reaches the main request handler.
The vulnerable functions are the anonymous route handlers for GET and POST requests in lib/routes.js. In the unpatched versions, these handlers would process potentially malicious requests without any CSRF validation. During exploitation, these are the functions that would appear in a runtime profile as they process the forged request.