@webdecoy/node 0.22.3 → 0.23.0

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.mjs CHANGED
@@ -11,7 +11,7 @@ import {
11
11
  resolveGateEnv,
12
12
  ruleBlockResponse,
13
13
  shouldSkipPath
14
- } from "./chunk-MW35VR5L.mjs";
14
+ } from "./chunk-HQVMNN6X.mjs";
15
15
  import {
16
16
  ATTACK_SIGNATURE_IDS,
17
17
  AgentVerifier,
@@ -68,7 +68,7 @@ import {
68
68
  tripwire,
69
69
  upstashRateLimitStore,
70
70
  webBotAuth
71
- } from "./chunk-MMJO4XZB.mjs";
71
+ } from "./chunk-ANOZMV4M.mjs";
72
72
 
73
73
  // src/client-signals.ts
74
74
  var MemoryClientSignalStore = class {
package/dist/testing.js CHANGED
@@ -1228,8 +1228,31 @@ function base64urlBytes(bytes) {
1228
1228
  return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
1229
1229
  }
1230
1230
 
1231
+ // src/agent/identity.ts
1232
+ var WELL_KNOWN_DIRECTORY = "/.well-known/http-message-signatures-directory";
1233
+ function resolveSignatureAgent(value) {
1234
+ if (!/^https:\/\/[^/\\]/i.test(value)) throw new Error("signature-agent must be an https URL");
1235
+ const url = new URL(value);
1236
+ if (url.protocol !== "https:" || !url.hostname || url.username || url.password) {
1237
+ throw new Error("signature-agent must be an https URL");
1238
+ }
1239
+ url.hash = "";
1240
+ const hasQuery = url.href.includes("?");
1241
+ const authority = canonicalAuthority(url);
1242
+ if (url.pathname === "/" && !hasQuery) {
1243
+ const wellKnown = `https://${authority}${WELL_KNOWN_DIRECTORY}`;
1244
+ return { fetch: wellKnown, identifier: wellKnown };
1245
+ }
1246
+ const path = url.pathname.replace(/%[0-9a-f]{2}/gi, (m) => m.toUpperCase());
1247
+ return { fetch: url.href, identifier: `https://${authority}${path}` };
1248
+ }
1249
+ function canonicalAuthority(url) {
1250
+ const host = url.hostname.toLowerCase();
1251
+ if (!url.port || url.port === "443") return host;
1252
+ return `${host}:${url.port}`;
1253
+ }
1254
+
1231
1255
  // src/agent/directory.ts
1232
- var WELL_KNOWN = "/.well-known/http-message-signatures-directory";
1233
1256
  var COLD_RETRY_BACKOFF_MS = 3e4;
1234
1257
  var DEFAULT_SIGNED_AGENT_DIRECTORIES = [
1235
1258
  { name: "OpenAI ChatGPT", category: "ai_crawlers", directory: "https://chatgpt.com" },
@@ -1242,10 +1265,14 @@ var DirectoryCache = class {
1242
1265
  this.refreshedAt = 0;
1243
1266
  this.nextColdRetryAt = 0;
1244
1267
  this.inflight = null;
1245
- /** Resolve a keyid to a verification key, fetching/refreshing as needed. */
1246
- this.resolveKey = async (keyId) => {
1268
+ /**
1269
+ * Resolve a keyid published by one agent identity (a resolved
1270
+ * Signature-Agent URL) to a verification key, fetching/refreshing as
1271
+ * needed. A key is only ever attributed to the directory that published it.
1272
+ */
1273
+ this.resolveKey = async (keyId, agent) => {
1247
1274
  await this.ensureFresh();
1248
- return this.keys.get(keyId);
1275
+ return this.keys.get(cacheKey(agent, keyId));
1249
1276
  };
1250
1277
  this.opts = opts;
1251
1278
  }
@@ -1284,7 +1311,7 @@ var DirectoryCache = class {
1284
1311
  for (const keys of results) {
1285
1312
  if (keys === null) continue;
1286
1313
  fetchedAny = true;
1287
- for (const k of keys) next.set(k.thumbprint, k.key);
1314
+ for (const k of keys) next.set(cacheKey(k.identifier, k.thumbprint), k.key);
1288
1315
  }
1289
1316
  if (fetchedAny) {
1290
1317
  this.keys = next;
@@ -1297,7 +1324,14 @@ var DirectoryCache = class {
1297
1324
  }
1298
1325
  }
1299
1326
  async fetchDirectory(src) {
1300
- const url = directoryUrl(src.directory);
1327
+ let url;
1328
+ let identifier;
1329
+ try {
1330
+ ({ fetch: url, identifier } = resolveSignatureAgent(src.directory));
1331
+ } catch {
1332
+ this.debug(`${src.name}: directory is not an https URL`);
1333
+ return null;
1334
+ }
1301
1335
  const controller = new AbortController();
1302
1336
  const timer = setTimeout(() => controller.abort(), this.opts.fetchTimeoutMs);
1303
1337
  try {
@@ -1306,7 +1340,8 @@ var DirectoryCache = class {
1306
1340
  headers: {
1307
1341
  Accept: "application/http-message-signatures-directory+json, application/jwk-set+json, application/json"
1308
1342
  },
1309
- redirect: "follow"
1343
+ // Web Bot Auth draft 01 §5.5: redirects are a discovery failure.
1344
+ redirect: "error"
1310
1345
  });
1311
1346
  if (!res.ok) {
1312
1347
  this.debug(`${src.name}: HTTP ${res.status}`);
@@ -1320,7 +1355,7 @@ var DirectoryCache = class {
1320
1355
  const out = [];
1321
1356
  for (const jwk of body.keys) {
1322
1357
  const resolved = await importKey(jwk, src);
1323
- if (resolved) out.push(resolved);
1358
+ if (resolved) out.push({ identifier, ...resolved });
1324
1359
  }
1325
1360
  this.debug(`${src.name}: ${out.length} key(s)`);
1326
1361
  return out;
@@ -1335,11 +1370,8 @@ var DirectoryCache = class {
1335
1370
  if (this.opts.debug) console.log(`[WebDecoy][web-bot-auth] ${msg}`);
1336
1371
  }
1337
1372
  };
1338
- function directoryUrl(directory) {
1339
- const u = new URL(directory);
1340
- if (u.pathname && u.pathname !== "/") return u.toString();
1341
- u.pathname = WELL_KNOWN;
1342
- return u.toString();
1373
+ function cacheKey(agent, keyId) {
1374
+ return `${agent} ${keyId}`;
1343
1375
  }
1344
1376
  async function importKey(jwk, src) {
1345
1377
  try {
@@ -1409,6 +1441,14 @@ var Cursor = class {
1409
1441
  while (!this.eof() && (this.peek() === " " || this.peek() === " ")) this.i++;
1410
1442
  }
1411
1443
  };
1444
+ function parseBareString(input) {
1445
+ const p = new Cursor(input);
1446
+ p.skipSP();
1447
+ const value = parseString(p);
1448
+ p.skipSP();
1449
+ if (!p.eof()) throw new Error("trailing data after string");
1450
+ return value;
1451
+ }
1412
1452
  function parseDictionary(input) {
1413
1453
  const p = new Cursor(input);
1414
1454
  const out = [];
@@ -1576,7 +1616,14 @@ async function verifyWebBotAuth(req, resolveKey, toleranceSec) {
1576
1616
  const keyid = m.params.get("keyid");
1577
1617
  if (typeof keyid !== "string") continue;
1578
1618
  if (!m.components.some((c) => c.name === "@authority" || c.name === "@target-uri")) continue;
1579
- const key = await resolveKey(keyid);
1619
+ let agent;
1620
+ try {
1621
+ agent = coveredSignatureAgent(req, m);
1622
+ } catch {
1623
+ best = higher(best, { outcome: "claimed", keyId: keyid, reason: "unbound-agent" });
1624
+ continue;
1625
+ }
1626
+ const key = await resolveKey(keyid, agent);
1580
1627
  if (!key) {
1581
1628
  best = higher(best, { outcome: "claimed", keyId: keyid, reason: "unknown-key" });
1582
1629
  continue;
@@ -1649,11 +1696,11 @@ function componentValue(req, c) {
1649
1696
  case "@method":
1650
1697
  return req.method.toUpperCase();
1651
1698
  case "@authority":
1652
- return canonicalAuthority(req.url);
1699
+ return canonicalAuthority2(req.url);
1653
1700
  case "@scheme":
1654
1701
  return req.url.protocol.replace(/:$/, "").toLowerCase();
1655
1702
  case "@target-uri":
1656
- return `${req.url.protocol}//${canonicalAuthority(req.url)}${req.url.pathname}${req.url.search}`;
1703
+ return `${req.url.protocol}//${canonicalAuthority2(req.url)}${req.url.pathname}${req.url.search}`;
1657
1704
  case "@path":
1658
1705
  return req.url.pathname;
1659
1706
  case "@query":
@@ -1672,7 +1719,7 @@ function componentValue(req, c) {
1672
1719
  }
1673
1720
  }
1674
1721
  }
1675
- function canonicalAuthority(url) {
1722
+ function canonicalAuthority2(url) {
1676
1723
  const host = url.hostname.toLowerCase();
1677
1724
  const port = url.port;
1678
1725
  if (!port) return host;
@@ -1687,6 +1734,28 @@ function dictMemberValue(header2, key) {
1687
1734
  if (!found) throw new Error(`signature-agent has no member ${key}`);
1688
1735
  return found.raw;
1689
1736
  }
1737
+ function coveredSignatureAgent(req, m) {
1738
+ const header2 = req.headers.get("signature-agent");
1739
+ if (header2 === null) throw new Error("missing signature-agent");
1740
+ const component = m.components.find((c) => c.name.toLowerCase() === "signature-agent");
1741
+ if (!component) throw new Error("signature-agent is not covered");
1742
+ const trimmed = header2.trim();
1743
+ let value;
1744
+ if (trimmed.startsWith('"')) {
1745
+ if (component.keyParam !== void 0) throw new Error("legacy string cannot select a member");
1746
+ value = parseBareString(trimmed);
1747
+ } else {
1748
+ if (component.keyParam === void 0) throw new Error("dictionary member is not covered");
1749
+ const member = parseDictionary(trimmed).find((d) => d.key === component.keyParam);
1750
+ const item = member?.items[0];
1751
+ if (!member || member.isList || member.items.length !== 1 || typeof item?.value !== "string") {
1752
+ throw new Error("signature-agent member is not a string");
1753
+ }
1754
+ if (item.params.size > 0) throw new Error("signature-agent member carries parameters");
1755
+ value = item.value;
1756
+ }
1757
+ return resolveSignatureAgent(value).identifier;
1758
+ }
1690
1759
  function parseSignatureInput(value) {
1691
1760
  return parseDictionary(value).map((d) => {
1692
1761
  if (!d.isList) throw new Error(`Signature-Input member ${d.key} is not an inner list`);
package/dist/testing.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  WebDecoy,
3
3
  silentLogger
4
- } from "./chunk-MMJO4XZB.mjs";
4
+ } from "./chunk-ANOZMV4M.mjs";
5
5
 
6
6
  // src/testing.ts
7
7
  function request(over = {}) {