File size: 10,439 Bytes
1a25764
 
7b6e647
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a25764
2e1ef81
1a25764
7b6e647
 
1a25764
7b6e647
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a25764
7b6e647
 
 
 
 
 
 
 
 
 
 
1a25764
 
7b6e647
 
 
 
 
 
 
1a25764
7b6e647
 
 
1a25764
 
7b6e647
 
 
 
2e1ef81
7b6e647
 
 
1a25764
7b6e647
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9d5bbe
 
 
 
 
 
 
 
7b6e647
e9d5bbe
7b6e647
e9d5bbe
 
1a25764
e9d5bbe
 
7b6e647
 
 
 
 
 
 
 
 
 
1a25764
 
 
7b6e647
 
 
1a25764
7b6e647
 
1a25764
7b6e647
 
 
1a25764
 
7b6e647
 
 
 
1a25764
 
 
7b6e647
 
 
1a25764
 
2e1ef81
7b6e647
1a25764
 
 
 
 
 
e9d5bbe
 
 
 
 
 
1a25764
 
7b6e647
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
'use strict';

const DEFAULT_WINDOW_MS = 60 * 1000;
const CLEANUP_INTERVAL_MS = 5 * 60 * 1000;
const BLOCK_DURATIONS_SEC = [300, 1200, 3600]; // seconds (5m, 20m, 1h)
const BLOCK_TRACKING_KEY_PREFIX = 'rl:block:';
const MAX_MEM_ENTRIES = 10000;

// Circuit breaker for Redis
const circuitBreaker = {
  isOpen: false,
  failureCount: 0,
  lastFailure: 0,
  resetTimeout: 30000 // 30 seconds
};

// Trusted proxies for secure IP extraction (supports CIDR)
const TRUSTED_PROXIES = ['127.0.0.1', '::1', '10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16'];

// Request cost multiplier (GET=1, POST=2, DELETE=3)
const REQUEST_COST = {
  GET: 1,
  HEAD: 1,
  POST: 2,
  PUT: 2,
  PATCH: 2,
  DELETE: 3
};

function cidrToRegex(cidr) {
  const [ip, bits] = cidr.split('/');
  /* v8 ignore next */
  const maskBits = bits ? parseInt(bits, 10) : 32;
  const mask = ~(2 ** (32 - maskBits) - 1);
  const ipParts = ip.split('.').map(Number);
  const networkInt = (ipParts[0] << 24 | ipParts[1] << 16 | ipParts[2] << 8 | ipParts[3]) & mask;
  return (testIp) => {
    const testParts = testIp.split('.').map(Number);
    const testInt = testParts[0] << 24 | testParts[1] << 16 | testParts[2] << 8 | testParts[3];
    return (testInt & mask) === networkInt;
  };
}

// Check if an IP is in a trusted CIDR range
function isTrustedProxy(ip) {
  if (!ip || ip === 'unknown') return false;

  return TRUSTED_PROXIES.some(cidr => {
    try {
      if (!cidr.includes('/')) {
        return ip === cidr;
      }
      const matcher = cidrToRegex(cidr);
      return matcher(ip);
    } catch { /* v8 ignore next */
      return false;
    }
  });
}

// Lua script for atomic rate limiting operations
const RATE_LIMIT_LUA_SCRIPT = `
local countKey = KEYS[1]
local blockKey = KEYS[2]
local maxRequests = tonumber(ARGV[1])
local windowSec = tonumber(ARGV[2])
local blockDurations = {${BLOCK_DURATIONS_SEC.join(',')}}

-- Increment request count
local count = redis.call('INCR', countKey)

-- Set expiry for countKey if this is the first request in the window
if count == 1 then
  redis.call('EXPIRE', countKey, windowSec)
end

-- Check if rate limit is exceeded
if count > maxRequests then
  -- Increment violation count
  local violationCount = redis.call('INCR', blockKey)

  -- Determine block duration (escalating)
  local idx = math.min(violationCount - 1, #blockDurations - 1)
  local blockSec = blockDurations[idx + 1]

  -- Set expiry for blockKey
  redis.call('EXPIRE', blockKey, blockSec)

  -- Return rate limit info (limited = true)
  return {count, 0, blockSec, blockSec, 1}
else
  -- Get TTL for countKey
  local ttl = redis.call('TTL', countKey)
  if ttl < 0 then
    ttl = windowSec
  end

  -- Return rate limit info (limited = false)
  return {count, math.max(0, maxRequests - count), ttl, 0, 0}
end`;

function createInMemoryStore() {
  const mem = new Map();
  // simple LRU helper
  function touch(key) {
    const v = mem.get(key);
    if (!v) return;
    mem.delete(key);
    mem.set(key, v);
  }
  function ensureLimit() {
    while (mem.size > MAX_MEM_ENTRIES) {
      const firstKey = mem.keys().next().value;
      mem.delete(firstKey);
    }
  }
  return {
    get: (k) => {
      touch(k);
      return mem.get(k);
    },
    set: (k, v) => {
      mem.set(k, v);
      ensureLimit();
    },
    delete: (k) => mem.delete(k),
    entries: () => mem.entries(),
    size: () => mem.size
  };
}

const memStore = createInMemoryStore();

function triggerCleanup() {
  const now = Date.now();
  for (const [ip, entry] of memStore.entries()) {
    if (entry.resetAt <= now) memStore.delete(ip);
  }
}

/* v8 ignore start */
setInterval(() => {
  triggerCleanup();
}, CLEANUP_INTERVAL_MS);
/* v8 ignore stop */

function memFallback(ip, max, windowMs) {
  const now = Date.now();
  let entry = memStore.get(ip) || null;

  if (!entry || entry.resetAt <= now) {
    // new window or expired
    const prevViolation = entry ? (entry.violationCount || 0) : 0;
    entry = {
      count: 0,
      resetAt: now + windowMs,
      violationCount: prevViolation
    };
  }

  entry.count = (entry.count || 0) + 1;

  // if exceeded, increment violationCount and set block expiry
  let limited = false;
  let resetSeconds;
  if (entry.count > max) {
    entry.violationCount = (entry.violationCount || 0) + 1;
    const idx = Math.min(entry.violationCount - 1, BLOCK_DURATIONS_SEC.length - 1);
    const blockSec = BLOCK_DURATIONS_SEC[idx];
    // set reset to now + blockSec
    entry.resetAt = now + blockSec * 1000;
    limited = true;
    resetSeconds = Math.ceil(blockSec);
  } else {
    resetSeconds = Math.ceil((entry.resetAt - now) / 1000);
  }

  memStore.set(ip, entry);

  return {
    count: entry.count,
    remaining: Math.max(0, max - entry.count),
    reset: Math.floor((entry.resetAt) / 1000), // epoch seconds
    retryAfter: limited ? resetSeconds : 0,
    limited,
    violationCount: entry.violationCount
  };
}

// Normalize IP address (IPv4-mapped IPv6, lowercase, etc.)
function normalizeIp(ip) {
  if (!ip || ip === 'unknown') return 'unknown';

  // Handle IPv6-mapped IPv4 addresses (::ffff:192.168.1.1)
  if (ip.startsWith('::ffff:')) {
    return ip.substring(7); // Convert to IPv4
  }

  // Lowercase for consistency
  return ip.toLowerCase();
}

// Secure IP extraction with proxy awareness
function getClientIp(req) {
  const xff = req.headers['x-forwarded-for'] || '';
  const ips = xff.split(',').map(ip => normalizeIp(ip.trim())).filter(ip => ip && ip !== 'unknown');

  // If request comes from a trusted proxy, use the first IP in XFF
  if (isTrustedProxy(normalizeIp(req.socket.remoteAddress))) {
    return ips[0] || normalizeIp(req.socket.remoteAddress);
  }
  // Otherwise, use the direct connection IP
  /* v8 ignore next */
  return normalizeIp(req.socket.remoteAddress) || 'unknown';
}

// Request cost multiplier (GET=1, POST=2, DELETE=3)
function getRequestCost(req) {
  return REQUEST_COST[req.method] || 1;
}

async function checkRedis(redis, ip, max, windowSec, retryDelayMs = null) {
  // Circuit breaker check
  if (circuitBreaker.isOpen) {
    if (Date.now() - circuitBreaker.lastFailure > circuitBreaker.resetTimeout) {
      circuitBreaker.isOpen = false;
      circuitBreaker.failureCount = 0;
    } else {
      throw new Error('Circuit breaker open - Redis unavailable');
    }
  }

  let retries = 0;
  const maxRetries = 3;

  while (retries < maxRetries) {
    try {
      const countKey = `rl:${ip}`;
      const blockKey = `${BLOCK_TRACKING_KEY_PREFIX}${ip}`;

      // Execute Lua script for atomic operations
      const result = await redis.eval(
        RATE_LIMIT_LUA_SCRIPT,
        2, // Number of keys
        countKey,
        blockKey,
        max,
        windowSec
      );

      // Reset failure count on success
      circuitBreaker.failureCount = 0;

      // Parse the result (Lua returns a table with 1-based array indices)
      return {
        count: result[0],
        remaining: result[1],
        reset: result[2],
        retryAfter: result[3],
        limited: result[4]
      };
    } catch (err) {
      retries++;
      circuitBreaker.failureCount++;
      circuitBreaker.lastFailure = Date.now();
      if (circuitBreaker.failureCount >= 3) {
        circuitBreaker.isOpen = true;
      }
      if (retries >= maxRetries) {
        throw new Error('Max retries exceeded');
      }
      // Exponential backoff for retries - use provided delay or default
      /* v8 ignore next */
      const delay = retryDelayMs !== null ? retryDelayMs : 100 * Math.pow(2, retries);
      await new Promise(resolve => setTimeout(resolve, delay));
    }
  }
}

function isExemptRoute(path, exemptRoutes = ['/health', '/status', '/favicon.ico']) {
  return exemptRoutes.includes(path);
}

function createRateLimiter(redis, options = {}) {
  const config = {
    enabled: true,
    max: 100,
    windowMs: DEFAULT_WINDOW_MS,
    exemptRoutes: ['/health', '/status', '/favicon.ico'],
    logger: console,
    retryDelayMs: null,
  };

  Object.assign(config, options);

  const middleware = async function rateLimiter(req, res, next) {
    if (!config.enabled) return next();

    const { max, windowMs, exemptRoutes, logger, retryDelayMs } = config;
    const windowSec = Math.floor(windowMs / 1000);
    const path = req.url ? req.url.split('?')[0] : (req.path || '/');
    if (isExemptRoute(path, exemptRoutes)) return next();

    const ip = getClientIp(req);
    const cost = getRequestCost(req);
    const effectiveMax = Math.max(1, Math.floor(max / cost)); // Adjust max based on request cost
    if (!ip || ip === 'unknown') {
      logger.warn('Invalid IP detected', { ip, path });
      return next();
    }

    let info;
    let usingRedis = false;
    try {
      if (redis && redis.connected) {
        info = await checkRedis(redis, ip, effectiveMax, windowSec, retryDelayMs);
        usingRedis = true;
      } else {
        info = memFallback(ip, effectiveMax, windowMs);
      }
    } catch (err) {
      logger.error('Redis error, falling back to memory', err.message);
      info = memFallback(ip, effectiveMax, windowMs);
    }

    // Standard headers
    res.setHeader('X-RateLimit-Limit', String(effectiveMax));
    res.setHeader('X-RateLimit-Remaining', String(info.remaining));
    res.setHeader('X-RateLimit-Reset', String(info.reset)); // epoch seconds
    res.setHeader('X-RateLimit-Store', usingRedis ? 'redis' : 'memory');

    if (info.limited) {
      logger.warn('Rate limit exceeded', { ip, path, retryAfter: info.retryAfter });
      res.setHeader('Retry-After', String(info.retryAfter));
      res.writeHead(429, { 'Content-Type': 'application/json' });
      res.end(JSON.stringify({
        error: 'Too Many Requests',
        message: `Rate limit exceeded. Max ${max} requests per ${windowSec}s window.`,
        retryAfter: info.retryAfter
      }));
      return;
    }

    next();
  };

  middleware.updateConfig = (updates) => {
    Object.assign(config, updates);
  };

  return middleware;
}

module.exports = {
  createRateLimiter,
  memFallback,
  checkRedis,
  getClientIp,
  isTrustedProxy,
  getRequestCost,
  getCircuitBreaker: () => circuitBreaker,
  resetCircuitBreaker: () => {
    circuitBreaker.isOpen = false;
    circuitBreaker.failureCount = 0;
    circuitBreaker.lastFailure = 0;
  },
  getMemStore: () => memStore,
  resetMemStore: () => memStore.entries().forEach(([k]) => memStore.delete(k)),
  triggerCleanup
};