@profullstack/threatcrush 0.13.62 → 0.13.63

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/index.js CHANGED
@@ -30409,37 +30409,63 @@ var DEFAULT_CONFIG = {
30409
30409
  config_dir: DEFAULT_CONFDIR
30410
30410
  }
30411
30411
  };
30412
- function loadConfig(configPath) {
30412
+ function loadConfig(configPath, onError) {
30413
30413
  const path2 = configPath || DEFAULT_CONFIG_PATH;
30414
30414
  if (!(0, import_node_fs5.existsSync)(path2)) {
30415
30415
  return { ...DEFAULT_CONFIG };
30416
30416
  }
30417
30417
  try {
30418
- const raw = (0, import_node_fs5.readFileSync)(path2, "utf-8");
30419
- const parsed = import_toml.default.parse(raw);
30420
- return {
30421
- daemon: { ...DEFAULT_CONFIG.daemon, ...parsed.daemon },
30422
- api: { ...DEFAULT_CONFIG.api, ...parsed.api },
30423
- alerts: parsed.alerts || {},
30424
- modules: { ...DEFAULT_CONFIG.modules, ...parsed.modules },
30425
- remediation: parsed.remediation,
30426
- detection: parsed.detection,
30427
- cloud: parsed.cloud,
30428
- watchdog: parsed.watchdog,
30429
- update: parsed.update,
30430
- threat_feeds: parsed.threat_feeds,
30431
- privileged: parsed.privileged,
30432
- lightning: parsed.lightning,
30433
- storage: parsed.storage,
30434
- // TOML's `[abuse-reporter]` parses to a hyphenated key; expose it under
30435
- // the JS-friendly name the rest of the daemon reads.
30436
- abuse_reporter: parsed["abuse-reporter"] ?? parsed.abuse_reporter,
30437
- license: parsed.license
30438
- };
30439
- } catch {
30418
+ return parseConfigText((0, import_node_fs5.readFileSync)(path2, "utf-8"));
30419
+ } catch (err) {
30420
+ onError?.(err);
30440
30421
  return { ...DEFAULT_CONFIG };
30441
30422
  }
30442
30423
  }
30424
+ function parseConfigText(raw) {
30425
+ const parsed = import_toml.default.parse(raw);
30426
+ return {
30427
+ daemon: { ...DEFAULT_CONFIG.daemon, ...parsed.daemon },
30428
+ api: { ...DEFAULT_CONFIG.api, ...parsed.api },
30429
+ alerts: parsed.alerts || {},
30430
+ modules: { ...DEFAULT_CONFIG.modules, ...parsed.modules },
30431
+ remediation: parsed.remediation,
30432
+ detection: parsed.detection,
30433
+ cloud: parsed.cloud,
30434
+ watchdog: parsed.watchdog,
30435
+ update: parsed.update,
30436
+ threat_feeds: parsed.threat_feeds,
30437
+ privileged: parsed.privileged,
30438
+ lightning: parsed.lightning,
30439
+ storage: parsed.storage,
30440
+ // TOML's `[abuse-reporter]` parses to a hyphenated key; expose it under
30441
+ // the JS-friendly name the rest of the daemon reads.
30442
+ abuse_reporter: parsed["abuse-reporter"] ?? parsed.abuse_reporter,
30443
+ license: parsed.license
30444
+ };
30445
+ }
30446
+ function loadDaemonConfig(configPath, lastGoodPath) {
30447
+ if (!(0, import_node_fs5.existsSync)(configPath)) return { config: { ...DEFAULT_CONFIG }, usedLastGood: false };
30448
+ let raw = "";
30449
+ try {
30450
+ raw = (0, import_node_fs5.readFileSync)(configPath, "utf-8");
30451
+ const config = parseConfigText(raw);
30452
+ try {
30453
+ (0, import_node_fs5.mkdirSync)((0, import_node_path4.dirname)(lastGoodPath), { recursive: true });
30454
+ (0, import_node_fs5.writeFileSync)(lastGoodPath, raw, { mode: 384 });
30455
+ } catch {
30456
+ }
30457
+ return { config, usedLastGood: false };
30458
+ } catch (err) {
30459
+ const error2 = `${configPath} is not valid TOML: ${err.message.split("\n")[0]}`;
30460
+ try {
30461
+ if ((0, import_node_fs5.existsSync)(lastGoodPath)) {
30462
+ return { config: parseConfigText((0, import_node_fs5.readFileSync)(lastGoodPath, "utf-8")), error: error2, usedLastGood: true };
30463
+ }
30464
+ } catch {
30465
+ }
30466
+ return { config: { ...DEFAULT_CONFIG }, error: error2, usedLastGood: false };
30467
+ }
30468
+ }
30443
30469
  function loadModuleConfigs(confDir) {
30444
30470
  const dir = confDir || DEFAULT_CONFDIR;
30445
30471
  const configs = /* @__PURE__ */ new Map();
@@ -48427,7 +48453,11 @@ async function runDaemon() {
48427
48453
  } catch (err) {
48428
48454
  logLine(`[daemon] state db unavailable: ${err.message}`);
48429
48455
  }
48430
- const config = loadConfig((0, import_node_fs47.existsSync)(PATHS.configFile) ? PATHS.configFile : void 0);
48456
+ const loaded = loadDaemonConfig(PATHS.configFile, (0, import_node_path36.join)(PATHS.stateDir, "threatcrushd.conf.last-good"));
48457
+ const config = loaded.config;
48458
+ if (loaded.error) {
48459
+ logLine(`[daemon] CONFIG ERROR: ${loaded.error}; ${loaded.usedLastGood ? "running on the last config that parsed" : "no earlier good copy, running on DEFAULTS (no protected list)"}`);
48460
+ }
48431
48461
  configureAttackDetection(config.detection);
48432
48462
  const retentionSeconds = parseDuration(config.storage?.event_retention) ?? DEFAULT_EVENT_RETENTION_SECONDS;
48433
48463
  const maxEvents = config.storage?.max_events ?? DEFAULT_MAX_EVENTS;
@@ -48528,6 +48558,16 @@ async function runDaemon() {
48528
48558
  lightningWatch.start();
48529
48559
  let cloudSyncRef = null;
48530
48560
  new AlertDispatcher(bus, config, { cloudHealthy: () => cloudSyncRef?.healthy ?? false, log: logLine });
48561
+ if (loaded.error) {
48562
+ setTimeout(() => bus.publish({
48563
+ timestamp: /* @__PURE__ */ new Date(),
48564
+ module: "daemon",
48565
+ category: "system",
48566
+ severity: "critical",
48567
+ message: `threatcrushd.conf does not parse; ${loaded.usedLastGood ? "running on the last good copy" : "running on defaults with NO protected list"}: ${loaded.error}`.slice(0, 500),
48568
+ details: { rule_id: "daemon-config-invalid", used_last_good: loaded.usedLastGood }
48569
+ }), 15e3).unref?.();
48570
+ }
48531
48571
  const runsWorker = new RunsWorker(bus, {
48532
48572
  enabled: config.cloud?.enabled !== false && config.cloud?.runs === true,
48533
48573
  scanRoots: Array.isArray(config.cloud?.run_scan_paths) ? config.cloud.run_scan_paths.filter((p) => typeof p === "string") : [],