Skip to content

feat: Implement Global API Rate Limiting and Implement Role-Based Acc…#249

Merged
Olowodarey merged 1 commit intoDevsol-01:mainfrom
tech-adrian:access-control/api-rating-limiting
Feb 26, 2026
Merged

feat: Implement Global API Rate Limiting and Implement Role-Based Acc…#249
Olowodarey merged 1 commit intoDevsol-01:mainfrom
tech-adrian:access-control/api-rating-limiting

Conversation

@tech-adrian
Copy link
Contributor

Security Enhancements: RBAC + Rate Limiting

Summary

Fixes #244 & #245 - Complete security implementation with RBAC and global rate limiting

🛡️ RBAC Implementation (Issue #244)

Role-Based Access Control with comprehensive endpoint protection

Features

  • Role enum (USER, ADMIN) for user role definitions
  • @roles decorator using NestJS SetMetadata for endpoint role requirements
  • RolesGuard for enforcing role-based access control
  • Test controller with multiple access levels for verification
  • Comprehensive tests (19 tests passing) covering all scenarios

Test Endpoints

  • GET /test-rbac/public - No authentication required
  • GET /test-rbac/user - Requires USER role or higher
  • GET /test-rbac/admin - Requires ADMIN role only
  • GET /test-rbac/user-or-admin - Requires USER or ADMIN role

Usage

@Controller('example')
@UseGuards(JwtAuthGuard, RolesGuard)
export class ExampleController {
  @Get('admin')
  @Roles(Role.ADMIN)
  getAdminEndpoint() {
    return { message: 'Admin only content' };
  }
}

⚡ Rate Limiting Implementation (Issue #245)

Global API rate limiting with bypass mechanism

Features

  • @nestjs/throttler configured globally (100 requests/60s per IP)
  • ThrottlerGuard bound globally to protect all routes
  • @SkipThrottle decorator for unlimited endpoints (webhooks, health checks)
  • Test controller with rate limited and unlimited endpoints
  • Testing script for verifying HTTP 429 responses

Configuration

ThrottlerModule.forRoot([
  {
    ttl: 60000,  // 60 seconds
    limit: 100,  // 100 requests per window
  },
])

Test Endpoints

  • GET /test-throttling - Rate limited (100 req/min)
  • GET /test-throttling/skip - Unlimited (bypasses rate limiting)
  • POST /test-throttling/webhook - Unlimited webhook endpoint
  • GET /test-throttling/burst - For testing rate limit triggers

Usage

@Controller('example')
export class ExampleController {
  @Get()
  getRateLimited() {
    // Rate limited: 100 requests/minute
  }

  @Get('webhook')
  @SkipThrottle()
  handleWebhook() {
    // Unlimited: bypasses rate limiting
  }
}

🚀 Combined Security Benefits

Protection Layers

  1. Authentication - JWT token validation
  2. Authorization - Role-based access control
  3. Rate Limiting - DoS and abuse prevention

Security Features

  • 🛡️ DoS protection - Prevents brute-force attacks
  • 🎯 Role enforcement - Proper access control
  • ⚖️ Fair usage - Equitable access for all users
  • 📊 Resource management - Controls server load
  • 🔍 Monitoring ready - Built-in violation tracking

Test Results

  • 29 tests passing across both implementations
  • Build successful with no compilation errors
  • Production ready security enhancements

📁 Files Created/Modified

RBAC Files

src/
├── common/enums/role.enum.ts
├── common/decorators/roles.decorator.ts
├── common/guards/roles.guard.ts
├── test-rbac/
│   ├── test-rbac.controller.ts
│   ├── test-rbac.controller.spec.ts
│   └── test-rbac.module.ts
└── app.module.ts (updated)

Rate Limiting Files

src/
├── test-throttling/
│   ├── test-throttling.controller.ts
│   ├── test-throttling.controller.spec.ts
│   └── test-throttling.module.ts
├── app.module.ts (updated)
└── test-rate-limiting.js

Documentation

  • RBAC_README.md - Complete RBAC guide
  • RATE_LIMITING_README.md - Rate limiting documentation

🔧 Integration

Both security features work seamlessly together:

@Controller('protected')
@UseGuards(JwtAuthGuard, RolesGuard, ThrottlerGuard)
export class ProtectedController {
  @Get('admin')
  @Roles(Role.ADMIN)
  getAdminEndpoint() {
    // 1. JWT Auth -> 2. Role Check -> 3. Rate Limit
  }

  @Get('webhook')
  @SkipThrottle()
  @Roles(Role.ADMIN)
  handleWebhook() {
    // 1. JWT Auth -> 2. Role Check (no rate limit)
  }
}

Closes #244

Closes #245

@vercel
Copy link

vercel bot commented Feb 26, 2026

@tech-adrian is attempting to deploy a commit to the devsol-01's projects Team on Vercel.

A member of the Team first needs to authorize it.

@Olowodarey Olowodarey merged commit a6622c7 into Devsol-01:main Feb 26, 2026
2 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement Global API Rate Limiting Implement Role-Based Access Control (RBAC) Guard

2 participants