badmfck-api-server 6.0.4 → 6.0.5

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.
@@ -8,6 +8,7 @@ const Backups_1 = require("./Backups");
8
8
  const Restore_1 = require("./Restore");
9
9
  const BackupManager_1 = require("./BackupManager");
10
10
  const HashPassword_1 = require("./HashPassword");
11
+ const ProjectConfigWizard_1 = require("./ProjectConfigWizard");
11
12
  function usage() {
12
13
  console.error("Usage:");
13
14
  console.error(" badmfck deploy [config.json] — build + upload the current project");
@@ -17,6 +18,7 @@ function usage() {
17
18
  console.error(" badmfck restore [config.json] [backup] [--yes] — restore a backup (asks which, and confirms)");
18
19
  console.error(" badmfck hash-password [--deployer] — hash a password for the monitor (or deployer setup.json)");
19
20
  console.error(" badmfck project-token [--hash] — new project token + its hash (or hash an existing one)");
21
+ console.error(" badmfck project-config — build a project config for the deployer, step by step");
20
22
  console.error("");
21
23
  console.error("Config path is optional (auto-discovers deploy/ folder → deploy.json when omitted).");
22
24
  console.error("Via npm, prefix args with -- e.g. npm run deploy -- deploy/custom.json");
@@ -103,6 +105,14 @@ async function main() {
103
105
  await (0, HashPassword_1.HashPassword)(rest.includes("--deployer") ? "deployer" : "monitor");
104
106
  break;
105
107
  }
108
+ case "project-config": {
109
+ if (rest.length) {
110
+ console.error("project-config takes no arguments — everything is asked interactively.");
111
+ process.exit(1);
112
+ }
113
+ await (0, ProjectConfigWizard_1.ProjectConfigWizard)();
114
+ break;
115
+ }
106
116
  case "project-token": {
107
117
  const unknown = rest.filter(a => a !== "--hash");
108
118
  if (unknown.length) {
@@ -4,6 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.Documentation = void 0;
7
+ const LogService_1 = require("../LogService");
7
8
  const APIService_1 = require("../APIService");
8
9
  const BaseEndpoint_1 = require("../BaseEndpoint");
9
10
  const DocumentService_1 = require("../DocumentService");
@@ -88,8 +89,10 @@ class Documentation extends BaseEndpoint_1.BaseEndpoint {
88
89
  }
89
90
  async checkAuthorization(req) {
90
91
  const users = await APIService_1.REQ_DOC_USERS.request();
91
- if (!users || users.length == 0)
92
- throw { ...DefaultErrors_1.default.UNAUTHORIZED, details: "No users found for documentation access, please set up users first." };
92
+ if (!users || users.length == 0) {
93
+ (0, LogService_1.logWarn)("Documentation: access refused — no documentation users are configured (access.documentation)");
94
+ throw { ...DefaultErrors_1.default.UNAUTHORIZED };
95
+ }
93
96
  let header = req.headers['authorization'];
94
97
  if (!header)
95
98
  throw { ...DefaultErrors_1.default.UNAUTHORIZED, details: "No authorization header found" };
@@ -40,6 +40,9 @@ export declare class Monitor extends BaseEndpoint {
40
40
  inventory(req: HTTPRequestVO): Promise<TransferPacketVO>;
41
41
  metrics(req: HTTPRequestVO): Promise<TransferPacketVO>;
42
42
  threats(req: HTTPRequestVO): Promise<TransferPacketVO>;
43
+ blocked(req: HTTPRequestVO): Promise<TransferPacketVO>;
44
+ block(req: HTTPRequestVO): Promise<TransferPacketVO>;
45
+ unblock(req: HTTPRequestVO): Promise<TransferPacketVO>;
43
46
  logfiles(req: HTTPRequestVO): Promise<TransferPacketVO>;
44
47
  logtail(req: HTTPRequestVO): Promise<TransferPacketVO>;
45
48
  metricsWeeks(req: HTTPRequestVO): Promise<TransferPacketVO>;
@@ -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, details: "The monitor is closed: its address allow-list is invalid." } };
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, details: "The monitor is not available from this address." } };
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
- throw { ...DefaultErrors_1.default.UNAUTHORIZED, details: "No users found for monitor access, please set up users first." };
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
- return { data: ThreatMonitor_1.ThreatMonitor.snapshot(limit) };
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, details: "No users found for monitor access, please set up users first." };
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 — authenticate via sm/login" };
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: matches ? MonitorAuth.PLAINTEXT_ERROR : "Invalid user" };
381
+ return { error: "Invalid user" };
380
382
  }
381
383
  if (!(await PasswordHash_1.PasswordHash.verify(password || "", candidate.password))) {
382
384
  MonitorAuth.registerFailure(login);