codemod/apollo-graphql-v3-enable-csrf-prevention
Automatically enable CSRF prevention in Apollo GraphQL Server v3 by setting csrfPrevention: true in server configuration
# apollo-graphql-v3-enable-csrf-prevention
**Automatically enable CSRF prevention in Apollo GraphQL Server v3 configurations to prevent Cross-Site Request Forgery attacks.**
## Security Issue
Apollo GraphQL Server v3 has CSRF prevention **disabled by default** (`csrfPrevention: false`), which can expose GraphQL endpoints to Cross-Site Request Forgery (CSRF) attacks. This codemod automatically enables CSRF prevention by setting `csrfPrevention: true` in all Apollo Server configurations.
### Security References
- **CWE-352**: Cross-Site Request Forgery (CSRF) - [https://cwe.mitre.org/data/definitions/352.html](https://cwe.mitre.org/data/definitions/352.html)
- **OWASP**: Cross-Site Request Forgery Prevention - [https://owasp.org/www-community/attacks/csrf](https://owasp.org/www-community/attacks/csrf)
- **Apollo Docs**: CSRF Prevention - [https://www.apollographql.com/docs/apollo-server/security/cors/#csrf-prevention](https://www.apollographql.com/docs/apollo-server/security/cors/#csrf-prevention)
## Installation
```bash
# Run directly from registry
npx codemod@latest run apollo-graphql-v3-enable-csrf-prevention
# Or install and run locally
npm install -g @codemod/apollo-graphql-v3-enable-csrf-prevention
codemod run apollo-graphql-v3-enable-csrf-prevention
```
## What it does
This codemod identifies Apollo Server v3 configurations and automatically enables CSRF prevention by:
1. **Adding missing `csrfPrevention: true`** to ApolloServer constructor configurations
2. **Changing `csrfPrevention: false` to `csrfPrevention: true`** when explicitly disabled
3. **Handling configuration objects passed as variables** by wrapping them with spread syntax
4. **Supporting all Apollo Server variants** (apollo-server, apollo-server-express, apollo-server-fastify, etc.)
## Transformations
### Basic Object Configuration
**Before:**
```javascript
const server = new ApolloServer({
typeDefs,
resolvers,
context: ({ req }) => ({ user: req.user })
});
```
**After:**
```javascript
const server = new ApolloServer({
typeDefs,
resolvers,
context: ({ req }) => ({ user: req.user }),
csrfPrevention: true
});
```
### Changing `false` to `true`
**Before:**
```javascript
const server = new ApolloServer({
typeDefs,
resolvers,
csrfPrevention: false
});
```
**After:**
```javascript
const server = new ApolloServer({
typeDefs,
resolvers,
csrfPrevention: true
});
```
### Configuration Variable
**Before:**
```javascript
const config = { typeDefs, resolvers };
const server = new ApolloServer(config);
```
**After:**
```javascript
const config = { typeDefs, resolvers };
const server = new ApolloServer({
...config,
csrfPrevention: true
});
```
### Empty Configuration
**Before:**
```javascript
const server = new ApolloServer({});
```
**After:**
```javascript
const server = new ApolloServer({
csrfPrevention: true
});
```
## Supported Apollo Server Packages
- `apollo-server`
- `apollo-server-express`
- `apollo-server-fastify`
- `apollo-server-koa`
- `apollo-server-lambda`
- `apollo-server-azure-functions`
- `apollo-server-cloud-functions`
## File Coverage
The codemod processes:
- **JavaScript files**: `.js`
- **TypeScript files**: `.ts`
- **JSX files**: `.jsx`
- **TSX files**: `.tsx`
**Excludes:**
- `node_modules` directories
- Test files (`*.test.*`, `*.spec.*`, `/test/`, `/tests/`, `/__tests__/`)
- Build output directories (`/dist/`, `/build/`)
## Edge Cases Handled
- ✅ Configurations with existing `csrfPrevention: true` (unchanged)
- ✅ Complex configurations with plugins, context, and other options
- ✅ Multiple ApolloServer instances in the same file
- ✅ Files without Apollo Server imports (skipped)
- ✅ Different import styles (named, default, destructured)
## Limitations
- The codemod cannot distinguish between Apollo Server versions from imports alone
- Assumes all apollo-server imports are v3 (appropriate for security-focused transformation)
- Does not modify configurations in external config files imported dynamically
## Development
```bash
# Test the codemod
pnpm test
# Run on specific test case
pnpm test --filter basic-object-config
# Update test snapshots
pnpm test -u
# Type check
pnpm check-types
```
## Contributing
When adding test cases:
1. Create a new directory in `tests/` with descriptive name
2. Add `input.js` and `expected.js` files
3. Run tests to verify transformation
4. Update documentation if needed
## License
MIT