@futdevpro/nts-dynamo 1.15.145 → 1.15.151
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/.dynamo/logs/cicd-pipeline/output.log +1806 -1957
- package/.dynamo/logs/cicd-pipeline/status.json +33 -33
- package/README.md +16 -0
- package/__documentations/2026-08-21-distributed-rate-limit-bfr-ctm-010.md +59 -0
- package/__documentations/2026-08-22-npm-token-state-classifier.md +92 -0
- package/build/_collections/npm-token-check.util.d.ts +139 -0
- package/build/_collections/npm-token-check.util.d.ts.map +1 -0
- package/build/_collections/npm-token-check.util.js +302 -0
- package/build/_collections/npm-token-check.util.js.map +1 -0
- package/build/_modules/rate-limit/_enums/rate-limit-store-kind.type-enum.d.ts +6 -0
- package/build/_modules/rate-limit/_enums/rate-limit-store-kind.type-enum.d.ts.map +1 -0
- package/build/_modules/rate-limit/_enums/rate-limit-store-kind.type-enum.js +10 -0
- package/build/_modules/rate-limit/_enums/rate-limit-store-kind.type-enum.js.map +1 -0
- package/build/_modules/rate-limit/_models/rate-limit-config.interface.d.ts +15 -6
- package/build/_modules/rate-limit/_models/rate-limit-config.interface.d.ts.map +1 -1
- package/build/_modules/rate-limit/_models/rate-limit-consume-request.interface.d.ts +9 -0
- package/build/_modules/rate-limit/_models/rate-limit-consume-request.interface.d.ts.map +1 -0
- package/build/_modules/rate-limit/_models/rate-limit-consume-request.interface.js +3 -0
- package/build/_modules/rate-limit/_models/rate-limit-consume-request.interface.js.map +1 -0
- package/build/_modules/rate-limit/_models/rate-limit-consume-result.interface.d.ts +7 -0
- package/build/_modules/rate-limit/_models/rate-limit-consume-result.interface.d.ts.map +1 -0
- package/build/_modules/rate-limit/_models/rate-limit-consume-result.interface.js +3 -0
- package/build/_modules/rate-limit/_models/rate-limit-consume-result.interface.js.map +1 -0
- package/build/_modules/rate-limit/_models/rate-limit-store.interface.d.ts +9 -0
- package/build/_modules/rate-limit/_models/rate-limit-store.interface.d.ts.map +1 -0
- package/build/_modules/rate-limit/_models/rate-limit-store.interface.js +3 -0
- package/build/_modules/rate-limit/_models/rate-limit-store.interface.js.map +1 -0
- package/build/_modules/rate-limit/index.d.ts +5 -0
- package/build/_modules/rate-limit/index.d.ts.map +1 -1
- package/build/_modules/rate-limit/index.js +5 -1
- package/build/_modules/rate-limit/index.js.map +1 -1
- package/build/_modules/rate-limit/rate-limit-in-memory.store.d.ts +19 -0
- package/build/_modules/rate-limit/rate-limit-in-memory.store.d.ts.map +1 -0
- package/build/_modules/rate-limit/rate-limit-in-memory.store.js +71 -0
- package/build/_modules/rate-limit/rate-limit-in-memory.store.js.map +1 -0
- package/build/_modules/rate-limit/rate-limit.middleware.d.ts +39 -103
- package/build/_modules/rate-limit/rate-limit.middleware.d.ts.map +1 -1
- package/build/_modules/rate-limit/rate-limit.middleware.js +325 -236
- package/build/_modules/rate-limit/rate-limit.middleware.js.map +1 -1
- package/build/index.d.ts +1 -0
- package/build/index.d.ts.map +1 -1
- package/build/index.js +1 -0
- package/build/index.js.map +1 -1
- package/package.json +2 -2
- package/src/_collections/npm-token-check.util.spec.ts +218 -0
- package/src/_collections/npm-token-check.util.ts +327 -0
- package/src/_modules/rate-limit/_enums/rate-limit-store-kind.type-enum.ts +5 -0
- package/src/_modules/rate-limit/_models/rate-limit-config.interface.ts +19 -6
- package/src/_modules/rate-limit/_models/rate-limit-consume-request.interface.ts +9 -0
- package/src/_modules/rate-limit/_models/rate-limit-consume-result.interface.ts +6 -0
- package/src/_modules/rate-limit/_models/rate-limit-store.interface.ts +9 -0
- package/src/_modules/rate-limit/index.ts +5 -0
- package/src/_modules/rate-limit/rate-limit-in-memory.store.ts +91 -0
- package/src/_modules/rate-limit/rate-limit.middleware.spec.ts +424 -200
- package/src/_modules/rate-limit/rate-limit.middleware.ts +436 -275
- package/src/index.ts +1 -0
|
@@ -2,288 +2,377 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.DyNTS_RateLimit_Middleware = void 0;
|
|
4
4
|
const fsm_dynamo_1 = require("@futdevpro/fsm-dynamo");
|
|
5
|
-
const singleton_service_base_1 = require("../../_services/base/singleton.service-base");
|
|
6
5
|
const global_settings_const_1 = require("../../_collections/global-settings.const");
|
|
7
|
-
|
|
6
|
+
const singleton_service_base_1 = require("../../_services/base/singleton.service-base");
|
|
7
|
+
const rate_limit_store_kind_type_enum_1 = require("./_enums/rate-limit-store-kind.type-enum");
|
|
8
|
+
const rate_limit_in_memory_store_1 = require("./rate-limit-in-memory.store");
|
|
8
9
|
const DEFAULT_LIMIT = 100;
|
|
9
|
-
|
|
10
|
-
const DEFAULT_WINDOW_MS = 60000;
|
|
11
|
-
/** Default response-header allitas. */
|
|
10
|
+
const DEFAULT_WINDOW_MS = 60_000;
|
|
12
11
|
const DEFAULT_RESPONSE_HEADERS = true;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const GC_INTERVAL_MS = 5 * 60 * 1000;
|
|
17
|
-
/** Service-nev az error-okhoz. */
|
|
12
|
+
const MAX_LIMIT = 1_000_000;
|
|
13
|
+
const MAX_WINDOW_MS = 7 * 24 * 60 * 60 * 1_000;
|
|
14
|
+
const MAX_BUCKET_PART_LENGTH = 240;
|
|
18
15
|
const SERVICE_NAME = 'DyNTS_RateLimit_Middleware';
|
|
19
|
-
/**
|
|
20
|
-
const buildErrorCode = (subcode) => {
|
|
21
|
-
const sys = global_settings_const_1.DyNTS_global_settings.systemShortCodeName ?? 'DyNTS';
|
|
22
|
-
return `${sys}|DyNTS-RL-${subcode}`;
|
|
23
|
-
};
|
|
24
|
-
/**
|
|
25
|
-
* Sliding-window HTTP rate-limit middleware — opt-in service-szel, a meglevo
|
|
26
|
-
* `DyNTS_Endpoint_Params.preProcesses` mechanizmus mellol mukodik.
|
|
27
|
-
*
|
|
28
|
-
* **Hasznalat (host app):**
|
|
29
|
-
* ```ts
|
|
30
|
-
* const rateLimit = DyNTS_RateLimit_Middleware.getInstance();
|
|
31
|
-
* rateLimit.configure({
|
|
32
|
-
* defaultLimit: 100, // 100 req/perc default
|
|
33
|
-
* defaultWindowMs: 60_000,
|
|
34
|
-
* keyExtractor: (req) => req.headers['x-api-key'] as string || req.ip,
|
|
35
|
-
* });
|
|
36
|
-
*
|
|
37
|
-
* new DyNTS_Endpoint_Params({
|
|
38
|
-
* ...,
|
|
39
|
-
* preProcesses: [rateLimit.check, ...other],
|
|
40
|
-
* });
|
|
41
|
-
*
|
|
42
|
-
* // subscription-tier-up: per-kulcs egyedi limit
|
|
43
|
-
* rateLimit.setPolicyForKey('subscriber-tier-key-123', {
|
|
44
|
-
* limit: 1000,
|
|
45
|
-
* windowMs: 60_000,
|
|
46
|
-
* });
|
|
47
|
-
* ```
|
|
48
|
-
*
|
|
49
|
-
* **Viselkedes:**
|
|
50
|
-
* - Sliding-window algoritmus: minden request egy timestamp; a window-on
|
|
51
|
-
* kivuli timestamp-ek nem szamolnak. Tobb pontos mint a fix-bucket
|
|
52
|
-
* (boundary-burst nincs).
|
|
53
|
-
* - In-memory storage — single-instance MVP-nek megfelelo. Multi-instance
|
|
54
|
-
* prod-hoz Redis-backed extension kell (lasd a kozelebb dokumentumaltot).
|
|
55
|
-
* - Limit lepes: 429 DyFM_Error + `X-RateLimit-*` + `Retry-After` header-ek.
|
|
56
|
-
*
|
|
57
|
-
* **Storage:** `Map<storageKey, timestamps[]>` ahol storageKey = `${subject}|${endpoint}`.
|
|
58
|
-
* Periodikus GC takaritja a inaktiv kulcsokat.
|
|
59
|
-
*
|
|
60
|
-
* **Singleton:** `getInstance()`-szel hivd. A `.check` mezo binding-elve van
|
|
61
|
-
* `this`-re, igy direkt atadhato `preProcesses`-be ujracsomagolas nelkul.
|
|
62
|
-
*/
|
|
16
|
+
/** Atomic HTTP rate-limit boundary with an explicit shared-store production gate. */
|
|
63
17
|
class DyNTS_RateLimit_Middleware extends singleton_service_base_1.DyNTS_SingletonServiceBase {
|
|
64
|
-
static getInstance() {
|
|
65
|
-
return DyNTS_RateLimit_Middleware.getSingletonInstance();
|
|
66
|
-
}
|
|
67
18
|
defaultLimit = DEFAULT_LIMIT;
|
|
68
19
|
defaultWindowMs = DEFAULT_WINDOW_MS;
|
|
69
|
-
|
|
70
|
-
|
|
20
|
+
isResponseHeadersEnabled = DEFAULT_RESPONSE_HEADERS;
|
|
21
|
+
isSharedStoreRequired = false;
|
|
22
|
+
isKeyExtractorExplicit = false;
|
|
23
|
+
isEndpointGrouperExplicit = false;
|
|
24
|
+
keyExtractor = (req) => req.ip ?? 'unknown';
|
|
71
25
|
endpointGrouper = (req) => req.path;
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
26
|
+
costExtractor = (req) => {
|
|
27
|
+
void req;
|
|
28
|
+
return 1;
|
|
29
|
+
};
|
|
30
|
+
clock = () => Date.now();
|
|
31
|
+
store = new rate_limit_in_memory_store_1.DyNTS_RateLimit_InMemoryStore();
|
|
75
32
|
keyPolicies = new Map();
|
|
76
|
-
/** Per-endpoint(-csoport) egyedi policy-k (a `endpointGrouper` outputjara kulcsolva). */
|
|
77
33
|
endpointPolicies = new Map();
|
|
78
|
-
/**
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
*/
|
|
34
|
+
/** Returns the process-wide rate-limit middleware. */
|
|
35
|
+
static getInstance() {
|
|
36
|
+
return DyNTS_RateLimit_Middleware.getSingletonInstance();
|
|
37
|
+
}
|
|
38
|
+
/** Applies a validated configuration atomically; invalid settings do not partially mutate the service. */
|
|
84
39
|
configure(config) {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
}
|
|
100
|
-
if (config.initialKeyPolicies !== undefined) {
|
|
101
|
-
for (const [key, policy] of Object.entries(config.initialKeyPolicies)) {
|
|
102
|
-
this.keyPolicies.set(key, policy);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
if (config.initialEndpointPolicies !== undefined) {
|
|
106
|
-
for (const [endpoint, policy] of Object.entries(config.initialEndpointPolicies)) {
|
|
107
|
-
this.endpointPolicies.set(endpoint, policy);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
this.startGcTimer();
|
|
40
|
+
const snapshot = this.buildConfigSnapshot(config);
|
|
41
|
+
this.defaultLimit = snapshot.defaultLimit;
|
|
42
|
+
this.defaultWindowMs = snapshot.defaultWindowMs;
|
|
43
|
+
this.isResponseHeadersEnabled = snapshot.isResponseHeadersEnabled;
|
|
44
|
+
this.isSharedStoreRequired = snapshot.isSharedStoreRequired;
|
|
45
|
+
this.keyExtractor = snapshot.keyExtractor;
|
|
46
|
+
this.endpointGrouper = snapshot.endpointGrouper;
|
|
47
|
+
this.costExtractor = snapshot.costExtractor;
|
|
48
|
+
this.clock = snapshot.clock;
|
|
49
|
+
this.store = snapshot.store;
|
|
50
|
+
this.isKeyExtractorExplicit = snapshot.isKeyExtractorExplicit;
|
|
51
|
+
this.isEndpointGrouperExplicit = snapshot.isEndpointGrouperExplicit;
|
|
52
|
+
this.keyPolicies = snapshot.keyPolicies;
|
|
53
|
+
this.endpointPolicies = snapshot.endpointPolicies;
|
|
111
54
|
}
|
|
112
|
-
/**
|
|
113
|
-
* Aktualis konfig olvasasa (diagnosztika celokra).
|
|
114
|
-
*/
|
|
55
|
+
/** Returns content-free configuration diagnostics. */
|
|
115
56
|
getConfig() {
|
|
116
57
|
return {
|
|
117
58
|
defaultLimit: this.defaultLimit,
|
|
118
59
|
defaultWindowMs: this.defaultWindowMs,
|
|
119
|
-
responseHeaders: this.
|
|
60
|
+
responseHeaders: this.isResponseHeadersEnabled,
|
|
120
61
|
activeKeyPolicies: this.keyPolicies.size,
|
|
121
62
|
activeEndpointPolicies: this.endpointPolicies.size,
|
|
122
|
-
trackedStorageKeys: this.
|
|
63
|
+
trackedStorageKeys: this.store instanceof rate_limit_in_memory_store_1.DyNTS_RateLimit_InMemoryStore
|
|
64
|
+
? this.store.getTrackedBucketCount()
|
|
65
|
+
: 0,
|
|
66
|
+
storeKind: this.store.storeKind,
|
|
67
|
+
requireSharedStore: this.isSharedStoreRequired,
|
|
123
68
|
};
|
|
124
69
|
}
|
|
125
|
-
/**
|
|
126
|
-
* Per-kulcs egyedi policy beallitas (pl. subscription-tier alapjan).
|
|
127
|
-
* A `key`-nek pontosan azzal a stringgel kell egyeznie, amit a `keyExtractor`
|
|
128
|
-
* visszaad.
|
|
129
|
-
*/
|
|
70
|
+
/** Sets one validated subject policy. */
|
|
130
71
|
setPolicyForKey(key, policy) {
|
|
131
|
-
this.
|
|
72
|
+
this.assertBucketPart(key);
|
|
73
|
+
this.assertPolicy(policy);
|
|
74
|
+
this.keyPolicies.set(key, { ...policy });
|
|
132
75
|
}
|
|
133
|
-
/**
|
|
134
|
-
* Per-kulcs policy torlese (visszaall a default-ra).
|
|
135
|
-
*/
|
|
76
|
+
/** Removes one subject policy. */
|
|
136
77
|
clearPolicyForKey(key) {
|
|
137
78
|
this.keyPolicies.delete(key);
|
|
138
79
|
}
|
|
139
|
-
/**
|
|
140
|
-
* Per-endpoint(-csoport) egyedi policy beallitas. Az `endpoint`-nek pontosan
|
|
141
|
-
* azzal a stringgel kell egyeznie, amit az `endpointGrouper` visszaad (default: `req.path`).
|
|
142
|
-
* Akkor hasznos, ha egy endpointnak a globalis default-tol eltero limit kell — pl. egy
|
|
143
|
-
* webhook nagy burst-toleranciat igenyel (a legit, distributed forgalom ne bukjon), mig
|
|
144
|
-
* egy admin-endpoint szuk limitet. Precedencia: per-kulcs policy > per-endpoint policy > default.
|
|
145
|
-
*/
|
|
80
|
+
/** Sets one validated endpoint-group policy. */
|
|
146
81
|
setPolicyForEndpoint(endpoint, policy) {
|
|
147
|
-
this.
|
|
82
|
+
this.assertBucketPart(endpoint);
|
|
83
|
+
this.assertPolicy(policy);
|
|
84
|
+
this.endpointPolicies.set(endpoint, { ...policy });
|
|
148
85
|
}
|
|
149
|
-
/**
|
|
150
|
-
* Per-endpoint policy torlese (visszaall a default-ra).
|
|
151
|
-
*/
|
|
86
|
+
/** Removes one endpoint-group policy. */
|
|
152
87
|
clearPolicyForEndpoint(endpoint) {
|
|
153
88
|
this.endpointPolicies.delete(endpoint);
|
|
154
89
|
}
|
|
155
|
-
/**
|
|
156
|
-
* Pre-process function — atadhato `DyNTS_Endpoint_Params.preProcesses`-be.
|
|
157
|
-
*
|
|
158
|
-
* Throws:
|
|
159
|
-
* - 429 ha az aktualis request meghaladna a limit-et a sliding window-on
|
|
160
|
-
*
|
|
161
|
-
* Side-effect: ha `responseHeaders === true`, beallitja az `X-RateLimit-Limit`,
|
|
162
|
-
* `X-RateLimit-Remaining`, `X-RateLimit-Reset` header-eket; 429 eseten
|
|
163
|
-
* a `Retry-After` header-t is.
|
|
164
|
-
*/
|
|
90
|
+
/** Atomically consumes one request permit or raises a stable content-free error. */
|
|
165
91
|
check = async (req, res) => {
|
|
166
|
-
const subject = this.keyExtractor
|
|
167
|
-
const endpoint = this.endpointGrouper
|
|
168
|
-
const
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
this.
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
const oldest = recent[0];
|
|
183
|
-
const resetAt = oldest + policy.windowMs;
|
|
184
|
-
const retryAfterSec = Math.max(1, Math.ceil((resetAt - now) / 1000));
|
|
185
|
-
if (this.responseHeaders) {
|
|
186
|
-
res.setHeader('X-RateLimit-Limit', policy.limit.toString());
|
|
187
|
-
res.setHeader('X-RateLimit-Remaining', '0');
|
|
188
|
-
res.setHeader('X-RateLimit-Reset', Math.ceil(resetAt / 1000).toString());
|
|
189
|
-
res.setHeader('Retry-After', retryAfterSec.toString());
|
|
190
|
-
}
|
|
191
|
-
// Frissitjuk a log-ot a kiszurt verzioval (felesleges regi timestamp-eket eldobtuk)
|
|
192
|
-
this.requestLog.set(storageKey, recent);
|
|
193
|
-
throw new fsm_dynamo_1.DyFM_Error({
|
|
194
|
-
status: 429,
|
|
195
|
-
errorCode: buildErrorCode('LIMIT'),
|
|
196
|
-
addECToUserMsg: true,
|
|
197
|
-
message: `Rate limit exceeded: ${policy.limit} req per ${policy.windowMs}ms for ${storageKey}`,
|
|
198
|
-
userMessage: `Too many requests, retry after ${retryAfterSec}s`,
|
|
199
|
-
issuerService: SERVICE_NAME,
|
|
92
|
+
const subject = this.extractBucketPart(this.keyExtractor, req);
|
|
93
|
+
const endpoint = this.extractBucketPart(this.endpointGrouper, req);
|
|
94
|
+
const cost = this.extractCost(req);
|
|
95
|
+
const policy = this.keyPolicies.get(subject)
|
|
96
|
+
?? this.endpointPolicies.get(endpoint)
|
|
97
|
+
?? { limit: this.defaultLimit, windowMs: this.defaultWindowMs };
|
|
98
|
+
this.assertPolicy(policy);
|
|
99
|
+
this.assertCost(cost, policy);
|
|
100
|
+
const now = this.getNow();
|
|
101
|
+
let result;
|
|
102
|
+
try {
|
|
103
|
+
result = await this.store.consume({
|
|
104
|
+
bucketKey: this.buildBucketKey(subject, endpoint),
|
|
105
|
+
policy: policy,
|
|
106
|
+
cost: cost,
|
|
107
|
+
now: now,
|
|
200
108
|
});
|
|
201
109
|
}
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
110
|
+
catch {
|
|
111
|
+
this.throwRateLimitError({
|
|
112
|
+
status: 503,
|
|
113
|
+
subcode: 'STORE_UNAVAILABLE',
|
|
114
|
+
message: 'The configured rate-limit store could not commit a permit decision.',
|
|
115
|
+
userMessage: 'Request protection is temporarily unavailable. Please try again later.',
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
this.assertStoreResult(result, policy, now);
|
|
119
|
+
this.setResponseHeaders(res, {
|
|
120
|
+
policy: policy,
|
|
121
|
+
result: result,
|
|
122
|
+
now: now,
|
|
123
|
+
});
|
|
124
|
+
if (!result.allowed) {
|
|
125
|
+
const retryAfterSeconds = this.getRetryAfterSeconds(result.resetAt, now);
|
|
126
|
+
this.throwRateLimitError({
|
|
127
|
+
status: 429,
|
|
128
|
+
subcode: 'LIMIT',
|
|
129
|
+
message: 'The rate-limit policy rejected a request.',
|
|
130
|
+
userMessage: `Too many requests. Retry after ${retryAfterSeconds} seconds.`,
|
|
131
|
+
});
|
|
209
132
|
}
|
|
210
133
|
};
|
|
211
|
-
/**
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
defaultKeyExtractor(req) {
|
|
216
|
-
const xff = req.headers['x-forwarded-for'];
|
|
217
|
-
if (typeof xff === 'string' && xff.length > 0) {
|
|
218
|
-
const first = xff.split(',')[0]?.trim() ?? '';
|
|
219
|
-
if (first.length > 0) {
|
|
220
|
-
return first;
|
|
221
|
-
}
|
|
134
|
+
/** Performs explicit opportunistic cleanup only for the local test/development store. */
|
|
135
|
+
runGc() {
|
|
136
|
+
if (this.store instanceof rate_limit_in_memory_store_1.DyNTS_RateLimit_InMemoryStore) {
|
|
137
|
+
this.store.cleanup(this.clock());
|
|
222
138
|
}
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
139
|
+
}
|
|
140
|
+
/** Backward-compatible no-op: this implementation never starts a timer. */
|
|
141
|
+
stopGcTimer() { }
|
|
142
|
+
/** Resets all state to deterministic local defaults for tests. */
|
|
143
|
+
_resetForTesting() {
|
|
144
|
+
this.defaultLimit = DEFAULT_LIMIT;
|
|
145
|
+
this.defaultWindowMs = DEFAULT_WINDOW_MS;
|
|
146
|
+
this.isResponseHeadersEnabled = DEFAULT_RESPONSE_HEADERS;
|
|
147
|
+
this.isSharedStoreRequired = false;
|
|
148
|
+
this.isKeyExtractorExplicit = false;
|
|
149
|
+
this.isEndpointGrouperExplicit = false;
|
|
150
|
+
this.keyExtractor = (req) => req.ip ?? 'unknown';
|
|
151
|
+
this.endpointGrouper = (req) => req.path;
|
|
152
|
+
this.costExtractor = (req) => {
|
|
153
|
+
void req;
|
|
154
|
+
return 1;
|
|
155
|
+
};
|
|
156
|
+
this.clock = () => Date.now();
|
|
157
|
+
this.store = new rate_limit_in_memory_store_1.DyNTS_RateLimit_InMemoryStore();
|
|
158
|
+
this.keyPolicies.clear();
|
|
159
|
+
this.endpointPolicies.clear();
|
|
160
|
+
}
|
|
161
|
+
buildConfigSnapshot(config) {
|
|
162
|
+
const defaultLimit = config.defaultLimit ?? this.defaultLimit;
|
|
163
|
+
const defaultWindowMs = config.defaultWindowMs ?? this.defaultWindowMs;
|
|
164
|
+
const store = config.store ?? this.store;
|
|
165
|
+
const isSharedStoreRequired = config.requireSharedStore ?? this.isSharedStoreRequired;
|
|
166
|
+
const isKeyExtractorExplicit = config.keyExtractor !== undefined || this.isKeyExtractorExplicit;
|
|
167
|
+
const isEndpointGrouperExplicit = config.endpointGrouper !== undefined
|
|
168
|
+
|| this.isEndpointGrouperExplicit;
|
|
169
|
+
const keyPolicies = new Map(this.keyPolicies);
|
|
170
|
+
const endpointPolicies = new Map(this.endpointPolicies);
|
|
171
|
+
this.assertPolicy({ limit: defaultLimit, windowMs: defaultWindowMs });
|
|
172
|
+
this.assertConfigurationFunctions(config);
|
|
173
|
+
this.assertStore(store);
|
|
174
|
+
this.addInitialPolicies(keyPolicies, config.initialKeyPolicies);
|
|
175
|
+
this.addInitialPolicies(endpointPolicies, config.initialEndpointPolicies);
|
|
176
|
+
if (isSharedStoreRequired
|
|
177
|
+
&& (store.storeKind !== rate_limit_store_kind_type_enum_1.DyNTS_RateLimitStoreKind_Type.shared
|
|
178
|
+
|| !isKeyExtractorExplicit
|
|
179
|
+
|| !isEndpointGrouperExplicit)) {
|
|
180
|
+
this.throwRateLimitError({
|
|
181
|
+
status: 500,
|
|
182
|
+
subcode: 'SHARED_STORE_REQUIRED',
|
|
183
|
+
message: 'Production rate limiting requires a shared store and explicit opaque bucket extractors.',
|
|
184
|
+
userMessage: 'Request protection is not configured.',
|
|
185
|
+
});
|
|
228
186
|
}
|
|
229
|
-
return
|
|
187
|
+
return {
|
|
188
|
+
defaultLimit: defaultLimit,
|
|
189
|
+
defaultWindowMs: defaultWindowMs,
|
|
190
|
+
isResponseHeadersEnabled: config.responseHeaders ?? this.isResponseHeadersEnabled,
|
|
191
|
+
isSharedStoreRequired: isSharedStoreRequired,
|
|
192
|
+
keyExtractor: config.keyExtractor ?? this.keyExtractor,
|
|
193
|
+
endpointGrouper: config.endpointGrouper ?? this.endpointGrouper,
|
|
194
|
+
costExtractor: config.costExtractor ?? this.costExtractor,
|
|
195
|
+
clock: config.clock ?? this.clock,
|
|
196
|
+
store: store,
|
|
197
|
+
isKeyExtractorExplicit: isKeyExtractorExplicit,
|
|
198
|
+
isEndpointGrouperExplicit: isEndpointGrouperExplicit,
|
|
199
|
+
keyPolicies: keyPolicies,
|
|
200
|
+
endpointPolicies: endpointPolicies,
|
|
201
|
+
};
|
|
230
202
|
}
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
* storage-key-eket a request-log-bol — memory-leak prevention.
|
|
234
|
-
*/
|
|
235
|
-
startGcTimer() {
|
|
236
|
-
if (this.gcTimer !== null) {
|
|
203
|
+
addInitialPolicies(target, policies) {
|
|
204
|
+
if (!policies) {
|
|
237
205
|
return;
|
|
238
206
|
}
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
207
|
+
for (const entry of Object.entries(policies)) {
|
|
208
|
+
const key = entry[0];
|
|
209
|
+
const policy = entry[1];
|
|
210
|
+
this.assertBucketPart(key);
|
|
211
|
+
this.assertPolicy(policy);
|
|
212
|
+
target.set(key, { ...policy });
|
|
245
213
|
}
|
|
246
214
|
}
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
215
|
+
assertPolicy(policy) {
|
|
216
|
+
if (!Number.isSafeInteger(policy.limit)
|
|
217
|
+
|| policy.limit <= 0
|
|
218
|
+
|| policy.limit > MAX_LIMIT
|
|
219
|
+
|| !Number.isSafeInteger(policy.windowMs)
|
|
220
|
+
|| policy.windowMs <= 0
|
|
221
|
+
|| policy.windowMs > MAX_WINDOW_MS) {
|
|
222
|
+
this.throwRateLimitError({
|
|
223
|
+
status: 500,
|
|
224
|
+
subcode: 'POLICY_INVALID',
|
|
225
|
+
message: 'A rate-limit policy is outside the supported safety bounds.',
|
|
226
|
+
userMessage: 'Request protection is not configured.',
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
assertCost(cost, policy) {
|
|
231
|
+
if (!Number.isSafeInteger(cost) || cost <= 0 || cost > policy.limit) {
|
|
232
|
+
this.throwRateLimitError({
|
|
233
|
+
status: 500,
|
|
234
|
+
subcode: 'COST_INVALID',
|
|
235
|
+
message: 'A rate-limit request cost is outside the active policy.',
|
|
236
|
+
userMessage: 'Request protection is not configured.',
|
|
237
|
+
});
|
|
262
238
|
}
|
|
263
239
|
}
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
240
|
+
assertConfigurationFunctions(config) {
|
|
241
|
+
const hasInvalidFunction = (config.keyExtractor !== undefined && typeof config.keyExtractor !== 'function') || (config.endpointGrouper !== undefined && typeof config.endpointGrouper !== 'function') || (config.costExtractor !== undefined && typeof config.costExtractor !== 'function') || (config.clock !== undefined && typeof config.clock !== 'function');
|
|
242
|
+
if (hasInvalidFunction) {
|
|
243
|
+
this.throwRateLimitError({
|
|
244
|
+
status: 500,
|
|
245
|
+
subcode: 'CONFIG_INVALID',
|
|
246
|
+
message: 'A rate-limit configuration callback is invalid.',
|
|
247
|
+
userMessage: 'Request protection is not configured.',
|
|
248
|
+
});
|
|
271
249
|
}
|
|
272
250
|
}
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
251
|
+
assertStore(store) {
|
|
252
|
+
const hasKnownKind = store?.storeKind === rate_limit_store_kind_type_enum_1.DyNTS_RateLimitStoreKind_Type.inMemory
|
|
253
|
+
|| store?.storeKind === rate_limit_store_kind_type_enum_1.DyNTS_RateLimitStoreKind_Type.shared;
|
|
254
|
+
if (!hasKnownKind || typeof store.consume !== 'function') {
|
|
255
|
+
this.throwRateLimitError({
|
|
256
|
+
status: 500,
|
|
257
|
+
subcode: 'STORE_INVALID',
|
|
258
|
+
message: 'The configured rate-limit store does not satisfy the Bedrock contract.',
|
|
259
|
+
userMessage: 'Request protection is not configured.',
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
extractBucketPart(extractor, req) {
|
|
264
|
+
let value;
|
|
265
|
+
try {
|
|
266
|
+
value = extractor(req);
|
|
267
|
+
}
|
|
268
|
+
catch {
|
|
269
|
+
value = undefined;
|
|
270
|
+
}
|
|
271
|
+
if (typeof value !== 'string') {
|
|
272
|
+
this.throwBucketKeyInvalid();
|
|
273
|
+
}
|
|
274
|
+
const normalized = value.trim();
|
|
275
|
+
this.assertBucketPart(normalized);
|
|
276
|
+
return normalized;
|
|
277
|
+
}
|
|
278
|
+
extractCost(req) {
|
|
279
|
+
try {
|
|
280
|
+
return this.costExtractor(req);
|
|
281
|
+
}
|
|
282
|
+
catch {
|
|
283
|
+
this.throwRateLimitError({
|
|
284
|
+
status: 500,
|
|
285
|
+
subcode: 'COST_INVALID',
|
|
286
|
+
message: 'The rate-limit request cost could not be derived.',
|
|
287
|
+
userMessage: 'Request protection is not configured.',
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
buildBucketKey(subject, endpoint) {
|
|
292
|
+
return `${subject.length}:${subject}${endpoint.length}:${endpoint}`;
|
|
293
|
+
}
|
|
294
|
+
getNow() {
|
|
295
|
+
let now;
|
|
296
|
+
try {
|
|
297
|
+
now = this.clock();
|
|
298
|
+
}
|
|
299
|
+
catch {
|
|
300
|
+
now = undefined;
|
|
301
|
+
}
|
|
302
|
+
if (!Number.isSafeInteger(now) || typeof now !== 'number' || now < 0) {
|
|
303
|
+
this.throwRateLimitError({
|
|
304
|
+
status: 503,
|
|
305
|
+
subcode: 'CLOCK_INVALID',
|
|
306
|
+
message: 'The rate-limit clock returned an invalid timestamp.',
|
|
307
|
+
userMessage: 'Request protection is temporarily unavailable. Please try again later.',
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
return now;
|
|
311
|
+
}
|
|
312
|
+
assertBucketPart(value) {
|
|
313
|
+
if (!value || value.length > MAX_BUCKET_PART_LENGTH) {
|
|
314
|
+
this.throwBucketKeyInvalid();
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
throwBucketKeyInvalid() {
|
|
318
|
+
this.throwRateLimitError({
|
|
319
|
+
status: 500,
|
|
320
|
+
subcode: 'BUCKET_KEY_INVALID',
|
|
321
|
+
message: 'A rate-limit bucket component is missing, invalid or too large.',
|
|
322
|
+
userMessage: 'Request protection is not configured.',
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
assertStoreResult(result, policy, now) {
|
|
326
|
+
const isValid = typeof result === 'object'
|
|
327
|
+
&& result !== null
|
|
328
|
+
&& 'allowed' in result
|
|
329
|
+
&& 'remaining' in result
|
|
330
|
+
&& 'resetAt' in result
|
|
331
|
+
&& typeof result.allowed === 'boolean'
|
|
332
|
+
&& typeof result.remaining === 'number'
|
|
333
|
+
&& Number.isSafeInteger(result.remaining)
|
|
334
|
+
&& result.remaining >= 0
|
|
335
|
+
&& result.remaining <= policy.limit
|
|
336
|
+
&& typeof result.resetAt === 'number'
|
|
337
|
+
&& Number.isSafeInteger(result.resetAt)
|
|
338
|
+
&& result.resetAt >= now;
|
|
339
|
+
if (!isValid) {
|
|
340
|
+
this.throwRateLimitError({
|
|
341
|
+
status: 503,
|
|
342
|
+
subcode: 'STORE_RESULT_INVALID',
|
|
343
|
+
message: 'The rate-limit store returned an invalid permit decision.',
|
|
344
|
+
userMessage: 'Request protection is temporarily unavailable. Please try again later.',
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
setResponseHeaders(res, context) {
|
|
349
|
+
if (!this.isResponseHeadersEnabled) {
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
const resetSeconds = Math.ceil(context.result.resetAt / 1_000).toString();
|
|
353
|
+
res.setHeader('X-RateLimit-Limit', context.policy.limit.toString());
|
|
354
|
+
res.setHeader('X-RateLimit-Remaining', context.result.remaining.toString());
|
|
355
|
+
res.setHeader('X-RateLimit-Reset', resetSeconds);
|
|
356
|
+
res.setHeader('RateLimit-Limit', context.policy.limit.toString());
|
|
357
|
+
res.setHeader('RateLimit-Remaining', context.result.remaining.toString());
|
|
358
|
+
res.setHeader('RateLimit-Reset', Math.max(0, Math.ceil((context.result.resetAt - context.now) / 1_000)).toString());
|
|
359
|
+
if (!context.result.allowed) {
|
|
360
|
+
res.setHeader('Retry-After', this.getRetryAfterSeconds(context.result.resetAt, context.now).toString());
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
getRetryAfterSeconds(resetAt, now) {
|
|
364
|
+
return Math.max(1, Math.ceil((resetAt - now) / 1_000));
|
|
365
|
+
}
|
|
366
|
+
throwRateLimitError(context) {
|
|
367
|
+
const systemShortCode = global_settings_const_1.DyNTS_global_settings.systemShortCodeName ?? 'DyNTS';
|
|
368
|
+
throw new fsm_dynamo_1.DyFM_Error({
|
|
369
|
+
status: context.status,
|
|
370
|
+
errorCode: `${systemShortCode}|DyNTS-RL-${context.subcode}`,
|
|
371
|
+
addECToUserMsg: true,
|
|
372
|
+
message: context.message,
|
|
373
|
+
userMessage: context.userMessage,
|
|
374
|
+
issuerService: SERVICE_NAME,
|
|
375
|
+
});
|
|
287
376
|
}
|
|
288
377
|
}
|
|
289
378
|
exports.DyNTS_RateLimit_Middleware = DyNTS_RateLimit_Middleware;
|