Ccodemod

codemod/fix-apollo-cors-origin-reflection

Fixes dangerous CORS origin reflection vulnerabilities in Apollo GraphQL server configurations

securitycorsapollographqlvulnerabilityfix
Public
0 executions

Run locally

npx codemod @codemod/fix-apollo-cors-origin-reflection

Fix Apollo CORS Origin Reflection

A security-focused codemod that fixes dangerous CORS origin reflection vulnerabilities in Apollo GraphQL server configurations. This codemod addresses CWE-346: Origin Validation Error and prevents cross-origin credential theft attacks.

Security Issue

Apollo GraphQL servers configured with CORS origin reflection (returning the request origin in Access-Control-Allow-Origin header) create critical security vulnerabilities:

  • Cross-Origin Credential Theft: Malicious websites can make authenticated requests and steal user credentials
  • CSRF Attacks: Bypass same-origin policy protections
  • Unauthorized API Access: Allow malicious origins to access protected GraphQL endpoints

This vulnerability is particularly dangerous when credentials: true is enabled, as it allows cookies and authentication headers to be sent cross-origin.

What This Codemod Fixes

Dangerous Patterns

Fixed: Callback function that reflects any origin

javascript

Fixed: Origin set to true with credentials enabled

javascript

Fixed: Simple arrow function that returns origin

javascript

Fixed: Direct request header access

javascript

Safe Patterns (Not Modified)

Already Secure: Explicit origin whitelist arrays

javascript

Already Secure: CORS disabled

javascript

Already Secure: No CORS configuration

javascript

Installation & Usage

bash

Supported Apollo Server Packages

This codemod works with all Apollo Server variants:

  • apollo-server
  • apollo-server-express
  • apollo-server-fastify
  • apollo-server-koa
  • apollo-server-lambda

Important Post-Migration Steps

⚠️ CRITICAL: After running this codemod, you MUST update the allowedOrigins array with your actual trusted domains:

javascript

Security References

Development

bash

License

MIT

Ready to contribute?

Build your own codemod and share it with the community.