@stacksjs/ts-cloud 0.7.108 → 0.7.110
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/dist/alerts/delivery.d.ts +10 -0
- package/dist/alerts/model.d.ts +1 -1
- package/dist/api/client.d.ts +53 -0
- package/dist/api/handler.d.ts +10 -0
- package/dist/api/service.d.ts +1 -0
- package/dist/aws/index.js +10 -10
- package/dist/aws/lambda.d.ts +19 -0
- package/dist/bin/cli.js +1520 -1204
- package/dist/bin/dashboard-server.js +935 -621
- package/dist/chunk-19bs955w.js +12203 -0
- package/dist/{chunk-fmt40tpw.js → chunk-53jatzaw.js} +2 -2
- package/dist/{chunk-ef2dwfrg.js → chunk-8kh577qh.js} +5 -5
- package/dist/{chunk-s9c2x5jv.js → chunk-9v2pwxv9.js} +2 -2
- package/dist/chunk-aa2dkn7b.js +205 -0
- package/dist/{chunk-33sy72r8.js → chunk-d3vx2ekv.js} +71 -25
- package/dist/chunk-d48vtdj4.js +433 -0
- package/dist/{chunk-c6db5m63.js → chunk-t2s3gdq6.js} +253 -8
- package/dist/chunk-va2yd85b.js +200 -0
- package/dist/{chunk-3g4r09dd.js → chunk-wmbep3b5.js} +1 -1
- package/dist/{chunk-eg8ck25v.js → chunk-wmz8x3cy.js} +6935 -15274
- package/dist/{chunk-ybcz6sxc.js → chunk-x6yzgv7y.js} +33 -0
- package/dist/control-plane/types.d.ts +1 -1
- package/dist/deploy/dashboard-protection.test.d.ts +1 -0
- package/dist/deploy/dashboard-spend.test.d.ts +1 -0
- package/dist/deploy/index.js +11 -8
- package/dist/deploy/local-dashboard-server.d.ts +2 -0
- package/dist/drivers/index.js +4 -2
- package/dist/drivers/shared/protection.d.ts +43 -0
- package/dist/drivers/shared/protection.test.d.ts +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +364 -102
- package/dist/protection/controls.d.ts +127 -0
- package/dist/protection/controls.test.d.ts +1 -0
- package/dist/protection/ddos.d.ts +130 -0
- package/dist/protection/ddos.test.d.ts +1 -0
- package/dist/protection/index.d.ts +11 -0
- package/dist/protection/index.js +89 -0
- package/dist/protection/ratelimit.d.ts +154 -0
- package/dist/protection/ratelimit.test.d.ts +1 -0
- package/dist/protection/waf.d.ts +143 -0
- package/dist/protection/waf.test.d.ts +1 -0
- package/dist/spend/anomaly-config.d.ts +130 -0
- package/dist/spend/anomaly-config.test.d.ts +1 -0
- package/dist/spend/anomaly.d.ts +114 -0
- package/dist/spend/anomaly.test.d.ts +1 -0
- package/dist/spend/api.d.ts +42 -0
- package/dist/spend/api.test.d.ts +1 -0
- package/dist/spend/appliers.d.ts +126 -0
- package/dist/spend/docs.test.d.ts +1 -0
- package/dist/spend/enforcement.d.ts +111 -0
- package/dist/spend/enforcement.integration.test.d.ts +1 -0
- package/dist/spend/evaluator.d.ts +70 -0
- package/dist/spend/evaluator.test.d.ts +1 -0
- package/dist/spend/focus.d.ts +93 -0
- package/dist/spend/focus.test.d.ts +1 -0
- package/dist/spend/gate.d.ts +96 -0
- package/dist/spend/index.d.ts +20 -0
- package/dist/spend/index.js +173 -0
- package/dist/spend/lease.d.ts +54 -0
- package/dist/spend/lease.test.d.ts +1 -0
- package/dist/spend/meter.d.ts +70 -0
- package/dist/spend/meter.test.d.ts +1 -0
- package/dist/spend/model.d.ts +228 -0
- package/dist/spend/notifications.d.ts +58 -0
- package/dist/spend/pricing.d.ts +109 -0
- package/dist/spend/pricing.test.d.ts +1 -0
- package/dist/spend/projection.d.ts +36 -0
- package/dist/spend/runner.d.ts +130 -0
- package/dist/spend/service.d.ts +129 -0
- package/dist/spend/signals.d.ts +131 -0
- package/dist/spend/signals.test.d.ts +1 -0
- package/dist/spend/sms-sdk.test.d.ts +1 -0
- package/dist/spend/spend.integration.test.d.ts +1 -0
- package/dist/spend/store.d.ts +166 -0
- package/dist/spend/transports.d.ts +169 -0
- package/dist/spend/transports.test.d.ts +1 -0
- package/dist/spend/window.d.ts +52 -0
- package/dist/spend/window.test.d.ts +1 -0
- package/dist/ui/404.html +1 -0
- package/dist/ui/access-denied.html +1 -1
- package/dist/ui/account/automation.html +3 -3
- package/dist/ui/account/security.html +2 -2
- package/dist/ui/applications/compose.html +4 -4
- package/dist/ui/applications/new.html +2 -2
- package/dist/ui/data/backups.html +4 -4
- package/dist/ui/data/services.html +4 -4
- package/dist/ui/data/volumes.html +4 -4
- package/dist/ui/index.html +4 -4
- package/dist/ui/infrastructure/topology.html +4 -4
- package/dist/ui/integrations.html +2 -2
- package/dist/ui/operations/alerts.html +4 -4
- package/dist/ui/operations/configuration.html +4 -4
- package/dist/ui/operations/jobs.html +3 -3
- package/dist/ui/operations/maintenance.html +4 -4
- package/dist/ui/operations/observability.html +4 -4
- package/dist/ui/operations/previews.html +4 -4
- package/dist/ui/operations/queue.html +3 -3
- package/dist/ui/operations/regions.html +4 -4
- package/dist/ui/operations/releases.html +4 -4
- package/dist/ui/operations/spend.html +1634 -0
- package/dist/ui/operations/workloads.html +4 -4
- package/dist/ui/security.html +2 -2
- package/dist/ui/server/actions.html +4 -4
- package/dist/ui/server/activity.html +2 -2
- package/dist/ui/server/capacity.html +4 -4
- package/dist/ui/server/database.html +4 -4
- package/dist/ui/server/deployments.html +4 -4
- package/dist/ui/server/diagnostics.html +2 -2
- package/dist/ui/server/firewall.html +100 -161
- package/dist/ui/server/fleet.html +4 -4
- package/dist/ui/server/logs.html +4 -4
- package/dist/ui/server/metrics.html +4 -4
- package/dist/ui/server/security.html +2 -2
- package/dist/ui/server/services.html +2 -2
- package/dist/ui/server/sites.html +4 -4
- package/dist/ui/server/ssh-keys.html +4 -4
- package/dist/ui/server/team.html +4 -4
- package/dist/ui/server/terminal.html +2 -2
- package/dist/ui/serverless/alarms.html +4 -4
- package/dist/ui/serverless/assets.html +2 -2
- package/dist/ui/serverless/cost.html +2 -2
- package/dist/ui/serverless/data.html +4 -4
- package/dist/ui/serverless/deployments.html +1 -1
- package/dist/ui/serverless/firewall.html +2 -2
- package/dist/ui/serverless/functions.html +4 -4
- package/dist/ui/serverless/logs.html +4 -4
- package/dist/ui/serverless/metrics.html +2 -2
- package/dist/ui/serverless/queues.html +4 -4
- package/dist/ui/serverless/secrets.html +4 -4
- package/dist/ui/serverless/traces.html +4 -4
- package/dist/ui/serverless.html +3 -3
- package/dist/ui-src/pages/operations/spend.stx +225 -0
- package/dist/ui-src/pages/partials/nav.stx +2 -0
- package/dist/ui-src/pages/server/firewall.stx +89 -0
- package/package.json +11 -3
|
@@ -0,0 +1,433 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isValidCidr
|
|
3
|
+
} from "./chunk-aa2dkn7b.js";
|
|
4
|
+
// src/protection/ratelimit.ts
|
|
5
|
+
var RATE_LIMIT_KEY_SEPARATOR = "\x00";
|
|
6
|
+
var GLOB_PLACEHOLDER = "\x01";
|
|
7
|
+
function globMatches(pattern, value) {
|
|
8
|
+
if (pattern === "*" || pattern === "**")
|
|
9
|
+
return true;
|
|
10
|
+
const escaped = pattern.replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*\*/g, GLOB_PLACEHOLDER).replace(/\*/g, "[^/]*").replace(/\u0001/g, ".*");
|
|
11
|
+
return new RegExp(`^${escaped}$`).test(value);
|
|
12
|
+
}
|
|
13
|
+
function hostMatches(pattern, host) {
|
|
14
|
+
if (pattern.startsWith("*.")) {
|
|
15
|
+
const suffix = pattern.slice(1);
|
|
16
|
+
return host.endsWith(suffix) && host.length > suffix.length;
|
|
17
|
+
}
|
|
18
|
+
return pattern === host;
|
|
19
|
+
}
|
|
20
|
+
function ruleMatches(rule, request) {
|
|
21
|
+
if (rule.enabled === false)
|
|
22
|
+
return false;
|
|
23
|
+
if (rule.methods?.length && !rule.methods.some((method) => method.toUpperCase() === request.method.toUpperCase()))
|
|
24
|
+
return false;
|
|
25
|
+
if (rule.path && !globMatches(rule.path, request.path))
|
|
26
|
+
return false;
|
|
27
|
+
if (rule.host && !hostMatches(rule.host, request.host ?? ""))
|
|
28
|
+
return false;
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
function rateLimitKey(rule, request) {
|
|
32
|
+
const spec = rule.key ?? { source: "ip" };
|
|
33
|
+
const fallback = spec.fallbackToIp !== false;
|
|
34
|
+
let identity;
|
|
35
|
+
if (spec.source === "ip")
|
|
36
|
+
identity = request.ip;
|
|
37
|
+
else if (spec.source === "global")
|
|
38
|
+
identity = "global";
|
|
39
|
+
else if (spec.source === "host")
|
|
40
|
+
identity = request.host ?? "";
|
|
41
|
+
else if (spec.source === "path")
|
|
42
|
+
identity = request.path;
|
|
43
|
+
else if (spec.source === "header")
|
|
44
|
+
identity = spec.name ? request.headers?.[spec.name.toLowerCase()] : undefined;
|
|
45
|
+
else if (spec.source === "cookie")
|
|
46
|
+
identity = spec.name ? request.cookies?.[spec.name] : undefined;
|
|
47
|
+
if (!identity && spec.source !== "global")
|
|
48
|
+
identity = fallback ? request.ip : "__missing__";
|
|
49
|
+
return `${rule.id}${RATE_LIMIT_KEY_SEPARATOR}${identity}`;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
class RateLimiter {
|
|
53
|
+
clock;
|
|
54
|
+
maxTrackedKeys;
|
|
55
|
+
buckets = new Map;
|
|
56
|
+
windows = new Map;
|
|
57
|
+
rules;
|
|
58
|
+
constructor(rules, clock = () => Date.now(), maxTrackedKeys = 1e5) {
|
|
59
|
+
this.clock = clock;
|
|
60
|
+
this.maxTrackedKeys = maxTrackedKeys;
|
|
61
|
+
this.rules = [...rules].sort((a, b) => (a.priority ?? 0) - (b.priority ?? 0));
|
|
62
|
+
}
|
|
63
|
+
check(request) {
|
|
64
|
+
const now = this.clock();
|
|
65
|
+
const matching = this.rules.filter((rule) => ruleMatches(rule, request));
|
|
66
|
+
let blocked;
|
|
67
|
+
for (const rule of matching) {
|
|
68
|
+
const test = this.test(rule, request, now);
|
|
69
|
+
if (test.allowed)
|
|
70
|
+
continue;
|
|
71
|
+
if (!blocked || severity(test.action) > severity(blocked.action))
|
|
72
|
+
blocked = test;
|
|
73
|
+
}
|
|
74
|
+
if (blocked)
|
|
75
|
+
return blocked;
|
|
76
|
+
let tightest = { action: "allow", allowed: true };
|
|
77
|
+
for (const rule of matching) {
|
|
78
|
+
const decision = this.consume(rule, request, now);
|
|
79
|
+
if (decision.remaining != null && (tightest.remaining ?? Infinity) > decision.remaining)
|
|
80
|
+
tightest = decision;
|
|
81
|
+
}
|
|
82
|
+
return tightest;
|
|
83
|
+
}
|
|
84
|
+
test(rule, request, now) {
|
|
85
|
+
const key = rateLimitKey(rule, request);
|
|
86
|
+
if ((rule.algorithm ?? "token_bucket") === "token_bucket") {
|
|
87
|
+
const capacity = rule.burst ?? rule.limit;
|
|
88
|
+
const state = this.buckets.get(key);
|
|
89
|
+
const tokens = state ? Math.min(capacity, state.tokens + this.refill(rule, now - state.updatedAt)) : capacity;
|
|
90
|
+
if (tokens >= 1)
|
|
91
|
+
return { action: "allow", allowed: true };
|
|
92
|
+
return this.blocked(rule, key, 0, this.msPerToken(rule) * (1 - tokens), now);
|
|
93
|
+
}
|
|
94
|
+
const hits = (this.windows.get(key)?.hits ?? []).filter((at) => at > now - rule.windowMs);
|
|
95
|
+
if (hits.length < rule.limit)
|
|
96
|
+
return { action: "allow", allowed: true };
|
|
97
|
+
return this.blocked(rule, key, 0, hits[0] + rule.windowMs - now, now);
|
|
98
|
+
}
|
|
99
|
+
peek(request) {
|
|
100
|
+
const now = this.clock();
|
|
101
|
+
for (const rule of this.rules) {
|
|
102
|
+
if (!ruleMatches(rule, request))
|
|
103
|
+
continue;
|
|
104
|
+
const decision = this.test(rule, request, now);
|
|
105
|
+
if (!decision.allowed)
|
|
106
|
+
return decision;
|
|
107
|
+
}
|
|
108
|
+
return { action: "allow", allowed: true };
|
|
109
|
+
}
|
|
110
|
+
msPerToken(rule) {
|
|
111
|
+
return rule.windowMs / Math.max(1, rule.limit);
|
|
112
|
+
}
|
|
113
|
+
refill(rule, elapsedMs) {
|
|
114
|
+
return Math.max(0, elapsedMs) / this.msPerToken(rule);
|
|
115
|
+
}
|
|
116
|
+
blocked(rule, key, remaining, retryAfterMs, now) {
|
|
117
|
+
const action = rule.action ?? "deny";
|
|
118
|
+
return {
|
|
119
|
+
action,
|
|
120
|
+
allowed: action === "allow" || action === "log",
|
|
121
|
+
ruleId: rule.id,
|
|
122
|
+
limit: rule.limit,
|
|
123
|
+
remaining,
|
|
124
|
+
retryAfterMs: Math.max(0, Math.ceil(retryAfterMs)),
|
|
125
|
+
resetAt: now + Math.max(0, retryAfterMs),
|
|
126
|
+
key
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
consume(rule, request, now) {
|
|
130
|
+
const key = rateLimitKey(rule, request);
|
|
131
|
+
const algorithm = rule.algorithm ?? "token_bucket";
|
|
132
|
+
if (algorithm === "token_bucket") {
|
|
133
|
+
const capacity = rule.burst ?? rule.limit;
|
|
134
|
+
const state2 = this.buckets.get(key) ?? { tokens: capacity, updatedAt: now };
|
|
135
|
+
const tokens = Math.min(capacity, state2.tokens + this.refill(rule, now - state2.updatedAt));
|
|
136
|
+
if (tokens < 1) {
|
|
137
|
+
this.buckets.set(key, { tokens, updatedAt: now });
|
|
138
|
+
return this.blocked(rule, key, 0, this.msPerToken(rule) * (1 - tokens), now);
|
|
139
|
+
}
|
|
140
|
+
this.evictIfNeeded(this.buckets);
|
|
141
|
+
this.buckets.set(key, { tokens: tokens - 1, updatedAt: now });
|
|
142
|
+
return {
|
|
143
|
+
action: "allow",
|
|
144
|
+
allowed: true,
|
|
145
|
+
ruleId: rule.id,
|
|
146
|
+
limit: capacity,
|
|
147
|
+
remaining: Math.floor(tokens - 1),
|
|
148
|
+
resetAt: now + this.msPerToken(rule) * (capacity - (tokens - 1)),
|
|
149
|
+
key
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
const state = this.windows.get(key) ?? { hits: [] };
|
|
153
|
+
const cutoff = now - rule.windowMs;
|
|
154
|
+
const hits = state.hits.filter((at) => at > cutoff);
|
|
155
|
+
if (hits.length >= rule.limit) {
|
|
156
|
+
this.windows.set(key, { hits });
|
|
157
|
+
return this.blocked(rule, key, 0, hits[0] + rule.windowMs - now, now);
|
|
158
|
+
}
|
|
159
|
+
hits.push(now);
|
|
160
|
+
this.evictIfNeeded(this.windows);
|
|
161
|
+
this.windows.set(key, { hits });
|
|
162
|
+
return {
|
|
163
|
+
action: "allow",
|
|
164
|
+
allowed: true,
|
|
165
|
+
ruleId: rule.id,
|
|
166
|
+
limit: rule.limit,
|
|
167
|
+
remaining: rule.limit - hits.length,
|
|
168
|
+
resetAt: hits[0] + rule.windowMs,
|
|
169
|
+
key
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
evictIfNeeded(map) {
|
|
173
|
+
if (map.size < this.maxTrackedKeys)
|
|
174
|
+
return;
|
|
175
|
+
const excess = map.size - this.maxTrackedKeys + 1;
|
|
176
|
+
let removed = 0;
|
|
177
|
+
for (const key of map.keys()) {
|
|
178
|
+
map.delete(key);
|
|
179
|
+
if (++removed >= excess)
|
|
180
|
+
break;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
sweep() {
|
|
184
|
+
const now = this.clock();
|
|
185
|
+
let removed = 0;
|
|
186
|
+
for (const [key, state] of this.windows) {
|
|
187
|
+
const ruleId = key.split(RATE_LIMIT_KEY_SEPARATOR)[0];
|
|
188
|
+
const rule = this.rules.find((candidate) => candidate.id === ruleId);
|
|
189
|
+
if (!rule) {
|
|
190
|
+
this.windows.delete(key);
|
|
191
|
+
removed++;
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
if (state.hits.every((at) => at <= now - rule.windowMs)) {
|
|
195
|
+
this.windows.delete(key);
|
|
196
|
+
removed++;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
for (const [key, state] of this.buckets) {
|
|
200
|
+
const ruleId = key.split(RATE_LIMIT_KEY_SEPARATOR)[0];
|
|
201
|
+
const rule = this.rules.find((candidate) => candidate.id === ruleId);
|
|
202
|
+
if (!rule || now - state.updatedAt > rule.windowMs * 2) {
|
|
203
|
+
this.buckets.delete(key);
|
|
204
|
+
removed++;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
return removed;
|
|
208
|
+
}
|
|
209
|
+
get trackedKeys() {
|
|
210
|
+
return this.buckets.size + this.windows.size;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
function severity(action) {
|
|
214
|
+
return { allow: 0, log: 1, throttle: 2, challenge: 3, deny: 4 }[action];
|
|
215
|
+
}
|
|
216
|
+
function rateLimitHeaders(decision) {
|
|
217
|
+
const headers = {};
|
|
218
|
+
if (decision.limit != null)
|
|
219
|
+
headers["ratelimit-limit"] = String(decision.limit);
|
|
220
|
+
if (decision.remaining != null)
|
|
221
|
+
headers["ratelimit-remaining"] = String(decision.remaining);
|
|
222
|
+
if (decision.resetAt != null)
|
|
223
|
+
headers["ratelimit-reset"] = String(Math.max(0, Math.ceil((decision.resetAt - Date.now()) / 1000)));
|
|
224
|
+
if (!decision.allowed && decision.retryAfterMs != null)
|
|
225
|
+
headers["retry-after"] = String(Math.max(1, Math.ceil(decision.retryAfterMs / 1000)));
|
|
226
|
+
return headers;
|
|
227
|
+
}
|
|
228
|
+
function defaultRateLimitRules() {
|
|
229
|
+
return [
|
|
230
|
+
{ id: "global-ip", limit: 600, windowMs: 60000, burst: 100, key: { source: "ip" }, action: "deny", priority: 100 },
|
|
231
|
+
{
|
|
232
|
+
id: "api-ip",
|
|
233
|
+
limit: 120,
|
|
234
|
+
windowMs: 60000,
|
|
235
|
+
burst: 30,
|
|
236
|
+
path: "/api/**",
|
|
237
|
+
key: { source: "ip" },
|
|
238
|
+
action: "deny",
|
|
239
|
+
priority: 50
|
|
240
|
+
},
|
|
241
|
+
{
|
|
242
|
+
id: "auth-ip",
|
|
243
|
+
limit: 10,
|
|
244
|
+
windowMs: 60000,
|
|
245
|
+
algorithm: "sliding_window",
|
|
246
|
+
path: "/auth/**",
|
|
247
|
+
methods: ["POST"],
|
|
248
|
+
key: { source: "ip" },
|
|
249
|
+
action: "challenge",
|
|
250
|
+
priority: 10
|
|
251
|
+
}
|
|
252
|
+
];
|
|
253
|
+
}
|
|
254
|
+
// src/protection/controls.ts
|
|
255
|
+
var PROTECTION_SETTING = "protection.controls";
|
|
256
|
+
var MAX_CONTROL_HOURS = 24;
|
|
257
|
+
var DEFAULT_ATTACK_MODE_HOURS = 4;
|
|
258
|
+
var EMPTY = { ipRules: { allow: [], block: [] } };
|
|
259
|
+
function activeControl(control, now) {
|
|
260
|
+
if (!control?.enabled)
|
|
261
|
+
return;
|
|
262
|
+
return new Date(control.expiresAt).getTime() > now.getTime() ? control : undefined;
|
|
263
|
+
}
|
|
264
|
+
function clampHours(hours, fallback) {
|
|
265
|
+
const value = Number.isFinite(hours) ? Number(hours) : fallback;
|
|
266
|
+
return Math.min(MAX_CONTROL_HOURS, Math.max(0.25, value));
|
|
267
|
+
}
|
|
268
|
+
var MAX_IP_RULES = 500;
|
|
269
|
+
|
|
270
|
+
class ProtectionControlStore {
|
|
271
|
+
controlPlane;
|
|
272
|
+
options;
|
|
273
|
+
constructor(controlPlane, options = {}) {
|
|
274
|
+
this.controlPlane = controlPlane;
|
|
275
|
+
this.options = options;
|
|
276
|
+
}
|
|
277
|
+
now() {
|
|
278
|
+
return this.options.now?.() ?? new Date;
|
|
279
|
+
}
|
|
280
|
+
read() {
|
|
281
|
+
const raw = this.controlPlane.getSetting(PROTECTION_SETTING);
|
|
282
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw))
|
|
283
|
+
return { ...EMPTY, ipRules: { allow: [], block: [] } };
|
|
284
|
+
const record = raw;
|
|
285
|
+
return {
|
|
286
|
+
attackMode: record.attackMode,
|
|
287
|
+
mitigationPause: record.mitigationPause,
|
|
288
|
+
ipRules: {
|
|
289
|
+
allow: Array.isArray(record.ipRules?.allow) ? record.ipRules.allow : [],
|
|
290
|
+
block: Array.isArray(record.ipRules?.block) ? record.ipRules.block : []
|
|
291
|
+
},
|
|
292
|
+
updatedAt: record.updatedAt
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
write(next) {
|
|
296
|
+
const stored = { ...next, updatedAt: this.now().toISOString() };
|
|
297
|
+
this.controlPlane.setSetting(PROTECTION_SETTING, stored);
|
|
298
|
+
return stored;
|
|
299
|
+
}
|
|
300
|
+
raw() {
|
|
301
|
+
return this.read();
|
|
302
|
+
}
|
|
303
|
+
current() {
|
|
304
|
+
const now = this.now();
|
|
305
|
+
const stored = this.read();
|
|
306
|
+
return {
|
|
307
|
+
attackMode: activeControl(stored.attackMode, now),
|
|
308
|
+
mitigationPause: activeControl(stored.mitigationPause, now),
|
|
309
|
+
ipRules: stored.ipRules
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
enableAttackMode(input) {
|
|
313
|
+
const now = this.now();
|
|
314
|
+
const control = {
|
|
315
|
+
enabled: true,
|
|
316
|
+
startedAt: now.toISOString(),
|
|
317
|
+
expiresAt: new Date(now.getTime() + clampHours(input.hours, DEFAULT_ATTACK_MODE_HOURS) * 3600000).toISOString(),
|
|
318
|
+
reason: input.reason.trim() || "Attack mode enabled by an operator.",
|
|
319
|
+
actorId: input.actorId
|
|
320
|
+
};
|
|
321
|
+
this.write({ ...this.read(), attackMode: control });
|
|
322
|
+
return control;
|
|
323
|
+
}
|
|
324
|
+
disableAttackMode() {
|
|
325
|
+
const stored = this.read();
|
|
326
|
+
if (!stored.attackMode?.enabled)
|
|
327
|
+
return false;
|
|
328
|
+
this.write({ ...stored, attackMode: { ...stored.attackMode, enabled: false } });
|
|
329
|
+
return true;
|
|
330
|
+
}
|
|
331
|
+
pauseMitigations(input) {
|
|
332
|
+
const reason = input.reason.trim();
|
|
333
|
+
if (!reason)
|
|
334
|
+
throw new Error("Pausing mitigations requires a reason: you are liable for the traffic it admits.");
|
|
335
|
+
const now = this.now();
|
|
336
|
+
const control = {
|
|
337
|
+
enabled: true,
|
|
338
|
+
startedAt: now.toISOString(),
|
|
339
|
+
expiresAt: new Date(now.getTime() + clampHours(input.hours, MAX_CONTROL_HOURS) * 3600000).toISOString(),
|
|
340
|
+
reason,
|
|
341
|
+
actorId: input.actorId
|
|
342
|
+
};
|
|
343
|
+
this.write({ ...this.read(), mitigationPause: control });
|
|
344
|
+
return control;
|
|
345
|
+
}
|
|
346
|
+
resumeMitigations() {
|
|
347
|
+
const stored = this.read();
|
|
348
|
+
if (!stored.mitigationPause?.enabled)
|
|
349
|
+
return false;
|
|
350
|
+
this.write({ ...stored, mitigationPause: { ...stored.mitigationPause, enabled: false } });
|
|
351
|
+
return true;
|
|
352
|
+
}
|
|
353
|
+
addIpRule(list, cidr) {
|
|
354
|
+
const value = cidr.trim();
|
|
355
|
+
if (!isValidCidr(value))
|
|
356
|
+
throw new Error(`Not a valid IP or CIDR: ${cidr}`);
|
|
357
|
+
const stored = this.read();
|
|
358
|
+
const next = { ...stored.ipRules, [list]: [...new Set([...stored.ipRules[list], value])] };
|
|
359
|
+
if (next[list].length > MAX_IP_RULES)
|
|
360
|
+
throw new Error(`At most ${MAX_IP_RULES} ${list} rules are supported.`);
|
|
361
|
+
const other = list === "allow" ? "block" : "allow";
|
|
362
|
+
next[other] = next[other].filter((entry) => entry !== value);
|
|
363
|
+
this.write({ ...stored, ipRules: next });
|
|
364
|
+
return next;
|
|
365
|
+
}
|
|
366
|
+
removeIpRule(list, cidr) {
|
|
367
|
+
const stored = this.read();
|
|
368
|
+
const next = { ...stored.ipRules, [list]: stored.ipRules[list].filter((entry) => entry !== cidr.trim()) };
|
|
369
|
+
this.write({ ...stored, ipRules: next });
|
|
370
|
+
return next;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
function describePosture(controls) {
|
|
374
|
+
if (controls.mitigationPause)
|
|
375
|
+
return {
|
|
376
|
+
attackMode: !!controls.attackMode,
|
|
377
|
+
mitigationPaused: true,
|
|
378
|
+
summary: `Automatic mitigation is paused until ${controls.mitigationPause.expiresAt}. All traffic is being served and billed.`,
|
|
379
|
+
expiresAt: controls.mitigationPause.expiresAt
|
|
380
|
+
};
|
|
381
|
+
if (controls.attackMode)
|
|
382
|
+
return {
|
|
383
|
+
attackMode: true,
|
|
384
|
+
mitigationPaused: false,
|
|
385
|
+
summary: `Attack mode is on until ${controls.attackMode.expiresAt}. Every visitor is being challenged.`,
|
|
386
|
+
expiresAt: controls.attackMode.expiresAt
|
|
387
|
+
};
|
|
388
|
+
return { attackMode: false, mitigationPaused: false, summary: "Protection is in its normal posture." };
|
|
389
|
+
}
|
|
390
|
+
function applyControlsToRateLimits(rules, controls) {
|
|
391
|
+
if (controls.mitigationPause)
|
|
392
|
+
return rules.map((rule) => ({ ...rule, enabled: false }));
|
|
393
|
+
if (!controls.attackMode)
|
|
394
|
+
return [...rules];
|
|
395
|
+
return rules.map((rule) => ({
|
|
396
|
+
...rule,
|
|
397
|
+
action: rule.action === "deny" ? "deny" : "challenge",
|
|
398
|
+
burst: Math.max(1, Math.min(rule.burst ?? rule.limit, 5))
|
|
399
|
+
}));
|
|
400
|
+
}
|
|
401
|
+
function applyControlsToDdos(config, controls) {
|
|
402
|
+
const allowlist = [...new Set([...config.allowlist ?? [], ...controls.ipRules.allow])];
|
|
403
|
+
const blocklist = [...new Set([...config.blocklist ?? [], ...controls.ipRules.block])];
|
|
404
|
+
if (controls.mitigationPause)
|
|
405
|
+
return { ...config, allowlist, blocklist, monitorOnly: true };
|
|
406
|
+
if (!controls.attackMode)
|
|
407
|
+
return { ...config, allowlist, blocklist };
|
|
408
|
+
const thresholds = config.thresholds ?? {};
|
|
409
|
+
return {
|
|
410
|
+
...config,
|
|
411
|
+
allowlist,
|
|
412
|
+
blocklist,
|
|
413
|
+
thresholds: {
|
|
414
|
+
...thresholds,
|
|
415
|
+
newConnectionsPerSecond: Math.max(1, Math.floor((thresholds.newConnectionsPerSecond ?? 50) / 5)),
|
|
416
|
+
concurrentPerSource: Math.max(1, Math.floor((thresholds.concurrentPerSource ?? 100) / 5))
|
|
417
|
+
}
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
function mergeControlsIntoConfig(config, controls) {
|
|
421
|
+
const compute = config?.infrastructure?.compute;
|
|
422
|
+
if (!compute || compute.ddos === false)
|
|
423
|
+
return config;
|
|
424
|
+
const base = typeof compute.ddos === "object" && compute.ddos !== null ? compute.ddos : {};
|
|
425
|
+
return {
|
|
426
|
+
...config,
|
|
427
|
+
infrastructure: {
|
|
428
|
+
...config.infrastructure,
|
|
429
|
+
compute: { ...compute, ddos: applyControlsToDdos(base, controls) }
|
|
430
|
+
}
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
export { RATE_LIMIT_KEY_SEPARATOR, globMatches, ruleMatches, rateLimitKey, RateLimiter, rateLimitHeaders, defaultRateLimitRules, PROTECTION_SETTING, MAX_CONTROL_HOURS, DEFAULT_ATTACK_MODE_HOURS, ProtectionControlStore, describePosture, applyControlsToRateLimits, applyControlsToDdos, mergeControlsIntoConfig };
|