codemod/hono-rate-limiting-middleware
Automatically add rate limiting middleware to Hono.js applications to prevent abuse and DoS attacks
# Hono Rate Limiting Middleware Codemod
Automatically add rate limiting middleware to Hono.js applications to prevent DoS attacks (CWE-770) and brute force attacks (CWE-307).
## Overview
This codemod enhances the security of Hono.js applications by:
- Adding general rate limiting middleware to all routes
- Implementing stricter rate limiting for authentication endpoints
- Protecting against Denial of Service (DoS) attacks
- Preventing brute force authentication attempts
## Security Context
**Vulnerabilities Addressed:**
- **CWE-770**: Allocation of Resources Without Limits or Throttling
- **CWE-307**: Improper Restriction of Excessive Authentication Attempts
Without rate limiting, Hono applications are vulnerable to:
- DoS attacks that overwhelm the server with requests
- Brute force attacks on login endpoints
- Resource exhaustion from malicious clients
## Installation & Usage
```bash
# Run the codemod on your project
npx codemod@latest run @codemod/hono-rate-limiting-middleware
# Or install and run locally
npm install -g @codemod/hono-rate-limiting-middleware
codemod run @codemod/hono-rate-limiting-middleware ./src
```
## Transformations
### Basic Hono App
**Before:**
```typescript
import { Hono } from 'hono'
const app = new Hono()
app.get('/', (c) => {
return c.text('Hello Hono!')
})
export default app
```
**After:**
```typescript
import { Hono } from 'hono'
import { rateLimiter } from 'hono-rate-limiter'
const app = new Hono()
// Rate limiting middleware
app.use('*', rateLimiter({
windowMs: 15 * 60 * 1000, // 15 minutes
limit: 100, // Limit each IP to 100 requests per windowMs
standardHeaders: 'draft-6',
legacyHeaders: false,
}))
app.get('/', (c) => {
return c.text('Hello Hono!')
})
export default app
```
### Authentication Endpoints
**Before:**
```typescript
import { Hono } from 'hono'
const app = new Hono()
app.post('/login', async (c) => {
// login logic
return c.json({ success: true })
})
```
**After:**
```typescript
import { Hono } from 'hono'
import { rateLimiter } from 'hono-rate-limiter'
const app = new Hono()
// Rate limiting middleware
app.use('*', rateLimiter({
windowMs: 15 * 60 * 1000, // 15 minutes
limit: 100, // Limit each IP to 100 requests per windowMs
standardHeaders: 'draft-6',
legacyHeaders: false,
}))
// Stricter rate limiting for auth endpoints
app.use('/login', rateLimiter({
windowMs: 15 * 60 * 1000, // 15 minutes
limit: 5, // Limit login attempts
standardHeaders: 'draft-6',
legacyHeaders: false,
}))
app.post('/login', async (c) => {
// login logic
return c.json({ success: true })
})
```
## Detection Scope
The codemod applies to:
- Files importing from the 'hono' package
- TypeScript/JavaScript files with Hono app instantiation
- Files with HTTP method route definitions (`.get()`, `.post()`, `.put()`, `.delete()`)
- API route files in typical Hono project structures
## Exclusions
The codemod will **NOT** transform:
- Test files (`.test.js`, `*.spec.ts`, `__tests__/`)
- Configuration files (`config.js`, `settings.ts`)
- Files already containing rate limiting middleware
- Health check endpoints (`/health`, `/ping`, `/status`)
- Files with explicit disable comments (`@codemod-disable-rate-limiting`)
## Rate Limiting Configuration
### General Routes
- **Window**: 15 minutes
- **Limit**: 100 requests per IP
- **Headers**: Uses draft-6 standard headers
### Authentication Routes
Authentication endpoints receive stricter limits:
- **Login endpoints** (`/login`): 5 requests per 15 minutes
- **API auth routes** (`/api/auth/*`): 10 requests per 15 minutes
- **Registration** (`/register`, `/signup`): 5 requests per 15 minutes
## Dependencies
This codemod adds the following dependency to your project:
```json
{
"dependencies": {
"hono-rate-limiter": "^0.4.0"
}
}
```
You'll need to install it after running the codemod:
```bash
npm install hono-rate-limiter
```
## Customization
After applying the codemod, you can customize the rate limiting configuration:
```typescript
// Adjust limits per your application's needs
app.use('*', rateLimiter({
windowMs: 10 * 60 * 1000, // 10 minutes
limit: 200, // Increase limit for high-traffic apps
standardHeaders: 'draft-6',
legacyHeaders: false,
message: 'Too many requests, please try again later.',
}))
```
## Security References
- [CWE-770: Allocation of Resources Without Limits or Throttling](https://cwe.mitre.org/data/definitions/770.html)
- [CWE-307: Improper Restriction of Excessive Authentication Attempts](https://cwe.mitre.org/data/definitions/307.html)
- [OWASP API Security - Rate Limiting](https://cheatsheetseries.owasp.org/cheatsheets/REST_Security_Cheat_Sheet.html#rate-limiting)
- [Hono Rate Limiter Documentation](https://hono.dev/middleware/builtin/rate-limiter)
## Contributing
1. Clone the repository
2. Install dependencies: `pnpm install`
3. Run tests: `pnpm test`
4. Make your changes
5. Ensure tests pass: `pnpm test`
6. Submit a pull request
## Development
```bash
# Test the transformation
pnpm test
# Update test snapshots
pnpm test -u
# Validate the codemod
pnpm build
# Run on a specific directory
npx codemod@latest jssg run ./scripts/codemod.ts /path/to/target
```
## License
MIT