Skip to content

Conversation

@clintjeff2
Copy link
Contributor

Implement Rate Limiting for PDF Generation API

Closes #16

Overview

This PR implements comprehensive rate limiting for the PDF generation endpoint to prevent abuse and ensure fair usage across all users.

Features Added

Rate Limiting

  • Limit: 20 requests per minute per user/IP
  • Response: HTTP 429 Too Many Requests when limit exceeded
  • Headers: Standard rate limit headers (X-RateLimit-*) included in responses
  • Tracking: Prioritizes user ID over IP address for authenticated requests

Violation Logging

  • All rate limit violations are logged to database with detailed metadata
  • Tracks user ID, IP address, user agent, and request patterns
  • Enables audit and analysis of potential abuse attempts

Configuration

  • Fully configurable through environment variables
  • PDF_RATE_LIMIT_WINDOW_MS: Time window (default: 60000ms)
  • PDF_RATE_LIMIT_MAX_REQUESTS: Max requests per window (default: 20)
  • PDF_RATE_LIMIT_TRACK_BY_USER: Enable user-based tracking (default: true)

Analytics & Monitoring

  • New endpoint: GET /pdf/analytics/rate-limits for violation statistics
  • Track violations by user and IP address
  • Monitor recent violations and patterns
  • Integration with existing PDF analytics system

Implementation Details

New Files

  • src/pdf/services/rate-limit.service.ts - Core rate limiting logic
  • src/pdf/guards/rate-limit.guard.ts - NestJS guard for route protection
  • src/pdf/tasks/rate-limit-cleanup.task.ts - Cleanup expired entries

Modified Files

  • src/pdf/controllers/pdf.controller.ts - Added rate limiting and analytics
  • src/pdf/pdf.module.ts - Integrated new services and dependencies
  • prisma/schema.prisma - Added RateLimitViolation model
  • .env.development & .env.example - Added configuration variables

Database Changes

-- New table for tracking rate limit violations
CREATE TABLE rate_limit_violation (
  id VARCHAR PRIMARY KEY,
  user_id VARCHAR REFERENCES user(id),
  ip_address VARCHAR(45),
  user_agent VARCHAR(255),
  endpoint VARCHAR(100),
  request_count INTEGER,
  window_start TIMESTAMP,
  violated_at TIMESTAMP DEFAULT NOW()
);

Usage Example

# Normal request (within limits)
curl -H "Authorization: Bearer <token>" \
     -X POST /pdf/generate \
     -d '{"html": "<h1>Test</h1>"}'

# Response headers include:
# X-RateLimit-Limit: 20
# X-RateLimit-Remaining: 19
# X-RateLimit-Reset: 2024-01-01T12:01:00.000Z

# When limit exceeded:
# HTTP 429 Too Many Requests
# {
#   "error": "Too Many Requests",
#   "message": "PDF generation rate limit exceeded. Please wait before making another request.",
#   "rateLimitInfo": {
#     "limit": 20,
#     "remaining": 0,
#     "resetTime": "2024-01-01T12:01:00.000Z",
#     "retryAfter": 45
#   }
# }

clintjeff2 added 30 commits June 4, 2025 19:42
clintjeff2 added 20 commits June 4, 2025 20:26
- Add PdfGenerationLog model with comprehensive tracking fields
- Add PdfInputType enum for HTML/MARKDOWN distinction
- Include metadata fields: input size, processing time, success status
- Add error logging fields: error message and stack trace
- Include request tracing: user agent, IP address, request ID
- Add performance indexes for efficient querying
- Establish foreign key relationship with User model
- Create PdfLogService for database logging operations
- Add detailed PDF generation request logging with metadata
- Implement user analytics with success rates and performance metrics
- Add system-wide analytics for administrative monitoring
- Include error pattern analysis for debugging support
- Provide safe input snippet creation with sensitive data removal
- Add request tracing and performance monitoring capabilities
- Add detailed request logging with unique request IDs
- Implement safe input snippet creation for debugging
- Add comprehensive error logging with stack traces
- Include request metadata tracking (user agent, IP address)
- Add new analytics endpoints for users and administrators
- Implement processing time measurement and performance logging
- Add error pattern analysis endpoint for system monitoring
- Add PdfLogService to PDF module providers
- Import PrismaModule for database access
- Export PdfLogService for potential use in other modules
- Update module dependencies for logging functionality
@clintjeff2
Copy link
Contributor Author

Hello @yusuftomilola, please review and merge. Opened this over 5 days ago.

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 Rate Limiting and Abuse Prevention

1 participant