@futdevpro/nts-dynamo 1.15.145 → 1.15.150
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 +1665 -1803
- 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/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 +1 -1
- 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,342 +2,503 @@ import { Request, Response } from 'express';
|
|
|
2
2
|
|
|
3
3
|
import { DyFM_Error } from '@futdevpro/fsm-dynamo';
|
|
4
4
|
|
|
5
|
-
import { DyNTS_SingletonServiceBase } from '../../_services/base/singleton.service-base';
|
|
6
5
|
import { DyNTS_global_settings } from '../../_collections/global-settings.const';
|
|
7
|
-
|
|
6
|
+
import { DyNTS_SingletonServiceBase } from '../../_services/base/singleton.service-base';
|
|
7
|
+
import { DyNTS_RateLimitStoreKind_Type } from './_enums/rate-limit-store-kind.type-enum';
|
|
8
8
|
import { DyNTS_RateLimit_Config } from './_models/rate-limit-config.interface';
|
|
9
|
+
import { DyNTS_RateLimit_ConsumeResult_Interface } from './_models/rate-limit-consume-result.interface';
|
|
9
10
|
import { DyNTS_RateLimit_Policy } from './_models/rate-limit-policy.interface';
|
|
11
|
+
import { DyNTS_RateLimit_Store_Interface } from './_models/rate-limit-store.interface';
|
|
12
|
+
import { DyNTS_RateLimit_InMemoryStore } from './rate-limit-in-memory.store';
|
|
10
13
|
|
|
11
|
-
|
|
12
|
-
/** Default request-limit per default-window. */
|
|
13
14
|
const DEFAULT_LIMIT: number = 100;
|
|
14
|
-
|
|
15
|
-
/** Default sliding-window hossza ms-ben (1 perc). */
|
|
16
|
-
const DEFAULT_WINDOW_MS: number = 60000;
|
|
17
|
-
|
|
18
|
-
/** Default response-header allitas. */
|
|
15
|
+
const DEFAULT_WINDOW_MS: number = 60_000;
|
|
19
16
|
const DEFAULT_RESPONSE_HEADERS: boolean = true;
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
*/
|
|
24
|
-
const GC_INTERVAL_MS: number = 5 * 60 * 1000;
|
|
25
|
-
|
|
26
|
-
/** Service-nev az error-okhoz. */
|
|
17
|
+
const MAX_LIMIT: number = 1_000_000;
|
|
18
|
+
const MAX_WINDOW_MS: number = 7 * 24 * 60 * 60 * 1_000;
|
|
19
|
+
const MAX_BUCKET_PART_LENGTH: number = 240;
|
|
27
20
|
const SERVICE_NAME: string = 'DyNTS_RateLimit_Middleware';
|
|
28
21
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Sliding-window HTTP rate-limit middleware — opt-in service-szel, a meglevo
|
|
38
|
-
* `DyNTS_Endpoint_Params.preProcesses` mechanizmus mellol mukodik.
|
|
39
|
-
*
|
|
40
|
-
* **Hasznalat (host app):**
|
|
41
|
-
* ```ts
|
|
42
|
-
* const rateLimit = DyNTS_RateLimit_Middleware.getInstance();
|
|
43
|
-
* rateLimit.configure({
|
|
44
|
-
* defaultLimit: 100, // 100 req/perc default
|
|
45
|
-
* defaultWindowMs: 60_000,
|
|
46
|
-
* keyExtractor: (req) => req.headers['x-api-key'] as string || req.ip,
|
|
47
|
-
* });
|
|
48
|
-
*
|
|
49
|
-
* new DyNTS_Endpoint_Params({
|
|
50
|
-
* ...,
|
|
51
|
-
* preProcesses: [rateLimit.check, ...other],
|
|
52
|
-
* });
|
|
53
|
-
*
|
|
54
|
-
* // subscription-tier-up: per-kulcs egyedi limit
|
|
55
|
-
* rateLimit.setPolicyForKey('subscriber-tier-key-123', {
|
|
56
|
-
* limit: 1000,
|
|
57
|
-
* windowMs: 60_000,
|
|
58
|
-
* });
|
|
59
|
-
* ```
|
|
60
|
-
*
|
|
61
|
-
* **Viselkedes:**
|
|
62
|
-
* - Sliding-window algoritmus: minden request egy timestamp; a window-on
|
|
63
|
-
* kivuli timestamp-ek nem szamolnak. Tobb pontos mint a fix-bucket
|
|
64
|
-
* (boundary-burst nincs).
|
|
65
|
-
* - In-memory storage — single-instance MVP-nek megfelelo. Multi-instance
|
|
66
|
-
* prod-hoz Redis-backed extension kell (lasd a kozelebb dokumentumaltot).
|
|
67
|
-
* - Limit lepes: 429 DyFM_Error + `X-RateLimit-*` + `Retry-After` header-ek.
|
|
68
|
-
*
|
|
69
|
-
* **Storage:** `Map<storageKey, timestamps[]>` ahol storageKey = `${subject}|${endpoint}`.
|
|
70
|
-
* Periodikus GC takaritja a inaktiv kulcsokat.
|
|
71
|
-
*
|
|
72
|
-
* **Singleton:** `getInstance()`-szel hivd. A `.check` mezo binding-elve van
|
|
73
|
-
* `this`-re, igy direkt atadhato `preProcesses`-be ujracsomagolas nelkul.
|
|
74
|
-
*/
|
|
75
|
-
export class DyNTS_RateLimit_Middleware extends DyNTS_SingletonServiceBase {
|
|
22
|
+
interface DyNTS_RateLimit_ErrorContext_Interface {
|
|
23
|
+
status: number;
|
|
24
|
+
subcode: string;
|
|
25
|
+
message: string;
|
|
26
|
+
userMessage: string;
|
|
27
|
+
}
|
|
76
28
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
29
|
+
interface DyNTS_RateLimit_HeaderContext_Interface {
|
|
30
|
+
policy: DyNTS_RateLimit_Policy;
|
|
31
|
+
result: DyNTS_RateLimit_ConsumeResult_Interface;
|
|
32
|
+
now: number;
|
|
33
|
+
}
|
|
80
34
|
|
|
35
|
+
interface DyNTS_RateLimit_ConfigSnapshot_Interface {
|
|
36
|
+
defaultLimit: number;
|
|
37
|
+
defaultWindowMs: number;
|
|
38
|
+
isResponseHeadersEnabled: boolean;
|
|
39
|
+
isSharedStoreRequired: boolean;
|
|
40
|
+
keyExtractor: (req: Request) => string;
|
|
41
|
+
endpointGrouper: (req: Request) => string;
|
|
42
|
+
costExtractor: (req: Request) => number;
|
|
43
|
+
clock: () => number;
|
|
44
|
+
store: DyNTS_RateLimit_Store_Interface;
|
|
45
|
+
isKeyExtractorExplicit: boolean;
|
|
46
|
+
isEndpointGrouperExplicit: boolean;
|
|
47
|
+
keyPolicies: Map<string, DyNTS_RateLimit_Policy>;
|
|
48
|
+
endpointPolicies: Map<string, DyNTS_RateLimit_Policy>;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Atomic HTTP rate-limit boundary with an explicit shared-store production gate. */
|
|
52
|
+
export class DyNTS_RateLimit_Middleware extends DyNTS_SingletonServiceBase {
|
|
81
53
|
private defaultLimit: number = DEFAULT_LIMIT;
|
|
82
54
|
private defaultWindowMs: number = DEFAULT_WINDOW_MS;
|
|
83
|
-
private
|
|
84
|
-
|
|
85
|
-
private
|
|
86
|
-
|
|
87
|
-
private
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
55
|
+
private isResponseHeadersEnabled: boolean = DEFAULT_RESPONSE_HEADERS;
|
|
56
|
+
private isSharedStoreRequired: boolean = false;
|
|
57
|
+
private isKeyExtractorExplicit: boolean = false;
|
|
58
|
+
private isEndpointGrouperExplicit: boolean = false;
|
|
59
|
+
private keyExtractor: (req: Request) => string = (req: Request): string => req.ip ?? 'unknown';
|
|
60
|
+
private endpointGrouper: (req: Request) => string = (req: Request): string => req.path;
|
|
61
|
+
private costExtractor: (req: Request) => number = (req: Request): number => {
|
|
62
|
+
void req;
|
|
63
|
+
|
|
64
|
+
return 1;
|
|
65
|
+
};
|
|
66
|
+
private clock: () => number = (): number => Date.now();
|
|
67
|
+
private store: DyNTS_RateLimit_Store_Interface = new DyNTS_RateLimit_InMemoryStore();
|
|
94
68
|
private keyPolicies: Map<string, DyNTS_RateLimit_Policy> = new Map();
|
|
95
|
-
|
|
96
|
-
/** Per-endpoint(-csoport) egyedi policy-k (a `endpointGrouper` outputjara kulcsolva). */
|
|
97
69
|
private endpointPolicies: Map<string, DyNTS_RateLimit_Policy> = new Map();
|
|
98
70
|
|
|
99
|
-
/**
|
|
100
|
-
|
|
101
|
-
|
|
71
|
+
/** Returns the process-wide rate-limit middleware. */
|
|
72
|
+
public static getInstance(): DyNTS_RateLimit_Middleware {
|
|
73
|
+
return DyNTS_RateLimit_Middleware.getSingletonInstance();
|
|
74
|
+
}
|
|
102
75
|
|
|
103
|
-
/**
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
if (config.responseHeaders !== undefined) {
|
|
121
|
-
this.responseHeaders = config.responseHeaders;
|
|
122
|
-
}
|
|
123
|
-
if (config.initialKeyPolicies !== undefined) {
|
|
124
|
-
for (const [ key, policy ] of Object.entries(config.initialKeyPolicies)) {
|
|
125
|
-
this.keyPolicies.set(key, policy);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
if (config.initialEndpointPolicies !== undefined) {
|
|
129
|
-
for (const [ endpoint, policy ] of Object.entries(config.initialEndpointPolicies)) {
|
|
130
|
-
this.endpointPolicies.set(endpoint, policy);
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
this.startGcTimer();
|
|
76
|
+
/** Applies a validated configuration atomically; invalid settings do not partially mutate the service. */
|
|
77
|
+
public configure(config: DyNTS_RateLimit_Config): void {
|
|
78
|
+
const snapshot: DyNTS_RateLimit_ConfigSnapshot_Interface = this.buildConfigSnapshot(config);
|
|
79
|
+
|
|
80
|
+
this.defaultLimit = snapshot.defaultLimit;
|
|
81
|
+
this.defaultWindowMs = snapshot.defaultWindowMs;
|
|
82
|
+
this.isResponseHeadersEnabled = snapshot.isResponseHeadersEnabled;
|
|
83
|
+
this.isSharedStoreRequired = snapshot.isSharedStoreRequired;
|
|
84
|
+
this.keyExtractor = snapshot.keyExtractor;
|
|
85
|
+
this.endpointGrouper = snapshot.endpointGrouper;
|
|
86
|
+
this.costExtractor = snapshot.costExtractor;
|
|
87
|
+
this.clock = snapshot.clock;
|
|
88
|
+
this.store = snapshot.store;
|
|
89
|
+
this.isKeyExtractorExplicit = snapshot.isKeyExtractorExplicit;
|
|
90
|
+
this.isEndpointGrouperExplicit = snapshot.isEndpointGrouperExplicit;
|
|
91
|
+
this.keyPolicies = snapshot.keyPolicies;
|
|
92
|
+
this.endpointPolicies = snapshot.endpointPolicies;
|
|
134
93
|
}
|
|
135
94
|
|
|
136
|
-
/**
|
|
137
|
-
|
|
138
|
-
*/
|
|
139
|
-
getConfig(): {
|
|
95
|
+
/** Returns content-free configuration diagnostics. */
|
|
96
|
+
public getConfig(): {
|
|
140
97
|
defaultLimit: number;
|
|
141
98
|
defaultWindowMs: number;
|
|
142
99
|
responseHeaders: boolean;
|
|
143
100
|
activeKeyPolicies: number;
|
|
144
101
|
activeEndpointPolicies: number;
|
|
145
102
|
trackedStorageKeys: number;
|
|
146
|
-
|
|
103
|
+
storeKind: DyNTS_RateLimitStoreKind_Type;
|
|
104
|
+
requireSharedStore: boolean;
|
|
105
|
+
} {
|
|
147
106
|
return {
|
|
148
|
-
defaultLimit:
|
|
149
|
-
defaultWindowMs:
|
|
150
|
-
responseHeaders:
|
|
151
|
-
activeKeyPolicies:
|
|
152
|
-
activeEndpointPolicies:
|
|
153
|
-
trackedStorageKeys:
|
|
107
|
+
defaultLimit: this.defaultLimit,
|
|
108
|
+
defaultWindowMs: this.defaultWindowMs,
|
|
109
|
+
responseHeaders: this.isResponseHeadersEnabled,
|
|
110
|
+
activeKeyPolicies: this.keyPolicies.size,
|
|
111
|
+
activeEndpointPolicies: this.endpointPolicies.size,
|
|
112
|
+
trackedStorageKeys: this.store instanceof DyNTS_RateLimit_InMemoryStore
|
|
113
|
+
? this.store.getTrackedBucketCount()
|
|
114
|
+
: 0,
|
|
115
|
+
storeKind: this.store.storeKind,
|
|
116
|
+
requireSharedStore: this.isSharedStoreRequired,
|
|
154
117
|
};
|
|
155
118
|
}
|
|
156
119
|
|
|
157
|
-
/**
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
setPolicyForKey(key: string, policy: DyNTS_RateLimit_Policy): void {
|
|
163
|
-
this.keyPolicies.set(key, policy);
|
|
120
|
+
/** Sets one validated subject policy. */
|
|
121
|
+
public setPolicyForKey(key: string, policy: DyNTS_RateLimit_Policy): void {
|
|
122
|
+
this.assertBucketPart(key);
|
|
123
|
+
this.assertPolicy(policy);
|
|
124
|
+
this.keyPolicies.set(key, { ...policy });
|
|
164
125
|
}
|
|
165
126
|
|
|
166
|
-
/**
|
|
167
|
-
|
|
168
|
-
*/
|
|
169
|
-
clearPolicyForKey(key: string): void {
|
|
127
|
+
/** Removes one subject policy. */
|
|
128
|
+
public clearPolicyForKey(key: string): void {
|
|
170
129
|
this.keyPolicies.delete(key);
|
|
171
130
|
}
|
|
172
131
|
|
|
173
|
-
/**
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
* egy admin-endpoint szuk limitet. Precedencia: per-kulcs policy > per-endpoint policy > default.
|
|
179
|
-
*/
|
|
180
|
-
setPolicyForEndpoint(endpoint: string, policy: DyNTS_RateLimit_Policy): void {
|
|
181
|
-
this.endpointPolicies.set(endpoint, policy);
|
|
132
|
+
/** Sets one validated endpoint-group policy. */
|
|
133
|
+
public setPolicyForEndpoint(endpoint: string, policy: DyNTS_RateLimit_Policy): void {
|
|
134
|
+
this.assertBucketPart(endpoint);
|
|
135
|
+
this.assertPolicy(policy);
|
|
136
|
+
this.endpointPolicies.set(endpoint, { ...policy });
|
|
182
137
|
}
|
|
183
138
|
|
|
184
|
-
/**
|
|
185
|
-
|
|
186
|
-
*/
|
|
187
|
-
clearPolicyForEndpoint(endpoint: string): void {
|
|
139
|
+
/** Removes one endpoint-group policy. */
|
|
140
|
+
public clearPolicyForEndpoint(endpoint: string): void {
|
|
188
141
|
this.endpointPolicies.delete(endpoint);
|
|
189
142
|
}
|
|
190
143
|
|
|
144
|
+
/** Atomically consumes one request permit or raises a stable content-free error. */
|
|
145
|
+
public readonly check = async (req: Request, res: Response): Promise<void> => {
|
|
146
|
+
const subject: string = this.extractBucketPart(this.keyExtractor, req);
|
|
147
|
+
const endpoint: string = this.extractBucketPart(this.endpointGrouper, req);
|
|
148
|
+
const cost: number = this.extractCost(req);
|
|
149
|
+
|
|
150
|
+
const policy: DyNTS_RateLimit_Policy = this.keyPolicies.get(subject)
|
|
151
|
+
?? this.endpointPolicies.get(endpoint)
|
|
152
|
+
?? { limit: this.defaultLimit, windowMs: this.defaultWindowMs };
|
|
153
|
+
|
|
154
|
+
this.assertPolicy(policy);
|
|
155
|
+
this.assertCost(cost, policy);
|
|
156
|
+
|
|
157
|
+
const now: number = this.getNow();
|
|
158
|
+
let result: unknown;
|
|
191
159
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
const storageKey: string = `${subject}|${endpoint}`;
|
|
206
|
-
|
|
207
|
-
// precedencia: per-kulcs policy (legspecifikusabb, pl. subscriber-tier) > per-endpoint policy > default
|
|
208
|
-
const policy: DyNTS_RateLimit_Policy =
|
|
209
|
-
this.keyPolicies.get(subject) ??
|
|
210
|
-
this.endpointPolicies.get(endpoint) ??
|
|
211
|
-
{
|
|
212
|
-
limit: this.defaultLimit,
|
|
213
|
-
windowMs: this.defaultWindowMs,
|
|
214
|
-
};
|
|
215
|
-
|
|
216
|
-
const now: number = Date.now();
|
|
217
|
-
const windowStart: number = now - policy.windowMs;
|
|
218
|
-
|
|
219
|
-
// sliding-window: tartomanyon kivuli timestamp-eket eldobjuk
|
|
220
|
-
const existing: number[] = this.requestLog.get(storageKey) ?? [];
|
|
221
|
-
const recent: number[] = existing.filter((t: number): boolean => t > windowStart);
|
|
222
|
-
|
|
223
|
-
if (recent.length >= policy.limit) {
|
|
224
|
-
const oldest: number = recent[0];
|
|
225
|
-
const resetAt: number = oldest + policy.windowMs;
|
|
226
|
-
const retryAfterSec: number = Math.max(1, Math.ceil((resetAt - now) / 1000));
|
|
227
|
-
|
|
228
|
-
if (this.responseHeaders) {
|
|
229
|
-
res.setHeader('X-RateLimit-Limit', policy.limit.toString());
|
|
230
|
-
res.setHeader('X-RateLimit-Remaining', '0');
|
|
231
|
-
res.setHeader('X-RateLimit-Reset', Math.ceil(resetAt / 1000).toString());
|
|
232
|
-
res.setHeader('Retry-After', retryAfterSec.toString());
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
// Frissitjuk a log-ot a kiszurt verzioval (felesleges regi timestamp-eket eldobtuk)
|
|
236
|
-
this.requestLog.set(storageKey, recent);
|
|
237
|
-
|
|
238
|
-
throw new DyFM_Error({
|
|
239
|
-
status: 429,
|
|
240
|
-
errorCode: buildErrorCode('LIMIT'),
|
|
241
|
-
addECToUserMsg: true,
|
|
242
|
-
message: `Rate limit exceeded: ${policy.limit} req per ${policy.windowMs}ms for ${storageKey}`,
|
|
243
|
-
userMessage: `Too many requests, retry after ${retryAfterSec}s`,
|
|
244
|
-
issuerService: SERVICE_NAME,
|
|
160
|
+
try {
|
|
161
|
+
result = await this.store.consume({
|
|
162
|
+
bucketKey: this.buildBucketKey(subject, endpoint),
|
|
163
|
+
policy: policy,
|
|
164
|
+
cost: cost,
|
|
165
|
+
now: now,
|
|
166
|
+
});
|
|
167
|
+
} catch {
|
|
168
|
+
this.throwRateLimitError({
|
|
169
|
+
status: 503,
|
|
170
|
+
subcode: 'STORE_UNAVAILABLE',
|
|
171
|
+
message: 'The configured rate-limit store could not commit a permit decision.',
|
|
172
|
+
userMessage: 'Request protection is temporarily unavailable. Please try again later.',
|
|
245
173
|
});
|
|
246
174
|
}
|
|
247
175
|
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
176
|
+
this.assertStoreResult(result, policy, now);
|
|
177
|
+
this.setResponseHeaders(res, {
|
|
178
|
+
policy: policy,
|
|
179
|
+
result: result,
|
|
180
|
+
now: now,
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
if (!result.allowed) {
|
|
184
|
+
const retryAfterSeconds: number = this.getRetryAfterSeconds(result.resetAt, now);
|
|
185
|
+
|
|
186
|
+
this.throwRateLimitError({
|
|
187
|
+
status: 429,
|
|
188
|
+
subcode: 'LIMIT',
|
|
189
|
+
message: 'The rate-limit policy rejected a request.',
|
|
190
|
+
userMessage: `Too many requests. Retry after ${retryAfterSeconds} seconds.`,
|
|
191
|
+
});
|
|
256
192
|
}
|
|
257
193
|
};
|
|
258
194
|
|
|
195
|
+
/** Performs explicit opportunistic cleanup only for the local test/development store. */
|
|
196
|
+
public runGc(): void {
|
|
197
|
+
if (this.store instanceof DyNTS_RateLimit_InMemoryStore) {
|
|
198
|
+
this.store.cleanup(this.clock());
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** Backward-compatible no-op: this implementation never starts a timer. */
|
|
203
|
+
public stopGcTimer(): void {}
|
|
204
|
+
|
|
205
|
+
/** Resets all state to deterministic local defaults for tests. */
|
|
206
|
+
public _resetForTesting(): void {
|
|
207
|
+
this.defaultLimit = DEFAULT_LIMIT;
|
|
208
|
+
this.defaultWindowMs = DEFAULT_WINDOW_MS;
|
|
209
|
+
this.isResponseHeadersEnabled = DEFAULT_RESPONSE_HEADERS;
|
|
210
|
+
this.isSharedStoreRequired = false;
|
|
211
|
+
this.isKeyExtractorExplicit = false;
|
|
212
|
+
this.isEndpointGrouperExplicit = false;
|
|
213
|
+
this.keyExtractor = (req: Request): string => req.ip ?? 'unknown';
|
|
214
|
+
this.endpointGrouper = (req: Request): string => req.path;
|
|
215
|
+
this.costExtractor = (req: Request): number => {
|
|
216
|
+
void req;
|
|
217
|
+
|
|
218
|
+
return 1;
|
|
219
|
+
};
|
|
220
|
+
this.clock = (): number => Date.now();
|
|
221
|
+
this.store = new DyNTS_RateLimit_InMemoryStore();
|
|
222
|
+
this.keyPolicies.clear();
|
|
223
|
+
this.endpointPolicies.clear();
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
private buildConfigSnapshot(config: DyNTS_RateLimit_Config): DyNTS_RateLimit_ConfigSnapshot_Interface {
|
|
227
|
+
const defaultLimit: number = config.defaultLimit ?? this.defaultLimit;
|
|
228
|
+
const defaultWindowMs: number = config.defaultWindowMs ?? this.defaultWindowMs;
|
|
229
|
+
const store: DyNTS_RateLimit_Store_Interface = config.store ?? this.store;
|
|
230
|
+
const isSharedStoreRequired: boolean = config.requireSharedStore ?? this.isSharedStoreRequired;
|
|
231
|
+
const isKeyExtractorExplicit: boolean = config.keyExtractor !== undefined || this.isKeyExtractorExplicit;
|
|
232
|
+
const isEndpointGrouperExplicit: boolean = config.endpointGrouper !== undefined
|
|
233
|
+
|| this.isEndpointGrouperExplicit;
|
|
234
|
+
const keyPolicies: Map<string, DyNTS_RateLimit_Policy> = new Map(this.keyPolicies);
|
|
235
|
+
const endpointPolicies: Map<string, DyNTS_RateLimit_Policy> = new Map(this.endpointPolicies);
|
|
236
|
+
|
|
237
|
+
this.assertPolicy({ limit: defaultLimit, windowMs: defaultWindowMs });
|
|
238
|
+
this.assertConfigurationFunctions(config);
|
|
239
|
+
this.assertStore(store);
|
|
240
|
+
this.addInitialPolicies(keyPolicies, config.initialKeyPolicies);
|
|
241
|
+
this.addInitialPolicies(endpointPolicies, config.initialEndpointPolicies);
|
|
242
|
+
|
|
243
|
+
if (
|
|
244
|
+
isSharedStoreRequired
|
|
245
|
+
&& (
|
|
246
|
+
store.storeKind !== DyNTS_RateLimitStoreKind_Type.shared
|
|
247
|
+
|| !isKeyExtractorExplicit
|
|
248
|
+
|| !isEndpointGrouperExplicit
|
|
249
|
+
)
|
|
250
|
+
) {
|
|
251
|
+
this.throwRateLimitError({
|
|
252
|
+
status: 500,
|
|
253
|
+
subcode: 'SHARED_STORE_REQUIRED',
|
|
254
|
+
message: 'Production rate limiting requires a shared store and explicit opaque bucket extractors.',
|
|
255
|
+
userMessage: 'Request protection is not configured.',
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
return {
|
|
260
|
+
defaultLimit: defaultLimit,
|
|
261
|
+
defaultWindowMs: defaultWindowMs,
|
|
262
|
+
isResponseHeadersEnabled: config.responseHeaders ?? this.isResponseHeadersEnabled,
|
|
263
|
+
isSharedStoreRequired: isSharedStoreRequired,
|
|
264
|
+
keyExtractor: config.keyExtractor ?? this.keyExtractor,
|
|
265
|
+
endpointGrouper: config.endpointGrouper ?? this.endpointGrouper,
|
|
266
|
+
costExtractor: config.costExtractor ?? this.costExtractor,
|
|
267
|
+
clock: config.clock ?? this.clock,
|
|
268
|
+
store: store,
|
|
269
|
+
isKeyExtractorExplicit: isKeyExtractorExplicit,
|
|
270
|
+
isEndpointGrouperExplicit: isEndpointGrouperExplicit,
|
|
271
|
+
keyPolicies: keyPolicies,
|
|
272
|
+
endpointPolicies: endpointPolicies,
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
private addInitialPolicies(
|
|
277
|
+
target: Map<string, DyNTS_RateLimit_Policy>,
|
|
278
|
+
policies?: Record<string, DyNTS_RateLimit_Policy>
|
|
279
|
+
): void {
|
|
280
|
+
if (!policies) {
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
259
283
|
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
const first: string = xff.split(',')[0]?.trim() ?? '';
|
|
268
|
-
if (first.length > 0) {
|
|
269
|
-
return first;
|
|
270
|
-
}
|
|
284
|
+
for (const entry of Object.entries(policies)) {
|
|
285
|
+
const key: string = entry[0];
|
|
286
|
+
const policy: DyNTS_RateLimit_Policy = entry[1];
|
|
287
|
+
|
|
288
|
+
this.assertBucketPart(key);
|
|
289
|
+
this.assertPolicy(policy);
|
|
290
|
+
target.set(key, { ...policy });
|
|
271
291
|
}
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
private assertPolicy(policy: DyNTS_RateLimit_Policy): void {
|
|
295
|
+
if (
|
|
296
|
+
!Number.isSafeInteger(policy.limit)
|
|
297
|
+
|| policy.limit <= 0
|
|
298
|
+
|| policy.limit > MAX_LIMIT
|
|
299
|
+
|| !Number.isSafeInteger(policy.windowMs)
|
|
300
|
+
|| policy.windowMs <= 0
|
|
301
|
+
|| policy.windowMs > MAX_WINDOW_MS
|
|
302
|
+
) {
|
|
303
|
+
this.throwRateLimitError({
|
|
304
|
+
status: 500,
|
|
305
|
+
subcode: 'POLICY_INVALID',
|
|
306
|
+
message: 'A rate-limit policy is outside the supported safety bounds.',
|
|
307
|
+
userMessage: 'Request protection is not configured.',
|
|
308
|
+
});
|
|
277
309
|
}
|
|
278
|
-
return req.ip ?? 'unknown';
|
|
279
310
|
}
|
|
280
311
|
|
|
312
|
+
private assertCost(cost: number, policy: DyNTS_RateLimit_Policy): void {
|
|
313
|
+
if (!Number.isSafeInteger(cost) || cost <= 0 || cost > policy.limit) {
|
|
314
|
+
this.throwRateLimitError({
|
|
315
|
+
status: 500,
|
|
316
|
+
subcode: 'COST_INVALID',
|
|
317
|
+
message: 'A rate-limit request cost is outside the active policy.',
|
|
318
|
+
userMessage: 'Request protection is not configured.',
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
}
|
|
281
322
|
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
323
|
+
private assertConfigurationFunctions(config: DyNTS_RateLimit_Config): void {
|
|
324
|
+
const hasInvalidFunction: boolean = (
|
|
325
|
+
config.keyExtractor !== undefined && typeof config.keyExtractor !== 'function'
|
|
326
|
+
) || (
|
|
327
|
+
config.endpointGrouper !== undefined && typeof config.endpointGrouper !== 'function'
|
|
328
|
+
) || (
|
|
329
|
+
config.costExtractor !== undefined && typeof config.costExtractor !== 'function'
|
|
330
|
+
) || (
|
|
331
|
+
config.clock !== undefined && typeof config.clock !== 'function'
|
|
332
|
+
);
|
|
333
|
+
|
|
334
|
+
if (hasInvalidFunction) {
|
|
335
|
+
this.throwRateLimitError({
|
|
336
|
+
status: 500,
|
|
337
|
+
subcode: 'CONFIG_INVALID',
|
|
338
|
+
message: 'A rate-limit configuration callback is invalid.',
|
|
339
|
+
userMessage: 'Request protection is not configured.',
|
|
340
|
+
});
|
|
289
341
|
}
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
private assertStore(store: DyNTS_RateLimit_Store_Interface): void {
|
|
345
|
+
const hasKnownKind: boolean = store?.storeKind === DyNTS_RateLimitStoreKind_Type.inMemory
|
|
346
|
+
|| store?.storeKind === DyNTS_RateLimitStoreKind_Type.shared;
|
|
347
|
+
|
|
348
|
+
if (!hasKnownKind || typeof store.consume !== 'function') {
|
|
349
|
+
this.throwRateLimitError({
|
|
350
|
+
status: 500,
|
|
351
|
+
subcode: 'STORE_INVALID',
|
|
352
|
+
message: 'The configured rate-limit store does not satisfy the Bedrock contract.',
|
|
353
|
+
userMessage: 'Request protection is not configured.',
|
|
354
|
+
});
|
|
296
355
|
}
|
|
297
356
|
}
|
|
298
357
|
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
for (const [ key, timestamps ] of this.requestLog) {
|
|
308
|
-
const recent: number[] = timestamps.filter((t: number): boolean => t > horizon);
|
|
309
|
-
if (recent.length === 0) {
|
|
310
|
-
this.requestLog.delete(key);
|
|
311
|
-
} else {
|
|
312
|
-
this.requestLog.set(key, recent);
|
|
313
|
-
}
|
|
358
|
+
private extractBucketPart(extractor: (req: Request) => string, req: Request): string {
|
|
359
|
+
let value: unknown;
|
|
360
|
+
|
|
361
|
+
try {
|
|
362
|
+
value = extractor(req);
|
|
363
|
+
} catch {
|
|
364
|
+
value = undefined;
|
|
314
365
|
}
|
|
366
|
+
|
|
367
|
+
if (typeof value !== 'string') {
|
|
368
|
+
this.throwBucketKeyInvalid();
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
const normalized: string = value.trim();
|
|
372
|
+
|
|
373
|
+
this.assertBucketPart(normalized);
|
|
374
|
+
|
|
375
|
+
return normalized;
|
|
315
376
|
}
|
|
316
377
|
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
378
|
+
private extractCost(req: Request): number {
|
|
379
|
+
try {
|
|
380
|
+
return this.costExtractor(req);
|
|
381
|
+
} catch {
|
|
382
|
+
this.throwRateLimitError({
|
|
383
|
+
status: 500,
|
|
384
|
+
subcode: 'COST_INVALID',
|
|
385
|
+
message: 'The rate-limit request cost could not be derived.',
|
|
386
|
+
userMessage: 'Request protection is not configured.',
|
|
387
|
+
});
|
|
324
388
|
}
|
|
325
389
|
}
|
|
326
390
|
|
|
391
|
+
private buildBucketKey(subject: string, endpoint: string): string {
|
|
392
|
+
return `${subject.length}:${subject}${endpoint.length}:${endpoint}`;
|
|
393
|
+
}
|
|
327
394
|
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
395
|
+
private getNow(): number {
|
|
396
|
+
let now: unknown;
|
|
397
|
+
|
|
398
|
+
try {
|
|
399
|
+
now = this.clock();
|
|
400
|
+
} catch {
|
|
401
|
+
now = undefined;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
if (!Number.isSafeInteger(now) || typeof now !== 'number' || now < 0) {
|
|
405
|
+
this.throwRateLimitError({
|
|
406
|
+
status: 503,
|
|
407
|
+
subcode: 'CLOCK_INVALID',
|
|
408
|
+
message: 'The rate-limit clock returned an invalid timestamp.',
|
|
409
|
+
userMessage: 'Request protection is temporarily unavailable. Please try again later.',
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
return now;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
private assertBucketPart(value: string): void {
|
|
417
|
+
if (!value || value.length > MAX_BUCKET_PART_LENGTH) {
|
|
418
|
+
this.throwBucketKeyInvalid();
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
private throwBucketKeyInvalid(): never {
|
|
423
|
+
this.throwRateLimitError({
|
|
424
|
+
status: 500,
|
|
425
|
+
subcode: 'BUCKET_KEY_INVALID',
|
|
426
|
+
message: 'A rate-limit bucket component is missing, invalid or too large.',
|
|
427
|
+
userMessage: 'Request protection is not configured.',
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
private assertStoreResult(
|
|
432
|
+
result: unknown,
|
|
433
|
+
policy: DyNTS_RateLimit_Policy,
|
|
434
|
+
now: number
|
|
435
|
+
): asserts result is DyNTS_RateLimit_ConsumeResult_Interface {
|
|
436
|
+
const isValid: boolean = typeof result === 'object'
|
|
437
|
+
&& result !== null
|
|
438
|
+
&& 'allowed' in result
|
|
439
|
+
&& 'remaining' in result
|
|
440
|
+
&& 'resetAt' in result
|
|
441
|
+
&& typeof result.allowed === 'boolean'
|
|
442
|
+
&& typeof result.remaining === 'number'
|
|
443
|
+
&& Number.isSafeInteger(result.remaining)
|
|
444
|
+
&& result.remaining >= 0
|
|
445
|
+
&& result.remaining <= policy.limit
|
|
446
|
+
&& typeof result.resetAt === 'number'
|
|
447
|
+
&& Number.isSafeInteger(result.resetAt)
|
|
448
|
+
&& result.resetAt >= now;
|
|
449
|
+
|
|
450
|
+
if (!isValid) {
|
|
451
|
+
this.throwRateLimitError({
|
|
452
|
+
status: 503,
|
|
453
|
+
subcode: 'STORE_RESULT_INVALID',
|
|
454
|
+
message: 'The rate-limit store returned an invalid permit decision.',
|
|
455
|
+
userMessage: 'Request protection is temporarily unavailable. Please try again later.',
|
|
456
|
+
});
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
private setResponseHeaders(
|
|
461
|
+
res: Response,
|
|
462
|
+
context: DyNTS_RateLimit_HeaderContext_Interface
|
|
463
|
+
): void {
|
|
464
|
+
if (!this.isResponseHeadersEnabled) {
|
|
465
|
+
return;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
const resetSeconds: string = Math.ceil(context.result.resetAt / 1_000).toString();
|
|
469
|
+
|
|
470
|
+
res.setHeader('X-RateLimit-Limit', context.policy.limit.toString());
|
|
471
|
+
res.setHeader('X-RateLimit-Remaining', context.result.remaining.toString());
|
|
472
|
+
res.setHeader('X-RateLimit-Reset', resetSeconds);
|
|
473
|
+
res.setHeader('RateLimit-Limit', context.policy.limit.toString());
|
|
474
|
+
res.setHeader('RateLimit-Remaining', context.result.remaining.toString());
|
|
475
|
+
res.setHeader(
|
|
476
|
+
'RateLimit-Reset',
|
|
477
|
+
Math.max(0, Math.ceil((context.result.resetAt - context.now) / 1_000)).toString()
|
|
478
|
+
);
|
|
479
|
+
|
|
480
|
+
if (!context.result.allowed) {
|
|
481
|
+
res.setHeader(
|
|
482
|
+
'Retry-After',
|
|
483
|
+
this.getRetryAfterSeconds(context.result.resetAt, context.now).toString()
|
|
484
|
+
);
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
private getRetryAfterSeconds(resetAt: number, now: number): number {
|
|
489
|
+
return Math.max(1, Math.ceil((resetAt - now) / 1_000));
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
private throwRateLimitError(context: DyNTS_RateLimit_ErrorContext_Interface): never {
|
|
493
|
+
const systemShortCode: string = DyNTS_global_settings.systemShortCodeName ?? 'DyNTS';
|
|
494
|
+
|
|
495
|
+
throw new DyFM_Error({
|
|
496
|
+
status: context.status,
|
|
497
|
+
errorCode: `${systemShortCode}|DyNTS-RL-${context.subcode}`,
|
|
498
|
+
addECToUserMsg: true,
|
|
499
|
+
message: context.message,
|
|
500
|
+
userMessage: context.userMessage,
|
|
501
|
+
issuerService: SERVICE_NAME,
|
|
502
|
+
});
|
|
342
503
|
}
|
|
343
504
|
}
|