@verax-ai/body 0.5.1 → 0.6.1

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 (50) hide show
  1. package/README.md +4 -3
  2. package/dist/approval-signature.d.ts +19 -0
  3. package/dist/approval-signature.js +70 -5
  4. package/dist/approve-cli.js +20 -24
  5. package/dist/cli.d.ts +1 -0
  6. package/dist/cli.js +146 -14
  7. package/dist/config.d.ts +27 -0
  8. package/dist/config.js +72 -1
  9. package/dist/control-signature.d.ts +4 -0
  10. package/dist/control-signature.js +20 -5
  11. package/dist/definitions.d.ts +9 -0
  12. package/dist/definitions.js +53 -0
  13. package/dist/demo.js +11 -11
  14. package/dist/doctor.js +72 -0
  15. package/dist/downstream-pin.d.ts +9 -0
  16. package/dist/downstream-pin.js +273 -0
  17. package/dist/downstream.d.ts +22 -2
  18. package/dist/downstream.js +312 -44
  19. package/dist/heartbeat.d.ts +26 -2
  20. package/dist/heartbeat.js +90 -7
  21. package/dist/held-spend.d.ts +16 -0
  22. package/dist/held-spend.js +20 -0
  23. package/dist/http-io.d.ts +14 -0
  24. package/dist/http-io.js +46 -0
  25. package/dist/init-local.d.ts +11 -0
  26. package/dist/init-local.js +32 -3
  27. package/dist/install.d.ts +5 -0
  28. package/dist/install.js +37 -5
  29. package/dist/main.d.ts +1 -0
  30. package/dist/main.js +40 -6
  31. package/dist/operator-cli.d.ts +49 -1
  32. package/dist/operator-cli.js +136 -8
  33. package/dist/operator-credentials.d.ts +40 -1
  34. package/dist/operator-credentials.js +97 -7
  35. package/dist/operator-door-page.d.ts +3 -0
  36. package/dist/operator-door-page.js +819 -0
  37. package/dist/operator-door.d.ts +22 -0
  38. package/dist/operator-door.js +487 -0
  39. package/dist/operator-handlers.d.ts +28 -0
  40. package/dist/operator-handlers.js +221 -0
  41. package/dist/operator-pairing.d.ts +2 -0
  42. package/dist/operator-pairing.js +11 -0
  43. package/dist/reconcile-cli.js +3 -1
  44. package/dist/server.d.ts +2 -0
  45. package/dist/server.js +133 -256
  46. package/dist/verify-cli.js +30 -10
  47. package/dist/webauthn-verify.d.ts +48 -4
  48. package/dist/webauthn-verify.js +183 -25
  49. package/dist/wiring.js +14 -1
  50. package/package.json +4 -4
@@ -1,7 +1,8 @@
1
1
  /**
2
- * WebAuthn assertion checks for an HTTP approval, using only node:crypto
3
- * and the CBOR decoder already in @cedulon/cose. The body does not take a
4
- * certificate stack to verify one signature.
2
+ * WebAuthn assertion checks for an HTTP approval, and the reading of a new
3
+ * credential at enrolment, using only node:crypto and the CBOR decoder
4
+ * already in @cedulon/cose. The body does not take a certificate stack to
5
+ * verify one signature.
5
6
  */
6
7
  import { type KeyObject } from "node:crypto";
7
8
  export type AssertionVerdict = {
@@ -12,6 +13,21 @@ export type AssertionVerdict = {
12
13
  ok: false;
13
14
  reason: string;
14
15
  };
16
+ export type RegistrationVerdict = {
17
+ ok: true;
18
+ credentialId: string;
19
+ publicKeyCose: string;
20
+ counter: number;
21
+ flags: number;
22
+ } | {
23
+ ok: false;
24
+ reason: string;
25
+ };
26
+ /** The COSE algorithms `coseKeyToPublicKey` takes. An enrolment offers these and no others. */
27
+ export declare const COSE_ALGORITHMS: readonly number[];
28
+ /** The shortest and the longest RSA modulus taken, for a sign-in as for an enrolment. */
29
+ export declare const RSA_MIN_BITS = 2048;
30
+ export declare const RSA_MAX_BITS = 8192;
15
31
  type PostedAssertion = {
16
32
  response?: {
17
33
  clientDataJSON?: unknown;
@@ -19,9 +35,18 @@ type PostedAssertion = {
19
35
  signature?: unknown;
20
36
  };
21
37
  };
38
+ export type PostedRegistration = {
39
+ id?: unknown;
40
+ rawId?: unknown;
41
+ response?: {
42
+ clientDataJSON?: unknown;
43
+ attestationObject?: unknown;
44
+ };
45
+ };
22
46
  /**
23
47
  * COSE public key, base64url, to a node key. EC2 is P-256 / ES256, OKP is
24
- * Ed25519 / EdDSA, RSA is RS256. Any other kty, alg, or curve is refused.
48
+ * Ed25519 / EdDSA, RSA is RS256 with an odd modulus of 2048 to 8192 bits
49
+ * and the exponent 65537. Any other kty, alg, curve or RSA shape is refused.
25
50
  * A refusal returns null; this does not throw.
26
51
  */
27
52
  export declare function coseKeyToPublicKey(base64urlCose: string): KeyObject | null;
@@ -37,4 +62,23 @@ export declare function verifyAssertion(opts: {
37
62
  rpId: string;
38
63
  publicKeyCose: string;
39
64
  }): AssertionVerdict;
65
+ /**
66
+ * Read the credential a `navigator.credentials.create()` answer carries. This
67
+ * proves nothing about the device: the attestation asked for is "none", any
68
+ * other format is refused rather than believed, and what vouches for the
69
+ * caller is whatever the caller checked before calling (at the door, a
70
+ * pairing code from the machine). What it does establish is that the answer
71
+ * was made for this challenge, origin and relying party with the user
72
+ * verified, that every byte of the authenticator data is accounted for, and
73
+ * that the key passes `coseKeyToPublicKey`, the same reading a sign-in
74
+ * gives it. Nothing here
75
+ * is signed, so it does not show the caller holds the private key: the
76
+ * first sign-in does.
77
+ */
78
+ export declare function verifyRegistration(opts: {
79
+ response: PostedRegistration;
80
+ expectedChallenge: string;
81
+ expectedOrigins: readonly string[];
82
+ rpId: string;
83
+ }): RegistrationVerdict;
40
84
  export {};
@@ -1,10 +1,16 @@
1
1
  /**
2
- * WebAuthn assertion checks for an HTTP approval, using only node:crypto
3
- * and the CBOR decoder already in @cedulon/cose. The body does not take a
4
- * certificate stack to verify one signature.
2
+ * WebAuthn assertion checks for an HTTP approval, and the reading of a new
3
+ * credential at enrolment, using only node:crypto and the CBOR decoder
4
+ * already in @cedulon/cose. The body does not take a certificate stack to
5
+ * verify one signature.
5
6
  */
6
7
  import { constants, createHash, createPublicKey, verify as cryptoVerify } from "node:crypto";
7
8
  import { decodeCbor, isCborMap, mapGet } from "@cedulon/cose";
9
+ /** The COSE algorithms `coseKeyToPublicKey` takes. An enrolment offers these and no others. */
10
+ export const COSE_ALGORITHMS = [-7, -8, -257];
11
+ /** The shortest and the longest RSA modulus taken, for a sign-in as for an enrolment. */
12
+ export const RSA_MIN_BITS = 2048;
13
+ export const RSA_MAX_BITS = 8192;
8
14
  function b64url(bytes) {
9
15
  return Buffer.from(bytes).toString("base64url");
10
16
  }
@@ -33,7 +39,8 @@ function mapBytes(map, key, length) {
33
39
  }
34
40
  /**
35
41
  * COSE public key, base64url, to a node key. EC2 is P-256 / ES256, OKP is
36
- * Ed25519 / EdDSA, RSA is RS256. Any other kty, alg, or curve is refused.
42
+ * Ed25519 / EdDSA, RSA is RS256 with an odd modulus of 2048 to 8192 bits
43
+ * and the exponent 65537. Any other kty, alg, curve or RSA shape is refused.
37
44
  * A refusal returns null; this does not throw.
38
45
  */
39
46
  export function coseKeyToPublicKey(base64urlCose) {
@@ -70,10 +77,17 @@ export function coseKeyToPublicKey(base64urlCose) {
70
77
  const e = mapBytes(decoded, -2);
71
78
  if (!n || !e)
72
79
  return null;
73
- return createPublicKey({
80
+ const key = createPublicKey({
74
81
  format: "jwk",
75
82
  key: { kty: "RSA", alg: "RS256", n: b64url(n), e: b64url(e) },
76
83
  });
84
+ // A key that imports is not yet one to store. The modulus is odd and
85
+ // from 2048 to 8192 bits, and the exponent is 65537, which is what
86
+ // authenticators make; anything else is refused rather than reasoned about.
87
+ const bits = key.asymmetricKeyDetails?.modulusLength ?? 0;
88
+ if (bits < RSA_MIN_BITS || bits > RSA_MAX_BITS || (n[n.length - 1] & 1) === 0)
89
+ return null;
90
+ return key.asymmetricKeyDetails?.publicExponent === 65537n ? key : null;
77
91
  }
78
92
  return null;
79
93
  }
@@ -97,6 +111,29 @@ function signatureHolds(key, data, signature) {
97
111
  return false;
98
112
  }
99
113
  }
114
+ /** Why the client data is not what was asked for, or null when it is. */
115
+ function clientDataRefusal(clientBytes, type, expectedChallenge, expectedOrigins) {
116
+ let client;
117
+ try {
118
+ const parsed = JSON.parse(clientBytes.toString("utf8"));
119
+ if (!parsed || typeof parsed !== "object")
120
+ return "bad-client-data";
121
+ client = parsed;
122
+ }
123
+ catch {
124
+ return "bad-client-data";
125
+ }
126
+ if (client.type !== type)
127
+ return "type";
128
+ if (client.challenge !== expectedChallenge)
129
+ return "challenge";
130
+ if (typeof client.origin !== "string" || !expectedOrigins.includes(client.origin))
131
+ return "origin";
132
+ // Absent or false only: a string such as "true" is not a same-origin answer either.
133
+ if (client.crossOrigin !== undefined && client.crossOrigin !== false)
134
+ return "cross-origin";
135
+ return null;
136
+ }
100
137
  /**
101
138
  * Check one authentication assertion. `expectedOrigins` is the caller's
102
139
  * allow-list; a caller that does not have one passes the origin it read
@@ -115,26 +152,9 @@ export function verifyAssertion(opts) {
115
152
  const signature = fromBase64Url(sigB64);
116
153
  if (!clientBytes || !authenticatorData || !signature)
117
154
  return { ok: false, reason: "bad-assertion" };
118
- let client;
119
- try {
120
- const parsed = JSON.parse(clientBytes.toString("utf8"));
121
- if (!parsed || typeof parsed !== "object")
122
- return { ok: false, reason: "bad-client-data" };
123
- client = parsed;
124
- }
125
- catch {
126
- return { ok: false, reason: "bad-client-data" };
127
- }
128
- if (client.type !== "webauthn.get")
129
- return { ok: false, reason: "type" };
130
- if (client.challenge !== opts.expectedChallenge)
131
- return { ok: false, reason: "challenge" };
132
- if (typeof client.origin !== "string" || !opts.expectedOrigins.includes(client.origin)) {
133
- return { ok: false, reason: "origin" };
134
- }
135
- // Absent or false only: a string such as "true" is not a same-origin answer either.
136
- if (client.crossOrigin !== undefined && client.crossOrigin !== false)
137
- return { ok: false, reason: "cross-origin" };
155
+ const refused = clientDataRefusal(clientBytes, "webauthn.get", opts.expectedChallenge, opts.expectedOrigins);
156
+ if (refused)
157
+ return { ok: false, reason: refused };
138
158
  if (authenticatorData.length < 37)
139
159
  return { ok: false, reason: "authenticator-data" };
140
160
  const rpIdHash = createHash("sha256").update(opts.rpId).digest();
@@ -157,3 +177,141 @@ export function verifyAssertion(opts) {
157
177
  return { ok: false, reason: "signature" };
158
178
  }
159
179
  }
180
+ /** A credential key is a flat map; an honest one never nests this deep. */
181
+ const CBOR_SKIP_DEPTH = 4;
182
+ /**
183
+ * Where the CBOR item that starts at `start` ends, or null when it is not one
184
+ * this file reads. `decodeCbor` refuses bytes after an item, and in
185
+ * authenticator data the credential key may be followed by an extensions map,
186
+ * so the key's length has to be found before it can be decoded. Integers,
187
+ * strings, arrays, maps and the three simple values, definite lengths under
188
+ * 2^32 only. The slice this marks out is then decoded by `decodeCbor`, which
189
+ * is the judge of what is in it.
190
+ */
191
+ function cborItemEnd(bytes, start, depth = 0) {
192
+ if (depth > CBOR_SKIP_DEPTH)
193
+ return null;
194
+ const ib = bytes[start];
195
+ if (ib === undefined)
196
+ return null;
197
+ let at = start + 1;
198
+ if (ib === 0xf4 || ib === 0xf5 || ib === 0xf6)
199
+ return at;
200
+ const major = ib >> 5;
201
+ const ai = ib & 31;
202
+ if (major > 5)
203
+ return null;
204
+ let n = ai;
205
+ if (ai >= 24) {
206
+ const width = ai === 24 ? 1 : ai === 25 ? 2 : ai === 26 ? 4 : 0;
207
+ if (width === 0 || at + width > bytes.length)
208
+ return null;
209
+ n = bytes.readUIntBE(at, width);
210
+ at += width;
211
+ }
212
+ if (major <= 1)
213
+ return at;
214
+ if (major === 2 || major === 3)
215
+ return at + n <= bytes.length ? at + n : null;
216
+ // Every item is at least one byte, so a count past what is left ends at the first one missing.
217
+ const items = major === 4 ? n : n * 2;
218
+ for (let i = 0; i < items; i += 1) {
219
+ const next = cborItemEnd(bytes, at, depth + 1);
220
+ if (next === null)
221
+ return null;
222
+ at = next;
223
+ }
224
+ return at;
225
+ }
226
+ function decoded(bytes) {
227
+ try {
228
+ return decodeCbor(bytes);
229
+ }
230
+ catch {
231
+ return undefined;
232
+ }
233
+ }
234
+ /**
235
+ * Read the credential a `navigator.credentials.create()` answer carries. This
236
+ * proves nothing about the device: the attestation asked for is "none", any
237
+ * other format is refused rather than believed, and what vouches for the
238
+ * caller is whatever the caller checked before calling (at the door, a
239
+ * pairing code from the machine). What it does establish is that the answer
240
+ * was made for this challenge, origin and relying party with the user
241
+ * verified, that every byte of the authenticator data is accounted for, and
242
+ * that the key passes `coseKeyToPublicKey`, the same reading a sign-in
243
+ * gives it. Nothing here
244
+ * is signed, so it does not show the caller holds the private key: the
245
+ * first sign-in does.
246
+ */
247
+ export function verifyRegistration(opts) {
248
+ try {
249
+ const clientB64 = opts.response.response?.clientDataJSON;
250
+ const attestationB64 = opts.response.response?.attestationObject;
251
+ if (typeof clientB64 !== "string" || typeof attestationB64 !== "string") {
252
+ return { ok: false, reason: "bad-registration" };
253
+ }
254
+ const clientBytes = fromBase64Url(clientB64);
255
+ const attestationBytes = fromBase64Url(attestationB64);
256
+ if (!clientBytes || !attestationBytes)
257
+ return { ok: false, reason: "bad-registration" };
258
+ const refused = clientDataRefusal(clientBytes, "webauthn.create", opts.expectedChallenge, opts.expectedOrigins);
259
+ if (refused)
260
+ return { ok: false, reason: refused };
261
+ const attestation = decoded(new Uint8Array(attestationBytes));
262
+ if (attestation === undefined || !isCborMap(attestation))
263
+ return { ok: false, reason: "attestation" };
264
+ if (mapGet(attestation, "fmt") !== "none")
265
+ return { ok: false, reason: "attestation-format" };
266
+ const statement = mapGet(attestation, "attStmt");
267
+ if (statement === undefined || !isCborMap(statement) || statement.$map.length !== 0) {
268
+ return { ok: false, reason: "attestation-statement" };
269
+ }
270
+ const rawAuthData = mapGet(attestation, "authData");
271
+ if (!(rawAuthData instanceof Uint8Array))
272
+ return { ok: false, reason: "authenticator-data" };
273
+ const authData = Buffer.from(rawAuthData);
274
+ // rpIdHash 32, flags 1, counter 4, then aaguid 16 and the id's length 2.
275
+ if (authData.length < 55)
276
+ return { ok: false, reason: "authenticator-data" };
277
+ const rpIdHash = createHash("sha256").update(opts.rpId).digest();
278
+ if (Buffer.compare(authData.subarray(0, 32), rpIdHash) !== 0)
279
+ return { ok: false, reason: "rp-id" };
280
+ const flags = authData[32];
281
+ if (flags === undefined || (flags & 0x05) !== 0x05)
282
+ return { ok: false, reason: "user-verification" };
283
+ if ((flags & 0x40) === 0)
284
+ return { ok: false, reason: "attested-credential" };
285
+ const counter = authData.readUInt32BE(33);
286
+ const idLength = authData.readUInt16BE(53);
287
+ const keyStart = 55 + idLength;
288
+ if (idLength === 0 || idLength > 1023 || keyStart >= authData.length) {
289
+ return { ok: false, reason: "credential-id" };
290
+ }
291
+ const credentialId = b64url(authData.subarray(55, keyStart));
292
+ // The id the page names is the one the authenticator made, or this is two answers glued together.
293
+ if (opts.response.id !== credentialId)
294
+ return { ok: false, reason: "credential-id" };
295
+ if (opts.response.rawId !== undefined && opts.response.rawId !== credentialId) {
296
+ return { ok: false, reason: "credential-id" };
297
+ }
298
+ const keyEnd = cborItemEnd(authData, keyStart);
299
+ if (keyEnd === null)
300
+ return { ok: false, reason: "public-key" };
301
+ if ((flags & 0x80) !== 0) {
302
+ const extensions = keyEnd < authData.length ? decoded(new Uint8Array(authData.subarray(keyEnd))) : undefined;
303
+ if (extensions === undefined || !isCborMap(extensions))
304
+ return { ok: false, reason: "extensions" };
305
+ }
306
+ else if (keyEnd !== authData.length) {
307
+ return { ok: false, reason: "authenticator-data" };
308
+ }
309
+ const publicKeyCose = b64url(authData.subarray(keyStart, keyEnd));
310
+ if (!coseKeyToPublicKey(publicKeyCose))
311
+ return { ok: false, reason: "public-key" };
312
+ return { ok: true, credentialId, publicKeyCose, counter, flags };
313
+ }
314
+ catch {
315
+ return { ok: false, reason: "bad-registration" };
316
+ }
317
+ }
package/dist/wiring.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { createProxy, FileLedger, loadPolicy, tenantKey, } from "@verax-ai/proxy";
2
2
  import { readFileSync } from "node:fs";
3
+ import { POLICY_FILE_ERROR_PREFIX, policyFileRefusal } from "./config.js";
3
4
  import { persistPolicySnapshot } from "./policy-store.js";
4
5
  import { inputsPrincipal } from "./inputs-read.js";
5
6
  import { memoryBelongsToOtherTenant, memoryExistsForTenant, memoryGet, memoryPut, readMemoryMeta, } from "./tools/memory.js";
@@ -36,7 +37,19 @@ export function createBodyServices(opts) {
36
37
  }
37
38
  // The policy is read before the ledger opens: opening takes the ledger lock,
38
39
  // and a start the policy stops must not leave that lock for the next one.
39
- const policyText = readFileSync(opts.policyFile, "utf8");
40
+ const policyRefusal = policyFileRefusal(opts.policyFile);
41
+ if (policyRefusal)
42
+ throw new Error(policyRefusal);
43
+ let policyText;
44
+ try {
45
+ policyText = readFileSync(opts.policyFile, "utf8");
46
+ }
47
+ catch (err) {
48
+ const code = err.code;
49
+ if (code === "EISDIR")
50
+ throw new Error(`${POLICY_FILE_ERROR_PREFIX}is a directory: ${opts.policyFile}`);
51
+ throw new Error(`${POLICY_FILE_ERROR_PREFIX}is missing or unreadable: ${opts.policyFile}`);
52
+ }
40
53
  const policyDocument = JSON.parse(policyText);
41
54
  const policy = loadPolicy(policyText);
42
55
  const ledger = new FileLedger(opts.stateDir);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@verax-ai/body",
3
- "version": "0.5.1",
3
+ "version": "0.6.1",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
6
  "engines": {
@@ -26,9 +26,9 @@
26
26
  "@cedulon/cose": "0.13.1",
27
27
  "@cedulon/effect-extract": "0.13.1",
28
28
  "@modelcontextprotocol/sdk": "^1.30.0",
29
- "@verax-ai/inventory": "^0.5.1",
30
- "@verax-ai/proxy": "^0.5.1",
31
- "jose": "^6.2.11"
29
+ "@verax-ai/inventory": "^0.6.1",
30
+ "@verax-ai/proxy": "^0.6.1",
31
+ "jose": "^6.2.12"
32
32
  },
33
33
  "description": "VERAX body: an MCP server that decides, records and proves what an agent did.",
34
34
  "homepage": "https://verax-ai.com",