Registry
Ccodemod avatar

codemod/apollo-graphql-enable-csrf

Changes explicitly disabled csrfPrevention option from false to true in Apollo Server v4 configurations to prevent CSRF attacks

codemod0 executions

  • security
  • csrf
  • apollo
  • graphql
  • vulnerability
  • transformation
Connect a repo
npx codemod @codemod/apollo-graphql-enable-csrf

@codemod/apollo-graphql-v4-fix-csrf-prevention-false

A security-focused codemod that fixes vulnerable Apollo Server v4 configurations by changing explicitly disabled csrfPrevention option from false to true.

Security Context

Apollo GraphQL v4 applications with csrfPrevention explicitly set to false are vulnerable to CSRF (Cross-Site Request Forgery) attacks. This setting disables Apollo Server's built-in CSRF protection mechanism, leaving the application exposed to attacks where malicious websites can make unauthorized GraphQL requests on behalf of authenticated users.

Severity: Critical
CWE: CWE-352 - Cross-Site Request Forgery (CSRF)
CVE: CVE-2023-26144

What This Codemod Does

This codemod finds all instances where csrfPrevention is explicitly set to false in Apollo Server configurations and changes them to true to enable CSRF protection.

Before

javascript
const server = new ApolloServer({
  typeDefs,
  resolvers,
  csrfPrevention: false  // Vulnerable!
});

After

javascript
const server = new ApolloServer({
  typeDefs,
  resolvers,
  csrfPrevention: true   // Secure!
});

Supported Patterns

The codemod handles various configuration patterns:

  • Direct ApolloServer constructor configurations
  • Configuration objects passed to ApolloServer
  • Multi-line configurations with mixed properties
  • Configurations using spread syntax

Installation & Usage

bash
# Run directly with codemod CLI
npx codemod @codemod/apollo-graphql-v4-fix-csrf-prevention-false

# Or install and run locally
npm install -g @codemod/apollo-graphql-v4-fix-csrf-prevention-false
codemod run -w workflow.yaml

What Gets Changed

โœ… Transforms:

  • csrfPrevention: false โ†’ csrfPrevention: true
  • Applies to any object containing this property/value pair
  • Preserves all other configuration options and formatting

โŒ Does NOT transform:

  • Properties already set to true
  • Configurations without csrfPrevention property
  • Commented-out configurations
  • Different property names (e.g., csrf: false)
  • Environment variable-based conditional settings

Security Impact

Enabling CSRF prevention helps protect against:

  • Cross-site request forgery attacks
  • Unauthorized GraphQL operations from malicious websites
  • Session hijacking through forged requests

References

Development

bash
# Test the transformation
pnpm test

# Run tests with updates
pnpm test -u

# Validate the workflow
codemod validate -w workflow.yaml

License

MIT

Ready to evolve your codebase?

Build tailored, compiler-aware skills to assess, plan, and automate complex codebase changes.