@kya-os/checkpoint-shared 1.5.0 → 1.5.2

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.
Files changed (141) hide show
  1. package/CHANGELOG.md +129 -0
  2. package/dist/beacon-nonce/index.d.mts +36 -0
  3. package/dist/beacon-nonce/index.d.mts.map +1 -0
  4. package/dist/beacon-nonce/index.d.ts +36 -0
  5. package/dist/beacon-nonce/index.d.ts.map +1 -0
  6. package/dist/beacon-nonce/index.js +305 -0
  7. package/dist/beacon-nonce/index.mjs +294 -0
  8. package/dist/beacon-nonce/index.mjs.map +1 -0
  9. package/dist/beacon-types/index.d.mts +50 -14
  10. package/dist/beacon-types/index.d.mts.map +1 -1
  11. package/dist/beacon-types/index.d.ts +50 -14
  12. package/dist/beacon-types/index.d.ts.map +1 -1
  13. package/dist/beacon-types/index.js +127 -8
  14. package/dist/beacon-types/index.mjs +122 -9
  15. package/dist/beacon-types/index.mjs.map +1 -1
  16. package/dist/browser-posture-D4j2a5gi.d.ts +169 -0
  17. package/dist/browser-posture-D4j2a5gi.d.ts.map +1 -0
  18. package/dist/browser-posture-DVoHMHjY.d.mts +169 -0
  19. package/dist/browser-posture-DVoHMHjY.d.mts.map +1 -0
  20. package/dist/browser-posture-cookie.d.mts +4 -2
  21. package/dist/browser-posture-cookie.d.mts.map +1 -0
  22. package/dist/browser-posture-cookie.d.ts +4 -2
  23. package/dist/browser-posture-cookie.d.ts.map +1 -0
  24. package/dist/browser-posture-cookie.mjs.map +1 -1
  25. package/dist/browser-posture-proof.d.mts +18 -0
  26. package/dist/browser-posture-proof.d.mts.map +1 -0
  27. package/dist/browser-posture-proof.d.ts +18 -0
  28. package/dist/browser-posture-proof.d.ts.map +1 -0
  29. package/dist/browser-posture-proof.js +66 -0
  30. package/dist/browser-posture-proof.mjs +63 -0
  31. package/dist/browser-posture-proof.mjs.map +1 -0
  32. package/dist/browser-posture-report.d.mts +59 -2
  33. package/dist/browser-posture-report.d.mts.map +1 -1
  34. package/dist/browser-posture-report.d.ts +59 -2
  35. package/dist/browser-posture-report.d.ts.map +1 -1
  36. package/dist/browser-posture-report.js +1 -1
  37. package/dist/browser-posture-report.mjs +1 -1
  38. package/dist/browser-posture-report.mjs.map +1 -1
  39. package/dist/browser-posture.d.mts +2 -83
  40. package/dist/browser-posture.d.ts +2 -83
  41. package/dist/browser-posture.js +214 -44
  42. package/dist/browser-posture.mjs +203 -45
  43. package/dist/browser-posture.mjs.map +1 -1
  44. package/dist/browser.d.mts +3 -11
  45. package/dist/browser.d.mts.map +1 -1
  46. package/dist/browser.d.ts +3 -11
  47. package/dist/browser.d.ts.map +1 -1
  48. package/dist/browser.js +1 -1
  49. package/dist/browser.mjs +1 -1
  50. package/dist/browser.mjs.map +1 -1
  51. package/dist/clearance.d.mts +1 -1
  52. package/dist/clearance.d.ts +1 -1
  53. package/dist/{client-ip-HOfe6ucV.mjs → client-ip-B2Xeo-GK.mjs} +2 -2
  54. package/dist/{client-ip-HOfe6ucV.mjs.map → client-ip-B2Xeo-GK.mjs.map} +1 -1
  55. package/dist/{client-ip-1y2iXAYW.js → client-ip-CW6pRyLf.js} +1 -1
  56. package/dist/consent-handoff.d.mts +41 -0
  57. package/dist/consent-handoff.d.mts.map +1 -0
  58. package/dist/consent-handoff.d.ts +41 -0
  59. package/dist/consent-handoff.d.ts.map +1 -0
  60. package/dist/consent-handoff.js +1 -0
  61. package/dist/consent-handoff.mjs +2 -0
  62. package/dist/consent-handoff.mjs.map +1 -0
  63. package/dist/errors/index.d.mts +138 -0
  64. package/dist/errors/index.d.mts.map +1 -1
  65. package/dist/errors/index.d.ts +138 -0
  66. package/dist/errors/index.d.ts.map +1 -1
  67. package/dist/errors/index.js +441 -9
  68. package/dist/errors/index.mjs +441 -10
  69. package/dist/errors/index.mjs.map +1 -1
  70. package/dist/{external-jr_S4iSS.d.mts → external-Ct8j_hto.d.mts} +2 -2
  71. package/dist/{external-jr_S4iSS.d.mts.map → external-Ct8j_hto.d.mts.map} +1 -1
  72. package/dist/{external-jr_S4iSS.d.ts → external-Ct8j_hto.d.ts} +2 -2
  73. package/dist/{external-jr_S4iSS.d.ts.map → external-Ct8j_hto.d.ts.map} +1 -1
  74. package/dist/gateway-client-ip.d.mts +1 -1
  75. package/dist/gateway-client-ip.d.ts +1 -1
  76. package/dist/gateway-client-ip.js +1 -1
  77. package/dist/gateway-client-ip.mjs +1 -1
  78. package/dist/index.d.mts +5227 -1063
  79. package/dist/index.d.mts.map +1 -1
  80. package/dist/index.d.ts +5227 -1063
  81. package/dist/index.d.ts.map +1 -1
  82. package/dist/index.js +3678 -546
  83. package/dist/index.mjs +3446 -534
  84. package/dist/index.mjs.map +1 -1
  85. package/dist/mcpi/approval-grant/index.d.mts +1 -1
  86. package/dist/mcpi/approval-grant/index.d.mts.map +1 -1
  87. package/dist/mcpi/approval-grant/index.d.ts +1 -1
  88. package/dist/mcpi/approval-grant/index.d.ts.map +1 -1
  89. package/dist/mcpi/approval-grant/index.js +48 -48
  90. package/dist/mcpi/approval-grant/index.mjs +48 -48
  91. package/dist/mcpi/approval-grant/index.mjs.map +1 -1
  92. package/dist/posture-origins/index.d.mts +41 -0
  93. package/dist/posture-origins/index.d.mts.map +1 -0
  94. package/dist/posture-origins/index.d.ts +41 -0
  95. package/dist/posture-origins/index.d.ts.map +1 -0
  96. package/dist/posture-origins/index.js +441 -0
  97. package/dist/posture-origins/index.mjs +430 -0
  98. package/dist/posture-origins/index.mjs.map +1 -0
  99. package/dist/posture-origins-CEm83u4D.js +621 -0
  100. package/dist/posture-origins-Dl7i8j93.mjs +533 -0
  101. package/dist/posture-origins-Dl7i8j93.mjs.map +1 -0
  102. package/dist/rules.json +0 -1
  103. package/dist/signals/index.d.mts +75 -55
  104. package/dist/signals/index.d.mts.map +1 -1
  105. package/dist/signals/index.d.ts +75 -55
  106. package/dist/signals/index.d.ts.map +1 -1
  107. package/dist/signals/index.js +246 -2
  108. package/dist/signals/index.mjs +236 -3
  109. package/dist/signals/index.mjs.map +1 -1
  110. package/fixtures/browser-integrity/captured-java-0.2.1-missing-score.json +10 -0
  111. package/fixtures/browser-integrity/null-score.json +11 -0
  112. package/fixtures/browser-integrity/numeric-score.json +11 -0
  113. package/fixtures/browser-posture-claims.json +168 -0
  114. package/fixtures/browser-posture-headers.json +6 -0
  115. package/fixtures/kya-http/README.md +1 -0
  116. package/fixtures/kya-http/consent-handoff.codes.json +34 -0
  117. package/fixtures/workbench/a2a-signed-request.v1.json +255 -0
  118. package/fixtures/workbench/artifact-bindings.v1.json +1003 -0
  119. package/fixtures/workbench/delegation-management.v1.json +507 -0
  120. package/fixtures/workbench/didcomm-task-source.v1.json +191 -0
  121. package/fixtures/workbench/graph-interfaces.v1.json +1887 -0
  122. package/fixtures/workbench/identity-provisioning.v1.json +643 -0
  123. package/fixtures/workbench/linear-installation.v1.json +484 -0
  124. package/fixtures/workbench/management-session.v1.json +716 -0
  125. package/fixtures/workbench/model-credentials.v1.json +630 -0
  126. package/fixtures/workbench/source-claim-pooled.v1.json +50 -0
  127. package/fixtures/workbench/task-grant.v1.json +695 -0
  128. package/fixtures/workbench/task-status-projection.v1.json +591 -0
  129. package/fixtures/workbench/workload-enrollment.v1.json +580 -0
  130. package/package.json +21 -2
  131. package/src/data/compiled/merged-rules.json +2 -9
  132. package/src/data/default-rules.json +0 -1
  133. package/dist/browser-posture-cookie-Dmq9Brf_.d.mts +0 -5
  134. package/dist/browser-posture-cookie-Dmq9Brf_.d.mts.map +0 -1
  135. package/dist/browser-posture-cookie-Dmq9Brf_.d.ts +0 -5
  136. package/dist/browser-posture-cookie-Dmq9Brf_.d.ts.map +0 -1
  137. package/dist/browser-posture.d.mts.map +0 -1
  138. package/dist/browser-posture.d.ts.map +0 -1
  139. package/dist/patterns-CkRVOdtV.js +0 -243
  140. package/dist/patterns-DzkGy_qk.mjs +0 -173
  141. package/dist/patterns-DzkGy_qk.mjs.map +0 -1
@@ -1,83 +1,2 @@
1
- import { C as ZodObject, O as ZodString, P as output, R as $strip, S as ZodNumber, b as ZodLiteral, g as ZodGUID, h as ZodEnum, l as ZodArray, p as ZodDefault, t as external_d_exports, u as ZodBoolean, x as ZodNullable } from "./external-jr_S4iSS.js";
2
- import { JWK, JWK as JWK$1 } from "jose";
3
- //#region src/browser-posture-cookie.d.ts
4
- export declare function postureCookieName(projectId: string): string;
5
- //#endregion
6
- //#region src/browser-posture-report.d.ts
7
- export declare const BrowserPostureReportSchema: ZodObject<{
8
- projectId: ZodString;
9
- sessionId: ZodGUID;
10
- verdict: ZodEnum<{
11
- "agent-driven": "agent-driven";
12
- human: "human";
13
- insufficient: "insufficient";
14
- }>;
15
- score: ZodNullable<ZodNumber>;
16
- scorerVersion: ZodNumber;
17
- reasons: ZodArray<ZodString>;
18
- issuedAt: ZodNumber;
19
- expiresAt: ZodNumber;
20
- source: ZodLiteral<"verified-posture">;
21
- }, $strip>;
22
- export declare function browserPostureReport(posture: BrowserPosture): {
23
- projectId: string;
24
- sessionId: string;
25
- verdict: "agent-driven" | "human" | "insufficient";
26
- score: number | null;
27
- scorerVersion: number;
28
- reasons: string[];
29
- issuedAt: number;
30
- expiresAt: number;
31
- source: "verified-posture";
32
- };
33
- //#endregion
34
- //#region src/browser-posture.d.ts
35
- export declare const UNSIGNED_BROWSER_AGENT: {
36
- readonly agentType: "Browser agent (unsigned)";
37
- readonly detectionClass: "ai_agent";
38
- readonly verificationMethod: "behavioral";
39
- };
40
- export declare const behavioralConfidence: (score: number | null) => number;
41
- export declare const POSTURE_TTL_SECONDS = 120;
42
- export declare const POSTURE_ISSUER = "checkpoint:browser-posture:v1";
43
- export declare const POSTURE_TYPE = "checkpoint-posture+jwt";
44
- export declare const BrowserPostureSchema: ZodObject<{
45
- v: ZodLiteral<1>;
46
- project_id: ZodString;
47
- origin: ZodString;
48
- session_id: ZodGUID;
49
- binding: ZodString;
50
- verdict: ZodEnum<{
51
- "agent-driven": "agent-driven";
52
- human: "human";
53
- insufficient: "insufficient";
54
- }>;
55
- score: ZodNullable<ZodNumber>;
56
- scorer_version: ZodNumber;
57
- reasons: ZodArray<ZodString>;
58
- automation_disclosed: ZodDefault<ZodBoolean>;
59
- automation_corroborated: ZodDefault<ZodBoolean>;
60
- iat: ZodNumber;
61
- exp: ZodNumber;
62
- }, $strip>;
63
- export type BrowserPosture = output<typeof BrowserPostureSchema>;
64
- export interface BrowserPostureConfig {
65
- publicKey: JWK$1;
66
- keyId: string;
67
- origin: string;
68
- sessionCookieName?: string;
69
- protectedRoutes?: Array<{
70
- path: string;
71
- scopes: string[];
72
- }>;
73
- }
74
- export declare function readPostureCookie(header: string | null | undefined, name: string): string | undefined;
75
- export declare function postureSessionBinding(sessionId: string): Promise<string>;
76
- export declare function signBrowserPosture(posture: BrowserPosture, privateKey: JWK$1, keyId: string): Promise<string>;
77
- export declare function verifyBrowserPosture(token: string | undefined, config: BrowserPostureConfig, projectId: string, browserSessionId: string | undefined, now?: number): Promise<BrowserPosture | undefined>;
78
- export declare function postureFromCookies(header: string | null | undefined, config: BrowserPostureConfig, projectId: string): Promise<BrowserPosture | undefined>;
79
- export declare function postureRequiredScopes(config: BrowserPostureConfig, path: string): string[] | undefined;
80
- export declare function validateBrowserPostureConfig(config: BrowserPostureConfig, projectId: string | undefined): void;
81
- //#endregion
82
- export type { JWK };
83
- //# sourceMappingURL=browser-posture.d.ts.map
1
+ import { A as POSTURE_VERIFICATION_STATUSES, C as verifyBrowserPosture, D as BrowserPostureRejectionReason, E as BrowserPostureOutcomeSchema, F as POSTURE_TTL_SECONDS, I as postureCookieName, M as browserPostureReport, N as BrowserPosture, O as BrowserPostureReportIngestSchema, P as BrowserPostureSchema, S as validateBrowserPostureConfig, T as BrowserPostureOutcome, _ as postureRequiredScopes, a as POSTURE_SESSION_HEADER, b as readPostureCookie, c as PostureVerification, d as inspectBrowserPosture, f as postureFromCookies, g as postureRequestFromHeaders, h as postureOutcomeReport, i as POSTURE_ISSUER, j as PostureVerificationStatus, k as BrowserPostureReportSchema, l as UNSIGNED_BROWSER_AGENT, m as postureOutcomeFromRequest, n as JWK, o as POSTURE_TYPE, p as postureFromRequest, r as POSTURE_HEADER, s as PostureRequest, t as BrowserPostureConfig, u as behavioralConfidence, v as postureSessionBinding, w as BROWSER_POSTURE_REJECTION_REASONS, x as signBrowserPosture, y as postureUpgrade } from "./browser-posture-D4j2a5gi.js";
2
+ export { BROWSER_POSTURE_REJECTION_REASONS, type BrowserPosture, BrowserPostureConfig, type BrowserPostureOutcome, BrowserPostureOutcomeSchema, type BrowserPostureRejectionReason, BrowserPostureReportIngestSchema, BrowserPostureReportSchema, BrowserPostureSchema, type JWK, POSTURE_HEADER, POSTURE_ISSUER, POSTURE_SESSION_HEADER, POSTURE_TTL_SECONDS, POSTURE_TYPE, POSTURE_VERIFICATION_STATUSES, PostureRequest, PostureVerification, type PostureVerificationStatus, UNSIGNED_BROWSER_AGENT, behavioralConfidence, browserPostureReport, inspectBrowserPosture, postureCookieName, postureFromCookies, postureFromRequest, postureOutcomeFromRequest, postureOutcomeReport, postureRequestFromHeaders, postureRequiredScopes, postureSessionBinding, postureUpgrade, readPostureCookie, signBrowserPosture, validateBrowserPostureConfig, verifyBrowserPosture };
@@ -1,6 +1,6 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
2
  const require_schemas = require('./schemas-BmjNLAE7.js');
3
- const require_patterns = require('./patterns-CkRVOdtV.js');
3
+ const require_posture_origins = require('./posture-origins-CEm83u4D.js');
4
4
  const require_project_verification_key = require('./project-verification-key-C3L5rd8a.js');
5
5
  let jose = require("jose");
6
6
 
@@ -17,12 +17,54 @@ let jose = require("jose");
17
17
  * reader will look for.
18
18
  */
19
19
  function postureCookieName(projectId) {
20
- if (!require_patterns.UUID_PATTERN.test(projectId) || projectId !== projectId.toLowerCase()) throw new Error("Invalid posture project id");
20
+ if (!require_posture_origins.UUID_PATTERN.test(projectId) || projectId !== projectId.toLowerCase()) throw new Error("Invalid posture project id");
21
21
  return `__Host-checkpoint_posture_${projectId}`;
22
22
  }
23
23
 
24
+ //#endregion
25
+ //#region src/beacon-types/index.ts
26
+ /**
27
+ * Lifetime of the signed browser posture token, in seconds. Defined here and
28
+ * re-exported by `browser-posture.ts` (which imports `jose`) so the
29
+ * size-budgeted beacon bundle can read it without pulling in a JWT library.
30
+ */
31
+ const POSTURE_TTL_SECONDS = 120;
32
+
33
+ //#endregion
34
+ //#region src/browser-posture-schema.ts
35
+ const BrowserPostureSchema = require_schemas.object({
36
+ v: require_schemas.literal(1),
37
+ project_id: require_schemas.string().min(1).max(128),
38
+ origin: require_schemas.string().url(),
39
+ session_id: require_schemas.guid(),
40
+ binding: require_schemas.string().regex(/^[a-f0-9]{64}$/),
41
+ verdict: require_schemas._enum([
42
+ "agent-driven",
43
+ "human",
44
+ "insufficient"
45
+ ]),
46
+ score: require_schemas.number().min(0).max(1).nullable(),
47
+ scorer_version: require_schemas.number().int().positive().max(2147483647),
48
+ reasons: require_schemas.array(require_schemas.string().max(160)).max(8),
49
+ automation_disclosed: require_schemas.boolean().default(false),
50
+ automation_corroborated: require_schemas.boolean().default(false),
51
+ detection: require_schemas.object({
52
+ class: require_schemas._enum(["ai_agent", "bot"]),
53
+ name: require_schemas.string().min(1).max(64).regex(/^[^\p{Cc}]+$/u),
54
+ confidence: require_schemas.number().int().min(0).max(100)
55
+ }).optional(),
56
+ cnf: require_schemas.object({ jkt: require_schemas.string().min(1).max(255) }).optional(),
57
+ iat: require_schemas.number().int().nonnegative(),
58
+ exp: require_schemas.number().int().positive()
59
+ });
60
+
24
61
  //#endregion
25
62
  //#region src/browser-posture-report.ts
63
+ const POSTURE_VERIFICATION_STATUSES = [
64
+ "verified",
65
+ "absent",
66
+ "rejected"
67
+ ];
26
68
  /** Audit projection: no token, cookie binding, raw counters, or credential fields. */
27
69
  const BrowserPostureReportSchema = require_schemas.object({
28
70
  projectId: require_schemas.string().min(1).max(128),
@@ -39,6 +81,31 @@ const BrowserPostureReportSchema = require_schemas.object({
39
81
  expiresAt: require_schemas.number().int().positive(),
40
82
  source: require_schemas.literal("verified-posture")
41
83
  });
84
+ const BROWSER_POSTURE_REJECTION_REASONS = [
85
+ "malformed",
86
+ "unknown-key",
87
+ "bad-signature",
88
+ "invalid-claims",
89
+ "project-mismatch",
90
+ "origin-mismatch",
91
+ "audience-mismatch",
92
+ "expired",
93
+ "not-yet-valid",
94
+ "binding-mismatch",
95
+ "session-missing"
96
+ ];
97
+ const BrowserPostureOutcomeSchema = require_schemas.object({
98
+ status: require_schemas._enum(POSTURE_VERIFICATION_STATUSES),
99
+ reason: require_schemas._enum(BROWSER_POSTURE_REJECTION_REASONS).optional()
100
+ }).refine((outcome) => outcome.status === "rejected" === (outcome.reason !== void 0), {
101
+ message: "reason is required if and only if status is rejected",
102
+ path: ["reason"]
103
+ });
104
+ const BROWSER_POSTURE_REASON_PATTERN = /^[a-z][a-z-]{0,31}$/;
105
+ const BROWSER_POSTURE_REJECTIONS_MAX_ENTRIES = 50;
106
+ const BROWSER_POSTURE_REJECTION_COUNT_MAX = 1e6;
107
+ const BrowserPostureRejectionsSchema = require_schemas.record(require_schemas.string(), require_schemas.unknown()).transform((counts) => Object.fromEntries(Object.entries(counts).filter((entry) => BROWSER_POSTURE_REASON_PATTERN.test(entry[0]) && typeof entry[1] === "number" && Number.isInteger(entry[1]) && entry[1] > 0).slice(0, 50).map(([reason, count]) => [reason, Math.min(count, BROWSER_POSTURE_REJECTION_COUNT_MAX)])));
108
+ const BrowserPostureReportIngestSchema = BrowserPostureReportSchema.extend({ score: BrowserPostureReportSchema.shape.score.default(null) });
42
109
  function browserPostureReport(posture) {
43
110
  return {
44
111
  projectId: posture.project_id,
@@ -61,28 +128,43 @@ const UNSIGNED_BROWSER_AGENT = {
61
128
  verificationMethod: "behavioral"
62
129
  };
63
130
  const behavioralConfidence = (score) => Math.min(85, Math.max(50, Math.round((score ?? .5) * 100)));
64
- const POSTURE_TTL_SECONDS = 120;
131
+ function postureUpgrade(posture) {
132
+ const { detection, reasons } = posture;
133
+ const verificationMethod = "behavioral";
134
+ if (detection) return {
135
+ detectionClass: detection.class,
136
+ isAgent: true,
137
+ agentName: detection.name,
138
+ confidence: detection.confidence,
139
+ verificationMethod,
140
+ reasons
141
+ };
142
+ if (posture.automation_disclosed) return {
143
+ detectionClass: "bot",
144
+ isAgent: true,
145
+ agentName: "Automation Tools",
146
+ confidence: 80,
147
+ verificationMethod,
148
+ reasons
149
+ };
150
+ if (posture.verdict === "agent-driven") return {
151
+ detectionClass: UNSIGNED_BROWSER_AGENT.detectionClass,
152
+ isAgent: true,
153
+ agentName: UNSIGNED_BROWSER_AGENT.agentType,
154
+ confidence: behavioralConfidence(posture.score),
155
+ verificationMethod,
156
+ reasons
157
+ };
158
+ if (posture.automation_corroborated) return {
159
+ detectionClass: "incomplete_data",
160
+ isAgent: false,
161
+ confidence: 40,
162
+ verificationMethod,
163
+ reasons
164
+ };
165
+ }
65
166
  const POSTURE_ISSUER = "checkpoint:browser-posture:v1";
66
167
  const POSTURE_TYPE = "checkpoint-posture+jwt";
67
- const BrowserPostureSchema = require_schemas.object({
68
- v: require_schemas.literal(1),
69
- project_id: require_schemas.string().min(1).max(128),
70
- origin: require_schemas.string().url(),
71
- session_id: require_schemas.guid(),
72
- binding: require_schemas.string().regex(/^[a-f0-9]{64}$/),
73
- verdict: require_schemas._enum([
74
- "agent-driven",
75
- "human",
76
- "insufficient"
77
- ]),
78
- score: require_schemas.number().min(0).max(1).nullable(),
79
- scorer_version: require_schemas.number().int().positive().max(2147483647),
80
- reasons: require_schemas.array(require_schemas.string().max(160)).max(8),
81
- automation_disclosed: require_schemas.boolean().default(false),
82
- automation_corroborated: require_schemas.boolean().default(false),
83
- iat: require_schemas.number().int().nonnegative(),
84
- exp: require_schemas.number().int().positive()
85
- });
86
168
  /** Duplicate cookies are ambiguous, so none is trusted. */
87
169
  function readPostureCookie(header, name) {
88
170
  const matches = (header ?? "").split(";").map((s) => s.trim()).filter((s) => s.startsWith(`${name}=`));
@@ -100,7 +182,7 @@ async function postureSessionBinding(sessionId) {
100
182
  function validOrigin(origin) {
101
183
  try {
102
184
  const url = new URL(origin);
103
- return url.protocol === "https:" && url.origin === origin;
185
+ return url.protocol === "https:" && url.origin === origin && /^[^.*][^*]*$/.test(url.hostname);
104
186
  } catch {
105
187
  return false;
106
188
  }
@@ -115,31 +197,101 @@ async function signBrowserPosture(posture, privateKey, keyId) {
115
197
  kid: keyId
116
198
  }).setIssuer(POSTURE_ISSUER).setAudience(claims.origin).sign(key);
117
199
  }
118
- async function verifyBrowserPosture(token, config, projectId, browserSessionId, now = Math.floor(Date.now() / 1e3)) {
119
- if (!token || token.length > 3800 || !browserSessionId || browserSessionId.length > 255 || !validOrigin(config.origin) || config.publicKey.d || config.publicKey.kty !== "EC" || config.publicKey.crv !== "P-256") return void 0;
200
+ const BASE64URL = /^[A-Za-z0-9_-]+$/;
201
+ const rejected = (reason) => ({
202
+ status: "rejected",
203
+ reason
204
+ });
205
+ const postureOutcomeReport = (outcome) => outcome.status === "verified" ? { status: "verified" } : outcome;
206
+ const verifiedPosture = (outcome) => outcome.status === "verified" ? outcome.posture : void 0;
207
+ async function inspectBrowserPosture(token, config, projectId, browserSessionId, now = Math.floor(Date.now() / 1e3)) {
208
+ if (!token) return { status: "absent" };
209
+ if (!browserSessionId || browserSessionId.length > 255) return rejected("session-missing");
210
+ const parts = token.split(".");
211
+ if (token.length > 3800 || parts.length !== 3 || !parts.every((part) => BASE64URL.test(part)) || parts[0].length % 4 === 1) return rejected("malformed");
212
+ let header;
120
213
  try {
121
- const publicKey = require_project_verification_key.projectVerificationKey(token, config, now);
122
- if (!publicKey) return void 0;
123
- const key = await (0, jose.importJWK)(publicKey, "ES256");
124
- const { payload, protectedHeader } = await (0, jose.jwtVerify)(token, key, {
125
- algorithms: ["ES256"],
126
- issuer: POSTURE_ISSUER,
127
- audience: config.origin,
128
- typ: POSTURE_TYPE,
129
- currentDate: /* @__PURE__ */ new Date(now * 1e3)
130
- });
131
- if ("crit" in protectedHeader || "b64" in protectedHeader || Array.isArray(payload.aud) && payload.aud.some((audience) => typeof audience !== "string")) return void 0;
132
- const parsed = BrowserPostureSchema.safeParse(payload);
133
- if (!parsed.success) return void 0;
134
- const claims = parsed.data;
135
- if (claims.project_id !== projectId || claims.origin !== config.origin || claims.iat > now + 5 || claims.exp <= now || claims.exp <= claims.iat || claims.exp - claims.iat > 120 || claims.binding !== await postureSessionBinding(browserSessionId)) return void 0;
136
- return claims;
214
+ header = (0, jose.decodeProtectedHeader)(token);
137
215
  } catch {
138
- return;
216
+ return rejected("malformed");
217
+ }
218
+ if (header.alg !== "ES256" || header.typ !== "checkpoint-posture+jwt" || typeof header.kid !== "string" || "crit" in header || "b64" in header) return rejected("malformed");
219
+ const publicKey = validOrigin(config.origin) && !config.publicKey.d && config.publicKey.kty === "EC" && config.publicKey.crv === "P-256" ? require_project_verification_key.projectVerificationKey(token, config, now) : void 0;
220
+ const key = publicKey && await (0, jose.importJWK)(publicKey, "ES256").catch(() => void 0);
221
+ if (!key) return rejected("unknown-key");
222
+ let signed;
223
+ try {
224
+ signed = await (0, jose.compactVerify)(token, key, { algorithms: ["ES256"] });
225
+ } catch {
226
+ return rejected("bad-signature");
139
227
  }
228
+ let json;
229
+ try {
230
+ json = JSON.parse(new TextDecoder().decode(signed.payload));
231
+ } catch {
232
+ return rejected("malformed");
233
+ }
234
+ const record$1 = require_schemas.record(require_schemas.string(), require_schemas.unknown()).safeParse(json);
235
+ if (!record$1.success) return rejected("invalid-claims");
236
+ const audiences = [record$1.data.aud].flat();
237
+ if (audiences.some((audience) => typeof audience !== "string")) return rejected("malformed");
238
+ const notBefore = record$1.data.nbf;
239
+ const parsed = BrowserPostureSchema.safeParse(record$1.data);
240
+ if (!parsed.success || record$1.data.iss !== "checkpoint:browser-posture:v1" || notBefore !== void 0 && typeof notBefore !== "number" || record$1.data.origin !== parsed.data.origin || !validOrigin(parsed.data.origin)) return rejected("invalid-claims");
241
+ const claims = parsed.data;
242
+ if (claims.project_id !== projectId) return rejected("project-mismatch");
243
+ if (claims.origin !== config.origin && !require_posture_origins.matchAllowedOrigin(claims.origin, config.origins ?? [])) return rejected("origin-mismatch");
244
+ if (!audiences.includes(claims.origin)) return rejected("audience-mismatch");
245
+ if (claims.exp <= now) return rejected("expired");
246
+ if (claims.iat > now + 5 || notBefore !== void 0 && notBefore > now) return rejected("not-yet-valid");
247
+ if (claims.exp <= claims.iat || claims.exp - claims.iat > 120) return rejected("invalid-claims");
248
+ if (claims.binding !== await postureSessionBinding(browserSessionId)) return rejected("binding-mismatch");
249
+ return {
250
+ status: "verified",
251
+ posture: claims
252
+ };
253
+ }
254
+ async function verifyBrowserPosture(token, config, projectId, browserSessionId, now = Math.floor(Date.now() / 1e3)) {
255
+ return verifiedPosture(await inspectBrowserPosture(token, config, projectId, browserSessionId, now));
256
+ }
257
+ /**
258
+ * Request headers a page sends when its posture cookies cannot reach the
259
+ * server: a request to another host (the cookies are host-only) or a browser
260
+ * that blocks cookies. The page reads both values from the beacon's
261
+ * `onReceipt`.
262
+ */
263
+ const POSTURE_HEADER = "kya-posture";
264
+ const POSTURE_SESSION_HEADER = "kya-posture-session";
265
+ /** The `PostureRequest` of any header store, read through its own lowercase-name getter. */
266
+ function postureRequestFromHeaders(get) {
267
+ const cookie = get("cookie");
268
+ return {
269
+ cookie: Array.isArray(cookie) ? cookie.join("; ") : cookie,
270
+ posture: get(POSTURE_HEADER),
271
+ session: get(POSTURE_SESSION_HEADER)
272
+ };
273
+ }
274
+ /**
275
+ * A header, when the request carries a non-empty one, wins over its cookie.
276
+ * A header sent twice, or joined into one comma-separated value, is ambiguous,
277
+ * so neither copy is trusted and the cookie is not consulted either: the page
278
+ * chose the header. Every runtime applies the same rule
279
+ * (`tests/browser-posture/vectors.json` `requests`).
280
+ */
281
+ function postureRequestValue(header, cookieHeader, cookieName) {
282
+ const values = (Array.isArray(header) ? header : [header]).map((value) => value?.trim()).filter((value) => Boolean(value));
283
+ if (values.length === 0) return readPostureCookie(cookieHeader, cookieName);
284
+ return values.length === 1 && !values[0].includes(",") ? values[0] : void 0;
285
+ }
286
+ /** Read and verify posture from the `KYA-Posture` headers, falling back to the cookies. */
287
+ async function postureOutcomeFromRequest(request, config, projectId, now) {
288
+ return inspectBrowserPosture(postureRequestValue(request.posture, request.cookie, postureCookieName(projectId)), config, projectId, postureRequestValue(request.session, request.cookie, config.sessionCookieName ?? "_as_beacon_session"), now);
289
+ }
290
+ async function postureFromRequest(request, config, projectId, now) {
291
+ return verifiedPosture(await postureOutcomeFromRequest(request, config, projectId, now));
140
292
  }
141
293
  async function postureFromCookies(header, config, projectId) {
142
- return verifyBrowserPosture(readPostureCookie(header, postureCookieName(projectId)), config, projectId, readPostureCookie(header, config.sessionCookieName ?? "_as_beacon_session"));
294
+ return postureFromRequest({ cookie: header }, config, projectId);
143
295
  }
144
296
  /** Exact paths or an explicit subtree ending in /*. Longest designation wins. */
145
297
  function postureRequiredScopes(config, path) {
@@ -150,8 +302,14 @@ function postureRequiredScopes(config, path) {
150
302
  }
151
303
  /** Validate operator configuration at startup, before any request can depend on it. */
152
304
  function validateBrowserPostureConfig(config, projectId) {
153
- if (!projectId || !require_patterns.VALIDATION_PATTERNS.UUID.test(projectId) || projectId !== projectId.toLowerCase()) throw new Error("Browser posture requires projectId to be the lowercase project UUID (the friendly id is not accepted)");
305
+ if (!projectId || !require_posture_origins.VALIDATION_PATTERNS.UUID.test(projectId) || projectId !== projectId.toLowerCase()) throw new Error("Browser posture requires projectId to be the lowercase project UUID (the friendly id is not accepted)");
154
306
  if (!validOrigin(config.origin) || !config.keyId || config.publicKey.kty !== "EC" || config.publicKey.crv !== "P-256" || !config.publicKey.x || !config.publicKey.y || config.publicKey.d) throw new Error("Browser posture requires projectId, an exact HTTPS origin, and a public ES256 key");
307
+ const origins = config.origins ?? [];
308
+ if (origins.length > 20) throw new Error(`Browser posture accepts at most ${20} additional origins`);
309
+ for (const entry of origins) {
310
+ const parsed = require_posture_origins.parseOriginPattern(entry);
311
+ if (!parsed.ok) throw new Error(`Invalid browser posture origin "${entry}": ${parsed.error}`);
312
+ }
155
313
  const paths = /* @__PURE__ */ new Set();
156
314
  for (const route of config.protectedRoutes ?? []) {
157
315
  if (!/^\/(?:[^?#*\\]*)(?:\/\*)?$/.test(route.path) && route.path !== "/*") throw new Error("Invalid integrity-protected path");
@@ -165,18 +323,30 @@ function validateBrowserPostureConfig(config, projectId) {
165
323
  }
166
324
 
167
325
  //#endregion
326
+ exports.BROWSER_POSTURE_REJECTION_REASONS = BROWSER_POSTURE_REJECTION_REASONS;
327
+ exports.BrowserPostureOutcomeSchema = BrowserPostureOutcomeSchema;
328
+ exports.BrowserPostureReportIngestSchema = BrowserPostureReportIngestSchema;
168
329
  exports.BrowserPostureReportSchema = BrowserPostureReportSchema;
169
330
  exports.BrowserPostureSchema = BrowserPostureSchema;
331
+ exports.POSTURE_HEADER = POSTURE_HEADER;
170
332
  exports.POSTURE_ISSUER = POSTURE_ISSUER;
333
+ exports.POSTURE_SESSION_HEADER = POSTURE_SESSION_HEADER;
171
334
  exports.POSTURE_TTL_SECONDS = POSTURE_TTL_SECONDS;
172
335
  exports.POSTURE_TYPE = POSTURE_TYPE;
336
+ exports.POSTURE_VERIFICATION_STATUSES = POSTURE_VERIFICATION_STATUSES;
173
337
  exports.UNSIGNED_BROWSER_AGENT = UNSIGNED_BROWSER_AGENT;
174
338
  exports.behavioralConfidence = behavioralConfidence;
175
339
  exports.browserPostureReport = browserPostureReport;
340
+ exports.inspectBrowserPosture = inspectBrowserPosture;
176
341
  exports.postureCookieName = postureCookieName;
177
342
  exports.postureFromCookies = postureFromCookies;
343
+ exports.postureFromRequest = postureFromRequest;
344
+ exports.postureOutcomeFromRequest = postureOutcomeFromRequest;
345
+ exports.postureOutcomeReport = postureOutcomeReport;
346
+ exports.postureRequestFromHeaders = postureRequestFromHeaders;
178
347
  exports.postureRequiredScopes = postureRequiredScopes;
179
348
  exports.postureSessionBinding = postureSessionBinding;
349
+ exports.postureUpgrade = postureUpgrade;
180
350
  exports.readPostureCookie = readPostureCookie;
181
351
  exports.signBrowserPosture = signBrowserPosture;
182
352
  exports.validateBrowserPostureConfig = validateBrowserPostureConfig;