@intentic/sandbox-contract 1.432.0 → 1.434.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.
Files changed (232) hide show
  1. package/README.md +15 -0
  2. package/dist/contracts/agent.contract.d.ts +252 -72
  3. package/dist/contracts/agent.contract.d.ts.map +1 -1
  4. package/dist/contracts/agents.contract.d.ts +67 -0
  5. package/dist/contracts/agents.contract.d.ts.map +1 -1
  6. package/dist/contracts/approvals.contract.d.ts +35 -0
  7. package/dist/contracts/approvals.contract.d.ts.map +1 -1
  8. package/dist/contracts/approvals.contract.js +13 -3
  9. package/dist/contracts/approvals.contract.js.map +1 -1
  10. package/dist/contracts/automations.contract.d.ts +1 -0
  11. package/dist/contracts/automations.contract.d.ts.map +1 -1
  12. package/dist/contracts/capabilities.contract.d.ts +3 -0
  13. package/dist/contracts/capabilities.contract.d.ts.map +1 -1
  14. package/dist/contracts/extensions.contract.d.ts +4 -0
  15. package/dist/contracts/extensions.contract.d.ts.map +1 -1
  16. package/dist/contracts/needs.contract.d.ts +198 -0
  17. package/dist/contracts/needs.contract.d.ts.map +1 -1
  18. package/dist/contracts/runner.contract.d.ts +156 -36
  19. package/dist/contracts/runner.contract.d.ts.map +1 -1
  20. package/dist/contracts/seal.contract.d.ts +5 -0
  21. package/dist/contracts/seal.contract.d.ts.map +1 -1
  22. package/dist/contracts/sessions.contract.d.ts +22 -0
  23. package/dist/contracts/sessions.contract.d.ts.map +1 -1
  24. package/dist/contracts/settings.contract.d.ts +16 -0
  25. package/dist/contracts/settings.contract.d.ts.map +1 -1
  26. package/dist/contracts/stack-grants.contract.d.ts +22 -0
  27. package/dist/contracts/stack-grants.contract.d.ts.map +1 -1
  28. package/dist/contracts/system.contract.d.ts +7 -4
  29. package/dist/contracts/system.contract.d.ts.map +1 -1
  30. package/dist/events/agent-events.d.ts +88 -0
  31. package/dist/events/agent-events.d.ts.map +1 -1
  32. package/dist/events/agent-transcript.d.ts +22 -0
  33. package/dist/events/agent-transcript.d.ts.map +1 -1
  34. package/dist/events/system-events.d.ts +4 -0
  35. package/dist/events/system-events.d.ts.map +1 -1
  36. package/dist/events/transcript.d.ts +110 -0
  37. package/dist/events/transcript.d.ts.map +1 -1
  38. package/dist/ids/conversation-ids.d.ts.map +1 -1
  39. package/dist/ids/conversation-ids.js.map +1 -1
  40. package/dist/protocol/raw/extension-protocol.d.ts +116 -0
  41. package/dist/protocol/raw/extension-protocol.d.ts.map +1 -1
  42. package/dist/protocol/raw/extension-protocol.js +17 -1
  43. package/dist/protocol/raw/extension-protocol.js.map +1 -1
  44. package/dist/protocol/raw/raw-json-routes.d.ts +2974 -0
  45. package/dist/protocol/raw/raw-json-routes.d.ts.map +1 -1
  46. package/dist/protocol/raw/raw-json-routes.js +39 -0
  47. package/dist/protocol/raw/raw-json-routes.js.map +1 -1
  48. package/dist/protocol/raw/raw-routes.d.ts +179 -0
  49. package/dist/protocol/raw/raw-routes.d.ts.map +1 -1
  50. package/dist/protocol/raw/raw-routes.js +41 -0
  51. package/dist/protocol/raw/raw-routes.js.map +1 -1
  52. package/dist/protocol/s2s/address.d.ts +53 -0
  53. package/dist/protocol/s2s/address.d.ts.map +1 -0
  54. package/dist/protocol/s2s/address.js +111 -0
  55. package/dist/protocol/s2s/address.js.map +1 -0
  56. package/dist/protocol/s2s/attestation.d.ts +41 -0
  57. package/dist/protocol/s2s/attestation.d.ts.map +1 -0
  58. package/dist/protocol/s2s/attestation.js +96 -0
  59. package/dist/protocol/s2s/attestation.js.map +1 -0
  60. package/dist/protocol/s2s/canonical.d.ts +10 -0
  61. package/dist/protocol/s2s/canonical.d.ts.map +1 -0
  62. package/dist/protocol/s2s/canonical.js +94 -0
  63. package/dist/protocol/s2s/canonical.js.map +1 -0
  64. package/dist/protocol/s2s/envelope.d.ts +40 -0
  65. package/dist/protocol/s2s/envelope.d.ts.map +1 -0
  66. package/dist/protocol/s2s/envelope.js +116 -0
  67. package/dist/protocol/s2s/envelope.js.map +1 -0
  68. package/dist/protocol/s2s/extension-sign.d.ts +21 -0
  69. package/dist/protocol/s2s/extension-sign.d.ts.map +1 -0
  70. package/dist/protocol/s2s/extension-sign.js +41 -0
  71. package/dist/protocol/s2s/extension-sign.js.map +1 -0
  72. package/dist/protocol/s2s/identity-doc.d.ts +50 -0
  73. package/dist/protocol/s2s/identity-doc.d.ts.map +1 -0
  74. package/dist/protocol/s2s/identity-doc.js +89 -0
  75. package/dist/protocol/s2s/identity-doc.js.map +1 -0
  76. package/dist/protocol/s2s/index.d.ts +12 -0
  77. package/dist/protocol/s2s/index.d.ts.map +1 -0
  78. package/dist/protocol/s2s/index.js +12 -0
  79. package/dist/protocol/s2s/index.js.map +1 -0
  80. package/dist/protocol/s2s/kem.d.ts +15 -0
  81. package/dist/protocol/s2s/kem.d.ts.map +1 -0
  82. package/dist/protocol/s2s/kem.js +34 -0
  83. package/dist/protocol/s2s/kem.js.map +1 -0
  84. package/dist/protocol/s2s/keys.d.ts +34 -0
  85. package/dist/protocol/s2s/keys.d.ts.map +1 -0
  86. package/dist/protocol/s2s/keys.js +81 -0
  87. package/dist/protocol/s2s/keys.js.map +1 -0
  88. package/dist/protocol/s2s/passkey-cosign.d.ts +44 -0
  89. package/dist/protocol/s2s/passkey-cosign.d.ts.map +1 -0
  90. package/dist/protocol/s2s/passkey-cosign.js +139 -0
  91. package/dist/protocol/s2s/passkey-cosign.js.map +1 -0
  92. package/dist/protocol/s2s/session.d.ts +61 -0
  93. package/dist/protocol/s2s/session.d.ts.map +1 -0
  94. package/dist/protocol/s2s/session.js +98 -0
  95. package/dist/protocol/s2s/session.js.map +1 -0
  96. package/dist/protocol/s2s/sign.d.ts +12 -0
  97. package/dist/protocol/s2s/sign.d.ts.map +1 -0
  98. package/dist/protocol/s2s/sign.js +33 -0
  99. package/dist/protocol/s2s/sign.js.map +1 -0
  100. package/dist/schemas/agents-list.d.ts +2 -0
  101. package/dist/schemas/agents-list.d.ts.map +1 -1
  102. package/dist/schemas/agents.d.ts +4 -0
  103. package/dist/schemas/agents.d.ts.map +1 -1
  104. package/dist/schemas/approvals.d.ts +100 -0
  105. package/dist/schemas/approvals.d.ts.map +1 -1
  106. package/dist/schemas/approvals.js +21 -0
  107. package/dist/schemas/approvals.js.map +1 -1
  108. package/dist/schemas/automations.d.ts +2 -0
  109. package/dist/schemas/automations.d.ts.map +1 -1
  110. package/dist/schemas/automations.js +1 -1
  111. package/dist/schemas/automations.js.map +1 -1
  112. package/dist/schemas/capabilities.d.ts +1 -0
  113. package/dist/schemas/capabilities.d.ts.map +1 -1
  114. package/dist/schemas/command-admission.d.ts +6 -0
  115. package/dist/schemas/command-admission.d.ts.map +1 -1
  116. package/dist/schemas/command-admission.js +2 -1
  117. package/dist/schemas/command-admission.js.map +1 -1
  118. package/dist/schemas/contact-need.d.ts +50 -0
  119. package/dist/schemas/contact-need.d.ts.map +1 -0
  120. package/dist/schemas/contact-need.js +19 -0
  121. package/dist/schemas/contact-need.js.map +1 -0
  122. package/dist/schemas/contacts-settings.d.ts +5 -0
  123. package/dist/schemas/contacts-settings.d.ts.map +1 -0
  124. package/dist/schemas/contacts-settings.js +11 -0
  125. package/dist/schemas/contacts-settings.js.map +1 -0
  126. package/dist/schemas/extension-updates.d.ts +8 -0
  127. package/dist/schemas/extension-updates.d.ts.map +1 -1
  128. package/dist/schemas/issues.d.ts +2 -0
  129. package/dist/schemas/issues.d.ts.map +1 -1
  130. package/dist/schemas/issues.js +1 -0
  131. package/dist/schemas/issues.js.map +1 -1
  132. package/dist/schemas/links.d.ts +8 -0
  133. package/dist/schemas/links.d.ts.map +1 -1
  134. package/dist/schemas/links.js +4 -0
  135. package/dist/schemas/links.js.map +1 -1
  136. package/dist/schemas/need-kinds.d.ts +2 -1
  137. package/dist/schemas/need-kinds.d.ts.map +1 -1
  138. package/dist/schemas/need-kinds.js +2 -2
  139. package/dist/schemas/need-kinds.js.map +1 -1
  140. package/dist/schemas/needs.d.ts +156 -13
  141. package/dist/schemas/needs.d.ts.map +1 -1
  142. package/dist/schemas/needs.js +5 -12
  143. package/dist/schemas/needs.js.map +1 -1
  144. package/dist/schemas/redirect-need.d.ts +15 -0
  145. package/dist/schemas/redirect-need.d.ts.map +1 -0
  146. package/dist/schemas/redirect-need.js +14 -0
  147. package/dist/schemas/redirect-need.js.map +1 -0
  148. package/dist/schemas/s2s-identity.d.ts +521 -0
  149. package/dist/schemas/s2s-identity.d.ts.map +1 -0
  150. package/dist/schemas/s2s-identity.js +208 -0
  151. package/dist/schemas/s2s-identity.js.map +1 -0
  152. package/dist/schemas/s2s-views.d.ts +2127 -0
  153. package/dist/schemas/s2s-views.d.ts.map +1 -0
  154. package/dist/schemas/s2s-views.js +217 -0
  155. package/dist/schemas/s2s-views.js.map +1 -0
  156. package/dist/schemas/s2s-wire.d.ts +1616 -0
  157. package/dist/schemas/s2s-wire.d.ts.map +1 -0
  158. package/dist/schemas/s2s-wire.js +276 -0
  159. package/dist/schemas/s2s-wire.js.map +1 -0
  160. package/dist/schemas/s2s.d.ts +5 -0
  161. package/dist/schemas/s2s.d.ts.map +1 -0
  162. package/dist/schemas/s2s.js +5 -0
  163. package/dist/schemas/s2s.js.map +1 -0
  164. package/dist/schemas/settings.d.ts +16 -0
  165. package/dist/schemas/settings.d.ts.map +1 -1
  166. package/dist/schemas/settings.js +2 -0
  167. package/dist/schemas/settings.js.map +1 -1
  168. package/dist/schemas/terminal.d.ts +2 -0
  169. package/dist/schemas/terminal.d.ts.map +1 -1
  170. package/dist/schemas/terminal.js +4 -0
  171. package/dist/schemas/terminal.js.map +1 -1
  172. package/dist/schemas/vpn.d.ts +4 -0
  173. package/dist/schemas/vpn.d.ts.map +1 -1
  174. package/dist/schemas/vpn.js +9 -0
  175. package/dist/schemas/vpn.js.map +1 -1
  176. package/dist/state/auth-state.d.ts.map +1 -1
  177. package/dist/state/auth-state.js +7 -0
  178. package/dist/state/auth-state.js.map +1 -1
  179. package/dist/state/definition.d.ts +18 -0
  180. package/dist/state/definition.d.ts.map +1 -1
  181. package/package.json +20 -5
  182. package/src/contracts/approvals.contract.ts +24 -3
  183. package/src/ids/conversation-ids.test.ts +22 -3
  184. package/src/ids/conversation-ids.ts +5 -7
  185. package/src/protocol/ingress-contract.fixture.json +6 -1
  186. package/src/protocol/raw/extension-protocol.ts +45 -1
  187. package/src/protocol/raw/raw-json-routes.ts +85 -0
  188. package/src/protocol/raw/raw-routes.ts +66 -3
  189. package/src/protocol/s2s/address.test.ts +157 -0
  190. package/src/protocol/s2s/address.ts +193 -0
  191. package/src/protocol/s2s/attestation.test.ts +262 -0
  192. package/src/protocol/s2s/attestation.ts +173 -0
  193. package/src/protocol/s2s/canonical.test.ts +70 -0
  194. package/src/protocol/s2s/canonical.ts +119 -0
  195. package/src/protocol/s2s/envelope.test.ts +162 -0
  196. package/src/protocol/s2s/envelope.ts +194 -0
  197. package/src/protocol/s2s/extension-sign.test.ts +117 -0
  198. package/src/protocol/s2s/extension-sign.ts +76 -0
  199. package/src/protocol/s2s/identity-doc.test.ts +187 -0
  200. package/src/protocol/s2s/identity-doc.ts +169 -0
  201. package/src/protocol/s2s/index.ts +15 -0
  202. package/src/protocol/s2s/kem.ts +62 -0
  203. package/src/protocol/s2s/keys.test.ts +76 -0
  204. package/src/protocol/s2s/keys.ts +155 -0
  205. package/src/protocol/s2s/passkey-cosign.test.ts +98 -0
  206. package/src/protocol/s2s/passkey-cosign.testing.ts +86 -0
  207. package/src/protocol/s2s/passkey-cosign.ts +192 -0
  208. package/src/protocol/s2s/s2s.testing.ts +22 -0
  209. package/src/protocol/s2s/session.test.ts +133 -0
  210. package/src/protocol/s2s/session.ts +179 -0
  211. package/src/protocol/s2s/sign.test.ts +66 -0
  212. package/src/protocol/s2s/sign.ts +53 -0
  213. package/src/schemas/approvals.ts +41 -0
  214. package/src/schemas/automations.ts +4 -3
  215. package/src/schemas/command-admission.ts +4 -1
  216. package/src/schemas/contact-need.ts +26 -0
  217. package/src/schemas/contacts-settings.ts +16 -0
  218. package/src/schemas/issues.ts +1 -0
  219. package/src/schemas/links.ts +4 -0
  220. package/src/schemas/need-kinds.ts +2 -2
  221. package/src/schemas/needs.ts +5 -18
  222. package/src/schemas/redirect-need.ts +19 -0
  223. package/src/schemas/s2s-identity.ts +329 -0
  224. package/src/schemas/s2s-views.ts +326 -0
  225. package/src/schemas/s2s-wire.ts +401 -0
  226. package/src/schemas/s2s.test.ts +175 -0
  227. package/src/schemas/s2s.ts +9 -0
  228. package/src/schemas/settings.ts +2 -0
  229. package/src/schemas/terminal.ts +6 -0
  230. package/src/schemas/version-seam.test.ts +9 -0
  231. package/src/schemas/vpn.ts +13 -0
  232. package/src/state/auth-state.ts +13 -0
@@ -267,6 +267,10 @@ export const RAW_ROUTES = {
267
267
  "POST /extension/sleep/ready": { floor: "maintainer", panel: false, control: "never" },
268
268
  // (2026-11-27) What the platform's sleep watch heard for the extension's own connections while the sandbox slept.
269
269
  "POST /extension/sleep/heard": { floor: "maintainer", panel: false, control: "never" },
270
+ // (2026-11-28) This sandbox's public identity document, and a signature as this sandbox over a record of the calling
271
+ // extension's own (`permissions.identity`). Reached the same way, undeclared; the sign door checks its declaration.
272
+ "GET /extension/identity": { floor: "maintainer", panel: false, control: "never" },
273
+ "POST /extension/identity/sign": { floor: "maintainer", panel: false, control: "never" },
270
274
  // The CI webhook receiver, gated by the per-sandbox webhook secret.
271
275
  "POST /ci/webhook/{host}": { auth: "door" },
272
276
  // (2026-11-25) What the forges said, as it is heard, for a program (schemas/ci.ts CiFeedEventSchema): Server-Sent
@@ -401,9 +405,10 @@ export const RAW_ROUTES = {
401
405
  "POST /account/routes/secret": { floor: "viewer", panel: false, control: "never" },
402
406
  "GET /account/routes": { floor: "viewer", control: "never" },
403
407
  // Routing as a person, a drop (schemas/account-routes.ts): another sandbox the person is in handing a note and files
404
- // into this one's inbox, admitted in the handler by a drop pass beside the person's pairing (admit-route.ts); it starts
405
- // no turn. The sending side, for `sandboxes send` on the agent's PATH. The inbox, for the `inbox` command and the
406
- // editor's Received list: each shown to the people a drop names and to maintainers, the handler cutting the list.
408
+ // into this one, admitted in the handler by a drop pass beside the person's pairing (admit-route.ts); it starts no
409
+ // turn, and lands as a message in its sender's thread (2026-11-27). The sending side, for `sandboxes send` on the
410
+ // agent's PATH. The old inbox, which nothing files into since: each drop shown to the people it names and to
411
+ // maintainers, the handler cutting the list.
407
412
  "POST /account/drop": { auth: "door", control: "never", lane: "bulk" },
408
413
  "POST /account/drops": { agent: true, control: "never" },
409
414
  "GET /account/inbox": { floor: "viewer", agent: true, control: "never" },
@@ -442,6 +447,64 @@ export const RAW_ROUTES = {
442
447
  "POST /link/operate/exec/{id}/stop": { auth: "door", control: "never" },
443
448
  "POST /link/operate/read": { auth: "door", control: "never" },
444
449
  "POST /link/operate/write": { auth: "door", control: "never" },
450
+ // SANDBOX-TO-SANDBOX (schemas/s2s.ts, docs/architecture/messages.md). The doors another daemon calls: this one's
451
+ // public identity (no credential: answering at all says it speaks s2s/1), the inbox each signed envelope is admitted
452
+ // at, call sessions and calls, and the one-time upgrades an existing link, route pairing or report pass vouches for.
453
+ "GET /s2s/identity": { auth: "door", control: "never" },
454
+ "POST /s2s/inbox": { auth: "door", control: "never", lane: "bulk" },
455
+ "POST /s2s/session": { auth: "door", control: "never" },
456
+ "POST /s2s/call/{method}": { auth: "door", control: "never" },
457
+ "POST /link/upgrade": { auth: "door", control: "never" },
458
+ "POST /account/pairings/upgrade": { auth: "door", control: "never" },
459
+ "POST /account/contact-bootstrap": { auth: "door", control: "never" },
460
+ // This sandbox's identity: its own domains and their checks, rotation, and the recovery key, which is the owner's alone.
461
+ "GET /s2s/me": { floor: "maintainer", control: "never" },
462
+ "POST /s2s/me/domains": { panel: false, control: "never" },
463
+ "DELETE /s2s/me/domains/{domain}": { panel: false, control: "never" },
464
+ "POST /s2s/me/domains/{domain}/check": { control: "never" },
465
+ "POST /s2s/me/rotate": { panel: false, control: "never" },
466
+ "POST /s2s/me/recovery": { floor: "owner", panel: false, control: "never" },
467
+ "POST /s2s/me/recover": { floor: "owner", panel: false, control: "never" },
468
+ // (2026-11-28) Attestations: what other sandboxes vouched for and this one carries in its document, listed, added from
469
+ // the contact's message and removed by the owner or a maintainer; asking a contact to vouch for this sandbox, and
470
+ // vouching for one, both the sandbox speaking as itself. No agent token: an agent never signs or adds one.
471
+ "GET /s2s/me/attestations": { floor: "maintainer", control: "never" },
472
+ "POST /s2s/me/attestations": { panel: false, control: "never" },
473
+ "DELETE /s2s/me/attestations/{id}": { panel: false, control: "never" },
474
+ "POST /s2s/me/attestations/ask": { panel: false, control: "never" },
475
+ "POST /s2s/attestations/issue": { panel: false, control: "never" },
476
+ // Contacts: a sandbox-scoped one is the owner's or a maintainer's to make, accept, re-grant and block. A person-scoped
477
+ // one is that person's own (a viewer's too), so listing, pairing back, removing and trusting a new key floor at the
478
+ // viewer and the handler holds a person to their own. `paired` first: it would otherwise read as a contact's id. The
479
+ // `messages` command lists the contacts and the outbox with its turn proof: the handler cuts both to the contacts that
480
+ // turn may use, as it cuts a person's to their own.
481
+ "GET /s2s/contacts": { floor: "viewer", agent: true, control: "never" },
482
+ "POST /s2s/contacts/paired": { floor: "viewer", panel: false, control: "never" },
483
+ "POST /s2s/contacts": { panel: false, control: "never" },
484
+ "POST /s2s/contacts/{id}": { panel: false, control: "never" },
485
+ "DELETE /s2s/contacts/{id}": { floor: "viewer", panel: false, control: "never" },
486
+ "POST /s2s/contacts/{id}/accept": { panel: false, control: "never" },
487
+ "POST /s2s/contacts/{id}/block": { panel: false, control: "never" },
488
+ "POST /s2s/contacts/{id}/trust-key": { floor: "viewer", panel: false, control: "never" },
489
+ // The tasks contacts handed this sandbox (cut to the contacts the reader may use), and the owner's or a maintainer's
490
+ // Allow or Decline for one that waits on them: what a link's task waiting on Access ▸ Programs was.
491
+ "GET /s2s/tasks": { control: "never" },
492
+ "POST /s2s/contacts/{id}/tasks/{request}": { panel: false, control: "never" },
493
+ // A task this sandbox handed a contact, its live status asked of that contact (`task.watch`), for whoever may use the
494
+ // contact (the handler holds a person to their own, as the contacts' list does).
495
+ "GET /s2s/contacts/{id}/tasks/{request}/watch": { floor: "viewer", control: "never" },
496
+ "GET /s2s/outbox": { floor: "viewer", agent: true, control: "never" },
497
+ // A signed-in person's side: pairing with a sandbox they are also in, and the inbox, each thread shown to the people it
498
+ // is for and to maintainers (the handler cuts the list); the `messages` command reads and sends with its turn proof.
499
+ // Taking files writes into the workspace, so it is the collaborator's.
500
+ "POST /s2s/pairings": { floor: "viewer", panel: false, control: "never" },
501
+ "GET /s2s/threads": { floor: "viewer", agent: true, control: "never" },
502
+ "GET /s2s/threads/{id}": { floor: "viewer", agent: true, control: "never" },
503
+ "DELETE /s2s/threads/{id}": { floor: "viewer", control: "never" },
504
+ "POST /s2s/threads/{id}/read": { floor: "viewer", control: "never" },
505
+ "POST /s2s/messages": { floor: "viewer", agent: true, control: "never" },
506
+ "GET /s2s/messages/{id}/files/{name}": { floor: "viewer", agent: true, control: "never", lane: "bulk" },
507
+ "POST /s2s/messages/{id}/take": { floor: "collaborator", agent: true, control: "never" },
445
508
  "POST /system/control/tokens": { panel: false, control: "never" },
446
509
  "GET /system/control/tokens": { floor: "maintainer", panel: false, control: "never" },
447
510
  "DELETE /system/control/tokens/{id}": { panel: false, control: "never" },
@@ -0,0 +1,157 @@
1
+ import type { SandboxIdentity } from "../../schemas/s2s-identity.js";
2
+ import {
3
+ type AddressIo,
4
+ type FetchedIdentity,
5
+ formatTxtRecord,
6
+ normalizeAddress,
7
+ normalizeDomain,
8
+ parseTxtRecord,
9
+ resolveAddress,
10
+ type TxtAnswer,
11
+ txtLookupOf,
12
+ txtRecordName,
13
+ } from "./address.js";
14
+ import { signIdentity } from "./identity-doc.js";
15
+ import { keySetFor } from "./s2s.testing.js";
16
+
17
+ const NOW = 1_800_000_000_000;
18
+ const HOST = `0123456789ab.sandbox.example`;
19
+ const DOMAIN = `ops.acme.example`;
20
+ const keys = keySetFor(`ops`);
21
+ const other = keySetFor(`other`);
22
+ const FP = keys.fingerprint;
23
+ const document = signIdentity({ sandbox: `0123456789ab`, name: `ops`, host: HOST, domains: [DOMAIN], issuedAt: NOW }, keys);
24
+
25
+ // One fake per seam: the DNS answers and the identities served, by name and host; anything else is missing or down.
26
+ const io = (txt: Readonly<Record<string, TxtAnswer>>, served: Readonly<Record<string, FetchedIdentity>>): AddressIo & { readonly asked: string[] } => {
27
+ const asked: string[] = [];
28
+ return {
29
+ asked,
30
+ resolveTxt: async (name) => {
31
+ asked.push(`txt ${name}`);
32
+ return txt[name] ?? { kind: `missing` };
33
+ },
34
+ fetchIdentity: async (host) => {
35
+ asked.push(`get ${host}`);
36
+ return served[host] ?? { ok: false, error: `connection refused` };
37
+ },
38
+ };
39
+ };
40
+ const serving = (doc: SandboxIdentity): FetchedIdentity => ({ ok: true, body: JSON.parse(JSON.stringify(doc)) });
41
+ const record = (value: string): TxtAnswer => ({ kind: `records`, records: [[value]] });
42
+
43
+ describe("an address", () => {
44
+ it("is normalized to lowercase punycode with no scheme, path, trailing dot or default port", () => {
45
+ expect(normalizeAddress(` HTTPS://Ops.Acme.Example./s2s/identity `)).toBe(`ops.acme.example`);
46
+ expect(normalizeAddress(`bücher.example`)).toBe(`xn--bcher-kva.example`);
47
+ expect(normalizeAddress(`ops.acme.example:443`)).toBe(`ops.acme.example`);
48
+ expect(normalizeAddress(`localhost:8443`)).toBe(`localhost:8443`);
49
+ });
50
+
51
+ it("is refused when it is no host", () => {
52
+ for (const raw of [``, `ops`, `ada@ops.example`, `-bad.example`, `under_score.example`, `ops..example`, `http://`]) {
53
+ expect(normalizeAddress(raw)).toBeUndefined();
54
+ }
55
+ });
56
+
57
+ it("is a domain only without a port and not localhost", () => {
58
+ expect(normalizeDomain(`Ops.Acme.Example`)).toBe(`ops.acme.example`);
59
+ expect(normalizeDomain(`ops.acme.example:8443`)).toBeUndefined();
60
+ expect(normalizeDomain(`localhost`)).toBeUndefined();
61
+ });
62
+ });
63
+
64
+ describe("a TXT record", () => {
65
+ it("is published under _intentic and reads back as it was written", () => {
66
+ expect(txtRecordName(DOMAIN)).toBe(`_intentic.ops.acme.example`);
67
+ const value = formatTxtRecord({ fingerprint: FP, host: HOST });
68
+ expect(value).toBe(`v=isk1 fp=${FP} host=${HOST}`);
69
+ expect(parseTxtRecord(value)).toEqual({ fingerprint: FP, host: HOST });
70
+ });
71
+
72
+ it("reads in any order, with any whitespace and case in keys and the fingerprint, skipping words it does not know", () => {
73
+ expect(parseTxtRecord(` HOST=${HOST.toUpperCase()}\tnote=hi FP=${FP.toUpperCase()} V=isk1 `)).toEqual({ fingerprint: FP, host: HOST });
74
+ });
75
+
76
+ it("does not read without its version, fingerprint or host, with one twice, or with a word that is no pair", () => {
77
+ for (const value of [
78
+ `fp=${FP} host=${HOST}`,
79
+ `v=isk2 fp=${FP} host=${HOST}`,
80
+ `v=isk1 host=${HOST}`,
81
+ `v=isk1 fp=${FP}`,
82
+ `v=isk1 fp=isk1:short host=${HOST}`,
83
+ `v=isk1 fp=${FP} host=not_a_host`,
84
+ `v=isk1 fp=${FP} fp=${other.fingerprint} host=${HOST}`,
85
+ `v=isk1 fp=${FP} host=${HOST} stray`,
86
+ `v=isk1 =x fp=${FP} host=${HOST}`,
87
+ ]) {
88
+ expect(parseTxtRecord(value)).toBeUndefined();
89
+ }
90
+ });
91
+
92
+ it("is looked up from DNS chunks: joined, others' records ignored, agreeing copies one, disagreeing ones a conflict", () => {
93
+ const value = formatTxtRecord({ fingerprint: FP, host: HOST });
94
+ expect(txtLookupOf([[`v=spf1 -all`], [value.slice(0, 20), value.slice(20)]])).toEqual({ kind: `record`, record: { fingerprint: FP, host: HOST } });
95
+ expect(txtLookupOf([[`v=spf1 -all`]])).toEqual({ kind: `none` });
96
+ expect(txtLookupOf([[value], [value]])).toEqual({ kind: `record`, record: { fingerprint: FP, host: HOST } });
97
+ expect(txtLookupOf([[value], [formatTxtRecord({ fingerprint: other.fingerprint, host: HOST })]])).toEqual({ kind: `conflict` });
98
+ expect(txtLookupOf([[value], [`v=isk1 fp=broken`]])).toEqual({ kind: `invalid` });
99
+ });
100
+ });
101
+
102
+ describe("resolving an address", () => {
103
+ it("takes a host with no record as itself, and checks the document it serves", async () => {
104
+ const fake = io({}, { [HOST]: serving(document) });
105
+ expect(await resolveAddress(HOST, fake)).toEqual({ ok: true, address: HOST, host: HOST, document, txt: undefined });
106
+ expect(fake.asked).toEqual([`txt _intentic.${HOST}`, `get ${HOST}`]);
107
+ });
108
+
109
+ it("follows a domain's record to its host, and checks the key and that the document lists the domain", async () => {
110
+ const fake = io({ [`_intentic.${DOMAIN}`]: record(formatTxtRecord({ fingerprint: FP, host: HOST })) }, { [HOST]: serving(document) });
111
+ expect(await resolveAddress(`https://${DOMAIN.toUpperCase()}`, fake)).toEqual({ ok: true, address: DOMAIN, host: HOST, document, txt: { fingerprint: FP, host: HOST } });
112
+ expect(fake.asked).toEqual([`txt _intentic.${DOMAIN}`, `get ${HOST}`]);
113
+ });
114
+
115
+ it("looks up no record for a host with a port", async () => {
116
+ const fake = io({}, { "localhost:8443": serving(signIdentity({ sandbox: `l`, name: `l`, host: `localhost:8443`, domains: [], issuedAt: NOW }, keys)) });
117
+ expect((await resolveAddress(`localhost:8443`, fake)).ok).toBe(true);
118
+ expect(fake.asked).toEqual([`get localhost:8443`]);
119
+ });
120
+
121
+ it("refuses a record naming another key than the one served", async () => {
122
+ const fake = io({ [`_intentic.${DOMAIN}`]: record(formatTxtRecord({ fingerprint: other.fingerprint, host: HOST })) }, { [HOST]: serving(document) });
123
+ expect(await resolveAddress(DOMAIN, fake)).toMatchObject({ ok: false, code: `fingerprint` });
124
+ });
125
+
126
+ it("refuses a domain the document does not list, and a document of another host", async () => {
127
+ const unlisted = signIdentity({ sandbox: `0123456789ab`, name: `ops`, host: HOST, domains: [], issuedAt: NOW }, keys);
128
+ const fake = io({ [`_intentic.${DOMAIN}`]: record(formatTxtRecord({ fingerprint: FP, host: HOST })) }, { [HOST]: serving(unlisted) });
129
+ expect(await resolveAddress(DOMAIN, fake)).toMatchObject({ ok: false, code: `domain` });
130
+ expect(await resolveAddress(`elsewhere.example`, io({}, { "elsewhere.example": serving(document) }))).toMatchObject({ ok: false, code: `host` });
131
+ });
132
+
133
+ it("refuses a document that does not verify, or is none at all", async () => {
134
+ expect(await resolveAddress(HOST, io({}, { [HOST]: serving({ ...document, name: `forged` }) }))).toMatchObject({ ok: false, code: `signature` });
135
+ expect(await resolveAddress(HOST, io({}, { [HOST]: { ok: true, body: { hello: `world` } } }))).toMatchObject({ ok: false, code: `malformed` });
136
+ });
137
+
138
+ it("tells a daemon from before (401) from one that does not answer", async () => {
139
+ expect(await resolveAddress(HOST, io({}, { [HOST]: { ok: false, status: 401, error: `unauthorized` } }))).toMatchObject({ ok: false, code: `too_old` });
140
+ expect(await resolveAddress(HOST, io({}, {}))).toEqual({ ok: false, code: `unreachable`, message: `${HOST} did not answer: connection refused` });
141
+ });
142
+
143
+ it("refuses when the lookup fails, or the record is broken or contradicts itself", async () => {
144
+ expect(await resolveAddress(DOMAIN, io({ [`_intentic.${DOMAIN}`]: { kind: `failed`, error: `SERVFAIL` } }, {}))).toEqual({
145
+ ok: false,
146
+ code: `dns`,
147
+ message: `looking up _intentic.${DOMAIN} failed: SERVFAIL`,
148
+ });
149
+ expect(await resolveAddress(DOMAIN, io({ [`_intentic.${DOMAIN}`]: record(`v=isk1 fp=nope`) }, {}))).toMatchObject({ ok: false, code: `txt` });
150
+ });
151
+
152
+ it("refuses what is no address before asking anything", async () => {
153
+ const fake = io({}, {});
154
+ expect(await resolveAddress(`not an address`, fake)).toMatchObject({ ok: false, code: `invalid_address` });
155
+ expect(fake.asked).toEqual([]);
156
+ });
157
+ });
@@ -0,0 +1,193 @@
1
+ import type { JsonValue } from "@intentic/base/json";
2
+ import {
3
+ S2S_TXT_LABEL,
4
+ S2sAddressSchema,
5
+ S2sDomainSchema,
6
+ S2sFingerprintSchema,
7
+ type S2sTxtRecord,
8
+ type SandboxIdentity,
9
+ SandboxIdentitySchema,
10
+ } from "../../schemas/s2s-identity.js";
11
+ import { speaksS2s, verifyIdentity } from "./identity-doc.js";
12
+
13
+ // ADDRESSES (docs/architecture/messages.md, "Addresses"): a contact is addressed by its sandbox host or by an own domain
14
+ // whose TXT record, `_intentic.<domain>` = `v=isk1 fp=<fingerprint> host=<host>`, says which key speaks for it and
15
+ // where. Resolving one takes its I/O as arguments (a DNS lookup, an HTTPS fetch), so every path is testable.
16
+
17
+ // The TXT record's version word.
18
+ const TXT_VERSION = "isk1";
19
+
20
+ // An address as a person or a document wrote it, in the one spelling this protocol compares: lowercase, punycode, no
21
+ // scheme, path, credentials or trailing dot, and no port 443. Undefined for anything that is no host.
22
+ export const normalizeAddress = (raw: string): string | undefined => {
23
+ const text = raw.trim().replace(/^https?:\/\//iu, "").split("/")[0] ?? "";
24
+ if (text === "" || text.includes("@")) {
25
+ return undefined;
26
+ }
27
+ let host: string;
28
+ try {
29
+ const url = new URL(`https://${text}`);
30
+ host = `${url.hostname.replace(/\.$/u, "")}${url.port === "" ? "" : `:${url.port}`}`;
31
+ } catch {
32
+ // allow(silent-catch): the URL parser refusing it is the answer: it is no host.
33
+ return undefined;
34
+ }
35
+ return S2sAddressSchema.safeParse(host).success ? host : undefined;
36
+ };
37
+
38
+ // An own domain: an address with no port that is not localhost.
39
+ export const normalizeDomain = (raw: string): string | undefined => {
40
+ const address = normalizeAddress(raw);
41
+ return address !== undefined && S2sDomainSchema.safeParse(address).success ? address : undefined;
42
+ };
43
+
44
+ // Where a domain's record is published.
45
+ export const txtRecordName = (domain: string): string => `${S2S_TXT_LABEL}.${domain}`;
46
+
47
+ // The record's value, as the Identity section shows it to publish.
48
+ export const formatTxtRecord = ({ fingerprint, host }: S2sTxtRecord): string => `v=${TXT_VERSION} fp=${fingerprint} host=${host}`;
49
+
50
+ // One record's value, or undefined when it is not one of ours or does not read: `key=value` words in any order,
51
+ // separated by whitespace, `v=isk1` among them, a fingerprint and a host. A word this build does not know is skipped,
52
+ // for a later version's sake; a word twice, or one without `=`, spoils the record.
53
+ export const parseTxtRecord = (text: string): S2sTxtRecord | undefined => {
54
+ const fields = new Map<string, string>();
55
+ for (const word of text.trim().split(/\s+/u)) {
56
+ const at = word.indexOf("=");
57
+ if (at <= 0 || fields.has(word.slice(0, at).toLowerCase())) {
58
+ return undefined;
59
+ }
60
+ fields.set(word.slice(0, at).toLowerCase(), word.slice(at + 1));
61
+ }
62
+ if (fields.get("v") !== TXT_VERSION) {
63
+ return undefined;
64
+ }
65
+ const fingerprint = fields.get("fp")?.toLowerCase();
66
+ const host = normalizeAddress(fields.get("host") ?? "");
67
+ return fingerprint !== undefined && S2sFingerprintSchema.safeParse(fingerprint).success && host !== undefined ? { fingerprint, host } : undefined;
68
+ };
69
+
70
+ const isOurs = (text: string): boolean => /(?:^|\s)v=isk1(?:\s|$)/iu.test(text.trim());
71
+
72
+ // What a domain's TXT records say. `none`: no record of ours. `record`: one, or several that agree. `invalid`: one of ours
73
+ // does not read. `conflict`: ours name two different keys or hosts. Each record arrives as DNS chunks, joined first.
74
+ export type TxtLookup =
75
+ | { readonly kind: "none" }
76
+ | { readonly kind: "record"; readonly record: S2sTxtRecord }
77
+ | { readonly kind: "invalid" | "conflict" };
78
+
79
+ export const txtLookupOf = (records: readonly (readonly string[])[]): TxtLookup => {
80
+ const ours = records.map((chunks) => chunks.join("")).filter(isOurs);
81
+ if (ours.length === 0) {
82
+ return { kind: "none" };
83
+ }
84
+ const parsed = ours.map(parseTxtRecord);
85
+ const [first] = parsed;
86
+ if (first === undefined || parsed.some((record) => record === undefined)) {
87
+ return { kind: "invalid" };
88
+ }
89
+ return parsed.every((record) => record?.fingerprint === first.fingerprint && record.host === first.host) ? { kind: "record", record: first } : { kind: "conflict" };
90
+ };
91
+
92
+ // The DNS error codes that mean the name has no record, not that the lookup failed (node:dns resolveTxt).
93
+ export const TXT_MISSING_CODES: readonly string[] = ["ENOTFOUND", "ENODATA"];
94
+
95
+ // The lookup, as the caller's resolver answered it: the records, none at all, or a failure in words.
96
+ export type TxtAnswer =
97
+ | { readonly kind: "records"; readonly records: readonly (readonly string[])[] }
98
+ | { readonly kind: "missing" }
99
+ | { readonly kind: "failed"; readonly error: string };
100
+
101
+ // GET https://<host>/s2s/identity, as the caller fetched it: the JSON it answered, or the status and why not.
102
+ export type FetchedIdentity =
103
+ | { readonly ok: true; readonly body: JsonValue }
104
+ | { readonly ok: false; readonly status?: number | undefined; readonly error: string };
105
+
106
+ export interface AddressIo {
107
+ readonly resolveTxt: (name: string) => Promise<TxtAnswer>;
108
+ readonly fetchIdentity: (host: string) => Promise<FetchedIdentity>;
109
+ }
110
+
111
+ // Why an address did not resolve. invalid_address: no host. dns: the lookup failed. txt: its record does not read or
112
+ // names two keys. unreachable: the host did not answer. too_old: it answers but speaks no s2s/1 (401 is what a daemon
113
+ // from before answers this door). malformed: what it answered is no identity document. signature: the document does not
114
+ // verify. fingerprint: its key is not the one the record names. host: it is another host's document. domain: the
115
+ // document does not list the domain it was resolved for.
116
+ export const RESOLVE_REFUSALS = ["invalid_address", "dns", "txt", "unreachable", "too_old", "malformed", "signature", "fingerprint", "host", "domain"] as const;
117
+ export type ResolveRefusal = (typeof RESOLVE_REFUSALS)[number];
118
+
119
+ export type ResolvedAddress =
120
+ | {
121
+ readonly ok: true;
122
+ // The address as normalized: what a contact is filed under.
123
+ readonly address: string;
124
+ // Where /s2s/* answers for it.
125
+ readonly host: string;
126
+ readonly document: SandboxIdentity;
127
+ // The domain's record, when it had one.
128
+ readonly txt?: S2sTxtRecord | undefined;
129
+ }
130
+ | { readonly ok: false; readonly code: ResolveRefusal; readonly message: string };
131
+
132
+ const refused = (code: ResolveRefusal, message: string): ResolvedAddress => ({ ok: false, code, message });
133
+
134
+ // A host with a port, or localhost, can be no domain, so it has no record to look up.
135
+ const mayHaveRecord = (address: string): boolean => S2sDomainSchema.safeParse(address).success;
136
+
137
+ const recordFor = async (address: string, io: AddressIo): Promise<S2sTxtRecord | ResolvedAddress | undefined> => {
138
+ if (!mayHaveRecord(address)) {
139
+ return undefined;
140
+ }
141
+ const answer = await io.resolveTxt(txtRecordName(address));
142
+ if (answer.kind === "failed") {
143
+ return refused("dns", `looking up ${txtRecordName(address)} failed: ${answer.error}`);
144
+ }
145
+ const lookup = answer.kind === "missing" ? ({ kind: "none" } as const) : txtLookupOf(answer.records);
146
+ if (lookup.kind === "invalid" || lookup.kind === "conflict") {
147
+ return refused("txt", `${txtRecordName(address)} ${lookup.kind === "invalid" ? "holds a record that does not read" : "names two different keys"}`);
148
+ }
149
+ return lookup.kind === "record" ? lookup.record : undefined;
150
+ };
151
+
152
+ const documentProblem = (document: SandboxIdentity, address: string, host: string, txt: S2sTxtRecord | undefined): ResolvedAddress | undefined => {
153
+ const check = verifyIdentity(document);
154
+ if (!check.ok) {
155
+ return refused("signature", `${host} serves an identity that does not verify: ${check.reason}`);
156
+ }
157
+ if (!speaksS2s(document)) {
158
+ return refused("too_old", `${host} does not speak s2s/1`);
159
+ }
160
+ if (normalizeAddress(document.host) !== host) {
161
+ return refused("host", `${host} serves the identity of ${document.host}`);
162
+ }
163
+ if (txt !== undefined && document.fingerprint !== txt.fingerprint) {
164
+ return refused("fingerprint", `${host} serves key ${document.fingerprint}, not the ${txt.fingerprint} that ${txtRecordName(address)} names`);
165
+ }
166
+ if (address !== host && !document.domains.includes(address)) {
167
+ return refused("domain", `${host} does not list ${address} among its own domains`);
168
+ }
169
+ return undefined;
170
+ };
171
+
172
+ // Resolves an address to a verified identity: its record, when it has one, names the host and the key; without one the
173
+ // address is the host. The document must verify, be that host's, carry the record's key, and list the domain.
174
+ export const resolveAddress = async (raw: string, io: AddressIo): Promise<ResolvedAddress> => {
175
+ const address = normalizeAddress(raw);
176
+ if (address === undefined) {
177
+ return refused("invalid_address", `"${raw.slice(0, 80)}" is no sandbox host or domain`);
178
+ }
179
+ const record = await recordFor(address, io);
180
+ if (record !== undefined && "ok" in record) {
181
+ return record;
182
+ }
183
+ const host = record?.host ?? address;
184
+ const fetched = await io.fetchIdentity(host);
185
+ if (!fetched.ok) {
186
+ return fetched.status === 401 ? refused("too_old", `${host} answers no s2s/1: its daemon is from before it`) : refused("unreachable", `${host} did not answer: ${fetched.error}`);
187
+ }
188
+ const parsed = SandboxIdentitySchema.safeParse(fetched.body);
189
+ if (!parsed.success) {
190
+ return refused("malformed", `${host} answered something that is no identity document`);
191
+ }
192
+ return documentProblem(parsed.data, address, host, record) ?? { ok: true, address, host, document: parsed.data, txt: record };
193
+ };