Ccodemod

codemod/hono-rate-limiting-middleware

Automatically add rate limiting middleware to Hono.js applications to prevent abuse and DoS attacks

securityhonorate-limitingdos-protectioncwe-770cwe-307middlewaretransformation
Public
0 executions

Run locally

npx codemod @codemod/hono-rate-limiting-middleware

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

Transformations

Basic Hono App

Before:

typescript

After:

typescript

Authentication Endpoints

Before:

typescript

After:

typescript

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

You'll need to install it after running the codemod:

bash

Customization

After applying the codemod, you can customize the rate limiting configuration:

typescript

Security References

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

License

MIT

Ready to contribute?

Build your own codemod and share it with the community.