@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/README.md +7 -1
- package/dist/{chunk-MMJO4XZB.mjs → chunk-ANOZMV4M.mjs} +86 -17
- package/dist/{chunk-MW35VR5L.mjs → chunk-HQVMNN6X.mjs} +206 -201
- package/dist/index.d.mts +11 -3
- package/dist/index.d.ts +11 -3
- package/dist/index.js +291 -217
- package/dist/index.mjs +2 -2
- package/dist/testing.js +86 -17
- package/dist/testing.mjs +1 -1
- package/dist/vercel.js +291 -217
- package/dist/vercel.mjs +2 -2
- package/package.json +20 -8
package/README.md
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="https://raw.githubusercontent.com/WebDecoy/node/main/docs/assets/node-banner.png" alt="WebDecoy for Node.js: bot detection, AI agent verification and local rules" width="100%">
|
|
3
|
+
</p>
|
|
4
|
+
|
|
1
5
|
# @webdecoy/node
|
|
2
6
|
|
|
3
|
-
|
|
7
|
+
TypeScript bot detection and protection for Node.js and edge runtimes. Run honeypot tripwires, rate limits, AI crawler policies and signed-agent verification locally without an account or API key. Add optional hosted analysis and WebDecoy dashboard reporting when you need them.
|
|
4
8
|
|
|
5
9
|
[](https://www.npmjs.com/package/@webdecoy/node)
|
|
6
10
|
[](https://opensource.org/licenses/MIT)
|
|
7
11
|
|
|
8
12
|
Full documentation: [docs.webdecoy.com/sdk-plugins/node-sdk](https://docs.webdecoy.com/sdk-plugins/node-sdk/). Copy-paste install snippets for every stack: [docs.webdecoy.com/installation/install-with-ai](https://docs.webdecoy.com/installation/install-with-ai/).
|
|
9
13
|
|
|
14
|
+
**[Try the Express playground in your browser](https://stackblitz.com/github/WebDecoy/node/tree/main/playgrounds/express?file=app.mjs)** · [Watch the 30-second demo](https://youtu.be/4jeuH3tKeos)
|
|
15
|
+
|
|
10
16
|
## Using a framework? Install its adapter
|
|
11
17
|
|
|
12
18
|
Most apps should install a framework adapter alongside this package. The adapters handle request mapping, client IP, monitor mode and honeytoken injection for you. Install `@webdecoy/node` too: it is where `tripwire()`, `rateLimit()` and the other rules are imported from.
|
|
@@ -1755,8 +1755,31 @@ function base64urlBytes(bytes) {
|
|
|
1755
1755
|
return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
1756
1756
|
}
|
|
1757
1757
|
|
|
1758
|
+
// src/agent/identity.ts
|
|
1759
|
+
var WELL_KNOWN_DIRECTORY = "/.well-known/http-message-signatures-directory";
|
|
1760
|
+
function resolveSignatureAgent(value) {
|
|
1761
|
+
if (!/^https:\/\/[^/\\]/i.test(value)) throw new Error("signature-agent must be an https URL");
|
|
1762
|
+
const url = new URL(value);
|
|
1763
|
+
if (url.protocol !== "https:" || !url.hostname || url.username || url.password) {
|
|
1764
|
+
throw new Error("signature-agent must be an https URL");
|
|
1765
|
+
}
|
|
1766
|
+
url.hash = "";
|
|
1767
|
+
const hasQuery = url.href.includes("?");
|
|
1768
|
+
const authority = canonicalAuthority(url);
|
|
1769
|
+
if (url.pathname === "/" && !hasQuery) {
|
|
1770
|
+
const wellKnown = `https://${authority}${WELL_KNOWN_DIRECTORY}`;
|
|
1771
|
+
return { fetch: wellKnown, identifier: wellKnown };
|
|
1772
|
+
}
|
|
1773
|
+
const path = url.pathname.replace(/%[0-9a-f]{2}/gi, (m) => m.toUpperCase());
|
|
1774
|
+
return { fetch: url.href, identifier: `https://${authority}${path}` };
|
|
1775
|
+
}
|
|
1776
|
+
function canonicalAuthority(url) {
|
|
1777
|
+
const host = url.hostname.toLowerCase();
|
|
1778
|
+
if (!url.port || url.port === "443") return host;
|
|
1779
|
+
return `${host}:${url.port}`;
|
|
1780
|
+
}
|
|
1781
|
+
|
|
1758
1782
|
// src/agent/directory.ts
|
|
1759
|
-
var WELL_KNOWN = "/.well-known/http-message-signatures-directory";
|
|
1760
1783
|
var COLD_RETRY_BACKOFF_MS = 3e4;
|
|
1761
1784
|
var DEFAULT_SIGNED_AGENT_DIRECTORIES = [
|
|
1762
1785
|
{ name: "OpenAI ChatGPT", category: "ai_crawlers", directory: "https://chatgpt.com" },
|
|
@@ -1769,10 +1792,14 @@ var DirectoryCache = class {
|
|
|
1769
1792
|
this.refreshedAt = 0;
|
|
1770
1793
|
this.nextColdRetryAt = 0;
|
|
1771
1794
|
this.inflight = null;
|
|
1772
|
-
/**
|
|
1773
|
-
|
|
1795
|
+
/**
|
|
1796
|
+
* Resolve a keyid published by one agent identity (a resolved
|
|
1797
|
+
* Signature-Agent URL) to a verification key, fetching/refreshing as
|
|
1798
|
+
* needed. A key is only ever attributed to the directory that published it.
|
|
1799
|
+
*/
|
|
1800
|
+
this.resolveKey = async (keyId, agent) => {
|
|
1774
1801
|
await this.ensureFresh();
|
|
1775
|
-
return this.keys.get(keyId);
|
|
1802
|
+
return this.keys.get(cacheKey(agent, keyId));
|
|
1776
1803
|
};
|
|
1777
1804
|
this.opts = opts;
|
|
1778
1805
|
}
|
|
@@ -1811,7 +1838,7 @@ var DirectoryCache = class {
|
|
|
1811
1838
|
for (const keys of results) {
|
|
1812
1839
|
if (keys === null) continue;
|
|
1813
1840
|
fetchedAny = true;
|
|
1814
|
-
for (const k of keys) next.set(k.thumbprint, k.key);
|
|
1841
|
+
for (const k of keys) next.set(cacheKey(k.identifier, k.thumbprint), k.key);
|
|
1815
1842
|
}
|
|
1816
1843
|
if (fetchedAny) {
|
|
1817
1844
|
this.keys = next;
|
|
@@ -1824,7 +1851,14 @@ var DirectoryCache = class {
|
|
|
1824
1851
|
}
|
|
1825
1852
|
}
|
|
1826
1853
|
async fetchDirectory(src) {
|
|
1827
|
-
|
|
1854
|
+
let url;
|
|
1855
|
+
let identifier;
|
|
1856
|
+
try {
|
|
1857
|
+
({ fetch: url, identifier } = resolveSignatureAgent(src.directory));
|
|
1858
|
+
} catch {
|
|
1859
|
+
this.debug(`${src.name}: directory is not an https URL`);
|
|
1860
|
+
return null;
|
|
1861
|
+
}
|
|
1828
1862
|
const controller = new AbortController();
|
|
1829
1863
|
const timer = setTimeout(() => controller.abort(), this.opts.fetchTimeoutMs);
|
|
1830
1864
|
try {
|
|
@@ -1833,7 +1867,8 @@ var DirectoryCache = class {
|
|
|
1833
1867
|
headers: {
|
|
1834
1868
|
Accept: "application/http-message-signatures-directory+json, application/jwk-set+json, application/json"
|
|
1835
1869
|
},
|
|
1836
|
-
|
|
1870
|
+
// Web Bot Auth draft 01 §5.5: redirects are a discovery failure.
|
|
1871
|
+
redirect: "error"
|
|
1837
1872
|
});
|
|
1838
1873
|
if (!res.ok) {
|
|
1839
1874
|
this.debug(`${src.name}: HTTP ${res.status}`);
|
|
@@ -1847,7 +1882,7 @@ var DirectoryCache = class {
|
|
|
1847
1882
|
const out = [];
|
|
1848
1883
|
for (const jwk of body.keys) {
|
|
1849
1884
|
const resolved = await importKey(jwk, src);
|
|
1850
|
-
if (resolved) out.push(resolved);
|
|
1885
|
+
if (resolved) out.push({ identifier, ...resolved });
|
|
1851
1886
|
}
|
|
1852
1887
|
this.debug(`${src.name}: ${out.length} key(s)`);
|
|
1853
1888
|
return out;
|
|
@@ -1862,11 +1897,8 @@ var DirectoryCache = class {
|
|
|
1862
1897
|
if (this.opts.debug) console.log(`[WebDecoy][web-bot-auth] ${msg}`);
|
|
1863
1898
|
}
|
|
1864
1899
|
};
|
|
1865
|
-
function
|
|
1866
|
-
|
|
1867
|
-
if (u.pathname && u.pathname !== "/") return u.toString();
|
|
1868
|
-
u.pathname = WELL_KNOWN;
|
|
1869
|
-
return u.toString();
|
|
1900
|
+
function cacheKey(agent, keyId) {
|
|
1901
|
+
return `${agent} ${keyId}`;
|
|
1870
1902
|
}
|
|
1871
1903
|
async function importKey(jwk, src) {
|
|
1872
1904
|
try {
|
|
@@ -1936,6 +1968,14 @@ var Cursor = class {
|
|
|
1936
1968
|
while (!this.eof() && (this.peek() === " " || this.peek() === " ")) this.i++;
|
|
1937
1969
|
}
|
|
1938
1970
|
};
|
|
1971
|
+
function parseBareString(input) {
|
|
1972
|
+
const p = new Cursor(input);
|
|
1973
|
+
p.skipSP();
|
|
1974
|
+
const value = parseString(p);
|
|
1975
|
+
p.skipSP();
|
|
1976
|
+
if (!p.eof()) throw new Error("trailing data after string");
|
|
1977
|
+
return value;
|
|
1978
|
+
}
|
|
1939
1979
|
function parseDictionary(input) {
|
|
1940
1980
|
const p = new Cursor(input);
|
|
1941
1981
|
const out = [];
|
|
@@ -2103,7 +2143,14 @@ async function verifyWebBotAuth(req, resolveKey, toleranceSec) {
|
|
|
2103
2143
|
const keyid = m.params.get("keyid");
|
|
2104
2144
|
if (typeof keyid !== "string") continue;
|
|
2105
2145
|
if (!m.components.some((c) => c.name === "@authority" || c.name === "@target-uri")) continue;
|
|
2106
|
-
|
|
2146
|
+
let agent;
|
|
2147
|
+
try {
|
|
2148
|
+
agent = coveredSignatureAgent(req, m);
|
|
2149
|
+
} catch {
|
|
2150
|
+
best = higher(best, { outcome: "claimed", keyId: keyid, reason: "unbound-agent" });
|
|
2151
|
+
continue;
|
|
2152
|
+
}
|
|
2153
|
+
const key = await resolveKey(keyid, agent);
|
|
2107
2154
|
if (!key) {
|
|
2108
2155
|
best = higher(best, { outcome: "claimed", keyId: keyid, reason: "unknown-key" });
|
|
2109
2156
|
continue;
|
|
@@ -2176,11 +2223,11 @@ function componentValue(req, c) {
|
|
|
2176
2223
|
case "@method":
|
|
2177
2224
|
return req.method.toUpperCase();
|
|
2178
2225
|
case "@authority":
|
|
2179
|
-
return
|
|
2226
|
+
return canonicalAuthority2(req.url);
|
|
2180
2227
|
case "@scheme":
|
|
2181
2228
|
return req.url.protocol.replace(/:$/, "").toLowerCase();
|
|
2182
2229
|
case "@target-uri":
|
|
2183
|
-
return `${req.url.protocol}//${
|
|
2230
|
+
return `${req.url.protocol}//${canonicalAuthority2(req.url)}${req.url.pathname}${req.url.search}`;
|
|
2184
2231
|
case "@path":
|
|
2185
2232
|
return req.url.pathname;
|
|
2186
2233
|
case "@query":
|
|
@@ -2199,7 +2246,7 @@ function componentValue(req, c) {
|
|
|
2199
2246
|
}
|
|
2200
2247
|
}
|
|
2201
2248
|
}
|
|
2202
|
-
function
|
|
2249
|
+
function canonicalAuthority2(url) {
|
|
2203
2250
|
const host = url.hostname.toLowerCase();
|
|
2204
2251
|
const port = url.port;
|
|
2205
2252
|
if (!port) return host;
|
|
@@ -2214,6 +2261,28 @@ function dictMemberValue(header2, key) {
|
|
|
2214
2261
|
if (!found) throw new Error(`signature-agent has no member ${key}`);
|
|
2215
2262
|
return found.raw;
|
|
2216
2263
|
}
|
|
2264
|
+
function coveredSignatureAgent(req, m) {
|
|
2265
|
+
const header2 = req.headers.get("signature-agent");
|
|
2266
|
+
if (header2 === null) throw new Error("missing signature-agent");
|
|
2267
|
+
const component = m.components.find((c) => c.name.toLowerCase() === "signature-agent");
|
|
2268
|
+
if (!component) throw new Error("signature-agent is not covered");
|
|
2269
|
+
const trimmed = header2.trim();
|
|
2270
|
+
let value;
|
|
2271
|
+
if (trimmed.startsWith('"')) {
|
|
2272
|
+
if (component.keyParam !== void 0) throw new Error("legacy string cannot select a member");
|
|
2273
|
+
value = parseBareString(trimmed);
|
|
2274
|
+
} else {
|
|
2275
|
+
if (component.keyParam === void 0) throw new Error("dictionary member is not covered");
|
|
2276
|
+
const member = parseDictionary(trimmed).find((d) => d.key === component.keyParam);
|
|
2277
|
+
const item = member?.items[0];
|
|
2278
|
+
if (!member || member.isList || member.items.length !== 1 || typeof item?.value !== "string") {
|
|
2279
|
+
throw new Error("signature-agent member is not a string");
|
|
2280
|
+
}
|
|
2281
|
+
if (item.params.size > 0) throw new Error("signature-agent member carries parameters");
|
|
2282
|
+
value = item.value;
|
|
2283
|
+
}
|
|
2284
|
+
return resolveSignatureAgent(value).identifier;
|
|
2285
|
+
}
|
|
2217
2286
|
function parseSignatureInput(value) {
|
|
2218
2287
|
return parseDictionary(value).map((d) => {
|
|
2219
2288
|
if (!d.isList) throw new Error(`Signature-Input member ${d.key} is not an inner list`);
|