@riocrypto/common-server 1.0.2800 → 1.0.2803

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -26,6 +26,32 @@ Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.createDynamicRateLimiter = void 0;
27
27
  const express_rate_limit_1 = __importDefault(require("express-rate-limit"));
28
28
  const custom_rate_limit_1 = require("../models/custom-rate-limit");
29
+ // Resolve the real client IP, preferring Cloudflare's cf-connecting-ip and
30
+ // falling back through x-forwarded-for and req.ip. Used both to look up
31
+ // per-IP custom rules and to key the underlying express-rate-limit instance,
32
+ // so a single attacker is bucketed to their own real IP rather than sharing
33
+ // a counter with every other client behind the ingress.
34
+ function resolveClientIp(req) {
35
+ var _a, _b, _c;
36
+ const cfConnectingIp = req.headers["cf-connecting-ip"];
37
+ const xForwardedFor = req.headers["x-forwarded-for"];
38
+ let raw;
39
+ if (typeof cfConnectingIp === "string" && cfConnectingIp.length > 0) {
40
+ raw = cfConnectingIp.trim();
41
+ }
42
+ else if (typeof xForwardedFor === "string" && xForwardedFor.length > 0) {
43
+ raw = (_a = xForwardedFor.split(",")[0]) === null || _a === void 0 ? void 0 : _a.trim();
44
+ }
45
+ else if (Array.isArray(xForwardedFor) && xForwardedFor.length > 0) {
46
+ raw = (_c = (_b = xForwardedFor[0]) === null || _b === void 0 ? void 0 : _b.split(",")[0]) === null || _c === void 0 ? void 0 : _c.trim();
47
+ }
48
+ else {
49
+ raw = req.ip;
50
+ }
51
+ if (!raw)
52
+ return undefined;
53
+ return raw.startsWith("::ffff:") ? raw.substring(7) : raw;
54
+ }
29
55
  // Cache for rate limiter instances
30
56
  const limiterInstanceCache = new Map();
31
57
  // Cache for compiled RegExp objects from path strings
@@ -84,32 +110,7 @@ function createDynamicRateLimiter(options) {
84
110
  (customIpRulesCache.size === 0 && lastCacheRefreshTime === 0)) {
85
111
  yield refreshCustomIpRulesCache(CustomRateLimit, req.path);
86
112
  }
87
- // Determine the client's IP address, prioritizing Cloudflare/proxy headers
88
- let rawClientIp;
89
- const cfConnectingIp = req.headers["cf-connecting-ip"];
90
- const xForwardedForHeader = req.headers["x-forwarded-for"];
91
- if (cfConnectingIp) {
92
- rawClientIp = cfConnectingIp;
93
- }
94
- else if (xForwardedForHeader) {
95
- // X-Forwarded-For can be a comma-separated list (client, proxy1, proxy2)
96
- // The first IP is the original client IP
97
- rawClientIp = xForwardedForHeader.split(",")[0].trim();
98
- }
99
- else {
100
- rawClientIp = req.ip; // Fallback to Express's req.ip
101
- }
102
- let normalizedIp;
103
- if (rawClientIp) {
104
- // Normalize IPv4-mapped IPv6 addresses (e.g., ::ffff:192.0.2.1 -> 192.0.2.1)
105
- // Also handles direct IPv4 and standard IPv6 addresses
106
- if (rawClientIp.startsWith("::ffff:")) {
107
- normalizedIp = rawClientIp.substring(7);
108
- }
109
- else {
110
- normalizedIp = rawClientIp;
111
- }
112
- }
113
+ const normalizedIp = resolveClientIp(req);
113
114
  let currentLimit = defaultMax;
114
115
  let currentWindowMs = defaultWindowMs;
115
116
  let ruleIdentifier = "default"; // Start with default identifier
@@ -149,13 +150,7 @@ function createDynamicRateLimiter(options) {
149
150
  let selectedLimiter = limiterInstanceCache.get(limiterCacheKey);
150
151
  if (!selectedLimiter) {
151
152
  // Create a new limiter instance with the determined limit/window
152
- selectedLimiter = (0, express_rate_limit_1.default)(Object.assign({ windowMs: currentWindowMs, max: currentLimit, keyGenerator: (request) => {
153
- let ipForKey = request.ip;
154
- if (ipForKey === null || ipForKey === void 0 ? void 0 : ipForKey.startsWith("::ffff:")) {
155
- ipForKey = ipForKey.substring(7);
156
- }
157
- return ipForKey || "unknown_ip_for_rate_limit";
158
- }, message: message, standardHeaders: standardHeaders, legacyHeaders: legacyHeaders, skip: skip, requestPropertyName: requestPropertyName }, otherRateLimitOptions));
153
+ selectedLimiter = (0, express_rate_limit_1.default)(Object.assign({ windowMs: currentWindowMs, max: currentLimit, keyGenerator: (request) => { var _a; return (_a = resolveClientIp(request)) !== null && _a !== void 0 ? _a : "unknown_ip_for_rate_limit"; }, message: message, standardHeaders: standardHeaders, legacyHeaders: legacyHeaders, skip: skip, requestPropertyName: requestPropertyName }, otherRateLimitOptions));
159
154
  limiterInstanceCache.set(limiterCacheKey, selectedLimiter);
160
155
  }
161
156
  return selectedLimiter(req, res, next);
@@ -35,6 +35,8 @@ interface AuthAttrs {
35
35
  emailVerificationAttempts?: number;
36
36
  securityAnswerAttempts?: number;
37
37
  securityAnswerLockedUntil?: Date;
38
+ authenticatorVerificationAttempts?: number;
39
+ authenticatorVerificationLockedUntil?: Date;
38
40
  authMethod?: AuthMethod;
39
41
  twoFactorConfigured?: boolean;
40
42
  twoFactorMethod?: string;
@@ -74,6 +76,8 @@ interface AuthDoc extends Document {
74
76
  emailVerificationAttempts?: number;
75
77
  securityAnswerAttempts?: number;
76
78
  securityAnswerLockedUntil?: Date;
79
+ authenticatorVerificationAttempts?: number;
80
+ authenticatorVerificationLockedUntil?: Date;
77
81
  authMethod?: AuthMethod;
78
82
  twoFactorConfigured?: boolean;
79
83
  twoFactorMethod?: string;
@@ -132,6 +132,13 @@ const buildAuth = (mongoose) => {
132
132
  securityAnswerLockedUntil: {
133
133
  type: Date,
134
134
  },
135
+ authenticatorVerificationAttempts: {
136
+ type: Number,
137
+ default: 0,
138
+ },
139
+ authenticatorVerificationLockedUntil: {
140
+ type: Date,
141
+ },
135
142
  authMethod: {
136
143
  type: String,
137
144
  },
@@ -160,6 +167,8 @@ const buildAuth = (mongoose) => {
160
167
  delete ret.emailVerificationAttempts;
161
168
  delete ret.securityAnswerAttempts;
162
169
  delete ret.securityAnswerLockedUntil;
170
+ delete ret.authenticatorVerificationAttempts;
171
+ delete ret.authenticatorVerificationLockedUntil;
163
172
  for (let apiKey of ret.apiKeys) {
164
173
  delete apiKey.value;
165
174
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@riocrypto/common-server",
3
- "version": "1.0.2800",
3
+ "version": "1.0.2803",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",