@profullstack/threatcrush 0.13.4 → 0.13.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.
package/dist/index.js CHANGED
@@ -25655,6 +25655,48 @@ function defaultGateways() {
25655
25655
  return [...found];
25656
25656
  }
25657
25657
 
25658
+ // src/daemon/firewall/crawlers.ts
25659
+ var import_node_dns = require("dns");
25660
+ var CRAWLER_DOMAINS = ["googlebot.com", "google.com", "search.msn.com"];
25661
+ var systemResolver = {
25662
+ reverse: (ip) => import_node_dns.promises.reverse(ip),
25663
+ lookup: async (host) => (await import_node_dns.promises.lookup(host, { all: true })).map((a) => a.address)
25664
+ };
25665
+ var TIMEOUT_MS = 2e3;
25666
+ var CACHE_MS = 6 * 36e5;
25667
+ function withTimeout(p) {
25668
+ return Promise.race([
25669
+ p,
25670
+ new Promise((_, reject) => setTimeout(() => reject(new Error("dns timeout")), TIMEOUT_MS).unref())
25671
+ ]);
25672
+ }
25673
+ function inCrawlerDomain(host) {
25674
+ const h = host.toLowerCase().replace(/\.$/, "");
25675
+ return CRAWLER_DOMAINS.some((d) => h === d || h.endsWith(`.${d}`));
25676
+ }
25677
+ function crawlerVerifier(resolver = systemResolver) {
25678
+ const cache = /* @__PURE__ */ new Map();
25679
+ return async (ip) => {
25680
+ const hit = cache.get(ip);
25681
+ if (hit && hit.until > Date.now()) return hit.host;
25682
+ let host = null;
25683
+ try {
25684
+ const names = await withTimeout(resolver.reverse(ip));
25685
+ for (const name of names.filter(inCrawlerDomain)) {
25686
+ const addrs = await withTimeout(resolver.lookup(name));
25687
+ if (addrs.includes(ip)) {
25688
+ host = name.replace(/\.$/, "");
25689
+ break;
25690
+ }
25691
+ }
25692
+ } catch {
25693
+ host = null;
25694
+ }
25695
+ cache.set(ip, { host, until: Date.now() + CACHE_MS });
25696
+ return host;
25697
+ };
25698
+ }
25699
+
25658
25700
  // src/daemon/firewall/remediation.ts
25659
25701
  var DEFAULT_CONFIG2 = {
25660
25702
  enabled: true,
@@ -25663,7 +25705,8 @@ var DEFAULT_CONFIG2 = {
25663
25705
  max_ban_seconds: 86400,
25664
25706
  strike_memory_seconds: 86400,
25665
25707
  allowlist: [],
25666
- protect_current_ssh_client: true
25708
+ protect_current_ssh_client: true,
25709
+ spare_verified_crawlers: true
25667
25710
  };
25668
25711
  var SEVERITY_RANK4 = {
25669
25712
  info: 0,
@@ -25683,9 +25726,10 @@ function explainBlockFailure(err, backend) {
25683
25726
  return `threatcrushd is running ${who} and cannot manage ${backend}. A ban is written by the daemon, not by whoever asked for it. Fix it with one command, which asks for root itself: threatcrush install-service`;
25684
25727
  }
25685
25728
  var RemediationManager = class {
25686
- constructor(adapter, bus2, config) {
25729
+ constructor(adapter, bus2, config, verifyCrawler = crawlerVerifier()) {
25687
25730
  this.adapter = adapter;
25688
25731
  this.bus = bus2;
25732
+ this.verifyCrawler = verifyCrawler;
25689
25733
  this.config = { ...DEFAULT_CONFIG2, ...config };
25690
25734
  this.protectedList = [...DEFAULT_PROTECTED, ...this.config.allowlist];
25691
25735
  for (const gateway of defaultGateways()) this.protectedList.push(gateway);
@@ -25697,6 +25741,7 @@ var RemediationManager = class {
25697
25741
  }
25698
25742
  adapter;
25699
25743
  bus;
25744
+ verifyCrawler;
25700
25745
  config;
25701
25746
  blocklist = [];
25702
25747
  strikes = {};
@@ -25738,6 +25783,13 @@ var RemediationManager = class {
25738
25783
  if (!ip || (0, import_node_net5.isIP)(ip) === 0) return;
25739
25784
  if (this.isAllowlisted(ip)) return;
25740
25785
  if (this.blocklist.some((b) => b.ip === ip)) return;
25786
+ if (this.config.spare_verified_crawlers) {
25787
+ const crawler = await this.verifyCrawler(ip);
25788
+ if (crawler) {
25789
+ this.logLine(`[remediation] not banning ${ip}: verified crawler ${crawler} (${event.message})`);
25790
+ return;
25791
+ }
25792
+ }
25741
25793
  const ruleId = event.details?.rule_id;
25742
25794
  await this.ban(ip, event.message, { ruleId, source: "auto" });
25743
25795
  }
@@ -26013,6 +26065,9 @@ function remediationSettings(section) {
26013
26065
  if (typeof section.protect_current_ssh_client === "boolean") {
26014
26066
  settings.protect_current_ssh_client = section.protect_current_ssh_client;
26015
26067
  }
26068
+ if (typeof section.spare_verified_crawlers === "boolean") {
26069
+ settings.spare_verified_crawlers = section.spare_verified_crawlers;
26070
+ }
26016
26071
  return settings;
26017
26072
  }
26018
26073