codemod/express-rate-limiting-middleware-addition
Automatically add rate limiting middleware to Express.js route handlers that don't have existing rate limiting
# Express Rate Limiting Middleware Addition
Automatically add rate limiting middleware to Express.js route handlers that don't have existing rate limiting protection. This helps prevent DDoS attacks and brute force attempts by adding appropriate rate limiting to API endpoints.
## What it does
This codemod automatically:
- **Detects Express routes** that serve API endpoints (paths starting with `/api/`)
- **Adds appropriate rate limiting** based on the route type:
- **General API routes**: 100 requests per 15 minutes
- **Authentication routes** (`/api/auth/`, `/api/login`, etc.): 5 requests per 15 minutes with stricter limits
- **Imports express-rate-limit** if not already present
- **Creates rate limiter configurations** (apiLimiter, authLimiter) with sensible defaults
- **Preserves existing middleware** by inserting rate limiting in the correct position
## Before and After
### Basic API Routes
**Before:**
```javascript
const express = require('express');
const app = express();
app.get('/api/users', (req, res) => {
res.json(users);
});
app.post('/api/data', validateInput, (req, res) => {
// logic
});
```
**After:**
```javascript
const rateLimit = require('express-rate-limit');
const express = require('express');
const app = express();
const apiLimiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // limit each IP to 100 requests per windowMs
message: 'Too many requests from this IP, please try again later.'
});
app.get('/api/users', apiLimiter, (req, res) => {
res.json(users);
});
app.post('/api/data', apiLimiter, validateInput, (req, res) => {
// logic
});
```
### Authentication Routes
**Before:**
```javascript
const express = require('express');
const router = express.Router();
router.post('/api/auth/login', (req, res) => {
// login logic
});
```
**After:**
```javascript
const rateLimit = require('express-rate-limit');
const express = require('express');
const router = express.Router();
const authLimiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 5, // limit each IP to 5 login attempts per windowMs
message: 'Too many login attempts from this IP, please try again later.',
skipSuccessfulRequests: true
});
router.post('/api/auth/login', authLimiter, (req, res) => {
// login logic
});
```
## What gets transformed
✅ **Routes that will be transformed:**
- Routes with paths starting with `/api/`
- Express app and router method calls (`app.get`, `router.post`, etc.)
- `app.use` calls with API paths
- Routes without existing rate limiting
❌ **Routes that are excluded:**
- Health check endpoints (`/health`, `/status`, `/ping`)
- Root path handlers (`/`)
- Static file serving (`express.static`)
- Routes already containing rate limiting middleware
- Routes with `// no-rate-limit` or `// @no-rate-limit` comments
- Routes containing rate limiting keywords (`rate`, `limit`, `throttle`)
- Non-API routes (paths not starting with `/api/`)
## Installation & Usage
```bash
# Run directly on your codebase
npx codemod@latest run express-rate-limiting-middleware-addition
# Or install and run locally
npm install -g @codemod/cli
codemod run express-rate-limiting-middleware-addition
```
## Requirements
- Your project must use Express.js
- Routes should follow the `/api/` path convention for API endpoints
- The codemod will add `express-rate-limit` as a dependency if not present
## Configuration
The codemod uses these default rate limiting configurations:
- **API Limiter**: 100 requests per 15 minutes for general API endpoints
- **Auth Limiter**: 5 requests per 15 minutes for authentication endpoints with `skipSuccessfulRequests: true`
You can customize these limits after running the codemod by modifying the generated `apiLimiter` and `authLimiter` configurations.
## Security Benefits
- **Prevents DDoS attacks** by limiting request rates
- **Reduces brute force attack effectiveness** with stricter auth limits
- **Improves API stability** under high load
- **Follows security best practices** as recommended by OWASP
## Development
```bash
# Test the transformation
npm test
# Check TypeScript types
npm run check-types
# Run on a specific file
npx codemod@latest jssg run --language javascript --target your-file.js ./scripts/codemod.ts
```
## License
MIT