@kya-os/checkpoint-shared 1.3.1 → 1.5.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/CHANGELOG.md +127 -2
- package/dist/beacon-types/index.d.mts +441 -432
- package/dist/beacon-types/index.d.mts.map +1 -0
- package/dist/beacon-types/index.d.ts +441 -432
- package/dist/beacon-types/index.d.ts.map +1 -0
- package/dist/beacon-types/index.js +198 -182
- package/dist/beacon-types/index.mjs +197 -179
- package/dist/beacon-types/index.mjs.map +1 -0
- package/dist/browser-posture-cookie-Dmq9Brf_.d.mts +5 -0
- package/dist/browser-posture-cookie-Dmq9Brf_.d.mts.map +1 -0
- package/dist/browser-posture-cookie-Dmq9Brf_.d.ts +5 -0
- package/dist/browser-posture-cookie-Dmq9Brf_.d.ts.map +1 -0
- package/dist/browser-posture-cookie.d.mts +2 -0
- package/dist/browser-posture-cookie.d.ts +2 -0
- package/dist/browser-posture-cookie.js +1 -0
- package/dist/browser-posture-cookie.mjs +2 -0
- package/dist/browser-posture-cookie.mjs.map +1 -0
- package/dist/browser-posture-report.d.mts +50 -82
- package/dist/browser-posture-report.d.mts.map +1 -0
- package/dist/browser-posture-report.d.ts +50 -82
- package/dist/browser-posture-report.d.ts.map +1 -0
- package/dist/browser-posture-report.js +1 -1
- package/dist/browser-posture-report.mjs +2 -1
- package/dist/browser-posture-report.mjs.map +1 -0
- package/dist/browser-posture.d.mts +80 -111
- package/dist/browser-posture.d.mts.map +1 -0
- package/dist/browser-posture.d.ts +80 -111
- package/dist/browser-posture.d.ts.map +1 -0
- package/dist/browser-posture.js +143 -4164
- package/dist/browser-posture.mjs +142 -4161
- package/dist/browser-posture.mjs.map +1 -0
- package/dist/browser.d.mts +323 -316
- package/dist/browser.d.mts.map +1 -0
- package/dist/browser.d.ts +323 -316
- package/dist/browser.d.ts.map +1 -0
- package/dist/browser.js +1 -1
- package/dist/browser.mjs +2 -1
- package/dist/browser.mjs.map +1 -0
- package/dist/canonical-signer/index.d.mts +38 -33
- package/dist/canonical-signer/index.d.mts.map +1 -0
- package/dist/canonical-signer/index.d.ts +38 -33
- package/dist/canonical-signer/index.d.ts.map +1 -0
- package/dist/canonical-signer/index.js +411 -181
- package/dist/canonical-signer/index.mjs +383 -175
- package/dist/canonical-signer/index.mjs.map +1 -0
- package/dist/clearance.d.mts +30 -0
- package/dist/clearance.d.mts.map +1 -0
- package/dist/clearance.d.ts +30 -0
- package/dist/clearance.d.ts.map +1 -0
- package/dist/clearance.js +120 -0
- package/dist/clearance.mjs +112 -0
- package/dist/clearance.mjs.map +1 -0
- package/dist/client-ip-1y2iXAYW.js +237 -0
- package/dist/client-ip-HOfe6ucV.mjs +215 -0
- package/dist/client-ip-HOfe6ucV.mjs.map +1 -0
- package/dist/deploy/index.d.mts +375 -810
- package/dist/deploy/index.d.mts.map +1 -0
- package/dist/deploy/index.d.ts +375 -810
- package/dist/deploy/index.d.ts.map +1 -0
- package/dist/deploy/index.js +505 -413
- package/dist/deploy/index.mjs +504 -410
- package/dist/deploy/index.mjs.map +1 -0
- package/dist/errors/index.d.mts +101 -101
- package/dist/errors/index.d.mts.map +1 -0
- package/dist/errors/index.d.ts +101 -101
- package/dist/errors/index.d.ts.map +1 -0
- package/dist/errors/index.js +341 -314
- package/dist/errors/index.mjs +340 -311
- package/dist/errors/index.mjs.map +1 -0
- package/dist/external-jr_S4iSS.d.mts +4187 -0
- package/dist/external-jr_S4iSS.d.mts.map +1 -0
- package/dist/external-jr_S4iSS.d.ts +4187 -0
- package/dist/external-jr_S4iSS.d.ts.map +1 -0
- package/dist/gateway/index.d.mts +154 -550
- package/dist/gateway/index.d.mts.map +1 -0
- package/dist/gateway/index.d.ts +154 -550
- package/dist/gateway/index.d.ts.map +1 -0
- package/dist/gateway/index.js +117 -81
- package/dist/gateway/index.mjs +116 -78
- package/dist/gateway/index.mjs.map +1 -0
- package/dist/gateway-client-ip.d.mts +59 -0
- package/dist/gateway-client-ip.d.mts.map +1 -0
- package/dist/gateway-client-ip.d.ts +59 -0
- package/dist/gateway-client-ip.d.ts.map +1 -0
- package/dist/gateway-client-ip.js +149 -0
- package/dist/gateway-client-ip.mjs +139 -0
- package/dist/gateway-client-ip.mjs.map +1 -0
- package/dist/index.d.mts +15523 -38968
- package/dist/index.d.mts.map +1 -0
- package/dist/index.d.ts +15523 -38968
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +14950 -16029
- package/dist/index.mjs +14846 -15955
- package/dist/index.mjs.map +1 -0
- package/dist/mcpi/agentshield-api/index.d.mts +45 -44
- package/dist/mcpi/agentshield-api/index.d.mts.map +1 -0
- package/dist/mcpi/agentshield-api/index.d.ts +45 -44
- package/dist/mcpi/agentshield-api/index.d.ts.map +1 -0
- package/dist/mcpi/agentshield-api/index.js +0 -2
- package/dist/mcpi/agentshield-api/index.mjs +1 -1
- package/dist/mcpi/approval-grant/index.d.mts +63 -61
- package/dist/mcpi/approval-grant/index.d.mts.map +1 -0
- package/dist/mcpi/approval-grant/index.d.ts +63 -61
- package/dist/mcpi/approval-grant/index.d.ts.map +1 -0
- package/dist/mcpi/approval-grant/index.js +220 -140
- package/dist/mcpi/approval-grant/index.mjs +219 -137
- package/dist/mcpi/approval-grant/index.mjs.map +1 -0
- package/dist/mcpi/dashboard-config/index.d.mts +2395 -13915
- package/dist/mcpi/dashboard-config/index.d.mts.map +1 -0
- package/dist/mcpi/dashboard-config/index.d.ts +2395 -13915
- package/dist/mcpi/dashboard-config/index.d.ts.map +1 -0
- package/dist/mcpi/dashboard-config/index.js +605 -458
- package/dist/mcpi/dashboard-config/index.mjs +604 -455
- package/dist/mcpi/dashboard-config/index.mjs.map +1 -0
- package/dist/mcpi/multibase-ed25519.d.mts +10 -8
- package/dist/mcpi/multibase-ed25519.d.mts.map +1 -0
- package/dist/mcpi/multibase-ed25519.d.ts +10 -8
- package/dist/mcpi/multibase-ed25519.d.ts.map +1 -0
- package/dist/mcpi/multibase-ed25519.js +182 -106
- package/dist/mcpi/multibase-ed25519.mjs +181 -103
- package/dist/mcpi/multibase-ed25519.mjs.map +1 -0
- package/dist/patterns-CkRVOdtV.js +243 -0
- package/dist/patterns-DzkGy_qk.mjs +173 -0
- package/dist/patterns-DzkGy_qk.mjs.map +1 -0
- package/dist/project-verification-key-C3L5rd8a.js +41 -0
- package/dist/project-verification-key-CgSVX9_m.mjs +37 -0
- package/dist/project-verification-key-CgSVX9_m.mjs.map +1 -0
- package/dist/rolldown-runtime-iY2fYEqi.mjs +19 -0
- package/dist/schemas-BmjNLAE7.js +5677 -0
- package/dist/schemas-DxY-dk9y.mjs +5511 -0
- package/dist/schemas-DxY-dk9y.mjs.map +1 -0
- package/dist/signals/index.d.mts +125 -115
- package/dist/signals/index.d.mts.map +1 -0
- package/dist/signals/index.d.ts +125 -115
- package/dist/signals/index.d.ts.map +1 -0
- package/dist/signals/index.js +1693 -1536
- package/dist/signals/index.mjs +1692 -1533
- package/dist/signals/index.mjs.map +1 -0
- package/dist/test-fixtures/index.d.mts +124 -119
- package/dist/test-fixtures/index.d.mts.map +1 -0
- package/dist/test-fixtures/index.d.ts +124 -119
- package/dist/test-fixtures/index.d.ts.map +1 -0
- package/dist/test-fixtures/index.js +460 -228
- package/dist/test-fixtures/index.mjs +459 -225
- package/dist/test-fixtures/index.mjs.map +1 -0
- package/dist/tool-protection/index.d.mts +368 -1067
- package/dist/tool-protection/index.d.mts.map +1 -0
- package/dist/tool-protection/index.d.ts +368 -1067
- package/dist/tool-protection/index.d.ts.map +1 -0
- package/dist/tool-protection/index.js +366 -285
- package/dist/tool-protection/index.mjs +365 -282
- package/dist/tool-protection/index.mjs.map +1 -0
- package/fixtures/bot-signal-resolution.json +142 -0
- package/fixtures/browser-posture-claims.json +421 -0
- package/fixtures/browser-posture-headers.json +46 -0
- package/fixtures/detection/reason-codes.json +10 -1
- package/fixtures/kya-http/challenge.delegation.fetcher-200.json +9 -3
- package/fixtures/kya-http/challenge.delegation.full.json +9 -3
- package/fixtures/kya-http/challenge.delegation.minimal.json +9 -3
- package/fixtures/kya-http/error-codes.json +7 -2
- package/package.json +21 -7
|
@@ -1,119 +1,197 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
//#region src/mcpi/did-key.ts
|
|
2
|
+
/**
|
|
3
|
+
* did:key (Ed25519 multikey) codec — draft-kya-http-02 / H1.2 (#3245).
|
|
4
|
+
*
|
|
5
|
+
* Format: `did:key:z<base58btc(0xed 0x01 || pubkey32)>`
|
|
6
|
+
* The multicodec prefix `0xed 0x01` identifies `ed25519-pub`; the multibase
|
|
7
|
+
* prefix `z` identifies base58btc. An Ed25519 public key is always 32 bytes,
|
|
8
|
+
* so the decoded multikey is always 34 bytes and the did:key always begins
|
|
9
|
+
* `z6Mk` (a fixed consequence of the `0xed01` prefix).
|
|
10
|
+
*
|
|
11
|
+
* This is a PURE-JS port of the base58btc decoder in
|
|
12
|
+
* `apps/web/workers/gateway/src/detection/did-verifier.ts` (repeated-division
|
|
13
|
+
* algorithm), lifted here so the H1.2 pickup endpoint — and future H2.x TS
|
|
14
|
+
* PEPs — can recover the raw key for `node:crypto` Ed25519 verification with
|
|
15
|
+
* zero crypto/runtime dependencies (it runs identically in Node, Cloudflare
|
|
16
|
+
* Workers, and the browser). The encoder is the inverse, for tests and any
|
|
17
|
+
* caller that emits a `did:key` from raw key bytes.
|
|
18
|
+
*/
|
|
19
|
+
const BASE58_ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
|
20
|
+
const BASE58_INDEX = (() => {
|
|
21
|
+
const idx = {};
|
|
22
|
+
for (let i = 0; i < 58; i++) idx[BASE58_ALPHABET[i]] = i;
|
|
23
|
+
return idx;
|
|
7
24
|
})();
|
|
25
|
+
const ED25519_MULTICODEC = [237, 1];
|
|
26
|
+
const ED25519_PUBKEY_LENGTH$1 = 32;
|
|
27
|
+
const ED25519_MULTIKEY_LENGTH$1 = ED25519_MULTICODEC.length + ED25519_PUBKEY_LENGTH$1;
|
|
28
|
+
/**
|
|
29
|
+
* Decode a base58btc string to bytes using the repeated-division algorithm.
|
|
30
|
+
* Returns null if the string contains a character outside the base58btc
|
|
31
|
+
* alphabet. Ported verbatim from the gateway worker's `did-verifier.ts`
|
|
32
|
+
* (kept algorithm-identical to avoid cross-runtime drift).
|
|
33
|
+
*/
|
|
8
34
|
function base58btcDecode(str) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
return out;
|
|
35
|
+
if (str.length === 0) return /* @__PURE__ */ new Uint8Array(0);
|
|
36
|
+
let zeros = 0;
|
|
37
|
+
while (zeros < str.length && str[zeros] === BASE58_ALPHABET[0]) zeros++;
|
|
38
|
+
const bytes = [0];
|
|
39
|
+
for (let i = 0; i < str.length; i++) {
|
|
40
|
+
const value = BASE58_INDEX[str[i]];
|
|
41
|
+
if (value === void 0) return null;
|
|
42
|
+
let carry = value;
|
|
43
|
+
for (let j = 0; j < bytes.length; j++) {
|
|
44
|
+
carry += bytes[j] * 58;
|
|
45
|
+
bytes[j] = carry & 255;
|
|
46
|
+
carry >>= 8;
|
|
47
|
+
}
|
|
48
|
+
while (carry > 0) {
|
|
49
|
+
bytes.push(carry & 255);
|
|
50
|
+
carry >>= 8;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
let high = bytes.length;
|
|
54
|
+
while (high > 0 && bytes[high - 1] === 0) high--;
|
|
55
|
+
const out = new Uint8Array(zeros + high);
|
|
56
|
+
for (let i = 0; i < high; i++) out[zeros + high - 1 - i] = bytes[i];
|
|
57
|
+
return out;
|
|
34
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* Encode bytes to a base58btc string — the exact inverse of
|
|
61
|
+
* {@link base58btcDecode}. Leading zero bytes map to leading `1`s; the value
|
|
62
|
+
* is accumulated as little-endian base58 digits (×256 + byte) and emitted
|
|
63
|
+
* big-endian. The high-order zero sentinel is dropped the same way the decoder
|
|
64
|
+
* drops its trailing zeros, so a single `0x00` byte encodes to `"1"`, not `"11"`.
|
|
65
|
+
*/
|
|
35
66
|
function base58btcEncode(bytes) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
}
|
|
58
|
-
return out;
|
|
67
|
+
if (bytes.length === 0) return "";
|
|
68
|
+
let zeros = 0;
|
|
69
|
+
while (zeros < bytes.length && bytes[zeros] === 0) zeros++;
|
|
70
|
+
const digits = [0];
|
|
71
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
72
|
+
let carry = bytes[i];
|
|
73
|
+
for (let j = 0; j < digits.length; j++) {
|
|
74
|
+
carry += digits[j] * 256;
|
|
75
|
+
digits[j] = carry % 58;
|
|
76
|
+
carry = Math.floor(carry / 58);
|
|
77
|
+
}
|
|
78
|
+
while (carry > 0) {
|
|
79
|
+
digits.push(carry % 58);
|
|
80
|
+
carry = Math.floor(carry / 58);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
let high = digits.length;
|
|
84
|
+
while (high > 0 && digits[high - 1] === 0) high--;
|
|
85
|
+
let out = BASE58_ALPHABET[0].repeat(zeros);
|
|
86
|
+
for (let i = high - 1; i >= 0; i--) out += BASE58_ALPHABET[digits[i]];
|
|
87
|
+
return out;
|
|
59
88
|
}
|
|
60
89
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
90
|
+
//#endregion
|
|
91
|
+
//#region src/mcpi/multibase-ed25519.ts
|
|
92
|
+
/**
|
|
93
|
+
* Tolerant + canonical multibase codec for Ed25519 public keys used across
|
|
94
|
+
* checkpoint's mcp-i auth surfaces (org DID docs, approver keys, worker
|
|
95
|
+
* signed-agent verification).
|
|
96
|
+
*
|
|
97
|
+
* Lifts two previously-divergent decoders into one shared implementation:
|
|
98
|
+
* - apps/gateway/src/lib/multibase.ts's `decodeEd25519PublicKeyMultibase`
|
|
99
|
+
* accepted ONLY `z` (base58btc) + exactly 34 bytes (multicodec), throwing
|
|
100
|
+
* on anything else.
|
|
101
|
+
* - apps/web/workers/gateway/src/detection/did-verifier.ts's local
|
|
102
|
+
* `decodeMultibase` accepted `z` and `m` (read as base64url, which
|
|
103
|
+
* deviates from the multibase spec where `m` = standard base64, but
|
|
104
|
+
* matches what knowthat.ai's DID generator actually emits), plus 32-byte
|
|
105
|
+
* raw or 34-byte multicodec bodies.
|
|
106
|
+
*
|
|
107
|
+
* Neither accepted `u`, the actual multibase-spec prefix for base64url.
|
|
108
|
+
* `decodeEd25519PublicKeyMultibaseTolerant` accepts z/m/u and both body
|
|
109
|
+
* shapes, and — unlike the strict gateway decoder — never throws; callers
|
|
110
|
+
* treat `null` as "not a decodable Ed25519 key" and route it into their
|
|
111
|
+
* existing fail-closed paths.
|
|
112
|
+
*
|
|
113
|
+
* Reuses the base58btc codec from `./did-key` rather than adding a third
|
|
114
|
+
* copy (a THIRD near-identical implementation was the state before this
|
|
115
|
+
* module existed).
|
|
116
|
+
*/
|
|
117
|
+
const ED25519_PUB_MULTICODEC = [237, 1];
|
|
118
|
+
const ED25519_PUBKEY_LENGTH = 32;
|
|
119
|
+
const ED25519_MULTIKEY_LENGTH = ED25519_PUB_MULTICODEC.length + ED25519_PUBKEY_LENGTH;
|
|
120
|
+
/**
|
|
121
|
+
* Length ceiling applied BEFORE decoding. The longest legitimate body is a
|
|
122
|
+
* 34-byte multicodec key: 47 chars in base58btc, 48 in padded base64. Bodies
|
|
123
|
+
* past this cap are rejected by the length check anyway — the point is to
|
|
124
|
+
* reject them before `base58btcDecode`'s O(n^2) repeated division runs, since
|
|
125
|
+
* `publicKeyMultibase` / `publicKeyBase58` arrive from DID documents fetched
|
|
126
|
+
* over the network and a hostile document could otherwise burn seconds of
|
|
127
|
+
* event-loop (gateway) or isolate (worker) CPU per key. Mirrors the same guard
|
|
128
|
+
* on `decodeDidKeyEd25519` in ./did-key.
|
|
129
|
+
*/
|
|
130
|
+
const MAX_ED25519_KEY_BODY_LENGTH = 64;
|
|
131
|
+
/**
|
|
132
|
+
* Decode a base64 body to bytes, accepting both the url-safe (`-`, `_`) and
|
|
133
|
+
* standard (`+`, `/`) alphabets and tolerating the presence or absence of
|
|
134
|
+
* `=` padding. Returns null on any malformed input — never throws.
|
|
135
|
+
*/
|
|
66
136
|
function base64AnyToBytes(body) {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
137
|
+
if (body.length === 0) return null;
|
|
138
|
+
let normalized = body.replace(/-/g, "+").replace(/_/g, "/");
|
|
139
|
+
const remainder = normalized.length % 4;
|
|
140
|
+
if (remainder === 1) return null;
|
|
141
|
+
if (remainder === 2) normalized += "==";
|
|
142
|
+
else if (remainder === 3) normalized += "=";
|
|
143
|
+
if (!/^[A-Za-z0-9+/]+={0,2}$/.test(normalized)) return null;
|
|
144
|
+
try {
|
|
145
|
+
const bin = atob(normalized);
|
|
146
|
+
const out = new Uint8Array(bin.length);
|
|
147
|
+
for (let i = 0; i < bin.length; i++) out[i] = bin.charCodeAt(i);
|
|
148
|
+
return out;
|
|
149
|
+
} catch {
|
|
150
|
+
return null;
|
|
151
|
+
}
|
|
82
152
|
}
|
|
153
|
+
/** Strip the ed25519-pub multicodec prefix if present; validate the length either way. */
|
|
83
154
|
function toRawEd25519Key(decoded) {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
}
|
|
88
|
-
return null;
|
|
155
|
+
if (decoded.length === ED25519_PUBKEY_LENGTH) return decoded;
|
|
156
|
+
if (decoded.length === ED25519_MULTIKEY_LENGTH && decoded[0] === ED25519_PUB_MULTICODEC[0] && decoded[1] === ED25519_PUB_MULTICODEC[1]) return decoded.slice(ED25519_PUB_MULTICODEC.length);
|
|
157
|
+
return null;
|
|
89
158
|
}
|
|
159
|
+
/**
|
|
160
|
+
* Decode a `publicKeyMultibase` value to a raw 32-byte Ed25519 public key.
|
|
161
|
+
* Accepts multibase prefixes `z` (base58btc), `m` (base64url — the value
|
|
162
|
+
* checkpoint's own DID documents emit, though not per the multibase spec),
|
|
163
|
+
* and `u` (base64url per the multibase spec). The decoded body may be 32
|
|
164
|
+
* raw key bytes or 34 bytes prefixed with the ed25519-pub multicodec
|
|
165
|
+
* (`0xed 0x01`), which is stripped. Returns null for any other prefix,
|
|
166
|
+
* length, or invalid encoding — never throws.
|
|
167
|
+
*/
|
|
90
168
|
function decodeEd25519PublicKeyMultibaseTolerant(mb) {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
return null;
|
|
102
|
-
}
|
|
103
|
-
if (!decoded) return null;
|
|
104
|
-
return toRawEd25519Key(decoded);
|
|
169
|
+
if (typeof mb !== "string" || mb.length < 2) return null;
|
|
170
|
+
const prefix = mb[0];
|
|
171
|
+
const body = mb.slice(1);
|
|
172
|
+
if (body.length > 64) return null;
|
|
173
|
+
let decoded;
|
|
174
|
+
if (prefix === "z") decoded = base58btcDecode(body);
|
|
175
|
+
else if (prefix === "m" || prefix === "u") decoded = base64AnyToBytes(body);
|
|
176
|
+
else return null;
|
|
177
|
+
if (!decoded) return null;
|
|
178
|
+
return toRawEd25519Key(decoded);
|
|
105
179
|
}
|
|
180
|
+
/**
|
|
181
|
+
* Encode a raw 32-byte Ed25519 public key as a canonical `z`-prefixed,
|
|
182
|
+
* ed25519-pub-multicodec multibase string. Throws if the key is not
|
|
183
|
+
* exactly 32 bytes — an emitter handing in the wrong length is a
|
|
184
|
+
* programming error, not untrusted input (unlike the tolerant decoder).
|
|
185
|
+
*/
|
|
106
186
|
function encodeEd25519PublicKeyMultibase(rawPubKey) {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
prefixed[0] = ED25519_PUB_MULTICODEC[0];
|
|
114
|
-
prefixed[1] = ED25519_PUB_MULTICODEC[1];
|
|
115
|
-
prefixed.set(rawPubKey, ED25519_PUB_MULTICODEC.length);
|
|
116
|
-
return "z" + base58btcEncode(prefixed);
|
|
187
|
+
if (rawPubKey.length !== ED25519_PUBKEY_LENGTH) throw new Error(`Ed25519 public key must be ${ED25519_PUBKEY_LENGTH} bytes, got ${rawPubKey.length}`);
|
|
188
|
+
const prefixed = new Uint8Array(ED25519_MULTIKEY_LENGTH);
|
|
189
|
+
prefixed[0] = ED25519_PUB_MULTICODEC[0];
|
|
190
|
+
prefixed[1] = ED25519_PUB_MULTICODEC[1];
|
|
191
|
+
prefixed.set(rawPubKey, ED25519_PUB_MULTICODEC.length);
|
|
192
|
+
return "z" + base58btcEncode(prefixed);
|
|
117
193
|
}
|
|
118
194
|
|
|
195
|
+
//#endregion
|
|
119
196
|
export { MAX_ED25519_KEY_BODY_LENGTH, base58btcDecode, base58btcEncode, decodeEd25519PublicKeyMultibaseTolerant, encodeEd25519PublicKeyMultibase };
|
|
197
|
+
//# sourceMappingURL=multibase-ed25519.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"multibase-ed25519.mjs","names":["ED25519_PUBKEY_LENGTH","ED25519_MULTIKEY_LENGTH"],"sources":["../../src/mcpi/did-key.ts","../../src/mcpi/multibase-ed25519.ts"],"sourcesContent":["/**\n * did:key (Ed25519 multikey) codec — draft-kya-http-02 / H1.2 (#3245).\n *\n * Format: `did:key:z<base58btc(0xed 0x01 || pubkey32)>`\n * The multicodec prefix `0xed 0x01` identifies `ed25519-pub`; the multibase\n * prefix `z` identifies base58btc. An Ed25519 public key is always 32 bytes,\n * so the decoded multikey is always 34 bytes and the did:key always begins\n * `z6Mk` (a fixed consequence of the `0xed01` prefix).\n *\n * This is a PURE-JS port of the base58btc decoder in\n * `apps/web/workers/gateway/src/detection/did-verifier.ts` (repeated-division\n * algorithm), lifted here so the H1.2 pickup endpoint — and future H2.x TS\n * PEPs — can recover the raw key for `node:crypto` Ed25519 verification with\n * zero crypto/runtime dependencies (it runs identically in Node, Cloudflare\n * Workers, and the browser). The encoder is the inverse, for tests and any\n * caller that emits a `did:key` from raw key bytes.\n */\n\nconst BASE58_ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';\nconst BASE58_INDEX: Record<string, number> = (() => {\n const idx: Record<string, number> = {};\n for (let i = 0; i < BASE58_ALPHABET.length; i++) idx[BASE58_ALPHABET[i]] = i;\n return idx;\n})();\n\nconst DID_KEY_PREFIX = 'did:key:';\nconst ED25519_MULTICODEC = [0xed, 0x01] as const; // ed25519-pub\nconst ED25519_PUBKEY_LENGTH = 32;\nconst ED25519_MULTIKEY_LENGTH = ED25519_MULTICODEC.length + ED25519_PUBKEY_LENGTH; // 34\n\n/**\n * Decode a base58btc string to bytes using the repeated-division algorithm.\n * Returns null if the string contains a character outside the base58btc\n * alphabet. Ported verbatim from the gateway worker's `did-verifier.ts`\n * (kept algorithm-identical to avoid cross-runtime drift).\n */\nexport function base58btcDecode(str: string): Uint8Array | null {\n if (str.length === 0) return new Uint8Array(0);\n\n let zeros = 0;\n while (zeros < str.length && str[zeros] === BASE58_ALPHABET[0]) zeros++;\n\n // Sentinel-seeded little-endian byte accumulator.\n const bytes: number[] = [0];\n for (let i = 0; i < str.length; i++) {\n const value = BASE58_INDEX[str[i]];\n if (value === undefined) return null;\n let carry = value;\n for (let j = 0; j < bytes.length; j++) {\n carry += bytes[j] * 58;\n bytes[j] = carry & 0xff;\n carry >>= 8;\n }\n while (carry > 0) {\n bytes.push(carry & 0xff);\n carry >>= 8;\n }\n }\n\n // Drop the trailing zero sentinel(s).\n let high = bytes.length;\n while (high > 0 && bytes[high - 1] === 0) high--;\n\n const out = new Uint8Array(zeros + high);\n for (let i = 0; i < high; i++) {\n out[zeros + high - 1 - i] = bytes[i];\n }\n return out;\n}\n\n/**\n * Encode bytes to a base58btc string — the exact inverse of\n * {@link base58btcDecode}. Leading zero bytes map to leading `1`s; the value\n * is accumulated as little-endian base58 digits (×256 + byte) and emitted\n * big-endian. The high-order zero sentinel is dropped the same way the decoder\n * drops its trailing zeros, so a single `0x00` byte encodes to `\"1\"`, not `\"11\"`.\n */\nexport function base58btcEncode(bytes: Uint8Array): string {\n if (bytes.length === 0) return '';\n\n let zeros = 0;\n while (zeros < bytes.length && bytes[zeros] === 0) zeros++;\n\n const digits: number[] = [0];\n for (let i = 0; i < bytes.length; i++) {\n let carry = bytes[i];\n for (let j = 0; j < digits.length; j++) {\n carry += digits[j] * 256;\n digits[j] = carry % 58;\n carry = Math.floor(carry / 58);\n }\n while (carry > 0) {\n digits.push(carry % 58);\n carry = Math.floor(carry / 58);\n }\n }\n\n // Drop the high-order zero sentinel(s); leading zero bytes become '1's.\n let high = digits.length;\n while (high > 0 && digits[high - 1] === 0) high--;\n\n let out = BASE58_ALPHABET[0].repeat(zeros);\n for (let i = high - 1; i >= 0; i--) {\n out += BASE58_ALPHABET[digits[i]];\n }\n return out;\n}\n\n/**\n * Recover the raw 32-byte Ed25519 public key from a `did:key:z6Mk…` string.\n * Returns null for any malformed input: wrong method, wrong multibase, invalid\n * base58, wrong decoded length, or a non-Ed25519 multicodec. Never throws —\n * callers treat null as \"not a verifiable Ed25519 did:key\".\n */\nexport function decodeDidKeyEd25519(did: string): Uint8Array | null {\n if (typeof did !== 'string' || !did.startsWith(DID_KEY_PREFIX)) return null;\n\n const suffix = did.slice(DID_KEY_PREFIX.length);\n if (!suffix.startsWith('z')) return null; // base58btc multibase prefix\n\n // A legit Ed25519 did:key body is fixed at 47 base58 chars. Cap the length\n // BEFORE the O(n^2) repeated-division decode: this keyid arrives from an\n // attacker-controlled Signature-Input header on the public pickup endpoint, so\n // an over-long body (which the 34-byte length check would reject anyway) must\n // not be allowed to burn quadratic event-loop CPU first.\n const body = suffix.slice(1);\n if (body.length > 64) return null;\n\n const decoded = base58btcDecode(body);\n if (!decoded || decoded.length !== ED25519_MULTIKEY_LENGTH) return null;\n if (decoded[0] !== ED25519_MULTICODEC[0] || decoded[1] !== ED25519_MULTICODEC[1]) return null;\n\n return decoded.slice(ED25519_MULTICODEC.length);\n}\n\n/** Recover the compressed SEC1 point from the HTTP profile's p256-pub\n * multicodec (0x1200). This runtime-independent codec validates carriage only;\n * the cryptographic consumer MUST validate that the point lies on P-256 before\n * trusting it or deriving a confirmation thumbprint. */\nexport function decodeDidKeyP256(did: string): Uint8Array | null {\n if (typeof did !== 'string' || !did.startsWith(`${DID_KEY_PREFIX}z`)) return null;\n const body = did.slice(DID_KEY_PREFIX.length + 1);\n // Bound the quadratic base58 decoder before processing untrusted keyids.\n if (body.length > 64) return null;\n const decoded = base58btcDecode(body);\n if (\n !decoded ||\n decoded.length !== 35 ||\n decoded[0] !== 0x80 ||\n decoded[1] !== 0x24 ||\n (decoded[2] !== 2 && decoded[2] !== 3)\n )\n return null;\n return decoded.slice(2);\n}\n\n/**\n * Encode a raw 32-byte Ed25519 public key as a `did:key:z6Mk…` string. Throws\n * if the key is not exactly 32 bytes — an emitter handing in the wrong length\n * is a programming error, not untrusted input (unlike {@link decodeDidKeyEd25519}).\n */\nexport function encodeDidKeyEd25519(publicKey: Uint8Array): string {\n if (!(publicKey instanceof Uint8Array) || publicKey.length !== ED25519_PUBKEY_LENGTH) {\n throw new Error(\n `encodeDidKeyEd25519: expected a ${ED25519_PUBKEY_LENGTH}-byte Ed25519 public key, got ${\n publicKey instanceof Uint8Array ? `${publicKey.length} bytes` : typeof publicKey\n }`\n );\n }\n\n const multikey = new Uint8Array(ED25519_MULTIKEY_LENGTH);\n multikey[0] = ED25519_MULTICODEC[0];\n multikey[1] = ED25519_MULTICODEC[1];\n multikey.set(publicKey, ED25519_MULTICODEC.length);\n\n return `${DID_KEY_PREFIX}z${base58btcEncode(multikey)}`;\n}\n","/**\n * Tolerant + canonical multibase codec for Ed25519 public keys used across\n * checkpoint's mcp-i auth surfaces (org DID docs, approver keys, worker\n * signed-agent verification).\n *\n * Lifts two previously-divergent decoders into one shared implementation:\n * - apps/gateway/src/lib/multibase.ts's `decodeEd25519PublicKeyMultibase`\n * accepted ONLY `z` (base58btc) + exactly 34 bytes (multicodec), throwing\n * on anything else.\n * - apps/web/workers/gateway/src/detection/did-verifier.ts's local\n * `decodeMultibase` accepted `z` and `m` (read as base64url, which\n * deviates from the multibase spec where `m` = standard base64, but\n * matches what knowthat.ai's DID generator actually emits), plus 32-byte\n * raw or 34-byte multicodec bodies.\n *\n * Neither accepted `u`, the actual multibase-spec prefix for base64url.\n * `decodeEd25519PublicKeyMultibaseTolerant` accepts z/m/u and both body\n * shapes, and — unlike the strict gateway decoder — never throws; callers\n * treat `null` as \"not a decodable Ed25519 key\" and route it into their\n * existing fail-closed paths.\n *\n * Reuses the base58btc codec from `./did-key` rather than adding a third\n * copy (a THIRD near-identical implementation was the state before this\n * module existed).\n */\n\nimport { base58btcDecode, base58btcEncode } from './did-key';\n\nconst ED25519_PUB_MULTICODEC = [0xed, 0x01] as const;\nconst ED25519_PUBKEY_LENGTH = 32;\nconst ED25519_MULTIKEY_LENGTH = ED25519_PUB_MULTICODEC.length + ED25519_PUBKEY_LENGTH; // 34\n\n/**\n * Length ceiling applied BEFORE decoding. The longest legitimate body is a\n * 34-byte multicodec key: 47 chars in base58btc, 48 in padded base64. Bodies\n * past this cap are rejected by the length check anyway — the point is to\n * reject them before `base58btcDecode`'s O(n^2) repeated division runs, since\n * `publicKeyMultibase` / `publicKeyBase58` arrive from DID documents fetched\n * over the network and a hostile document could otherwise burn seconds of\n * event-loop (gateway) or isolate (worker) CPU per key. Mirrors the same guard\n * on `decodeDidKeyEd25519` in ./did-key.\n */\nexport const MAX_ED25519_KEY_BODY_LENGTH = 64;\n\n/**\n * Decode a base64 body to bytes, accepting both the url-safe (`-`, `_`) and\n * standard (`+`, `/`) alphabets and tolerating the presence or absence of\n * `=` padding. Returns null on any malformed input — never throws.\n */\nfunction base64AnyToBytes(body: string): Uint8Array | null {\n if (body.length === 0) return null;\n let normalized = body.replace(/-/g, '+').replace(/_/g, '/');\n const remainder = normalized.length % 4;\n if (remainder === 1) return null; // no valid base64 encoding has this length\n if (remainder === 2) normalized += '==';\n else if (remainder === 3) normalized += '=';\n if (!/^[A-Za-z0-9+/]+={0,2}$/.test(normalized)) return null;\n\n try {\n const bin = atob(normalized);\n const out = new Uint8Array(bin.length);\n for (let i = 0; i < bin.length; i++) out[i] = bin.charCodeAt(i);\n return out;\n } catch {\n return null;\n }\n}\n\n/** Strip the ed25519-pub multicodec prefix if present; validate the length either way. */\nfunction toRawEd25519Key(decoded: Uint8Array): Uint8Array | null {\n if (decoded.length === ED25519_PUBKEY_LENGTH) return decoded;\n if (\n decoded.length === ED25519_MULTIKEY_LENGTH &&\n decoded[0] === ED25519_PUB_MULTICODEC[0] &&\n decoded[1] === ED25519_PUB_MULTICODEC[1]\n ) {\n return decoded.slice(ED25519_PUB_MULTICODEC.length);\n }\n return null;\n}\n\n/**\n * Decode a `publicKeyMultibase` value to a raw 32-byte Ed25519 public key.\n * Accepts multibase prefixes `z` (base58btc), `m` (base64url — the value\n * checkpoint's own DID documents emit, though not per the multibase spec),\n * and `u` (base64url per the multibase spec). The decoded body may be 32\n * raw key bytes or 34 bytes prefixed with the ed25519-pub multicodec\n * (`0xed 0x01`), which is stripped. Returns null for any other prefix,\n * length, or invalid encoding — never throws.\n */\nexport function decodeEd25519PublicKeyMultibaseTolerant(mb: string): Uint8Array | null {\n if (typeof mb !== 'string' || mb.length < 2) return null;\n\n const prefix = mb[0];\n const body = mb.slice(1);\n if (body.length > MAX_ED25519_KEY_BODY_LENGTH) return null;\n\n let decoded: Uint8Array | null;\n if (prefix === 'z') {\n decoded = base58btcDecode(body);\n } else if (prefix === 'm' || prefix === 'u') {\n decoded = base64AnyToBytes(body);\n } else {\n return null;\n }\n if (!decoded) return null;\n\n return toRawEd25519Key(decoded);\n}\n\n/**\n * Encode a raw 32-byte Ed25519 public key as a canonical `z`-prefixed,\n * ed25519-pub-multicodec multibase string. Throws if the key is not\n * exactly 32 bytes — an emitter handing in the wrong length is a\n * programming error, not untrusted input (unlike the tolerant decoder).\n */\nexport function encodeEd25519PublicKeyMultibase(rawPubKey: Uint8Array): string {\n if (rawPubKey.length !== ED25519_PUBKEY_LENGTH) {\n throw new Error(\n `Ed25519 public key must be ${ED25519_PUBKEY_LENGTH} bytes, got ${rawPubKey.length}`\n );\n }\n const prefixed = new Uint8Array(ED25519_MULTIKEY_LENGTH);\n prefixed[0] = ED25519_PUB_MULTICODEC[0];\n prefixed[1] = ED25519_PUB_MULTICODEC[1];\n prefixed.set(rawPubKey, ED25519_PUB_MULTICODEC.length);\n return 'z' + base58btcEncode(prefixed);\n}\n\n// Re-exported so a single subpath import also covers: (1) the legacy\n// `Ed25519VerificationKey2018` `publicKeyBase58` form (raw base58btc, no\n// multibase prefix, no multicodec) the worker needs, and (2) constructing\n// non-canonical-but-valid multibase fixtures (e.g. raw-32-byte z bodies) in\n// tests — without pulling the full package index either way.\nexport { base58btcDecode, base58btcEncode };\n"],"mappings":";;;;;;;;;;;;;;;;;;AAkBA,MAAM,kBAAkB;AACxB,M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AC/D,IAAI,QAAQ,WAAW,uBAAuB,OAAO;CACrD,IACE,QAAQ,WAAW,2BACnB,QAAQ,OAAO,uBAAuB,MACtC,QAAQ,OAAO,uBAAuB,IAEtC,OAAO,QAAQ,MAAM,uBAAuB,MAAM;CAEpD,OAAO;AACT;;;;;;;;;;AAWA,SAAgB,wCAAwC,IAA+B;CACrF,IAAI,OAAO,OAAO,YAAY,GAAG,SAAS,GAAG,OAAO;CAEpD,MAAM,SAAS,GAAG;CAClB,MAAM,OAAO,GAAG,MAAM,CAAC;CACvB,IAAI,KAAK,aAAsC,OAAO;CAEtD,IAAI;CACJ,IAAI,WAAW,KACb,UAAU,gBAAgB,IAAI;MACzB,IAAI,WAAW,OAAO,WAAW,KACtC,UAAU,iBAAiB,IAAI;MAE/B,OAAO;CAET,IAAI,CAAC,SAAS,OAAO;CAErB,OAAO,gBAAgB,OAAO;AAChC;;;;;;;AAQA,SAAgB,gCAAgC,WAA+B;CAC7E,IAAI,UAAU,WAAW,uBACvB,MAAM,IAAI,MACR,8BAA8B,sBAAsB,cAAc,UAAU,QAC9E;CAEF,MAAM,WAAW,IAAI,WAAW,uBAAuB;CACvD,SAAS,KAAK,uBAAuB;CACrC,SAAS,KAAK,uBAAuB;CACrC,SAAS,IAAI,WAAW,uBAAuB,MAAM;CACrD,OAAO,MAAM,gBAAgB,QAAQ;AACvC"}
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
//#region src/constants/patterns.ts
|
|
2
|
+
/**
|
|
3
|
+
* Regular Expression Patterns
|
|
4
|
+
*
|
|
5
|
+
* Common regex patterns used for validation and detection
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Validation Patterns
|
|
9
|
+
*/
|
|
10
|
+
const UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
11
|
+
const VALIDATION_PATTERNS = {
|
|
12
|
+
UUID: UUID_PAT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\.[A-Za-z0-9-_]+$/,
|
|
25
|
+
BASE64: /^[A-Za-z0-9+\/]*={0,2}$/,
|
|
26
|
+
HEX: /^[0-9a-fA-F]+$/,
|
|
27
|
+
MD5: /^[a-f0-9]{32}$/i,
|
|
28
|
+
SHA1: /^[a-f0-9]{40}$/i,
|
|
29
|
+
SHA256: /^[a-f0-9]{64}$/i,
|
|
30
|
+
SEMVER: /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/,
|
|
31
|
+
FILENAME: /^[a-zA-Z0-9](?:[a-zA-Z0-9 ._-]*[a-zA-Z0-9])?\.[a-zA-Z0-9_-]+$/,
|
|
32
|
+
PATH: /^(\/[^/ ]*)+\/?$/,
|
|
33
|
+
API_KEY: /^[A-Za-z0-9_-]{32,}$/,
|
|
34
|
+
SECRET_KEY: /^[A-Za-z0-9_-]{64,}$/,
|
|
35
|
+
PIXEL_ID: /^px_[A-Za-z0-9]{16}$/
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* User Agent Patterns for Agent Detection
|
|
39
|
+
*/
|
|
40
|
+
const USER_AGENT_PATTERNS = {
|
|
41
|
+
OPENAI: /(?:GPTBot|ChatGPT-User|OpenAI)/i,
|
|
42
|
+
ANTHROPIC: /(?:Claude-Web|Claude-User|anthropic-ai|claude-web|claude-user|ClaudeBot)/i,
|
|
43
|
+
PERPLEXITY: /(?:PerplexityBot|Perplexica)/i,
|
|
44
|
+
GOOGLE_AI: /(?:Google-Extended|Bard|Google-InspectionTool)/i,
|
|
45
|
+
MICROSOFT_AI: /(?:BingPreview\/.*ChatGPT|Copilot)/i,
|
|
46
|
+
GOOGLEBOT: /Googlebot/i,
|
|
47
|
+
BINGBOT: /(?:bingbot|BingPreview|msnbot)/i,
|
|
48
|
+
DUCKDUCKBOT: /DuckDuckBot/i,
|
|
49
|
+
BAIDUBOT: /Baiduspider/i,
|
|
50
|
+
YANDEXBOT: /YandexBot/i,
|
|
51
|
+
FACEBOOK: /(?:facebookexternalhit|facebookcatalog)/i,
|
|
52
|
+
TWITTER: /(?:Twitterbot|TwitterImageProxy)/i,
|
|
53
|
+
LINKEDIN: /LinkedInBot/i,
|
|
54
|
+
WHATSAPP: /WhatsApp/i,
|
|
55
|
+
TELEGRAM: /TelegramBot/i,
|
|
56
|
+
DISCORD: /Discordbot/i,
|
|
57
|
+
SLACK: /Slackbot/i,
|
|
58
|
+
GENERIC_BOT: /(?:bot|crawler|spider|scraper|agent)/i,
|
|
59
|
+
SCRAPY: /Scrapy/i,
|
|
60
|
+
PUPPETEER: /(?:HeadlessChrome|Puppeteer)/i,
|
|
61
|
+
PLAYWRIGHT: /Playwright/i,
|
|
62
|
+
SELENIUM: /(?:selenium|webdriver)/i,
|
|
63
|
+
POSTMAN: /PostmanRuntime/i,
|
|
64
|
+
INSOMNIA: /insomnia/i,
|
|
65
|
+
CURL: /curl/i,
|
|
66
|
+
WGET: /Wget/i,
|
|
67
|
+
HTTPIE: /HTTPie/i,
|
|
68
|
+
UPTIMEROBOT: /UptimeRobot/i,
|
|
69
|
+
PINGDOM: /Pingdom/i,
|
|
70
|
+
NEWRELIC: /NewRelicPinger/i,
|
|
71
|
+
DATADOG: /Datadog/i
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* Header Patterns for Agent Detection
|
|
75
|
+
*/
|
|
76
|
+
const HEADER_PATTERNS = {
|
|
77
|
+
AI_HEADERS: /^(?:X-(?:OpenAI|Anthropic|AI|Bot|Agent|LLM))/i,
|
|
78
|
+
AUTOMATION_HEADERS: /^(?:X-(?:Automated|Robot|Crawler|Scraper))/i,
|
|
79
|
+
MISSING_ACCEPT: /^$/,
|
|
80
|
+
MISSING_ACCEPT_LANGUAGE: /^$/,
|
|
81
|
+
MISSING_ACCEPT_ENCODING: /^$/,
|
|
82
|
+
GENERIC_ACCEPT: /^\*\/\*$/,
|
|
83
|
+
NO_COOKIES: /^$/,
|
|
84
|
+
PROXY_HEADERS: /^(?:X-Forwarded-|X-Real-IP|X-Originating-IP|Via|Forwarded)/i,
|
|
85
|
+
CLOUD_HEADERS: /^(?:X-(?:AWS|Azure|GCP|Cloudflare))/i
|
|
86
|
+
};
|
|
87
|
+
/**
|
|
88
|
+
* Browser Patterns
|
|
89
|
+
*/
|
|
90
|
+
const BROWSER_PATTERNS = {
|
|
91
|
+
CHROME: /Chrome\/([0-9.]+)/,
|
|
92
|
+
FIREFOX: /Firefox\/([0-9.]+)/,
|
|
93
|
+
SAFARI: /Version\/([0-9.]+).*Safari/,
|
|
94
|
+
EDGE: /Edg\/([0-9.]+)/,
|
|
95
|
+
OPERA: /OPR\/([0-9.]+)/,
|
|
96
|
+
IE: /MSIE ([0-9.]+)/
|
|
97
|
+
};
|
|
98
|
+
/**
|
|
99
|
+
* Operating System Patterns
|
|
100
|
+
*/
|
|
101
|
+
const OS_PATTERNS = {
|
|
102
|
+
WINDOWS: /Windows NT ([0-9.]+)/,
|
|
103
|
+
MACOS: /Mac OS X ([0-9_]+)/,
|
|
104
|
+
LINUX: /Linux/,
|
|
105
|
+
ANDROID: /Android ([0-9.]+)/,
|
|
106
|
+
IOS: /OS ([0-9_]+) like Mac OS X/
|
|
107
|
+
};
|
|
108
|
+
/**
|
|
109
|
+
* Mobile Device Patterns
|
|
110
|
+
*/
|
|
111
|
+
const MOBILE_PATTERNS = {
|
|
112
|
+
MOBILE: /(?:Mobile|Android|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini)/i,
|
|
113
|
+
TABLET: /(?:iPad|Android.*Tablet|Tablet.*Android|Kindle|Silk)/i,
|
|
114
|
+
DESKTOP: /(?:Windows NT|Mac OS X|Linux|CrOS)/i
|
|
115
|
+
};
|
|
116
|
+
/**
|
|
117
|
+
* Suspicious Behavior Patterns
|
|
118
|
+
*/
|
|
119
|
+
const SUSPICIOUS_PATTERNS = {
|
|
120
|
+
RAPID_REQUESTS: /^[0-9]{1,3}$/,
|
|
121
|
+
SEQUENTIAL_URLS: /\?(?:page|offset|id)=[0-9]+/,
|
|
122
|
+
API_ENDPOINTS: /\/api\/|\.json$|\.xml$/i,
|
|
123
|
+
DIRECT_POST: /^POST$/,
|
|
124
|
+
SQL_INJECTION: /(?:union|select|insert|update|delete|drop|create|alter|exec|script|javascript|onerror|onload|eval|src=)/i,
|
|
125
|
+
XSS_ATTEMPT: /(?:<script|javascript:|onerror=|onclick=|<iframe|<embed|<object)/i,
|
|
126
|
+
PATH_TRAVERSAL: /(?:\.\.\/|\.\.\\|%2e%2e%2f|%252e%252e%252f)/i,
|
|
127
|
+
URL_ENCODED: /%[0-9a-f]{2}/i,
|
|
128
|
+
BASE64_DATA: /data:[^;]+;base64,/i
|
|
129
|
+
};
|
|
130
|
+
/**
|
|
131
|
+
* Maximum input length for pattern matching. UAs, header values, and
|
|
132
|
+
* detection payloads are bounded in practice (<4KB even for the most
|
|
133
|
+
* verbose Chrome/Firefox UAs). Caps worst-case complexity for any
|
|
134
|
+
* polynomial-shaped pattern an external caller might supply to
|
|
135
|
+
* `testPattern` / `extractMatch`; CodeQL flagged the unbounded path
|
|
136
|
+
* (js/polynomial-redos) on PR #2576.
|
|
137
|
+
*/
|
|
138
|
+
const MAX_PATTERN_INPUT_LENGTH = 4096;
|
|
139
|
+
/**
|
|
140
|
+
* Helper function to test a pattern. Returns `false` on inputs longer
|
|
141
|
+
* than {@link MAX_PATTERN_INPUT_LENGTH} — an abuse signal we'd rather
|
|
142
|
+
* reject than process.
|
|
143
|
+
*/
|
|
144
|
+
function testPattern(pattern, value) {
|
|
145
|
+
if (value.length > MAX_PATTERN_INPUT_LENGTH) return false;
|
|
146
|
+
return pattern.test(value);
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Helper function to extract match from pattern. Returns `null` on
|
|
150
|
+
* over-long inputs (same ReDoS defense as `testPattern`).
|
|
151
|
+
*/
|
|
152
|
+
function extractMatch(pattern, value) {
|
|
153
|
+
if (value.length > MAX_PATTERN_INPUT_LENGTH) return null;
|
|
154
|
+
const match = value.match(pattern);
|
|
155
|
+
return match ? match[1] || match[0] : null;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Helper function to test multiple patterns
|
|
159
|
+
*/
|
|
160
|
+
function testPatterns(patterns, value) {
|
|
161
|
+
return patterns.some((pattern) => testPattern(pattern, value));
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Helper function to find matching pattern
|
|
165
|
+
*/
|
|
166
|
+
function findMatchingPattern(patterns, value) {
|
|
167
|
+
for (const [name, pattern] of Object.entries(patterns)) if (testPattern(pattern, value)) return name;
|
|
168
|
+
return null;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
//#endregion
|
|
172
|
+
Object.defineProperty(exports, 'BROWSER_PATTERNS', {
|
|
173
|
+
enumerable: true,
|
|
174
|
+
get: function () {
|
|
175
|
+
return BROWSER_PATTERNS;
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
Object.defineProperty(exports, 'HEADER_PATTERNS', {
|
|
179
|
+
enumerable: true,
|
|
180
|
+
get: function () {
|
|
181
|
+
return HEADER_PATTERNS;
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
Object.defineProperty(exports, 'MOBILE_PATTERNS', {
|
|
185
|
+
enumerable: true,
|
|
186
|
+
get: function () {
|
|
187
|
+
return MOBILE_PATTERNS;
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
Object.defineProperty(exports, 'OS_PATTERNS', {
|
|
191
|
+
enumerable: true,
|
|
192
|
+
get: function () {
|
|
193
|
+
return OS_PATTERNS;
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
Object.defineProperty(exports, 'SUSPICIOUS_PATTERNS', {
|
|
197
|
+
enumerable: true,
|
|
198
|
+
get: function () {
|
|
199
|
+
return SUSPICIOUS_PATTERNS;
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
Object.defineProperty(exports, 'USER_AGENT_PATTERNS', {
|
|
203
|
+
enumerable: true,
|
|
204
|
+
get: function () {
|
|
205
|
+
return USER_AGENT_PATTERNS;
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
Object.defineProperty(exports, 'UUID_PATTERN', {
|
|
209
|
+
enumerable: true,
|
|
210
|
+
get: function () {
|
|
211
|
+
return UUID_PATTERN;
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
Object.defineProperty(exports, 'VALIDATION_PATTERNS', {
|
|
215
|
+
enumerable: true,
|
|
216
|
+
get: function () {
|
|
217
|
+
return VALIDATION_PATTERNS;
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
Object.defineProperty(exports, 'extractMatch', {
|
|
221
|
+
enumerable: true,
|
|
222
|
+
get: function () {
|
|
223
|
+
return extractMatch;
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
Object.defineProperty(exports, 'findMatchingPattern', {
|
|
227
|
+
enumerable: true,
|
|
228
|
+
get: function () {
|
|
229
|
+
return findMatchingPattern;
|
|
230
|
+
}
|
|
231
|
+
});
|
|
232
|
+
Object.defineProperty(exports, 'testPattern', {
|
|
233
|
+
enumerable: true,
|
|
234
|
+
get: function () {
|
|
235
|
+
return testPattern;
|
|
236
|
+
}
|
|
237
|
+
});
|
|
238
|
+
Object.defineProperty(exports, 'testPatterns', {
|
|
239
|
+
enumerable: true,
|
|
240
|
+
get: function () {
|
|
241
|
+
return testPatterns;
|
|
242
|
+
}
|
|
243
|
+
});
|