All files / src/config rateLimiters.ts

100% Statements 170/170
52.17% Branches 24/46
100% Functions 0/0
100% Lines 147/147

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 1472x 2x 2x 2x 45x 2x 2x 2x 2x 2x 2x 45x 2x 2x 2x 2x 2x 2x 45x 2x 2x 2x 2x 2x 2x 45x 2x 2x 2x 2x 36x 2x 45x 4x 2x 2x 2x 2x 2x 45x 2x 2x 2x 2x 2x 2x 45x 4x 2x 2x 2x 2x 2x 2x 45x 2x 2x 2x 2x 2x 2x 45x 2x 45x 2x 2x 12x 2x 2x 2x 45x 2x 2x 14x 2x 4x 4x 45x 2x 2x 2x 2x 2x 2x 2x 45x 2x 2x 2x 2x 2x 2x 45x 2x 2x 2x 2x 2x 2x 45x 2x 2x 2x 2x 2x 2x 45x 2x 45x 2x 2x 2x 2x 2x 2x 45x 2x 2x 2x 2x 2x 2x 2x 45x 2x 2x 2x 2x 2x 2x 45x 2x 45x 2x 2x 2x 2x 2x 2x 45x 2x 2x 2x 2x 2x 2x 45x
// src/config/rateLimiters.ts
import rateLimit from 'express-rate-limit';
import { shouldSkipRateLimit } from '../utils/rateLimit';
 
const standardConfig = {
  standardHeaders: true,
  legacyHeaders: false,
  skip: shouldSkipRateLimit,
};
 
// --- AUTHENTICATION ---
export const loginLimiter = rateLimit({
  ...standardConfig,
  windowMs: 15 * 60 * 1000, // 15 minutes
  max: 5,
  message: 'Too many login attempts from this IP, please try again after 15 minutes.',
});
 
export const registerLimiter = rateLimit({
  ...standardConfig,
  windowMs: 60 * 60 * 1000, // 1 hour
  max: 5,
  message: 'Too many accounts created from this IP, please try again after an hour.',
});
 
export const forgotPasswordLimiter = rateLimit({
  ...standardConfig,
  windowMs: 15 * 60 * 1000, // 15 minutes
  max: 5,
  message: 'Too many password reset requests from this IP, please try again after 15 minutes.',
});
 
export const resetPasswordLimiter = rateLimit({
  ...standardConfig,
  windowMs: 15 * 60 * 1000, // 15 minutes
  max: 10,
  message: 'Too many password reset attempts from this IP, please try again after 15 minutes.',
});
 
export const refreshTokenLimiter = rateLimit({
  ...standardConfig,
  windowMs: 15 * 60 * 1000, // 15 minutes
  max: 20,
  message: 'Too many token refresh attempts from this IP, please try again after 15 minutes.',
});
 
export const logoutLimiter = rateLimit({
  ...standardConfig,
  windowMs: 15 * 60 * 1000, // 15 minutes
  max: 10,
  message: 'Too many logout attempts from this IP, please try again after 15 minutes.',
});
 
// --- GENERAL PUBLIC & USER ---
export const publicReadLimiter = rateLimit({
  ...standardConfig,
  windowMs: 15 * 60 * 1000, // 15 minutes
  max: 100,
  message: 'Too many requests from this IP, please try again later.',
});
 
export const userReadLimiter = publicReadLimiter; // Alias for consistency
 
export const userUpdateLimiter = rateLimit({
  ...standardConfig,
  windowMs: 15 * 60 * 1000, // 15 minutes
  max: 100,
  message: 'Too many update requests from this IP, please try again after 15 minutes.',
});
 
export const reactionToggleLimiter = rateLimit({
  ...standardConfig,
  windowMs: 15 * 60 * 1000, // 15 minutes
  max: 150,
  message: 'Too many reaction requests from this IP, please try again later.',
});
 
export const trackingLimiter = rateLimit({
  ...standardConfig,
  windowMs: 15 * 60 * 1000, // 15 minutes
  max: 200,
  message: 'Too many tracking requests from this IP, please try again later.',
});
 
// --- SENSITIVE / COSTLY ---
export const userSensitiveUpdateLimiter = rateLimit({
  ...standardConfig,
  windowMs: 60 * 60 * 1000, // 1 hour
  max: 5,
  message: 'Too many sensitive requests from this IP, please try again after an hour.',
});
 
export const adminTriggerLimiter = rateLimit({
  ...standardConfig,
  windowMs: 15 * 60 * 1000, // 15 minutes
  max: 30,
  message: 'Too many administrative triggers from this IP, please try again later.',
});
 
export const aiGenerationLimiter = rateLimit({
  ...standardConfig,
  windowMs: 15 * 60 * 1000, // 15 minutes
  max: 20,
  message: 'Too many AI generation requests from this IP, please try again after 15 minutes.',
});
 
export const suggestionLimiter = aiGenerationLimiter; // Alias
 
export const geocodeLimiter = rateLimit({
  ...standardConfig,
  windowMs: 60 * 60 * 1000, // 1 hour
  max: 100,
  message: 'Too many geocoding requests from this IP, please try again later.',
});
 
export const priceHistoryLimiter = rateLimit({
  ...standardConfig,
  windowMs: 15 * 60 * 1000, // 15 minutes
  max: 50,
  message: 'Too many price history requests from this IP, please try again later.',
});
 
// --- UPLOADS / BATCH ---
export const adminUploadLimiter = rateLimit({
  ...standardConfig,
  windowMs: 15 * 60 * 1000, // 15 minutes
  max: 20,
  message: 'Too many file uploads from this IP, please try again after 15 minutes.',
});
 
export const userUploadLimiter = adminUploadLimiter; // Alias
 
export const aiUploadLimiter = rateLimit({
  ...standardConfig,
  windowMs: 15 * 60 * 1000, // 15 minutes
  max: 10,
  message: 'Too many file uploads from this IP, please try again after 15 minutes.',
});
 
export const batchLimiter = rateLimit({
  ...standardConfig,
  windowMs: 15 * 60 * 1000, // 15 minutes
  max: 50,
  message: 'Too many batch requests from this IP, please try again later.',
});
 
export const budgetUpdateLimiter = batchLimiter; // Alias