@patchstack/connect 0.3.14 → 0.3.15

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.
@@ -20,6 +20,9 @@ import {
20
20
  } from "@patchstack/connect/protect";
21
21
  import fallbackRules from "./rules.json";
22
22
 
23
+ // Baked by `patchstack-connect protect` from .patchstackrc.json (empty if the app isn't scanned yet).
24
+ const PS_SITE_UUID = "__PATCHSTACK_SITE_UUID__";
25
+
23
26
  export { GUARD_PATH };
24
27
 
25
28
  // One shared protection policy for both guards (rules load once).
@@ -29,6 +32,7 @@ async function getProtection() {
29
32
  // Always-on: block by default. An explicit PATCHSTACK_MODE=dry-run downgrades to log-only.
30
33
  const mode = process.env.PATCHSTACK_MODE === "dry-run" ? "dry-run" : "block";
31
34
  const token = process.env.PATCHSTACK_WAF_TOKEN;
35
+ const siteUuid = PS_SITE_UUID.startsWith("__") ? process.env.PATCHSTACK_SITE_UUID : PS_SITE_UUID;
32
36
  // Egress SSRF screening: block the app's outbound calls to internal / metadata addresses,
33
37
  // but never its own Supabase project.
34
38
  let allowHosts: string[] = [];
@@ -39,9 +43,11 @@ async function getProtection() {
39
43
  }
40
44
  const common = { mode, egress: true, allowHosts };
41
45
  _protection = await createProtection(
42
- token
43
- ? { ...common, token, cacheDir: ".patchstack" } // live per-site rules from the Patchstack API (cached)
44
- : { ...common, rules: fallbackRules as never }, // demo fallback until a token is set
46
+ siteUuid
47
+ ? { ...common, siteUuid, rules: fallbackRules as never, cacheDir: ".patchstack" } // live per-site rules; bundled = offline fallback
48
+ : token
49
+ ? { ...common, token, cacheDir: ".patchstack" } // live per-site WAF rules from the Patchstack API (cached)
50
+ : { ...common, rules: fallbackRules as never }, // demo fallback until a site UUID / token is set
45
51
  );
46
52
  }
47
53
  return _protection;
package/dist/protect.cjs CHANGED
@@ -21,6 +21,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var runtime_exports = {};
22
22
  __export(runtime_exports, {
23
23
  GUARD_PATH: () => GUARD_PATH,
24
+ PulseRuleClient: () => PulseRuleClient,
24
25
  createProtection: () => createProtection,
25
26
  createServerFnGuard: () => createServerFnGuard,
26
27
  createSupabaseGuard: () => createSupabaseGuard
@@ -928,6 +929,64 @@ var RuleEngine = class {
928
929
  }
929
930
  };
930
931
 
932
+ // src/protect/engine/pulse-client.js
933
+ var DEFAULT_BASE_URL2 = "https://api.patchstack.com/monitor/pulse";
934
+ var DEFAULT_CACHE_TTL2 = 3e5;
935
+ var PulseRuleClient = class {
936
+ #siteUuid;
937
+ #baseUrl;
938
+ #cacheTtl;
939
+ #cache = null;
940
+ #cacheTime = null;
941
+ constructor({ siteUuid, baseUrl, cacheTtl } = {}) {
942
+ this.#siteUuid = siteUuid ?? process.env.PATCHSTACK_SITE_UUID;
943
+ this.#baseUrl = baseUrl ?? process.env.PATCHSTACK_PULSE_RULES_URL ?? DEFAULT_BASE_URL2;
944
+ this.#cacheTtl = cacheTtl ?? DEFAULT_CACHE_TTL2;
945
+ if (!this.#siteUuid) {
946
+ throw new Error("Patchstack site UUID is required. Pass { siteUuid } or set PATCHSTACK_SITE_UUID.");
947
+ }
948
+ }
949
+ async getRules() {
950
+ const now = Date.now();
951
+ if (this.#cache && this.#cacheTime && now - this.#cacheTime < this.#cacheTtl) {
952
+ return this.#cache;
953
+ }
954
+ const url = `${this.#baseUrl}/rules/${encodeURIComponent(this.#siteUuid)}`;
955
+ try {
956
+ const response = await fetch(url, {
957
+ method: "GET",
958
+ headers: { Accept: "application/json" },
959
+ signal: AbortSignal.timeout(3e4)
960
+ });
961
+ if (!response.ok) {
962
+ return { success: false, error: `API returned ${response.status}`, firewall: [], whitelists: [], whitelist_keys: {} };
963
+ }
964
+ const data = await response.json();
965
+ const result = {
966
+ success: true,
967
+ firewall: Array.isArray(data.firewall) ? data.firewall : [],
968
+ whitelists: Array.isArray(data.whitelists) ? data.whitelists : [],
969
+ whitelist_keys: data.whitelist_keys ?? {}
970
+ };
971
+ this.#cache = result;
972
+ this.#cacheTime = now;
973
+ return result;
974
+ } catch (err) {
975
+ return {
976
+ success: false,
977
+ error: err.name === "TimeoutError" ? "Request timed out" : err.message,
978
+ firewall: [],
979
+ whitelists: [],
980
+ whitelist_keys: {}
981
+ };
982
+ }
983
+ }
984
+ clearCache() {
985
+ this.#cache = null;
986
+ this.#cacheTime = null;
987
+ }
988
+ };
989
+
931
990
  // src/protect/engine/fetch.js
932
991
  async function fromFetchRequest(request) {
933
992
  const url = new URL(request.url);
@@ -1550,8 +1609,25 @@ function leakResponse() {
1550
1609
  });
1551
1610
  }
1552
1611
  async function resolveRules(options) {
1553
- if (options.rules) {
1554
- return normalizeBundle(options.rules);
1612
+ if (options.siteUuid) {
1613
+ const client = new PulseRuleClient({ siteUuid: options.siteUuid, baseUrl: options.pulseRulesUrl });
1614
+ const res = await client.getRules();
1615
+ if (res.success) {
1616
+ const bundle = normalizeBundle(res);
1617
+ cacheWrite(options.cacheDir, bundle);
1618
+ return bundle;
1619
+ }
1620
+ const cached = cacheRead(options.cacheDir);
1621
+ if (cached) {
1622
+ options.onError?.(new Error(`pulse rule fetch failed (${res.error}); using cached bundle`));
1623
+ return normalizeBundle(cached);
1624
+ }
1625
+ if (options.rules) {
1626
+ options.onError?.(new Error(`pulse rule fetch failed (${res.error}); using bundled fallback`));
1627
+ return normalizeBundle(options.rules);
1628
+ }
1629
+ options.onError?.(new Error(`pulse rule fetch failed (${res.error}); no cache \u2014 running with no rules`));
1630
+ return emptyBundle();
1555
1631
  }
1556
1632
  if (options.token) {
1557
1633
  const client = new PatchstackRuleClient({ token: options.token, baseUrl: options.baseUrl });
@@ -1569,6 +1645,9 @@ async function resolveRules(options) {
1569
1645
  options.onError?.(new Error(`rule fetch failed (${res.error}); no cache \u2014 running with no rules`));
1570
1646
  return emptyBundle();
1571
1647
  }
1648
+ if (options.rules) {
1649
+ return normalizeBundle(options.rules);
1650
+ }
1572
1651
  return emptyBundle();
1573
1652
  }
1574
1653
  function normalizeBundle(b) {
@@ -1620,6 +1699,7 @@ function defaultOnDetect({ phase, mode, category, rule, message }) {
1620
1699
  // Annotate the CommonJS export names for ESM import in node:
1621
1700
  0 && (module.exports = {
1622
1701
  GUARD_PATH,
1702
+ PulseRuleClient,
1623
1703
  createProtection,
1624
1704
  createServerFnGuard,
1625
1705
  createSupabaseGuard