badmfck-api-server 6.0.4 → 6.0.6
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/README.md +64 -13
- package/dist/apiServer/APIService.d.ts +4 -0
- package/dist/apiServer/APIService.js +17 -0
- package/dist/apiServer/deployment/ConfigService.d.ts +67 -3
- package/dist/apiServer/deployment/ConfigService.js +5 -1
- package/dist/apiServer/deployment/DeployerService.d.ts +0 -2
- package/dist/apiServer/deployment/DeployerService.js +7 -11
- package/dist/apiServer/deployment/HashPassword.d.ts +1 -0
- package/dist/apiServer/deployment/HashPassword.js +2 -1
- package/dist/apiServer/deployment/ProjectConfigWizard.d.ts +1 -0
- package/dist/apiServer/deployment/ProjectConfigWizard.js +171 -0
- package/dist/apiServer/deployment/cli.js +10 -0
- package/dist/apiServer/documentation/Documentation.js +5 -2
- package/dist/apiServer/monitor/Monitor.d.ts +3 -0
- package/dist/apiServer/monitor/Monitor.js +64 -7
- package/dist/apiServer/monitor/MonitorAuth.js +3 -1
- package/dist/apiServer/monitor/index.html +112 -67
- package/dist/apiServer/review/Review.js +1 -1
- package/dist/apiServer/security/BlockedIps.d.ts +46 -0
- package/dist/apiServer/security/BlockedIps.js +197 -0
- package/monitoring.help.md +40 -2
- package/package.json +1 -1
|
@@ -21,6 +21,7 @@ const ProcessMonitor_1 = require("./ProcessMonitor");
|
|
|
21
21
|
const LogFiles_1 = require("./LogFiles");
|
|
22
22
|
const MonitorAuth_1 = require("./MonitorAuth");
|
|
23
23
|
const ThreatMonitor_1 = require("./ThreatMonitor");
|
|
24
|
+
const BlockedIps_1 = require("../security/BlockedIps");
|
|
24
25
|
exports.S_MONITOR_REGISTRATE_ACTION = new badmfck_signal_1.Signal();
|
|
25
26
|
exports.S_MONITOR_RESTART_REQUESTED = new badmfck_signal_1.Signal();
|
|
26
27
|
const logMap = {
|
|
@@ -98,6 +99,11 @@ class Monitor extends BaseEndpoint_1.BaseEndpoint {
|
|
|
98
99
|
range: "",
|
|
99
100
|
$__range_values: ["week", "day", "hour"]
|
|
100
101
|
} },
|
|
102
|
+
{ ignoreInterceptor: true, endpoint: "blocked", handler: this.blocked },
|
|
103
|
+
{ ignoreInterceptor: true, endpoint: "block", handler: this.block, validationModel: {
|
|
104
|
+
ip: "", reason: "", $__reason_optional: true, hours: 0, $__hours_optional: true
|
|
105
|
+
} },
|
|
106
|
+
{ ignoreInterceptor: true, endpoint: "unblock", handler: this.unblock, validationModel: { ip: "" } },
|
|
101
107
|
{ ignoreInterceptor: true, endpoint: "controllers", handler: this.controllers },
|
|
102
108
|
{ ignoreInterceptor: true, endpoint: "changeLogLevel", handler: this.changeLogLevel, validationModel: {
|
|
103
109
|
level: Object.keys(logMap).join(",")
|
|
@@ -111,11 +117,11 @@ class Monitor extends BaseEndpoint_1.BaseEndpoint {
|
|
|
111
117
|
const { list, error } = IpAllowList_1.IpAllowList.build(entries);
|
|
112
118
|
if (error) {
|
|
113
119
|
(0, LogService_1.logError)(`Monitor: monitorAllowedIps is invalid (${error}) — the monitor refuses every request until it is fixed`);
|
|
114
|
-
return { error: { ...DefaultErrors_1.default.FORBIDDEN
|
|
120
|
+
return { error: { ...DefaultErrors_1.default.FORBIDDEN } };
|
|
115
121
|
}
|
|
116
122
|
if (!IpAllowList_1.IpAllowList.allows(list, req.ip)) {
|
|
117
123
|
(0, LogService_1.logWarn)(`Monitor: request refused from ${req.ip ?? "unknown"} — not in monitorAllowedIps`);
|
|
118
|
-
return { error: { ...DefaultErrors_1.default.FORBIDDEN
|
|
124
|
+
return { error: { ...DefaultErrors_1.default.FORBIDDEN } };
|
|
119
125
|
}
|
|
120
126
|
return null;
|
|
121
127
|
}
|
|
@@ -203,8 +209,10 @@ class Monitor extends BaseEndpoint_1.BaseEndpoint {
|
|
|
203
209
|
}
|
|
204
210
|
async login(req) {
|
|
205
211
|
const users = await APIService_1.REQ_MONITOR_USERS.request();
|
|
206
|
-
if (!users || users.length === 0)
|
|
207
|
-
|
|
212
|
+
if (!users || users.length === 0) {
|
|
213
|
+
(0, LogService_1.logWarn)("Monitor: sign-in refused — no monitor users are configured (access.monitor)");
|
|
214
|
+
throw { ...DefaultErrors_1.default.UNAUTHORIZED };
|
|
215
|
+
}
|
|
208
216
|
const { user, error, enrollmentRequired } = await MonitorAuth_1.MonitorAuth.authenticate(users, req.data.login, req.data.password, req.data.totp);
|
|
209
217
|
if (!user || error) {
|
|
210
218
|
(0, LogService_1.logWarn)(`Monitor login failed for "${req.data.login}" from ${req.ip ?? "unknown"}: ${error}`);
|
|
@@ -317,7 +325,56 @@ class Monitor extends BaseEndpoint_1.BaseEndpoint {
|
|
|
317
325
|
async threats(req) {
|
|
318
326
|
await this.requireAccess(req, "logs");
|
|
319
327
|
const limit = Number(req.data.limit) || 50;
|
|
320
|
-
|
|
328
|
+
const snap = ThreatMonitor_1.ThreatMonitor.snapshot(limit);
|
|
329
|
+
return { data: { ...snap, offenders: snap.offenders.map(o => ({ ...o, blocked: BlockedIps_1.BlockedIps.isBlocked(o.ip) })) } };
|
|
330
|
+
}
|
|
331
|
+
async blocked(req) {
|
|
332
|
+
await this.requireAccess(req, "logs");
|
|
333
|
+
return { data: BlockedIps_1.BlockedIps.snapshot() };
|
|
334
|
+
}
|
|
335
|
+
async block(req) {
|
|
336
|
+
const login = await this.requireAccess(req, "admin");
|
|
337
|
+
const ip = String(req.data.ip ?? "").trim();
|
|
338
|
+
const built = IpAllowList_1.IpAllowList.build([ip]);
|
|
339
|
+
if (!ip || built.error)
|
|
340
|
+
throw { ...DefaultErrors_1.default.BAD_REQUEST, details: built.error ?? "ip is required" };
|
|
341
|
+
if (IpAllowList_1.IpAllowList.allows(built.list, "127.0.0.1") || IpAllowList_1.IpAllowList.allows(built.list, "::1"))
|
|
342
|
+
throw { ...DefaultErrors_1.default.BAD_REQUEST, details: "Loopback can't be blocked" };
|
|
343
|
+
if (req.ip && IpAllowList_1.IpAllowList.allows(built.list, req.ip))
|
|
344
|
+
throw { ...DefaultErrors_1.default.BAD_REQUEST, details: `This would block your own address (${req.ip})` };
|
|
345
|
+
const hours = Number(req.data.hours) || 0;
|
|
346
|
+
if (hours < 0 || hours > 24 * 365)
|
|
347
|
+
throw { ...DefaultErrors_1.default.BAD_REQUEST, details: "hours must be between 0 (permanent) and 8760" };
|
|
348
|
+
const entry = {
|
|
349
|
+
ip,
|
|
350
|
+
...(req.data.reason ? { reason: String(req.data.reason).slice(0, 200) } : {}),
|
|
351
|
+
until: hours ? new Date(Date.now() + hours * 3600_000).toISOString() : null,
|
|
352
|
+
by: login,
|
|
353
|
+
at: new Date().toISOString(),
|
|
354
|
+
};
|
|
355
|
+
try {
|
|
356
|
+
BlockedIps_1.BlockedIps.add(entry);
|
|
357
|
+
}
|
|
358
|
+
catch (e) {
|
|
359
|
+
throw { ...DefaultErrors_1.default.INTERNAL_SERVER_ERROR, details: e?.message ?? String(e) };
|
|
360
|
+
}
|
|
361
|
+
(0, LogService_1.logWarn)(`Monitor: "${login}" blocked ${ip}${hours ? ` for ${hours} h` : " permanently"}${entry.reason ? ` — ${entry.reason}` : ""}`);
|
|
362
|
+
return { data: BlockedIps_1.BlockedIps.snapshot() };
|
|
363
|
+
}
|
|
364
|
+
async unblock(req) {
|
|
365
|
+
const login = await this.requireAccess(req, "admin");
|
|
366
|
+
const ip = String(req.data.ip ?? "").trim();
|
|
367
|
+
let removed;
|
|
368
|
+
try {
|
|
369
|
+
removed = BlockedIps_1.BlockedIps.remove(ip);
|
|
370
|
+
}
|
|
371
|
+
catch (e) {
|
|
372
|
+
throw { ...DefaultErrors_1.default.INTERNAL_SERVER_ERROR, details: e?.message ?? String(e) };
|
|
373
|
+
}
|
|
374
|
+
if (!removed)
|
|
375
|
+
throw { ...DefaultErrors_1.default.NOT_FOUND, details: `${ip} is not in the list` };
|
|
376
|
+
(0, LogService_1.logWarn)(`Monitor: "${login}" unblocked ${ip}`);
|
|
377
|
+
return { data: BlockedIps_1.BlockedIps.snapshot() };
|
|
321
378
|
}
|
|
322
379
|
async logfiles(req) {
|
|
323
380
|
await this.requireAccess(req, "logs");
|
|
@@ -374,7 +431,7 @@ class Monitor extends BaseEndpoint_1.BaseEndpoint {
|
|
|
374
431
|
async checkAuthentication(req) {
|
|
375
432
|
const users = await APIService_1.REQ_MONITOR_USERS.request();
|
|
376
433
|
if (!users || users.length == 0)
|
|
377
|
-
throw { ...DefaultErrors_1.default.UNAUTHORIZED
|
|
434
|
+
throw { ...DefaultErrors_1.default.UNAUTHORIZED };
|
|
378
435
|
const header = req.headers['authorization'];
|
|
379
436
|
if (!header)
|
|
380
437
|
throw { ...DefaultErrors_1.default.UNAUTHORIZED, details: "No authorization header found" };
|
|
@@ -383,7 +440,7 @@ class Monitor extends BaseEndpoint_1.BaseEndpoint {
|
|
|
383
440
|
throw { ...DefaultErrors_1.default.UNAUTHORIZED, details: "Wrong authorization header" };
|
|
384
441
|
const session = MonitorAuth_1.MonitorAuth.touchSession(token);
|
|
385
442
|
if (!session)
|
|
386
|
-
throw { ...DefaultErrors_1.default.UNAUTHORIZED, details: "Session is invalid or expired
|
|
443
|
+
throw { ...DefaultErrors_1.default.UNAUTHORIZED, details: "Session is invalid or expired" };
|
|
387
444
|
return session.login;
|
|
388
445
|
}
|
|
389
446
|
}
|
|
@@ -375,8 +375,10 @@ class MonitorAuth {
|
|
|
375
375
|
if (!PasswordHash_1.PasswordHash.isHash(candidate.password)) {
|
|
376
376
|
const matches = typeof candidate.password === "string" && candidate.password.length > 0
|
|
377
377
|
&& MonitorAuth.sameSecret(candidate.password, password || "");
|
|
378
|
+
if (matches)
|
|
379
|
+
(0, LogService_1.logError)(`Monitor: "${login}" signed in with the right password, but it is stored in the config as plain text — refused; store a hash: npx badmfck hash-password`);
|
|
378
380
|
MonitorAuth.registerFailure(login);
|
|
379
|
-
return { error:
|
|
381
|
+
return { error: "Invalid user" };
|
|
380
382
|
}
|
|
381
383
|
if (!(await PasswordHash_1.PasswordHash.verify(password || "", candidate.password))) {
|
|
382
384
|
MonitorAuth.registerFailure(login);
|