@riocrypto/common-server 1.0.2799 → 1.0.2802

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.
package/build/index.d.ts CHANGED
@@ -17,6 +17,7 @@ export * from "./validators/require-admin-auth";
17
17
  export * from "./validators/require-admin-or-cluster-auth";
18
18
  export * from "./validators/require-if-user-type";
19
19
  export * from "./validators/require-admin-auth-if-defined";
20
+ export * from "./validators/require-admin-special-permission";
20
21
  export * from "./validators/is-valid-date";
21
22
  export * from "./sanitizers/capitalize-first-letter";
22
23
  export * from "./models/admin-auth";
package/build/index.js CHANGED
@@ -33,6 +33,7 @@ __exportStar(require("./validators/require-admin-auth"), exports);
33
33
  __exportStar(require("./validators/require-admin-or-cluster-auth"), exports);
34
34
  __exportStar(require("./validators/require-if-user-type"), exports);
35
35
  __exportStar(require("./validators/require-admin-auth-if-defined"), exports);
36
+ __exportStar(require("./validators/require-admin-special-permission"), exports);
36
37
  __exportStar(require("./validators/is-valid-date"), exports);
37
38
  __exportStar(require("./sanitizers/capitalize-first-letter"), exports);
38
39
  __exportStar(require("./models/admin-auth"), exports);
@@ -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);
@@ -0,0 +1,3 @@
1
+ import { CustomValidator } from "express-validator";
2
+ import { AdminAuthSpecialPermission } from "@riocrypto/common";
3
+ export declare const requireAdminSpecialPermission: (permission: AdminAuthSpecialPermission) => CustomValidator;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.requireAdminSpecialPermission = void 0;
4
+ const requireAdminSpecialPermission = (permission) => (value, { req }) => {
5
+ var _a;
6
+ if (value === undefined)
7
+ return true;
8
+ if (!req.adminAuth)
9
+ return false;
10
+ if (!((_a = req.adminAuth.permissions) === null || _a === void 0 ? void 0 : _a.includes(permission)))
11
+ return false;
12
+ return true;
13
+ };
14
+ exports.requireAdminSpecialPermission = requireAdminSpecialPermission;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@riocrypto/common-server",
3
- "version": "1.0.2799",
3
+ "version": "1.0.2802",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",