@kya-os/checkpoint-shared 1.6.0 → 1.8.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 +122 -9
- package/README.md +24 -7
- package/dist/beacon-nonce/index.d.mts +1 -0
- package/dist/beacon-nonce/index.d.mts.map +1 -1
- package/dist/beacon-nonce/index.d.ts +1 -0
- package/dist/beacon-nonce/index.d.ts.map +1 -1
- package/dist/beacon-nonce/index.js +9 -3
- package/dist/beacon-nonce/index.mjs +9 -4
- package/dist/beacon-nonce/index.mjs.map +1 -1
- package/dist/beacon-types/index.d.mts +23 -1
- package/dist/beacon-types/index.d.mts.map +1 -1
- package/dist/beacon-types/index.d.ts +23 -1
- package/dist/beacon-types/index.d.ts.map +1 -1
- package/dist/beacon-types/index.js +49 -4
- package/dist/beacon-types/index.mjs +47 -4
- package/dist/beacon-types/index.mjs.map +1 -1
- package/dist/{browser-posture-BuChqmH5.d.mts → browser-posture-5fPrsxx7.d.mts} +2 -2
- package/dist/{browser-posture-BuChqmH5.d.mts.map → browser-posture-5fPrsxx7.d.mts.map} +1 -1
- package/dist/{browser-posture-DIaVLuW3.d.ts → browser-posture-CCR1kh5e.d.ts} +2 -2
- package/dist/{browser-posture-DIaVLuW3.d.ts.map → browser-posture-CCR1kh5e.d.ts.map} +1 -1
- package/dist/browser-posture-cookie.js +1 -1
- package/dist/browser-posture-cookie.mjs +1 -1
- package/dist/browser-posture-cookie.mjs.map +1 -1
- package/dist/browser-posture-proof.d.mts +16 -2
- package/dist/browser-posture-proof.d.mts.map +1 -1
- package/dist/browser-posture-proof.d.ts +16 -2
- package/dist/browser-posture-proof.d.ts.map +1 -1
- package/dist/browser-posture-proof.js +114 -17
- package/dist/browser-posture-proof.mjs +112 -17
- package/dist/browser-posture-proof.mjs.map +1 -1
- package/dist/browser-posture.d.mts +1 -1
- package/dist/browser-posture.d.ts +1 -1
- package/dist/browser-posture.js +6 -5
- package/dist/browser-posture.mjs +4 -3
- package/dist/browser-posture.mjs.map +1 -1
- package/dist/browser.d.mts +25 -4
- package/dist/browser.d.mts.map +1 -1
- package/dist/browser.d.ts +25 -4
- package/dist/browser.d.ts.map +1 -1
- package/dist/browser.js +1 -1
- package/dist/browser.mjs +1 -1
- package/dist/browser.mjs.map +1 -1
- package/dist/canonical-signer/index.d.mts.map +1 -1
- package/dist/canonical-signer/index.d.ts.map +1 -1
- package/dist/canonical-signer/index.js +116 -79
- package/dist/canonical-signer/index.mjs +117 -52
- package/dist/canonical-signer/index.mjs.map +1 -1
- package/dist/clearance.d.mts +1 -1
- package/dist/clearance.d.ts +1 -1
- package/dist/clearance.js +2 -2
- package/dist/clearance.mjs +2 -2
- package/dist/client-ip-B7w-uHfS.d.mts +55 -0
- package/dist/client-ip-B7w-uHfS.d.mts.map +1 -0
- package/dist/client-ip-B7w-uHfS.d.ts +55 -0
- package/dist/client-ip-B7w-uHfS.d.ts.map +1 -0
- package/dist/{client-ip-CW6pRyLf.js → client-ip-B8wFzJ5f.js} +117 -7
- package/dist/{client-ip-B2Xeo-GK.mjs → client-ip-DIJyr6px.mjs} +113 -9
- package/dist/client-ip-DIJyr6px.mjs.map +1 -0
- package/dist/client-ip.d.mts +2 -0
- package/dist/client-ip.d.ts +2 -0
- package/dist/client-ip.js +8 -0
- package/dist/client-ip.mjs +3 -0
- package/dist/detection-reason-mapping-B9RSkOs8.js +277 -0
- package/dist/detection-reason-mapping-BGymWIPl.mjs +249 -0
- package/dist/detection-reason-mapping-BGymWIPl.mjs.map +1 -0
- package/dist/detection-reason-mapping-CQexNvR7.d.mts +52 -0
- package/dist/detection-reason-mapping-CQexNvR7.d.mts.map +1 -0
- package/dist/detection-reason-mapping-CQexNvR7.d.ts +52 -0
- package/dist/detection-reason-mapping-CQexNvR7.d.ts.map +1 -0
- package/dist/engine-enforcing-AYEcgNsb.js +39 -0
- package/dist/engine-enforcing-B5eNfCpG.mjs +29 -0
- package/dist/engine-enforcing-B5eNfCpG.mjs.map +1 -0
- package/dist/engine-enforcing-DiOclGqf.d.mts +11 -0
- package/dist/engine-enforcing-DiOclGqf.d.mts.map +1 -0
- package/dist/engine-enforcing-DiOclGqf.d.ts +11 -0
- package/dist/engine-enforcing-DiOclGqf.d.ts.map +1 -0
- package/dist/engine-info.d.mts +2 -0
- package/dist/engine-info.d.ts +2 -0
- package/dist/engine-info.js +4 -0
- package/dist/engine-info.mjs +3 -0
- package/dist/errors/index.d.mts +5 -0
- package/dist/errors/index.d.mts.map +1 -1
- package/dist/errors/index.d.ts +5 -0
- package/dist/errors/index.d.ts.map +1 -1
- package/dist/errors/index.js +23 -4
- package/dist/errors/index.mjs +23 -4
- package/dist/errors/index.mjs.map +1 -1
- package/dist/{external-B7WI9FyQ.d.ts → external-m3Kmafwj.d.mts} +2 -2
- package/dist/{external-B7WI9FyQ.d.mts.map → external-m3Kmafwj.d.mts.map} +1 -1
- package/dist/{external-B7WI9FyQ.d.mts → external-m3Kmafwj.d.ts} +2 -2
- package/dist/{external-B7WI9FyQ.d.ts.map → external-m3Kmafwj.d.ts.map} +1 -1
- package/dist/fetcher-CivUCbhL.d.ts +44 -0
- package/dist/fetcher-CivUCbhL.d.ts.map +1 -0
- package/dist/fetcher-cXQsbm6s.d.mts +44 -0
- package/dist/fetcher-cXQsbm6s.d.mts.map +1 -0
- package/dist/gateway-client-ip.d.mts +1 -1
- package/dist/gateway-client-ip.d.ts +1 -1
- package/dist/gateway-client-ip.js +2 -2
- package/dist/gateway-client-ip.mjs +2 -2
- package/dist/host-adapter/index.d.mts +2 -0
- package/dist/host-adapter/index.d.ts +2 -0
- package/dist/host-adapter/index.js +24 -0
- package/dist/host-adapter/index.mjs +3 -0
- package/dist/host-adapter-0F8EpbD6.js +753 -0
- package/dist/host-adapter-DHyISsIf.mjs +593 -0
- package/dist/host-adapter-DHyISsIf.mjs.map +1 -0
- package/dist/index-B3cHxr3O.d.mts +156 -0
- package/dist/index-B3cHxr3O.d.mts.map +1 -0
- package/dist/index-CXbOY7gB.d.ts +156 -0
- package/dist/index-CXbOY7gB.d.ts.map +1 -0
- package/dist/index.d.mts +6997 -12170
- package/dist/index.d.mts.map +1 -1
- package/dist/index.d.ts +6997 -12170
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2510 -8893
- package/dist/index.mjs +2366 -8534
- package/dist/index.mjs.map +1 -1
- package/dist/kya-http-error-codes-7ewV5RuK.d.mts +29 -0
- package/dist/kya-http-error-codes-7ewV5RuK.d.mts.map +1 -0
- package/dist/kya-http-error-codes-7ewV5RuK.d.ts +29 -0
- package/dist/kya-http-error-codes-7ewV5RuK.d.ts.map +1 -0
- package/dist/kya-http-error-codes-C2JiWPsi.mjs +73 -0
- package/dist/kya-http-error-codes-C2JiWPsi.mjs.map +1 -0
- package/dist/kya-http-error-codes-UYb1zpGM.js +83 -0
- package/dist/kya-http.d.mts +3 -0
- package/dist/kya-http.d.ts +3 -0
- package/dist/kya-http.js +19 -0
- package/dist/kya-http.mjs +5 -0
- package/dist/mcpi/dashboard-config/index.d.mts.map +1 -1
- package/dist/mcpi/dashboard-config/index.d.ts.map +1 -1
- package/dist/mcpi/status-list/index.d.mts +72 -0
- package/dist/mcpi/status-list/index.d.mts.map +1 -0
- package/dist/mcpi/status-list/index.d.ts +72 -0
- package/dist/mcpi/status-list/index.d.ts.map +1 -0
- package/dist/mcpi/status-list/index.js +703 -0
- package/dist/mcpi/status-list/index.mjs +684 -0
- package/dist/mcpi/status-list/index.mjs.map +1 -0
- package/dist/patterns-B2r7M9wR.js +243 -0
- package/dist/patterns-B7dQoeE3.mjs +173 -0
- package/dist/patterns-B7dQoeE3.mjs.map +1 -0
- package/dist/patterns-Bp40cwh2.d.mts +110 -0
- package/dist/patterns-Bp40cwh2.d.mts.map +1 -0
- package/dist/patterns-Bp40cwh2.d.ts +110 -0
- package/dist/patterns-Bp40cwh2.d.ts.map +1 -0
- package/dist/patterns.d.mts +2 -0
- package/dist/patterns.d.ts +2 -0
- package/dist/patterns.js +15 -0
- package/dist/patterns.mjs +3 -0
- package/dist/policy/enforcement.d.mts +3 -0
- package/dist/policy/enforcement.d.ts +3 -0
- package/dist/policy/enforcement.js +21 -0
- package/dist/policy/enforcement.mjs +4 -0
- package/dist/policy/fetcher.d.mts +2 -0
- package/dist/policy/fetcher.d.ts +2 -0
- package/dist/policy/fetcher.js +240 -0
- package/dist/policy/fetcher.mjs +238 -0
- package/dist/policy/fetcher.mjs.map +1 -0
- package/dist/policy/schemas.d.mts +3 -0
- package/dist/policy/schemas.d.ts +3 -0
- package/dist/policy/schemas.js +27 -0
- package/dist/policy/schemas.mjs +4 -0
- package/dist/{posture-origins-Pn-ii-Pp.mjs → posture-origins-Ci6k4Zh1.js} +24 -173
- package/dist/{posture-origins-LFkX80oF.js → posture-origins-DGyS4Vuk.mjs} +2 -267
- package/dist/posture-origins-DGyS4Vuk.mjs.map +1 -0
- package/dist/{project-verification-key-BccT12tQ.js → project-verification-key-B-L3D2nC.js} +1 -1
- package/dist/{project-verification-key-66N4edKn.mjs → project-verification-key-CWXdu3-n.mjs} +2 -2
- package/dist/{project-verification-key-66N4edKn.mjs.map → project-verification-key-CWXdu3-n.mjs.map} +1 -1
- package/dist/provider-registry.d.mts +2 -0
- package/dist/provider-registry.d.ts +2 -0
- package/dist/provider-registry.js +51 -0
- package/dist/provider-registry.mjs +3 -0
- package/dist/reason-codes.d.mts +2 -0
- package/dist/reason-codes.d.ts +2 -0
- package/dist/reason-codes.js +8 -0
- package/dist/reason-codes.mjs +3 -0
- package/dist/registered-sid-26hNxp6t.mjs +151 -0
- package/dist/registered-sid-26hNxp6t.mjs.map +1 -0
- package/dist/registered-sid-D6KCaT1E.d.mts +33 -0
- package/dist/registered-sid-D6KCaT1E.d.mts.map +1 -0
- package/dist/registered-sid-D6KCaT1E.d.ts +33 -0
- package/dist/registered-sid-D6KCaT1E.d.ts.map +1 -0
- package/dist/registered-sid-DVGTSH0L.js +185 -0
- package/dist/ruleset-hash-Cmv8MSO5.js +34 -0
- package/dist/ruleset-hash-IRN5eI9H.d.mts +5 -0
- package/dist/ruleset-hash-IRN5eI9H.d.mts.map +1 -0
- package/dist/ruleset-hash-IRN5eI9H.d.ts +5 -0
- package/dist/ruleset-hash-IRN5eI9H.d.ts.map +1 -0
- package/dist/ruleset-hash-QQIaLuPi.mjs +30 -0
- package/dist/ruleset-hash-QQIaLuPi.mjs.map +1 -0
- package/dist/schemas-02CI7jgm.d.mts +535 -0
- package/dist/schemas-02CI7jgm.d.mts.map +1 -0
- package/dist/{schemas-YhagYDx_.js → schemas-BlScS1it.js} +0 -20
- package/dist/schemas-C_3C72SR.d.ts +535 -0
- package/dist/schemas-C_3C72SR.d.ts.map +1 -0
- package/dist/schemas-Cm4C4Z5G.js +703 -0
- package/dist/schemas-DPIT8usF.mjs +525 -0
- package/dist/schemas-DPIT8usF.mjs.map +1 -0
- package/dist/{schemas-7qodsG-Z.mjs → schemas-IHAhH3gV.mjs} +2 -16
- package/dist/schemas-IHAhH3gV.mjs.map +1 -0
- package/dist/step-up-BK0oBi11.mjs +1563 -0
- package/dist/step-up-BK0oBi11.mjs.map +1 -0
- package/dist/step-up-Bp8RAQwv.js +1759 -0
- package/dist/step-up-Bz12gYKT.d.mts +130 -0
- package/dist/step-up-Bz12gYKT.d.mts.map +1 -0
- package/dist/step-up-Bz12gYKT.d.ts +130 -0
- package/dist/step-up-Bz12gYKT.d.ts.map +1 -0
- package/dist/test-fixtures/index.d.mts +4 -4
- package/dist/test-fixtures/index.d.mts.map +1 -1
- package/dist/test-fixtures/index.d.ts +4 -4
- package/dist/test-fixtures/index.d.ts.map +1 -1
- package/dist/test-fixtures/index.js +4 -4
- package/dist/test-fixtures/index.mjs +4 -4
- package/dist/test-fixtures/index.mjs.map +1 -1
- package/dist/verification-BAw9N8Pt.js +181 -0
- package/dist/verification-Cvvv86TT.mjs +123 -0
- package/dist/verification-Cvvv86TT.mjs.map +1 -0
- package/fixtures/client-ip-resolution.n01.json +101 -0
- package/fixtures/reviewer-grant-revocation.v1.json +53 -81
- package/package.json +59 -2
- package/dist/client-ip-B2Xeo-GK.mjs.map +0 -1
- package/dist/posture-origins-Pn-ii-Pp.mjs.map +0 -1
- package/dist/schemas-7qodsG-Z.mjs.map +0 -1
|
@@ -0,0 +1,753 @@
|
|
|
1
|
+
const require_kya_http_error_codes = require('./kya-http-error-codes-UYb1zpGM.js');
|
|
2
|
+
|
|
3
|
+
//#region src/utils/detection-class.ts
|
|
4
|
+
/**
|
|
5
|
+
* Convert DetectionClass discriminated union to database enum string
|
|
6
|
+
* Note: Automation and Unknown are legacy types, converted to Bot and IncompleteData respectively
|
|
7
|
+
*/
|
|
8
|
+
function detectionClassToEnum(detectionClass) {
|
|
9
|
+
switch (detectionClass.type) {
|
|
10
|
+
case "Human": return "human";
|
|
11
|
+
case "AiAgent": return "ai_agent";
|
|
12
|
+
case "Automation": return "bot";
|
|
13
|
+
case "Bot": return "bot";
|
|
14
|
+
case "IncompleteData": return "incomplete_data";
|
|
15
|
+
default: return "incomplete_data";
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Convert database enum string to DetectionClass discriminated union
|
|
20
|
+
* Handles both current and legacy enum values for backwards compatibility
|
|
21
|
+
*/
|
|
22
|
+
function enumToDetectionClass(enumValue) {
|
|
23
|
+
switch (enumValue) {
|
|
24
|
+
case "human": return { type: "Human" };
|
|
25
|
+
case "ai_agent": return { type: "AiAgent" };
|
|
26
|
+
case "automation": return { type: "Bot" };
|
|
27
|
+
case "bot": return { type: "Bot" };
|
|
28
|
+
case "incomplete_data": return { type: "IncompleteData" };
|
|
29
|
+
case "unknown": return { type: "IncompleteData" };
|
|
30
|
+
default: return { type: "IncompleteData" };
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Check if a value is a valid string-based detection class (current or legacy)
|
|
35
|
+
*/
|
|
36
|
+
function isLegacyDetectionClass(value) {
|
|
37
|
+
return typeof value === "string" && [
|
|
38
|
+
"human",
|
|
39
|
+
"ai_agent",
|
|
40
|
+
"bot",
|
|
41
|
+
"incomplete_data",
|
|
42
|
+
"automation",
|
|
43
|
+
"unknown"
|
|
44
|
+
].includes(value);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Migrate legacy string detection class to new discriminated union
|
|
48
|
+
* @deprecated Use enumToDetectionClass instead
|
|
49
|
+
*/
|
|
50
|
+
function migrateLegacyDetectionClass(value) {
|
|
51
|
+
return enumToDetectionClass(value);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Safely convert any detection class value to the discriminated union format
|
|
55
|
+
*/
|
|
56
|
+
function normalizeDetectionClass(value) {
|
|
57
|
+
if (value && typeof value === "object" && "type" in value) return value;
|
|
58
|
+
if (isLegacyDetectionClass(value)) return enumToDetectionClass(value);
|
|
59
|
+
return { type: "IncompleteData" };
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Safely convert any detection class value to database enum format
|
|
63
|
+
*/
|
|
64
|
+
function normalizeDetectionClassEnum(value) {
|
|
65
|
+
if (value && typeof value === "object" && "type" in value) return detectionClassToEnum(value);
|
|
66
|
+
if (isLegacyDetectionClass(value)) return detectionClassToEnum(enumToDetectionClass(value));
|
|
67
|
+
return "incomplete_data";
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
//#endregion
|
|
71
|
+
//#region src/utils/bot-signals.ts
|
|
72
|
+
/** Cloudflare bot score: only 1-99 are real values; 0 or out-of-range normalize to absent. */
|
|
73
|
+
function normalizeBotScore(raw) {
|
|
74
|
+
if (typeof raw !== "number" || !Number.isFinite(raw) || !Number.isInteger(raw)) return void 0;
|
|
75
|
+
if (raw < 1 || raw > 99) return void 0;
|
|
76
|
+
return raw;
|
|
77
|
+
}
|
|
78
|
+
function normalizeVerifiedBot(raw) {
|
|
79
|
+
return typeof raw === "boolean" ? raw : void 0;
|
|
80
|
+
}
|
|
81
|
+
function normalizeString(raw) {
|
|
82
|
+
return raw && raw.length > 0 ? raw : void 0;
|
|
83
|
+
}
|
|
84
|
+
function normalizeAsn(raw) {
|
|
85
|
+
return typeof raw === "number" && Number.isFinite(raw) && Number.isInteger(raw) && raw > 0 ? raw : void 0;
|
|
86
|
+
}
|
|
87
|
+
function resolveExplicit(input) {
|
|
88
|
+
if (input.explicitBotSignalsTrusted !== true || !input.explicitBotSignals) return null;
|
|
89
|
+
const explicit = input.explicitBotSignals;
|
|
90
|
+
return {
|
|
91
|
+
score: normalizeBotScore(explicit.score),
|
|
92
|
+
verifiedBot: normalizeVerifiedBot(explicit.verifiedBot),
|
|
93
|
+
ja4: normalizeString(explicit.ja4),
|
|
94
|
+
asn: normalizeAsn(explicit.asn),
|
|
95
|
+
asOrganization: normalizeString(explicit.asOrganization),
|
|
96
|
+
source: "explicit"
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
function resolveCloudflare(input) {
|
|
100
|
+
const cf = input.cloudflareRequestCf;
|
|
101
|
+
if (!cf) return { source: "none" };
|
|
102
|
+
return {
|
|
103
|
+
score: normalizeBotScore(cf.botManagement?.score),
|
|
104
|
+
verifiedBot: normalizeVerifiedBot(cf.botManagement?.verifiedBot),
|
|
105
|
+
ja4: normalizeString(cf.botManagement?.ja4),
|
|
106
|
+
asn: normalizeAsn(cf.asn),
|
|
107
|
+
asOrganization: normalizeString(cf.asOrganization),
|
|
108
|
+
source: "cloudflare-bot-management"
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
/** Resolve bot signals using an explicit deployment trust profile. */
|
|
112
|
+
function resolveBotSignals(input) {
|
|
113
|
+
const explicit = resolveExplicit(input);
|
|
114
|
+
if (explicit) return explicit;
|
|
115
|
+
switch (input.policy.platform) {
|
|
116
|
+
case "cloudflare": return resolveCloudflare(input);
|
|
117
|
+
case "vercel":
|
|
118
|
+
case "trusted-proxy":
|
|
119
|
+
case "direct": return { source: "none" };
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
//#endregion
|
|
124
|
+
//#region src/host-adapter/verdict-cookie.ts
|
|
125
|
+
/**
|
|
126
|
+
* Canonical cookie name. Sites-1's `/blocked` page reads from this
|
|
127
|
+
* exact key; do not rename without updating the dashboard route.
|
|
128
|
+
*/
|
|
129
|
+
const VERDICT_COOKIE_NAME = "__checkpoint_verdict";
|
|
130
|
+
/**
|
|
131
|
+
* Compute the verdict label from a `RenderedResponse`.
|
|
132
|
+
*/
|
|
133
|
+
function deriveVerdict(rendered) {
|
|
134
|
+
if (rendered.status === null) return "allowed";
|
|
135
|
+
if (rendered.status === 302) return "redirected";
|
|
136
|
+
return "denied";
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Build the `__checkpoint_verdict` cookie value Sites-1's Sonner toast
|
|
140
|
+
* reads. Byte-format pinned by tests in both `checkpoint-nextjs` and
|
|
141
|
+
* `checkpoint-express`:
|
|
142
|
+
*
|
|
143
|
+
* - allowed: `{"verdict":"allowed"}`
|
|
144
|
+
* - denied: `{"verdict":"denied","reason":"<r>"}`
|
|
145
|
+
* - redirected: `{"verdict":"redirected"}`
|
|
146
|
+
*
|
|
147
|
+
* Returns raw JSON; the host's cookie-write call URL-encodes.
|
|
148
|
+
*/
|
|
149
|
+
function encodeVerdictCookie(rendered) {
|
|
150
|
+
const verdict = deriveVerdict(rendered);
|
|
151
|
+
const reason = rendered.headers["X-Checkpoint-Reason"];
|
|
152
|
+
return JSON.stringify(reason ? {
|
|
153
|
+
verdict,
|
|
154
|
+
reason
|
|
155
|
+
} : { verdict });
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
//#endregion
|
|
159
|
+
//#region src/host-adapter/content-negotiation.ts
|
|
160
|
+
/**
|
|
161
|
+
* Content negotiation — framework-agnostic Accept-header parsing for
|
|
162
|
+
* host adapters.
|
|
163
|
+
*
|
|
164
|
+
* The engine's `RenderedResponse` doesn't make HTML-vs-JSON branching
|
|
165
|
+
* decisions; the host wrapper does that based on what the client
|
|
166
|
+
* accepts. Both Next.js (NextRequest) and Express (Request) carry the
|
|
167
|
+
* Accept header in different shapes — `req.headers.get('accept')` vs
|
|
168
|
+
* `req.get('Accept')` vs `req.headers.accept` — so we accept any
|
|
169
|
+
* Headers-like input and do the parsing here.
|
|
170
|
+
*/
|
|
171
|
+
/**
|
|
172
|
+
* `true` when the client signaled it accepts HTML or XHTML. Used by
|
|
173
|
+
* host adapters to choose between an HTML `/blocked` rewrite (browsers)
|
|
174
|
+
* and a JSON 4xx response (curl, fetch-from-a-tool, ChatGPT-User).
|
|
175
|
+
*
|
|
176
|
+
* Loose match — q-values, parameters, and additional MIME types are
|
|
177
|
+
* allowed. The spec's q-value precedence (RFC 9110 § 12.5.1) doesn't
|
|
178
|
+
* matter here; we only care whether HTML is present at all.
|
|
179
|
+
*
|
|
180
|
+
* Accepts:
|
|
181
|
+
* - a `Headers` instance (Web Fetch API; Next.js, Hono, Cloudflare)
|
|
182
|
+
* - a plain `Record<string, string>` (Express, Connect, Fastify)
|
|
183
|
+
* - a single string (caller already extracted the Accept value)
|
|
184
|
+
* - `undefined` (treated as no-HTML)
|
|
185
|
+
*/
|
|
186
|
+
function acceptsHtml(input) {
|
|
187
|
+
const accept = extractAccept(input);
|
|
188
|
+
if (!accept) return false;
|
|
189
|
+
return accept.includes("text/html") || accept.includes("application/xhtml+xml");
|
|
190
|
+
}
|
|
191
|
+
function extractAccept(input) {
|
|
192
|
+
if (!input) return void 0;
|
|
193
|
+
if (typeof input === "string") return input;
|
|
194
|
+
if (typeof input.get === "function") return input.get("accept") ?? void 0;
|
|
195
|
+
const rec = input;
|
|
196
|
+
const value = rec.accept ?? rec.Accept;
|
|
197
|
+
if (Array.isArray(value)) return value.join(",");
|
|
198
|
+
return value;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
//#endregion
|
|
202
|
+
//#region src/host-adapter/decision-mapping.ts
|
|
203
|
+
/**
|
|
204
|
+
* Path the `/blocked` dashboard route lives at. HTML clients that hit
|
|
205
|
+
* a 4xx verdict get rewritten here at status 200 so the page renders
|
|
206
|
+
* with the verdict cookie set and Sonner can pick up the toast.
|
|
207
|
+
*
|
|
208
|
+
* Hard-coded by convention; if a customer ever needs a custom path,
|
|
209
|
+
* pass it through `CheckpointConfig.blockedPath` and override in the
|
|
210
|
+
* host adapter (not in this lookup).
|
|
211
|
+
*/
|
|
212
|
+
const BLOCKED_PATH = "/blocked";
|
|
213
|
+
/**
|
|
214
|
+
* Classify a `RenderedResponse` into one of the four host-adapter
|
|
215
|
+
* branches. `clientAcceptsHtml` comes from `acceptsHtml()` in
|
|
216
|
+
* `content-negotiation.ts`.
|
|
217
|
+
*/
|
|
218
|
+
function classifyResponseShape(rendered, clientAcceptsHtml) {
|
|
219
|
+
if (rendered.status === null) return "pass-through";
|
|
220
|
+
if (rendered.status === 302 && rendered.headers.Location) return "redirect";
|
|
221
|
+
if (clientAcceptsHtml && rendered.status >= 400 && rendered.status < 500) return "html-block";
|
|
222
|
+
return "json-block";
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
//#endregion
|
|
226
|
+
//#region src/host-adapter/fetcher-envelope.ts
|
|
227
|
+
/**
|
|
228
|
+
* Dual-envelope selection predicate (draft-kya-http-02 §8.1 fetcher-200).
|
|
229
|
+
*
|
|
230
|
+
* AI-agent fetchers (Claude-User WebFetch, ChatGPT) collapse 4xx/5xx
|
|
231
|
+
* responses and never surface the body to the LLM, so the §8.1 step-up
|
|
232
|
+
* challenge served at 401 is invisible. The fix is a dual envelope: keep
|
|
233
|
+
* the spec 401 for capable/discovery clients, and serve a body-readable
|
|
234
|
+
* HTTP 200 (no `WWW-Authenticate`) for cooperative, self-identifying
|
|
235
|
+
* agents so they can read the body + relay the consent link.
|
|
236
|
+
*
|
|
237
|
+
* `serveBodyReadable200` is the shared selection predicate every TS PEP
|
|
238
|
+
* (checkpoint-nextjs, checkpoint-express / wasm-runtime) consults when its
|
|
239
|
+
* challenge-emission mode is `negotiated`. Centralising it here — rather
|
|
240
|
+
* than re-implementing per PEP — makes drift structurally impossible; the
|
|
241
|
+
* fetcher-200 fixture freezes envelope BYTES, this function freezes the
|
|
242
|
+
* SELECTION.
|
|
243
|
+
*
|
|
244
|
+
* **This is a cooperative-UX bridge, NOT an access control.** A spoofer
|
|
245
|
+
* that hides its identity and does not opt in simply receives the spec
|
|
246
|
+
* 401 (and the original body-drop). Access is still gated entirely by the
|
|
247
|
+
* unchanged detection + delegation verification — never by which envelope
|
|
248
|
+
* is selected.
|
|
249
|
+
*/
|
|
250
|
+
/**
|
|
251
|
+
* Durable opt-in header — the primary, framework-agnostic signal that a
|
|
252
|
+
* caller is a cooperative agent able to handle the 200 fetcher envelope.
|
|
253
|
+
* Any non-empty value counts as opt-in (value semantics are reconciled
|
|
254
|
+
* with mcp-i-cloudflare's `KYA-Accept-Challenge: delegation` in the spec).
|
|
255
|
+
*/
|
|
256
|
+
const ACCEPT_CHALLENGE_HEADER = "kya-accept-challenge";
|
|
257
|
+
function readHeader(input, name) {
|
|
258
|
+
if (!input) return void 0;
|
|
259
|
+
if (typeof input.get === "function") return input.get(name) ?? void 0;
|
|
260
|
+
const rec = input;
|
|
261
|
+
const lower = name.toLowerCase();
|
|
262
|
+
for (const key of Object.keys(rec)) if (key.toLowerCase() === lower) {
|
|
263
|
+
const v = rec[key];
|
|
264
|
+
return Array.isArray(v) ? v[0] : v ?? void 0;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Decide whether a `negotiated`-mode PEP should serve the body-readable
|
|
269
|
+
* HTTP 200 fetcher envelope (`true`) or the spec 401 (`false`).
|
|
270
|
+
*
|
|
271
|
+
* Serve 200 when EITHER:
|
|
272
|
+
* 1. the caller opts in via a non-empty `KYA-Accept-Challenge` header
|
|
273
|
+
* (the durable, primary signal), OR
|
|
274
|
+
* 2. detection classifies the caller as an AI agent — BOTH `isAgent` and
|
|
275
|
+
* `detectionClass === 'ai_agent'` must agree (a transitional UA heuristic,
|
|
276
|
+
* retired as frameworks adopt the header).
|
|
277
|
+
*
|
|
278
|
+
* Otherwise serve the 401 — spoofers, silent, discovery, and OAuth
|
|
279
|
+
* clients all land here, which is acceptable because this is UX, not a
|
|
280
|
+
* security boundary.
|
|
281
|
+
*
|
|
282
|
+
* Requiring both `isAgent` AND the `ai_agent` class to fire the heuristic
|
|
283
|
+
* mirrors .NET `IsInteractiveAiAgent` (`IsAgent && DetectionClass.Type ==
|
|
284
|
+
* AiAgent`) and the gateway worker, so the cross-PEP selection cannot drift on
|
|
285
|
+
* the pathological case where the two signals disagree (class says agent,
|
|
286
|
+
* `isAgent` says no): that caller is treated as non-cooperative (spec 401),
|
|
287
|
+
* never demoted past it. In normal engine output the two signals agree, so
|
|
288
|
+
* who-gets-200 is unchanged for real traffic.
|
|
289
|
+
*
|
|
290
|
+
* @param headers request headers (for the opt-in signal)
|
|
291
|
+
* @param detectionClass the caller's detection class in any representation
|
|
292
|
+
* the engine produces (a `DetectionClass` object, the
|
|
293
|
+
* `ai_agent` wire string, or absent)
|
|
294
|
+
* @param isAgent the engine's `isAgent` verdict — gates the `ai_agent`
|
|
295
|
+
* heuristic so a class/`isAgent` disagreement gets the 401
|
|
296
|
+
*/
|
|
297
|
+
function serveBodyReadable200(headers, detectionClass, isAgent) {
|
|
298
|
+
const optIn = readHeader(headers, ACCEPT_CHALLENGE_HEADER);
|
|
299
|
+
if (optIn && optIn.trim().length > 0) return true;
|
|
300
|
+
if (isAgent && detectionClass !== void 0 && detectionClass !== null) {
|
|
301
|
+
if (normalizeDetectionClassEnum(detectionClass) === "ai_agent") return true;
|
|
302
|
+
}
|
|
303
|
+
return false;
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Resolve the challenge-emission mode + request signals into the single
|
|
307
|
+
* boolean each PEP needs: serve the body-readable 200 (`true`) or the spec
|
|
308
|
+
* 401 (`false`). Centralising the mode gate here — not just the predicate —
|
|
309
|
+
* keeps the rollout switch identical across every TS PEP.
|
|
310
|
+
*/
|
|
311
|
+
function selectBodyReadable200(mode, headers, detectionClass, isAgent) {
|
|
312
|
+
if (mode === "always-200") return true;
|
|
313
|
+
if (mode === "negotiated") return serveBodyReadable200(headers, detectionClass, isAgent);
|
|
314
|
+
return false;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
//#endregion
|
|
318
|
+
//#region src/host-adapter/challenge-wire.ts
|
|
319
|
+
/**
|
|
320
|
+
* Canonical fixture-pinned KYA HTTP challenge-wire builder.
|
|
321
|
+
*
|
|
322
|
+
* The builder is transport-neutral and Edge-safe. Hosts must register the
|
|
323
|
+
* challenge and resolve every URI before calling it; this module validates and
|
|
324
|
+
* serializes those trusted values without network, database, or request access.
|
|
325
|
+
*/
|
|
326
|
+
const PROTECTED_RESOURCE_METADATA_PATH = "/.well-known/oauth-protected-resource";
|
|
327
|
+
const DEFAULT_POLL_INTERVAL_S = 3;
|
|
328
|
+
const INVALID_QUOTED_VALUE = /[\u0000-\u001f\u007f"\\]/;
|
|
329
|
+
const SCOPE_TOKEN = /^[\x21\x23-\x5b\x5d-\x7e]+$/;
|
|
330
|
+
const SID = /^kya-[0-9a-f]{16,}$/;
|
|
331
|
+
const ASSURANCE_TIERS = /* @__PURE__ */ new Set([
|
|
332
|
+
"anonymous",
|
|
333
|
+
"attested-bearer",
|
|
334
|
+
"key-bound",
|
|
335
|
+
"registered",
|
|
336
|
+
"delegated"
|
|
337
|
+
]);
|
|
338
|
+
/** Typed validation failure raised before any response is materialized. */
|
|
339
|
+
var ChallengeWireError = class extends TypeError {
|
|
340
|
+
code;
|
|
341
|
+
constructor(code, message) {
|
|
342
|
+
super(message);
|
|
343
|
+
this.name = "ChallengeWireError";
|
|
344
|
+
this.code = code;
|
|
345
|
+
}
|
|
346
|
+
};
|
|
347
|
+
/** Generate a 128-bit, lower-case hexadecimal challenge id. */
|
|
348
|
+
function generateDelegationSid() {
|
|
349
|
+
const bytes = /* @__PURE__ */ new Uint8Array(16);
|
|
350
|
+
crypto.getRandomValues(bytes);
|
|
351
|
+
let hex = "";
|
|
352
|
+
for (const byte of bytes) hex += byte.toString(16).padStart(2, "0");
|
|
353
|
+
return `kya-${hex}`;
|
|
354
|
+
}
|
|
355
|
+
/** Derive pickup from the registered consent origin, never request Host. */
|
|
356
|
+
function deriveDelegationPickupUri(consentUri, sid) {
|
|
357
|
+
const consent = parseHttpsUrl("consentUri", consentUri);
|
|
358
|
+
assertQuotedValue("sid", sid, "invalid-sid");
|
|
359
|
+
return `${consent.origin}/api/delegation/pickup/${sid}`;
|
|
360
|
+
}
|
|
361
|
+
/** Build the byte-pinned Delegation authentication field value. */
|
|
362
|
+
function buildDelegationChallengeHeader(input) {
|
|
363
|
+
const normalized = validateInput(input);
|
|
364
|
+
const params = [
|
|
365
|
+
["realm", normalized.realm],
|
|
366
|
+
["sid", normalized.sid],
|
|
367
|
+
["consent_uri", normalized.consentUri],
|
|
368
|
+
["pickup_uri", normalized.pickupUri]
|
|
369
|
+
];
|
|
370
|
+
if (normalized.requiredScopes.length > 0) params.push(["scope", normalized.requiredScopes.join(" ")]);
|
|
371
|
+
if (normalized.minAssurance) params.push(["min_assurance", normalized.minAssurance]);
|
|
372
|
+
if (input.pollIntervalS !== void 0) params.push(["interval", String(normalized.pollIntervalS)]);
|
|
373
|
+
params.push(["kind", normalized.kind]);
|
|
374
|
+
return `Delegation ${params.map(([name, value]) => `${name}="${value}"`).join(", ")}`;
|
|
375
|
+
}
|
|
376
|
+
/** Build RFC 9728 Bearer discovery for a protected-resource realm. */
|
|
377
|
+
function buildDelegationBearerHeader(realm) {
|
|
378
|
+
assertRealm(realm);
|
|
379
|
+
return `Bearer resource_metadata="https://${realm}${PROTECTED_RESOURCE_METADATA_PATH}"`;
|
|
380
|
+
}
|
|
381
|
+
/** Build the four required machine-followable delegation instruction elements. */
|
|
382
|
+
function buildDelegationInstructions(consentUri, pickupUri, pollIntervalS) {
|
|
383
|
+
return `This action requires human authorization. First establish your pickup binding at ${pickupUri}. If you can sign HTTP requests, sign each poll with an ephemeral did:key; otherwise generate one random 32-byte token encoded as unpadded base64url and send it as X-KYA-Pickup-Token on EVERY poll. After receiving 202 pending, show your user this link and ask them to approve: ${consentUri}. Continue polling ${pickupUri} every ${pollIntervalS} seconds with the same binding. When the response status is 200, take the 'delegation' value and retry your original request with header 'Authorization: Bearer <delegation>'.`;
|
|
384
|
+
}
|
|
385
|
+
/** Build the proof-bound body shared byte-for-byte by both HTTP envelopes. */
|
|
386
|
+
function buildDelegationChallengeBody(input) {
|
|
387
|
+
const normalized = validateInput(input);
|
|
388
|
+
const common = {
|
|
389
|
+
sid: normalized.sid,
|
|
390
|
+
consent_uri: normalized.consentUri,
|
|
391
|
+
pickup_uri: normalized.pickupUri,
|
|
392
|
+
...normalized.requiredScopes.length > 0 ? { required_scopes: [...normalized.requiredScopes] } : {},
|
|
393
|
+
...normalized.minAssurance ? { min_assurance: normalized.minAssurance } : {},
|
|
394
|
+
poll_interval_s: normalized.pollIntervalS,
|
|
395
|
+
challenge_proof: input.challengeProof
|
|
396
|
+
};
|
|
397
|
+
if (normalized.kind === "approval_quorum") return {
|
|
398
|
+
error: require_kya_http_error_codes.KYA_HTTP_ERROR.AUTHORIZATION_REQUIRED,
|
|
399
|
+
kyaos: {
|
|
400
|
+
sid: common.sid,
|
|
401
|
+
kind: "approval_quorum",
|
|
402
|
+
quorum: normalized.quorum,
|
|
403
|
+
action_nonce: normalized.actionNonce,
|
|
404
|
+
consent_uri: common.consent_uri,
|
|
405
|
+
pickup_uri: common.pickup_uri,
|
|
406
|
+
...common.required_scopes ? { required_scopes: common.required_scopes } : {},
|
|
407
|
+
...common.min_assurance ? { min_assurance: common.min_assurance } : {},
|
|
408
|
+
poll_interval_s: common.poll_interval_s,
|
|
409
|
+
challenge_proof: common.challenge_proof
|
|
410
|
+
},
|
|
411
|
+
instructions: buildApprovalQuorumInstructions(normalized)
|
|
412
|
+
};
|
|
413
|
+
return {
|
|
414
|
+
error: require_kya_http_error_codes.KYA_HTTP_ERROR.AUTHORIZATION_REQUIRED,
|
|
415
|
+
kyaos: {
|
|
416
|
+
...common,
|
|
417
|
+
action: {
|
|
418
|
+
type: "delegation",
|
|
419
|
+
consent_uri: normalized.consentUri,
|
|
420
|
+
pickup_uri: normalized.pickupUri,
|
|
421
|
+
poll_interval_s: normalized.pollIntervalS,
|
|
422
|
+
pin: { header: "X-KYA-Pickup-Token" },
|
|
423
|
+
retry: {
|
|
424
|
+
header: "Authorization",
|
|
425
|
+
scheme: "Bearer",
|
|
426
|
+
value_from: "delegation"
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
},
|
|
430
|
+
instructions: buildDelegationInstructions(normalized.consentUri, normalized.pickupUri, normalized.pollIntervalS)
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
/** Build either the canonical 401 or its body-identical fetcher-200 envelope. */
|
|
434
|
+
function buildDelegationChallengeEnvelope(input, mode = "spec-401") {
|
|
435
|
+
const normalized = validateInput(input);
|
|
436
|
+
if (normalized.kind === "approval_quorum" && mode === "fetcher-200") throw new ChallengeWireError("unsupported-envelope", "fetcher-200 is not fixture-defined for approval_quorum");
|
|
437
|
+
const body = buildDelegationChallengeBody(input);
|
|
438
|
+
if (normalized.kind === "approval_quorum") return {
|
|
439
|
+
status: 401,
|
|
440
|
+
headers: [
|
|
441
|
+
["WWW-Authenticate", buildDelegationChallengeHeader(input)],
|
|
442
|
+
["Retry-After", String(normalized.pollIntervalS)],
|
|
443
|
+
["Content-Type", "application/json"],
|
|
444
|
+
["Cache-Control", "no-store"]
|
|
445
|
+
],
|
|
446
|
+
body
|
|
447
|
+
};
|
|
448
|
+
const hintHeaders = [
|
|
449
|
+
["Content-Type", "application/json"],
|
|
450
|
+
["Cache-Control", "no-store"],
|
|
451
|
+
["KYA-Auth-Required", "true"],
|
|
452
|
+
["KYA-Auth-Url", normalized.consentUri],
|
|
453
|
+
["Link", `<${normalized.consentUri}>; rel="kya-authorize"`],
|
|
454
|
+
["KYA-Action", "challenge"]
|
|
455
|
+
];
|
|
456
|
+
if (mode === "fetcher-200") return {
|
|
457
|
+
status: 200,
|
|
458
|
+
headers: hintHeaders,
|
|
459
|
+
body
|
|
460
|
+
};
|
|
461
|
+
return {
|
|
462
|
+
status: 401,
|
|
463
|
+
headers: [
|
|
464
|
+
["WWW-Authenticate", buildDelegationChallengeHeader(input)],
|
|
465
|
+
["WWW-Authenticate", `Bearer resource_metadata="${normalized.resourceMetadataUri}"`],
|
|
466
|
+
["Retry-After", String(normalized.pollIntervalS)],
|
|
467
|
+
...hintHeaders
|
|
468
|
+
],
|
|
469
|
+
body
|
|
470
|
+
};
|
|
471
|
+
}
|
|
472
|
+
/** Preferred generic name; the legacy export remains for host compatibility. */
|
|
473
|
+
const buildChallengeWire = buildDelegationChallengeEnvelope;
|
|
474
|
+
function validateInput(input) {
|
|
475
|
+
assertRealm(input.realm);
|
|
476
|
+
assertQuotedValue("sid", input.sid, "invalid-sid");
|
|
477
|
+
if (!SID.test(input.sid)) throw new ChallengeWireError("invalid-sid", "sid must start with kya- and contain at least 64 bits of lower-case hexadecimal entropy");
|
|
478
|
+
const consent = parseHttpsUrl("consentUri", input.consentUri);
|
|
479
|
+
const pickup = parseHttpsUrl("pickupUri", input.pickupUri);
|
|
480
|
+
const expectedPickup = `${consent.origin}/api/delegation/pickup/${input.sid}`;
|
|
481
|
+
if (pickup.href !== expectedPickup) throw new ChallengeWireError("invalid-uri", "pickupUri must use the registered consent origin and sid");
|
|
482
|
+
const pollIntervalS = input.pollIntervalS ?? DEFAULT_POLL_INTERVAL_S;
|
|
483
|
+
if (!Number.isInteger(pollIntervalS) || pollIntervalS < 1 || pollIntervalS > 999) throw new ChallengeWireError("invalid-interval", "pollIntervalS must be an integer from 1 through 999");
|
|
484
|
+
if (input.challengeProof === void 0 || input.challengeProof === null) throw new ChallengeWireError("invalid-proof", "challengeProof is required");
|
|
485
|
+
const requiredScopes = input.requiredScopes ?? [];
|
|
486
|
+
for (const scope of requiredScopes) if (!SCOPE_TOKEN.test(scope)) throw new ChallengeWireError("invalid-scope", `invalid required scope: ${scope}`);
|
|
487
|
+
if (input.minAssurance && !ASSURANCE_TIERS.has(input.minAssurance)) throw new ChallengeWireError("invalid-assurance", `invalid minAssurance: ${input.minAssurance}`);
|
|
488
|
+
const kind = input.kind ?? "delegation";
|
|
489
|
+
if (kind !== "delegation" && kind !== "approval_quorum") throw new ChallengeWireError("invalid-kind", `unsupported challenge kind: ${String(kind)}`);
|
|
490
|
+
let resourceMetadataUri;
|
|
491
|
+
let quorum;
|
|
492
|
+
let actionNonce;
|
|
493
|
+
if (kind === "delegation") {
|
|
494
|
+
resourceMetadataUri = parseHttpsUrl("resourceMetadataUri", input.resourceMetadataUri).href;
|
|
495
|
+
const expectedMetadata = `https://${input.realm}${PROTECTED_RESOURCE_METADATA_PATH}`;
|
|
496
|
+
if (resourceMetadataUri !== expectedMetadata) throw new ChallengeWireError("invalid-uri", "resourceMetadataUri must be the protected resource RFC 9728 metadata URL");
|
|
497
|
+
} else {
|
|
498
|
+
const approval = input;
|
|
499
|
+
quorum = approval.quorum;
|
|
500
|
+
actionNonce = approval.actionNonce;
|
|
501
|
+
if (!Number.isInteger(quorum) || quorum < 1) throw new ChallengeWireError("invalid-quorum", "quorum must be a positive integer");
|
|
502
|
+
assertQuotedValue("actionNonce", actionNonce, "invalid-action-nonce");
|
|
503
|
+
}
|
|
504
|
+
return {
|
|
505
|
+
realm: input.realm,
|
|
506
|
+
sid: input.sid,
|
|
507
|
+
consentUri: consent.href,
|
|
508
|
+
pickupUri: pickup.href,
|
|
509
|
+
resourceMetadataUri,
|
|
510
|
+
requiredScopes,
|
|
511
|
+
pollIntervalS,
|
|
512
|
+
minAssurance: input.minAssurance,
|
|
513
|
+
kind,
|
|
514
|
+
quorum,
|
|
515
|
+
actionNonce
|
|
516
|
+
};
|
|
517
|
+
}
|
|
518
|
+
function buildApprovalQuorumInstructions(input) {
|
|
519
|
+
return `This action requires approval from ${input.quorum} human approvers. Show your user this link to begin the approval process: ${input.consentUri}. Then poll ${input.pickupUri} every ${input.pollIntervalS} seconds (sign polls with an ephemeral did:key, or send one random 32-byte X-KYA-Pickup-Token on EVERY poll). When the response status is 200, retry your original request presenting the returned grants in the KYA-Approvals header.`;
|
|
520
|
+
}
|
|
521
|
+
function assertRealm(realm) {
|
|
522
|
+
assertQuotedValue("realm", realm, "invalid-realm");
|
|
523
|
+
if (realm.includes("/") || realm.includes("@") || realm.includes("?") || realm.includes("#")) throw new ChallengeWireError("invalid-realm", "realm must be a host with an optional port");
|
|
524
|
+
try {
|
|
525
|
+
if (new URL(`https://${realm}`).host !== realm) throw new Error("invalid authority");
|
|
526
|
+
} catch {
|
|
527
|
+
throw new ChallengeWireError("invalid-realm", "realm must be a valid host with an optional port");
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
function assertQuotedValue(name, value, code) {
|
|
531
|
+
if (typeof value !== "string" || value.length === 0 || INVALID_QUOTED_VALUE.test(value)) throw new ChallengeWireError(code, `${name} contains characters unsafe for an HTTP quoted-string`);
|
|
532
|
+
}
|
|
533
|
+
function parseHttpsUrl(name, value) {
|
|
534
|
+
assertQuotedValue(name, value, "invalid-uri");
|
|
535
|
+
let parsed;
|
|
536
|
+
try {
|
|
537
|
+
parsed = new URL(value);
|
|
538
|
+
} catch {
|
|
539
|
+
throw new ChallengeWireError("invalid-uri", `${name} must be an absolute HTTPS URL`);
|
|
540
|
+
}
|
|
541
|
+
if (parsed.protocol !== "https:" || parsed.username || parsed.password) throw new ChallengeWireError("invalid-uri", `${name} must be an absolute HTTPS URL without credentials`);
|
|
542
|
+
return parsed;
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
//#endregion
|
|
546
|
+
//#region src/host-adapter/challenge-context.ts
|
|
547
|
+
const DEFAULT_CHALLENGE_CONTEXT_TIMEOUT_MS = 3e3;
|
|
548
|
+
/**
|
|
549
|
+
* Resolve and validate registered challenge context at the async host boundary.
|
|
550
|
+
* Resolver failures become an absent context so the renderer can emit the
|
|
551
|
+
* canonical retryable status-pending response; secrets and proof values are
|
|
552
|
+
* never included in failure events.
|
|
553
|
+
*/
|
|
554
|
+
async function resolveChallengeContext(resolver, context, options = {}) {
|
|
555
|
+
if (!resolver) return void 0;
|
|
556
|
+
const timeoutMs = options.timeoutMs ?? 3e3;
|
|
557
|
+
if (!Number.isInteger(timeoutMs) || timeoutMs < 1) throw new TypeError("challenge context timeout must be a positive integer");
|
|
558
|
+
let timeoutId;
|
|
559
|
+
try {
|
|
560
|
+
const value = await Promise.race([Promise.resolve(resolver(context)), new Promise((_resolve, reject) => {
|
|
561
|
+
timeoutId = setTimeout(() => reject(new ChallengeContextTimeoutError(timeoutMs)), timeoutMs);
|
|
562
|
+
})]);
|
|
563
|
+
if (timeoutId !== void 0) clearTimeout(timeoutId);
|
|
564
|
+
if (!value) return void 0;
|
|
565
|
+
try {
|
|
566
|
+
buildChallengeWire(value, "spec-401");
|
|
567
|
+
return value;
|
|
568
|
+
} catch (error) {
|
|
569
|
+
options.onFailure?.({
|
|
570
|
+
reason: "invalid-context",
|
|
571
|
+
message: error.message
|
|
572
|
+
});
|
|
573
|
+
return;
|
|
574
|
+
}
|
|
575
|
+
} catch (error) {
|
|
576
|
+
if (timeoutId !== void 0) clearTimeout(timeoutId);
|
|
577
|
+
options.onFailure?.({
|
|
578
|
+
reason: error instanceof ChallengeContextTimeoutError ? "timeout" : "resolver-error",
|
|
579
|
+
message: error instanceof ChallengeContextTimeoutError ? error.message : "challenge context resolver failed"
|
|
580
|
+
});
|
|
581
|
+
return;
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
var ChallengeContextTimeoutError = class extends Error {
|
|
585
|
+
constructor(timeoutMs) {
|
|
586
|
+
super(`challenge context resolution exceeded ${timeoutMs}ms`);
|
|
587
|
+
this.name = "ChallengeContextTimeoutError";
|
|
588
|
+
}
|
|
589
|
+
};
|
|
590
|
+
|
|
591
|
+
//#endregion
|
|
592
|
+
Object.defineProperty(exports, 'ACCEPT_CHALLENGE_HEADER', {
|
|
593
|
+
enumerable: true,
|
|
594
|
+
get: function () {
|
|
595
|
+
return ACCEPT_CHALLENGE_HEADER;
|
|
596
|
+
}
|
|
597
|
+
});
|
|
598
|
+
Object.defineProperty(exports, 'BLOCKED_PATH', {
|
|
599
|
+
enumerable: true,
|
|
600
|
+
get: function () {
|
|
601
|
+
return BLOCKED_PATH;
|
|
602
|
+
}
|
|
603
|
+
});
|
|
604
|
+
Object.defineProperty(exports, 'ChallengeWireError', {
|
|
605
|
+
enumerable: true,
|
|
606
|
+
get: function () {
|
|
607
|
+
return ChallengeWireError;
|
|
608
|
+
}
|
|
609
|
+
});
|
|
610
|
+
Object.defineProperty(exports, 'DEFAULT_CHALLENGE_CONTEXT_TIMEOUT_MS', {
|
|
611
|
+
enumerable: true,
|
|
612
|
+
get: function () {
|
|
613
|
+
return DEFAULT_CHALLENGE_CONTEXT_TIMEOUT_MS;
|
|
614
|
+
}
|
|
615
|
+
});
|
|
616
|
+
Object.defineProperty(exports, 'VERDICT_COOKIE_NAME', {
|
|
617
|
+
enumerable: true,
|
|
618
|
+
get: function () {
|
|
619
|
+
return VERDICT_COOKIE_NAME;
|
|
620
|
+
}
|
|
621
|
+
});
|
|
622
|
+
Object.defineProperty(exports, 'acceptsHtml', {
|
|
623
|
+
enumerable: true,
|
|
624
|
+
get: function () {
|
|
625
|
+
return acceptsHtml;
|
|
626
|
+
}
|
|
627
|
+
});
|
|
628
|
+
Object.defineProperty(exports, 'buildChallengeWire', {
|
|
629
|
+
enumerable: true,
|
|
630
|
+
get: function () {
|
|
631
|
+
return buildChallengeWire;
|
|
632
|
+
}
|
|
633
|
+
});
|
|
634
|
+
Object.defineProperty(exports, 'buildDelegationBearerHeader', {
|
|
635
|
+
enumerable: true,
|
|
636
|
+
get: function () {
|
|
637
|
+
return buildDelegationBearerHeader;
|
|
638
|
+
}
|
|
639
|
+
});
|
|
640
|
+
Object.defineProperty(exports, 'buildDelegationChallengeBody', {
|
|
641
|
+
enumerable: true,
|
|
642
|
+
get: function () {
|
|
643
|
+
return buildDelegationChallengeBody;
|
|
644
|
+
}
|
|
645
|
+
});
|
|
646
|
+
Object.defineProperty(exports, 'buildDelegationChallengeEnvelope', {
|
|
647
|
+
enumerable: true,
|
|
648
|
+
get: function () {
|
|
649
|
+
return buildDelegationChallengeEnvelope;
|
|
650
|
+
}
|
|
651
|
+
});
|
|
652
|
+
Object.defineProperty(exports, 'buildDelegationChallengeHeader', {
|
|
653
|
+
enumerable: true,
|
|
654
|
+
get: function () {
|
|
655
|
+
return buildDelegationChallengeHeader;
|
|
656
|
+
}
|
|
657
|
+
});
|
|
658
|
+
Object.defineProperty(exports, 'buildDelegationInstructions', {
|
|
659
|
+
enumerable: true,
|
|
660
|
+
get: function () {
|
|
661
|
+
return buildDelegationInstructions;
|
|
662
|
+
}
|
|
663
|
+
});
|
|
664
|
+
Object.defineProperty(exports, 'classifyResponseShape', {
|
|
665
|
+
enumerable: true,
|
|
666
|
+
get: function () {
|
|
667
|
+
return classifyResponseShape;
|
|
668
|
+
}
|
|
669
|
+
});
|
|
670
|
+
Object.defineProperty(exports, 'deriveDelegationPickupUri', {
|
|
671
|
+
enumerable: true,
|
|
672
|
+
get: function () {
|
|
673
|
+
return deriveDelegationPickupUri;
|
|
674
|
+
}
|
|
675
|
+
});
|
|
676
|
+
Object.defineProperty(exports, 'deriveVerdict', {
|
|
677
|
+
enumerable: true,
|
|
678
|
+
get: function () {
|
|
679
|
+
return deriveVerdict;
|
|
680
|
+
}
|
|
681
|
+
});
|
|
682
|
+
Object.defineProperty(exports, 'detectionClassToEnum', {
|
|
683
|
+
enumerable: true,
|
|
684
|
+
get: function () {
|
|
685
|
+
return detectionClassToEnum;
|
|
686
|
+
}
|
|
687
|
+
});
|
|
688
|
+
Object.defineProperty(exports, 'encodeVerdictCookie', {
|
|
689
|
+
enumerable: true,
|
|
690
|
+
get: function () {
|
|
691
|
+
return encodeVerdictCookie;
|
|
692
|
+
}
|
|
693
|
+
});
|
|
694
|
+
Object.defineProperty(exports, 'enumToDetectionClass', {
|
|
695
|
+
enumerable: true,
|
|
696
|
+
get: function () {
|
|
697
|
+
return enumToDetectionClass;
|
|
698
|
+
}
|
|
699
|
+
});
|
|
700
|
+
Object.defineProperty(exports, 'generateDelegationSid', {
|
|
701
|
+
enumerable: true,
|
|
702
|
+
get: function () {
|
|
703
|
+
return generateDelegationSid;
|
|
704
|
+
}
|
|
705
|
+
});
|
|
706
|
+
Object.defineProperty(exports, 'isLegacyDetectionClass', {
|
|
707
|
+
enumerable: true,
|
|
708
|
+
get: function () {
|
|
709
|
+
return isLegacyDetectionClass;
|
|
710
|
+
}
|
|
711
|
+
});
|
|
712
|
+
Object.defineProperty(exports, 'migrateLegacyDetectionClass', {
|
|
713
|
+
enumerable: true,
|
|
714
|
+
get: function () {
|
|
715
|
+
return migrateLegacyDetectionClass;
|
|
716
|
+
}
|
|
717
|
+
});
|
|
718
|
+
Object.defineProperty(exports, 'normalizeDetectionClass', {
|
|
719
|
+
enumerable: true,
|
|
720
|
+
get: function () {
|
|
721
|
+
return normalizeDetectionClass;
|
|
722
|
+
}
|
|
723
|
+
});
|
|
724
|
+
Object.defineProperty(exports, 'normalizeDetectionClassEnum', {
|
|
725
|
+
enumerable: true,
|
|
726
|
+
get: function () {
|
|
727
|
+
return normalizeDetectionClassEnum;
|
|
728
|
+
}
|
|
729
|
+
});
|
|
730
|
+
Object.defineProperty(exports, 'resolveBotSignals', {
|
|
731
|
+
enumerable: true,
|
|
732
|
+
get: function () {
|
|
733
|
+
return resolveBotSignals;
|
|
734
|
+
}
|
|
735
|
+
});
|
|
736
|
+
Object.defineProperty(exports, 'resolveChallengeContext', {
|
|
737
|
+
enumerable: true,
|
|
738
|
+
get: function () {
|
|
739
|
+
return resolveChallengeContext;
|
|
740
|
+
}
|
|
741
|
+
});
|
|
742
|
+
Object.defineProperty(exports, 'selectBodyReadable200', {
|
|
743
|
+
enumerable: true,
|
|
744
|
+
get: function () {
|
|
745
|
+
return selectBodyReadable200;
|
|
746
|
+
}
|
|
747
|
+
});
|
|
748
|
+
Object.defineProperty(exports, 'serveBodyReadable200', {
|
|
749
|
+
enumerable: true,
|
|
750
|
+
get: function () {
|
|
751
|
+
return serveBodyReadable200;
|
|
752
|
+
}
|
|
753
|
+
});
|