@emilia-protocol/gate 0.10.0 → 0.13.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 +91 -0
- package/README.md +272 -0
- package/action-control-manifest.js +3 -268
- package/action-escrow-custodian.js +4 -0
- package/action-escrow-evidence.js +4 -0
- package/action-escrow-package.js +4 -0
- package/action-escrow-postgres.js +4 -0
- package/action-escrow-state.js +4 -0
- package/action-escrow-verifiers.js +4 -0
- package/action-escrow.js +4 -0
- package/action-packs.js +3 -140
- package/adapters/_kit.js +3 -104
- package/adapters/aws.js +3 -108
- package/adapters/cloudflare.js +3 -54
- package/adapters/gcp.js +3 -70
- package/adapters/github.js +3 -109
- package/adapters/jira.js +3 -55
- package/adapters/k8s.js +3 -70
- package/adapters/linear.js +3 -55
- package/adapters/salesforce.js +3 -55
- package/adapters/stripe.js +3 -81
- package/adapters/supabase.js +3 -121
- package/adapters/terraform.js +3 -59
- package/adapters/vercel.js +3 -84
- package/aec-execution.js +3 -312
- package/breakglass.js +3 -582
- package/capability-receipt.js +4 -0
- package/cf1-conformance.js +3 -106
- package/challenge-store.js +3 -69
- package/control-plane.js +3 -340
- package/coverage.js +3 -721
- package/demo.mjs +1 -1
- package/deployment-attestation.js +3 -247
- package/dist/action-control-manifest.d.ts +82 -0
- package/dist/action-control-manifest.d.ts.map +1 -0
- package/dist/action-control-manifest.js +422 -0
- package/dist/action-control-manifest.js.map +1 -0
- package/dist/action-escrow-custodian.d.ts +74 -0
- package/dist/action-escrow-custodian.d.ts.map +1 -0
- package/dist/action-escrow-custodian.js +363 -0
- package/dist/action-escrow-custodian.js.map +1 -0
- package/dist/action-escrow-evidence.d.ts +130 -0
- package/dist/action-escrow-evidence.d.ts.map +1 -0
- package/dist/action-escrow-evidence.js +1012 -0
- package/dist/action-escrow-evidence.js.map +1 -0
- package/dist/action-escrow-package.d.ts +10 -0
- package/dist/action-escrow-package.d.ts.map +1 -0
- package/dist/action-escrow-package.js +1583 -0
- package/dist/action-escrow-package.js.map +1 -0
- package/dist/action-escrow-postgres.d.ts +62 -0
- package/dist/action-escrow-postgres.d.ts.map +1 -0
- package/dist/action-escrow-postgres.js +339 -0
- package/dist/action-escrow-postgres.js.map +1 -0
- package/dist/action-escrow-state.d.ts +141 -0
- package/dist/action-escrow-state.d.ts.map +1 -0
- package/dist/action-escrow-state.js +413 -0
- package/dist/action-escrow-state.js.map +1 -0
- package/dist/action-escrow-verifiers.d.ts +65 -0
- package/dist/action-escrow-verifiers.d.ts.map +1 -0
- package/dist/action-escrow-verifiers.js +421 -0
- package/dist/action-escrow-verifiers.js.map +1 -0
- package/dist/action-escrow.d.ts +112 -0
- package/dist/action-escrow.d.ts.map +1 -0
- package/dist/action-escrow.js +2661 -0
- package/dist/action-escrow.js.map +1 -0
- package/dist/action-packs.d.ts +54 -0
- package/dist/action-packs.d.ts.map +1 -0
- package/dist/action-packs.js +143 -0
- package/dist/action-packs.js.map +1 -0
- package/dist/adapters/_kit.d.ts +50 -0
- package/dist/adapters/_kit.d.ts.map +1 -0
- package/dist/adapters/_kit.js +98 -0
- package/dist/adapters/_kit.js.map +1 -0
- package/dist/adapters/aws.d.ts +158 -0
- package/dist/adapters/aws.d.ts.map +1 -0
- package/dist/adapters/aws.js +136 -0
- package/dist/adapters/aws.js.map +1 -0
- package/dist/adapters/cloudflare.d.ts +116 -0
- package/dist/adapters/cloudflare.d.ts.map +1 -0
- package/dist/adapters/cloudflare.js +75 -0
- package/dist/adapters/cloudflare.js.map +1 -0
- package/dist/adapters/gcp.d.ts +139 -0
- package/dist/adapters/gcp.d.ts.map +1 -0
- package/dist/adapters/gcp.js +69 -0
- package/dist/adapters/gcp.js.map +1 -0
- package/dist/adapters/github.d.ts +123 -0
- package/dist/adapters/github.d.ts.map +1 -0
- package/dist/adapters/github.js +105 -0
- package/dist/adapters/github.js.map +1 -0
- package/dist/adapters/jira.d.ts +109 -0
- package/dist/adapters/jira.d.ts.map +1 -0
- package/dist/adapters/jira.js +54 -0
- package/dist/adapters/jira.js.map +1 -0
- package/dist/adapters/k8s.d.ts +139 -0
- package/dist/adapters/k8s.d.ts.map +1 -0
- package/dist/adapters/k8s.js +69 -0
- package/dist/adapters/k8s.js.map +1 -0
- package/dist/adapters/linear.d.ts +109 -0
- package/dist/adapters/linear.d.ts.map +1 -0
- package/dist/adapters/linear.js +54 -0
- package/dist/adapters/linear.js.map +1 -0
- package/dist/adapters/salesforce.d.ts +109 -0
- package/dist/adapters/salesforce.d.ts.map +1 -0
- package/dist/adapters/salesforce.js +54 -0
- package/dist/adapters/salesforce.js.map +1 -0
- package/dist/adapters/stripe.d.ts +116 -0
- package/dist/adapters/stripe.d.ts.map +1 -0
- package/dist/adapters/stripe.js +77 -0
- package/dist/adapters/stripe.js.map +1 -0
- package/dist/adapters/supabase.d.ts +127 -0
- package/dist/adapters/supabase.d.ts.map +1 -0
- package/dist/adapters/supabase.js +112 -0
- package/dist/adapters/supabase.js.map +1 -0
- package/dist/adapters/terraform.d.ts +109 -0
- package/dist/adapters/terraform.d.ts.map +1 -0
- package/dist/adapters/terraform.js +58 -0
- package/dist/adapters/terraform.js.map +1 -0
- package/dist/adapters/vercel.d.ts +114 -0
- package/dist/adapters/vercel.d.ts.map +1 -0
- package/dist/adapters/vercel.js +81 -0
- package/dist/adapters/vercel.js.map +1 -0
- package/dist/aec-execution.d.ts +107 -0
- package/dist/aec-execution.d.ts.map +1 -0
- package/dist/aec-execution.js +334 -0
- package/dist/aec-execution.js.map +1 -0
- package/dist/breakglass.d.ts +205 -0
- package/dist/breakglass.d.ts.map +1 -0
- package/dist/breakglass.js +627 -0
- package/dist/breakglass.js.map +1 -0
- package/dist/capability-receipt.d.ts +601 -0
- package/dist/capability-receipt.d.ts.map +1 -0
- package/dist/capability-receipt.js +1177 -0
- package/dist/capability-receipt.js.map +1 -0
- package/dist/cf1-conformance.d.ts +83 -0
- package/dist/cf1-conformance.d.ts.map +1 -0
- package/dist/cf1-conformance.js +94 -0
- package/dist/cf1-conformance.js.map +1 -0
- package/dist/challenge-store.d.ts +20 -0
- package/dist/challenge-store.d.ts.map +1 -0
- package/dist/challenge-store.js +62 -0
- package/dist/challenge-store.js.map +1 -0
- package/dist/control-plane.d.ts +136 -0
- package/dist/control-plane.d.ts.map +1 -0
- package/dist/control-plane.js +349 -0
- package/dist/control-plane.js.map +1 -0
- package/dist/coverage.d.ts +156 -0
- package/dist/coverage.d.ts.map +1 -0
- package/dist/coverage.js +768 -0
- package/dist/coverage.js.map +1 -0
- package/dist/deployment-attestation.d.ts +93 -0
- package/dist/deployment-attestation.d.ts.map +1 -0
- package/dist/deployment-attestation.js +262 -0
- package/dist/deployment-attestation.js.map +1 -0
- package/dist/eg1-conformance.d.ts +286 -0
- package/dist/eg1-conformance.d.ts.map +1 -0
- package/dist/eg1-conformance.js +379 -0
- package/dist/eg1-conformance.js.map +1 -0
- package/dist/enterprise.d.ts +88 -0
- package/dist/enterprise.d.ts.map +1 -0
- package/dist/enterprise.js +194 -0
- package/dist/enterprise.js.map +1 -0
- package/dist/evidence-postgres.d.ts +123 -0
- package/dist/evidence-postgres.d.ts.map +1 -0
- package/dist/evidence-postgres.js +343 -0
- package/dist/evidence-postgres.js.map +1 -0
- package/dist/evidence.d.ts +113 -0
- package/dist/evidence.d.ts.map +1 -0
- package/dist/evidence.js +369 -0
- package/dist/evidence.js.map +1 -0
- package/dist/execution-binding.d.ts +49 -0
- package/dist/execution-binding.d.ts.map +1 -0
- package/dist/execution-binding.js +243 -0
- package/dist/execution-binding.js.map +1 -0
- package/dist/formal-runtime-map.d.ts +42 -0
- package/dist/formal-runtime-map.d.ts.map +1 -0
- package/dist/formal-runtime-map.js +23 -0
- package/dist/formal-runtime-map.js.map +1 -0
- package/dist/index.d.ts +1002 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1736 -0
- package/dist/index.js.map +1 -0
- package/dist/key-registry.d.ts +60 -0
- package/dist/key-registry.d.ts.map +1 -0
- package/dist/key-registry.js +144 -0
- package/dist/key-registry.js.map +1 -0
- package/dist/mcp.d.ts +53 -0
- package/dist/mcp.d.ts.map +1 -0
- package/dist/mcp.js +112 -0
- package/dist/mcp.js.map +1 -0
- package/dist/metering.d.ts +80 -0
- package/dist/metering.d.ts.map +1 -0
- package/dist/metering.js +227 -0
- package/dist/metering.js.map +1 -0
- package/dist/metrics.d.ts +68 -0
- package/dist/metrics.d.ts.map +1 -0
- package/dist/metrics.js +242 -0
- package/dist/metrics.js.map +1 -0
- package/dist/network-witness.d.ts +167 -0
- package/dist/network-witness.d.ts.map +1 -0
- package/dist/network-witness.js +520 -0
- package/dist/network-witness.js.map +1 -0
- package/dist/protocol-state.d.ts +141 -0
- package/dist/protocol-state.d.ts.map +1 -0
- package/dist/protocol-state.js +106 -0
- package/dist/protocol-state.js.map +1 -0
- package/dist/receipt-program.d.ts +59 -0
- package/dist/receipt-program.d.ts.map +1 -0
- package/dist/receipt-program.js +1053 -0
- package/dist/receipt-program.js.map +1 -0
- package/dist/reliance-kernel.d.ts +66 -0
- package/dist/reliance-kernel.d.ts.map +1 -0
- package/dist/reliance-kernel.js +103 -0
- package/dist/reliance-kernel.js.map +1 -0
- package/dist/reliance-packet.d.ts +67 -0
- package/dist/reliance-packet.d.ts.map +1 -0
- package/dist/reliance-packet.js +176 -0
- package/dist/reliance-packet.js.map +1 -0
- package/dist/remedy-program-postgres.d.ts +37 -0
- package/dist/remedy-program-postgres.d.ts.map +1 -0
- package/dist/remedy-program-postgres.js +291 -0
- package/dist/remedy-program-postgres.js.map +1 -0
- package/dist/remedy-program-receipt.d.ts +97 -0
- package/dist/remedy-program-receipt.d.ts.map +1 -0
- package/dist/remedy-program-receipt.js +817 -0
- package/dist/remedy-program-receipt.js.map +1 -0
- package/dist/remedy-program.d.ts +76 -0
- package/dist/remedy-program.d.ts.map +1 -0
- package/dist/remedy-program.js +1346 -0
- package/dist/remedy-program.js.map +1 -0
- package/dist/reports/art14.d.ts +101 -0
- package/dist/reports/art14.d.ts.map +1 -0
- package/dist/reports/art14.js +340 -0
- package/dist/reports/art14.js.map +1 -0
- package/dist/reports/assurance-package.d.ts +197 -0
- package/dist/reports/assurance-package.d.ts.map +1 -0
- package/dist/reports/assurance-package.js +313 -0
- package/dist/reports/assurance-package.js.map +1 -0
- package/dist/reports/auditor-workpaper.d.ts +194 -0
- package/dist/reports/auditor-workpaper.d.ts.map +1 -0
- package/dist/reports/auditor-workpaper.js +545 -0
- package/dist/reports/auditor-workpaper.js.map +1 -0
- package/dist/reports/external-verification.d.ts +76 -0
- package/dist/reports/external-verification.d.ts.map +1 -0
- package/dist/reports/external-verification.js +209 -0
- package/dist/reports/external-verification.js.map +1 -0
- package/dist/reports/reperform.d.ts +122 -0
- package/dist/reports/reperform.d.ts.map +1 -0
- package/dist/reports/reperform.js +432 -0
- package/dist/reports/reperform.js.map +1 -0
- package/dist/reports/underwriter.d.ts +127 -0
- package/dist/reports/underwriter.d.ts.map +1 -0
- package/dist/reports/underwriter.js +348 -0
- package/dist/reports/underwriter.js.map +1 -0
- package/dist/retention.d.ts +88 -0
- package/dist/retention.d.ts.map +1 -0
- package/dist/retention.js +90 -0
- package/dist/retention.js.map +1 -0
- package/dist/roster.d.ts +113 -0
- package/dist/roster.d.ts.map +1 -0
- package/dist/roster.js +261 -0
- package/dist/roster.js.map +1 -0
- package/dist/runtime-monitor.d.ts +132 -0
- package/dist/runtime-monitor.d.ts.map +1 -0
- package/dist/runtime-monitor.js +197 -0
- package/dist/runtime-monitor.js.map +1 -0
- package/dist/settlement.d.ts +68 -0
- package/dist/settlement.d.ts.map +1 -0
- package/dist/settlement.js +306 -0
- package/dist/settlement.js.map +1 -0
- package/dist/siem.d.ts +261 -0
- package/dist/siem.d.ts.map +1 -0
- package/dist/siem.js +247 -0
- package/dist/siem.js.map +1 -0
- package/dist/store-postgres.d.ts +107 -0
- package/dist/store-postgres.d.ts.map +1 -0
- package/dist/store-postgres.js +152 -0
- package/dist/store-postgres.js.map +1 -0
- package/dist/store.d.ts +95 -0
- package/dist/store.d.ts.map +1 -0
- package/dist/store.js +202 -0
- package/dist/store.js.map +1 -0
- package/dist/strict-json.d.ts +14 -0
- package/dist/strict-json.d.ts.map +1 -0
- package/dist/strict-json.js +132 -0
- package/dist/strict-json.js.map +1 -0
- package/dist/trust-program-adapters.d.ts +122 -0
- package/dist/trust-program-adapters.d.ts.map +1 -0
- package/dist/trust-program-adapters.js +802 -0
- package/dist/trust-program-adapters.js.map +1 -0
- package/dist/trust-program-postgres.d.ts +41 -0
- package/dist/trust-program-postgres.d.ts.map +1 -0
- package/dist/trust-program-postgres.js +346 -0
- package/dist/trust-program-postgres.js.map +1 -0
- package/dist/trust-program-revocation.d.ts +206 -0
- package/dist/trust-program-revocation.d.ts.map +1 -0
- package/dist/trust-program-revocation.js +600 -0
- package/dist/trust-program-revocation.js.map +1 -0
- package/dist/trust-program.d.ts +197 -0
- package/dist/trust-program.d.ts.map +1 -0
- package/dist/trust-program.js +1351 -0
- package/dist/trust-program.js.map +1 -0
- package/dist/witness-postgres.d.ts +43 -0
- package/dist/witness-postgres.d.ts.map +1 -0
- package/dist/witness-postgres.js +92 -0
- package/dist/witness-postgres.js.map +1 -0
- package/dist/zk-range-proof.d.ts +78 -0
- package/dist/zk-range-proof.d.ts.map +1 -0
- package/dist/zk-range-proof.js +228 -0
- package/dist/zk-range-proof.js.map +1 -0
- package/eg1-conformance.js +3 -404
- package/enterprise.js +3 -180
- package/ep-assure.mjs +1 -0
- package/evidence-postgres.js +3 -356
- package/evidence.js +4 -360
- package/execution-binding.js +3 -212
- package/formal-runtime-map.js +4 -0
- package/index.js +4 -1249
- package/key-registry.js +3 -134
- package/mcp.js +3 -109
- package/metering.js +3 -226
- package/metrics.js +3 -231
- package/network-witness.js +3 -499
- package/package.json +47 -4
- package/receipt-program.js +4 -0
- package/reliance-kernel.js +3 -112
- package/reliance-packet.js +3 -175
- package/remedy-program-postgres.js +4 -0
- package/remedy-program-receipt.js +4 -0
- package/remedy-program.js +4 -0
- package/reports/art14.js +0 -0
- package/reports/assurance-package.js +2 -312
- package/reports/auditor-workpaper.js +3 -537
- package/reports/external-verification.js +3 -222
- package/reports/reperform.js +3 -424
- package/reports/underwriter.js +3 -339
- package/retention.js +3 -84
- package/roster.js +3 -264
- package/runtime-monitor.js +4 -0
- package/settlement.js +3 -299
- package/siem.js +3 -235
- package/src/action-control-manifest.ts +424 -0
- package/src/action-escrow-custodian.ts +427 -0
- package/src/action-escrow-evidence.ts +1178 -0
- package/src/action-escrow-package.ts +1821 -0
- package/src/action-escrow-postgres.ts +366 -0
- package/src/action-escrow-state.ts +498 -0
- package/src/action-escrow-verifiers.ts +484 -0
- package/src/action-escrow.ts +3039 -0
- package/src/action-packs.ts +170 -0
- package/src/adapters/_kit.ts +117 -0
- package/src/adapters/aws.ts +142 -0
- package/src/adapters/cloudflare.ts +78 -0
- package/src/adapters/gcp.ts +71 -0
- package/src/adapters/github.ts +110 -0
- package/src/adapters/jira.ts +56 -0
- package/src/adapters/k8s.ts +71 -0
- package/src/adapters/linear.ts +56 -0
- package/src/adapters/salesforce.ts +56 -0
- package/src/adapters/stripe.ts +82 -0
- package/src/adapters/supabase.ts +122 -0
- package/src/adapters/terraform.ts +60 -0
- package/src/adapters/vercel.ts +85 -0
- package/src/aec-execution.ts +354 -0
- package/src/breakglass.ts +647 -0
- package/src/capability-receipt.ts +1258 -0
- package/src/cf1-conformance.ts +120 -0
- package/src/challenge-store.ts +70 -0
- package/src/control-plane.ts +374 -0
- package/src/coverage.ts +832 -0
- package/src/deployment-attestation.ts +259 -0
- package/src/eg1-conformance.ts +471 -0
- package/src/enterprise.ts +195 -0
- package/src/evidence-postgres.ts +374 -0
- package/src/evidence.ts +367 -0
- package/src/execution-binding.ts +236 -0
- package/src/formal-runtime-map.ts +24 -0
- package/src/index.ts +2122 -0
- package/src/key-registry.ts +150 -0
- package/src/mcp.ts +120 -0
- package/src/metering.ts +237 -0
- package/src/metrics.ts +232 -0
- package/src/network-witness.ts +585 -0
- package/src/protocol-state.ts +143 -0
- package/src/protocol-state.typecheck.ts +94 -0
- package/src/receipt-program.ts +1105 -0
- package/src/reliance-kernel.ts +119 -0
- package/src/reliance-packet.ts +206 -0
- package/src/remedy-program-postgres.ts +367 -0
- package/src/remedy-program-receipt.ts +891 -0
- package/src/remedy-program.ts +1388 -0
- package/src/reports/art14.ts +361 -0
- package/src/reports/assurance-package.ts +330 -0
- package/src/reports/auditor-workpaper.ts +558 -0
- package/src/reports/external-verification.ts +232 -0
- package/src/reports/reperform.ts +441 -0
- package/src/reports/underwriter.ts +356 -0
- package/src/retention.ts +100 -0
- package/src/roster.ts +273 -0
- package/src/runtime-monitor.ts +217 -0
- package/src/settlement.ts +313 -0
- package/src/siem.ts +247 -0
- package/src/store-postgres.ts +167 -0
- package/src/store.ts +214 -0
- package/src/strict-json.ts +115 -0
- package/src/trust-program-adapters.ts +847 -0
- package/src/trust-program-postgres.ts +429 -0
- package/src/trust-program-revocation.ts +751 -0
- package/src/trust-program.ts +1460 -0
- package/src/witness-postgres.ts +106 -0
- package/src/zk-range-proof.ts +250 -0
- package/store-postgres.js +3 -161
- package/store.js +4 -198
- package/strict-json.js +3 -85
- package/trust-program-adapters.js +4 -0
- package/trust-program-postgres.js +4 -0
- package/trust-program-revocation.js +4 -0
- package/trust-program.js +4 -0
- package/witness-postgres.js +3 -96
- package/zk-range-proof.js +4 -0
package/evidence.js
CHANGED
|
@@ -1,360 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
*
|
|
4
|
-
|
|
5
|
-
* Every gate decision — allow or deny — appends a hash-chained record. Each
|
|
6
|
-
* record commits to the previous one, so removing or altering any decision
|
|
7
|
-
* breaks the chain and `verify()` catches it. This is the compliance and
|
|
8
|
-
* insurance artifact: a provable account of exactly which consequential actions
|
|
9
|
-
* were authorized, refused, and why. Default sink is in-memory; pass `sink` to
|
|
10
|
-
* stream records to storage. This local chain detects alteration only when the
|
|
11
|
-
* verifier receives its complete history from genesis; a sink alone cannot
|
|
12
|
-
* prevent truncation, restart-from-genesis, or cross-replica forks. Use
|
|
13
|
-
* createAtomicEvidenceLog for a shared production head.
|
|
14
|
-
*
|
|
15
|
-
* Hashing is over CANONICAL JSON (sorted keys) so the same logical record hashes
|
|
16
|
-
* identically across systems and languages — a plain JSON.stringify is
|
|
17
|
-
* insertion-order dependent and would make cross-system verification fragile.
|
|
18
|
-
*
|
|
19
|
-
* `strict` mode makes the log fail CLOSED: if a durable `sink` write fails, the
|
|
20
|
-
* record is NOT appended and `record()` throws. The gate uses this so it never
|
|
21
|
-
* authorizes an action it cannot durably account for. In non-strict (observe)
|
|
22
|
-
* mode a sink failure is best-effort and swallowed.
|
|
23
|
-
*/
|
|
24
|
-
import crypto from 'node:crypto';
|
|
25
|
-
|
|
26
|
-
function sha256hex(s) {
|
|
27
|
-
return crypto.createHash('sha256').update(s).digest('hex');
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/** Canonical JSON (recursive sorted keys) — matches @emilia-protocol/verify. */
|
|
31
|
-
export function canonicalEvidenceJson(v) {
|
|
32
|
-
if (v === null || v === undefined) return JSON.stringify(v);
|
|
33
|
-
if (Array.isArray(v)) return `[${v.map(canonicalEvidenceJson).join(',')}]`;
|
|
34
|
-
if (typeof v === 'object') {
|
|
35
|
-
return `{${Object.keys(v).sort().map((k) => JSON.stringify(k) + ':' + canonicalEvidenceJson(v[k])).join(',')}}`;
|
|
36
|
-
}
|
|
37
|
-
return JSON.stringify(v);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const canonical = canonicalEvidenceJson;
|
|
41
|
-
|
|
42
|
-
export function createEvidenceLog({ sink, strict = false } = {}) {
|
|
43
|
-
const records = [];
|
|
44
|
-
let prev = 'genesis';
|
|
45
|
-
|
|
46
|
-
return {
|
|
47
|
-
// A sink can persist this process-local chain, but it cannot make the head
|
|
48
|
-
// atomic across replicas or continue it after restart. Never advertise the
|
|
49
|
-
// local logger as a fleet-safe production ledger.
|
|
50
|
-
durable: false,
|
|
51
|
-
persisted: typeof sink === 'function' && strict === true,
|
|
52
|
-
strict: strict === true,
|
|
53
|
-
forkAware: false,
|
|
54
|
-
atomicAppend: false,
|
|
55
|
-
async record(entry) {
|
|
56
|
-
// Deep-copy the caller's entry: a shallow spread embeds live references to
|
|
57
|
-
// nested objects, so a caller mutating them after record() would silently
|
|
58
|
-
// corrupt the hash-chained evidence record (and anything a sink persisted).
|
|
59
|
-
const snapshot = entry && typeof entry === 'object' ? structuredClone(entry) : entry;
|
|
60
|
-
const body = { seq: records.length, prev_hash: prev, ...snapshot };
|
|
61
|
-
const hash = sha256hex(canonical(body));
|
|
62
|
-
const rec = { ...body, hash };
|
|
63
|
-
if (sink) {
|
|
64
|
-
try {
|
|
65
|
-
await sink(rec);
|
|
66
|
-
} catch (e) {
|
|
67
|
-
// Fail closed in strict mode: do NOT advance the chain, surface the
|
|
68
|
-
// failure so the caller (the gate) can refuse the action. The seq is
|
|
69
|
-
// left unconsumed so a retry produces a consistent chain.
|
|
70
|
-
if (strict) {
|
|
71
|
-
const err = new Error('evidence_sink_failed');
|
|
72
|
-
err.cause = e;
|
|
73
|
-
throw err;
|
|
74
|
-
}
|
|
75
|
-
/* non-strict (observe): best-effort, swallow — never break the gate */
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
records.push(rec);
|
|
79
|
-
prev = hash;
|
|
80
|
-
return rec;
|
|
81
|
-
},
|
|
82
|
-
|
|
83
|
-
all() {
|
|
84
|
-
return records.slice();
|
|
85
|
-
},
|
|
86
|
-
|
|
87
|
-
/** Recompute the chain; detects any altered or removed record. */
|
|
88
|
-
verify() {
|
|
89
|
-
let p = 'genesis';
|
|
90
|
-
for (const r of records) {
|
|
91
|
-
const { hash, ...body } = r;
|
|
92
|
-
if (body.prev_hash !== p) return { ok: false, at: r.seq, reason: 'prev_hash_mismatch' };
|
|
93
|
-
if (sha256hex(canonical(body)) !== hash) return { ok: false, at: r.seq, reason: 'hash_mismatch' };
|
|
94
|
-
p = hash;
|
|
95
|
-
}
|
|
96
|
-
return { ok: true, length: records.length, head: p === 'genesis' ? null : p };
|
|
97
|
-
},
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
const LOG_HEX_256 = /^[0-9a-f]{64}$/;
|
|
102
|
-
const LOG_RESERVED_FIELDS = new Set(['seq', 'prev_hash', 'record_id', 'hash']);
|
|
103
|
-
|
|
104
|
-
/** Verify one logger acknowledgement independently of the logger that emitted it. */
|
|
105
|
-
export function verifyEvidenceRecord(record, { atomicRequired = false, expectedEntry } = {}) {
|
|
106
|
-
try {
|
|
107
|
-
if (!record || typeof record !== 'object' || Array.isArray(record)
|
|
108
|
-
|| !Number.isSafeInteger(record.seq) || record.seq < 0
|
|
109
|
-
|| (record.prev_hash !== 'genesis' && !LOG_HEX_256.test(record.prev_hash))
|
|
110
|
-
|| !LOG_HEX_256.test(record.hash)) return false;
|
|
111
|
-
const hasRecordId = Object.prototype.hasOwnProperty.call(record, 'record_id');
|
|
112
|
-
if ((atomicRequired && !hasRecordId)
|
|
113
|
-
|| (hasRecordId && (typeof record.record_id !== 'string'
|
|
114
|
-
|| record.record_id.length < 16 || record.record_id.length > 256))) return false;
|
|
115
|
-
const { hash, ...body } = record;
|
|
116
|
-
if (sha256hex(canonical(body)) !== hash) return false;
|
|
117
|
-
if (expectedEntry !== undefined) {
|
|
118
|
-
if (!expectedEntry || typeof expectedEntry !== 'object' || Array.isArray(expectedEntry)) return false;
|
|
119
|
-
const entry = Object.fromEntries(
|
|
120
|
-
Object.entries(record).filter(([key]) => !LOG_RESERVED_FIELDS.has(key)),
|
|
121
|
-
);
|
|
122
|
-
if (canonical(entry) !== canonical(expectedEntry)) return false;
|
|
123
|
-
}
|
|
124
|
-
return true;
|
|
125
|
-
} catch {
|
|
126
|
-
return false;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
function assertLogEntry(entry) {
|
|
131
|
-
if (!entry || typeof entry !== 'object' || Array.isArray(entry)) {
|
|
132
|
-
throw new Error('atomic evidence entry must be an object');
|
|
133
|
-
}
|
|
134
|
-
for (const field of LOG_RESERVED_FIELDS) {
|
|
135
|
-
if (Object.prototype.hasOwnProperty.call(entry, field)) {
|
|
136
|
-
throw new Error(`atomic evidence entry must not supply reserved field ${field}`);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
const stack = [{ value: entry, depth: 0 }];
|
|
140
|
-
const seen = new WeakSet();
|
|
141
|
-
let nodes = 0;
|
|
142
|
-
let stringBytes = 0;
|
|
143
|
-
while (stack.length) {
|
|
144
|
-
const { value, depth } = stack.pop();
|
|
145
|
-
if (++nodes > 50000 || depth > 64) throw new Error('atomic evidence entry exceeds resource limits');
|
|
146
|
-
if (value === null || typeof value === 'boolean') continue;
|
|
147
|
-
if (typeof value === 'number') {
|
|
148
|
-
if (!Number.isSafeInteger(value)) throw new Error('atomic evidence entry contains a non-safe integer');
|
|
149
|
-
continue;
|
|
150
|
-
}
|
|
151
|
-
if (typeof value === 'string') {
|
|
152
|
-
stringBytes += Buffer.byteLength(value, 'utf8');
|
|
153
|
-
if (stringBytes > 1024 * 1024) throw new Error('atomic evidence entry exceeds string limit');
|
|
154
|
-
continue;
|
|
155
|
-
}
|
|
156
|
-
if (typeof value !== 'object') throw new Error('atomic evidence entry is not canonical JSON');
|
|
157
|
-
if (seen.has(value)) throw new Error('atomic evidence entry contains a cycle or alias');
|
|
158
|
-
seen.add(value);
|
|
159
|
-
if (Array.isArray(value)) {
|
|
160
|
-
for (const child of value) stack.push({ value: child, depth: depth + 1 });
|
|
161
|
-
} else {
|
|
162
|
-
for (const [key, child] of Object.entries(value)) {
|
|
163
|
-
stringBytes += Buffer.byteLength(key, 'utf8');
|
|
164
|
-
if (stringBytes > 1024 * 1024) throw new Error('atomic evidence entry exceeds string limit');
|
|
165
|
-
stack.push({ value: child, depth: depth + 1 });
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
function validateAtomicRecord(record, expectedId, expectedEntry, expectedRecord = null) {
|
|
172
|
-
return record?.record_id === expectedId
|
|
173
|
-
&& verifyEvidenceRecord(record, { atomicRequired: true, expectedEntry })
|
|
174
|
-
&& (expectedRecord === null || canonical(record) === canonical(expectedRecord));
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
function validHead(head) {
|
|
178
|
-
return head === null || Boolean(head && Number.isSafeInteger(head.seq) && head.seq >= 0 && LOG_HEX_256.test(head.hash));
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
/**
|
|
182
|
-
* Fleet-safe, fail-closed evidence log over an atomic shared-head backend.
|
|
183
|
-
*
|
|
184
|
-
* Backend contract (all operations are scoped by streamId):
|
|
185
|
-
* readHead(streamId) -> null | { seq, hash }
|
|
186
|
-
* getById(streamId, recordId) -> null | record
|
|
187
|
-
* appendIfHead(streamId, expectedHeadHash|null, record) -> boolean
|
|
188
|
-
* readAll(streamId) -> record[] // optional, for verify/all
|
|
189
|
-
*
|
|
190
|
-
* appendIfHead MUST atomically compare the current head, append the immutable
|
|
191
|
-
* record, reject duplicate record_id, and advance the head in one durable
|
|
192
|
-
* transaction. A true return MUST provide immediate read-after-write visibility
|
|
193
|
-
* through getById. `backend.durable === true` is a deployment capability
|
|
194
|
-
* assertion; this module tests the protocol but cannot prove storage hardware
|
|
195
|
-
* semantics.
|
|
196
|
-
*/
|
|
197
|
-
export function createAtomicEvidenceLog(backend, {
|
|
198
|
-
streamId = 'emilia-gate',
|
|
199
|
-
maxRetries = 32,
|
|
200
|
-
recordIdFactory = () => crypto.randomUUID(),
|
|
201
|
-
} = {}) {
|
|
202
|
-
for (const method of ['readHead', 'getById', 'appendIfHead']) {
|
|
203
|
-
if (typeof backend?.[method] !== 'function') {
|
|
204
|
-
throw new Error(`createAtomicEvidenceLog: backend must implement async ${method}()`);
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
if (typeof streamId !== 'string' || !streamId || streamId.length > 256) {
|
|
208
|
-
throw new Error('createAtomicEvidenceLog: streamId must be a non-empty string of at most 256 characters');
|
|
209
|
-
}
|
|
210
|
-
if (!Number.isSafeInteger(maxRetries) || maxRetries < 1 || maxRetries > 1024) {
|
|
211
|
-
throw new Error('createAtomicEvidenceLog: maxRetries must be an integer from 1 to 1024');
|
|
212
|
-
}
|
|
213
|
-
if (typeof recordIdFactory !== 'function') {
|
|
214
|
-
throw new Error('createAtomicEvidenceLog: recordIdFactory must be a function');
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
async function recover(recordId, snapshot, expectedRecord = null) {
|
|
218
|
-
const existing = await backend.getById(streamId, recordId);
|
|
219
|
-
if (existing === null || existing === undefined) return null;
|
|
220
|
-
if (!validateAtomicRecord(existing, recordId, snapshot, expectedRecord)) {
|
|
221
|
-
throw new Error('atomic evidence backend returned a conflicting record_id');
|
|
222
|
-
}
|
|
223
|
-
return structuredClone(existing);
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
return {
|
|
227
|
-
durable: backend.durable === true,
|
|
228
|
-
persisted: backend.durable === true,
|
|
229
|
-
strict: true,
|
|
230
|
-
forkAware: true,
|
|
231
|
-
atomicAppend: true,
|
|
232
|
-
streamId,
|
|
233
|
-
|
|
234
|
-
async health() {
|
|
235
|
-
if (typeof backend.health !== 'function') return { ok: false, reason: 'backend_health_unavailable' };
|
|
236
|
-
return backend.health();
|
|
237
|
-
},
|
|
238
|
-
|
|
239
|
-
async record(entry) {
|
|
240
|
-
const snapshot = structuredClone(entry);
|
|
241
|
-
assertLogEntry(snapshot);
|
|
242
|
-
const recordId = recordIdFactory();
|
|
243
|
-
if (typeof recordId !== 'string' || recordId.length < 16 || recordId.length > 256) {
|
|
244
|
-
throw new Error('atomic evidence record id must be a string of 16 to 256 characters');
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
for (let attempt = 0; attempt < maxRetries; attempt++) {
|
|
248
|
-
const recovered = await recover(recordId, snapshot);
|
|
249
|
-
if (recovered) return recovered;
|
|
250
|
-
|
|
251
|
-
const head = await backend.readHead(streamId);
|
|
252
|
-
if (!validHead(head)) throw new Error('atomic evidence backend returned a malformed head');
|
|
253
|
-
const seq = head === null ? 0 : head.seq + 1;
|
|
254
|
-
const prevHash = head === null ? 'genesis' : head.hash;
|
|
255
|
-
const body = { seq, prev_hash: prevHash, record_id: recordId, ...snapshot };
|
|
256
|
-
const record = { ...body, hash: sha256hex(canonical(body)) };
|
|
257
|
-
try {
|
|
258
|
-
if ((await backend.appendIfHead(streamId, head?.hash ?? null, record)) === true) {
|
|
259
|
-
const persisted = await recover(recordId, snapshot, record);
|
|
260
|
-
if (persisted) return persisted;
|
|
261
|
-
throw new Error('atomic_evidence_append_not_observable');
|
|
262
|
-
}
|
|
263
|
-
} catch (error) {
|
|
264
|
-
// The append may have linearized before its response was lost. Recover
|
|
265
|
-
// by stable id; if no record is visible, fail closed and let the caller
|
|
266
|
-
// retain/freeze its execution reservation.
|
|
267
|
-
const afterError = await recover(recordId, snapshot, record);
|
|
268
|
-
if (afterError) return afterError;
|
|
269
|
-
const wrapped = new Error('atomic_evidence_append_indeterminate');
|
|
270
|
-
wrapped.cause = error;
|
|
271
|
-
throw wrapped;
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
throw new Error('atomic_evidence_contention_limit');
|
|
275
|
-
},
|
|
276
|
-
|
|
277
|
-
async all() {
|
|
278
|
-
if (typeof backend.readAll !== 'function') throw new Error('atomic evidence backend does not expose readAll()');
|
|
279
|
-
const records = await backend.readAll(streamId);
|
|
280
|
-
if (!Array.isArray(records)) throw new Error('atomic evidence backend returned malformed history');
|
|
281
|
-
return structuredClone(records);
|
|
282
|
-
},
|
|
283
|
-
|
|
284
|
-
async verify() {
|
|
285
|
-
try {
|
|
286
|
-
if (typeof backend.readAll !== 'function') return { ok: false, reason: 'read_all_unavailable' };
|
|
287
|
-
const records = await backend.readAll(streamId);
|
|
288
|
-
if (!Array.isArray(records)) return { ok: false, reason: 'malformed_history' };
|
|
289
|
-
let prev = 'genesis';
|
|
290
|
-
const ids = new Set();
|
|
291
|
-
for (let index = 0; index < records.length; index++) {
|
|
292
|
-
const record = records[index];
|
|
293
|
-
if (!record || typeof record !== 'object' || Array.isArray(record)
|
|
294
|
-
|| record.seq !== index || record.prev_hash !== prev
|
|
295
|
-
|| typeof record.record_id !== 'string' || ids.has(record.record_id)) {
|
|
296
|
-
return { ok: false, at: index, reason: 'sequence_or_predecessor_mismatch' };
|
|
297
|
-
}
|
|
298
|
-
const { hash, ...body } = record;
|
|
299
|
-
if (!LOG_HEX_256.test(hash) || sha256hex(canonical(body)) !== hash) {
|
|
300
|
-
return { ok: false, at: index, reason: 'hash_mismatch' };
|
|
301
|
-
}
|
|
302
|
-
ids.add(record.record_id);
|
|
303
|
-
prev = hash;
|
|
304
|
-
}
|
|
305
|
-
const head = await backend.readHead(streamId);
|
|
306
|
-
const expectedHead = records.length === 0 ? null : { seq: records.length - 1, hash: prev };
|
|
307
|
-
if (canonical(head) !== canonical(expectedHead)) return { ok: false, reason: 'head_mismatch' };
|
|
308
|
-
return { ok: true, length: records.length, head: expectedHead?.hash ?? null };
|
|
309
|
-
} catch {
|
|
310
|
-
return { ok: false, reason: 'backend_read_failed_or_malformed' };
|
|
311
|
-
}
|
|
312
|
-
},
|
|
313
|
-
};
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
/** In-memory contract model for tests. It is intentionally not durable. */
|
|
317
|
-
export function createMemoryAtomicEvidenceBackend() {
|
|
318
|
-
const streams = new Map();
|
|
319
|
-
const state = (streamId) => {
|
|
320
|
-
if (!streams.has(streamId)) streams.set(streamId, { records: [], byId: new Map() });
|
|
321
|
-
return streams.get(streamId);
|
|
322
|
-
};
|
|
323
|
-
return {
|
|
324
|
-
durable: false,
|
|
325
|
-
async readHead(streamId) {
|
|
326
|
-
const records = state(streamId).records;
|
|
327
|
-
const record = records[records.length - 1];
|
|
328
|
-
return record ? { seq: record.seq, hash: record.hash } : null;
|
|
329
|
-
},
|
|
330
|
-
async getById(streamId, recordId) {
|
|
331
|
-
return structuredClone(state(streamId).byId.get(recordId) ?? null);
|
|
332
|
-
},
|
|
333
|
-
async appendIfHead(streamId, expectedHeadHash, record) {
|
|
334
|
-
const s = state(streamId);
|
|
335
|
-
const head = s.records[s.records.length - 1] ?? null;
|
|
336
|
-
const actual = head?.hash ?? null;
|
|
337
|
-
if (actual !== expectedHeadHash || s.byId.has(record.record_id)
|
|
338
|
-
|| record.seq !== s.records.length || record.prev_hash !== (head?.hash ?? 'genesis')) return false;
|
|
339
|
-
const snapshot = structuredClone(record);
|
|
340
|
-
s.records.push(snapshot);
|
|
341
|
-
s.byId.set(snapshot.record_id, snapshot);
|
|
342
|
-
return true;
|
|
343
|
-
},
|
|
344
|
-
async readAll(streamId) { return structuredClone(state(streamId).records); },
|
|
345
|
-
};
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
export const __atomicEvidenceSecurityInternals = Object.freeze({
|
|
349
|
-
canonical,
|
|
350
|
-
assertLogEntry,
|
|
351
|
-
validateAtomicRecord,
|
|
352
|
-
validHead,
|
|
353
|
-
});
|
|
354
|
-
|
|
355
|
-
export default {
|
|
356
|
-
createEvidenceLog,
|
|
357
|
-
verifyEvidenceRecord,
|
|
358
|
-
createAtomicEvidenceLog,
|
|
359
|
-
createMemoryAtomicEvidenceBackend,
|
|
360
|
-
};
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Compatibility entry point for the TypeScript evidence module.
|
|
3
|
+
export * from './dist/evidence.js';
|
|
4
|
+
export { default } from './dist/evidence.js';
|
package/execution-binding.js
CHANGED
|
@@ -1,213 +1,4 @@
|
|
|
1
1
|
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
-
//
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
import crypto from 'node:crypto';
|
|
6
|
-
|
|
7
|
-
export const EXECUTION_BINDING_VERSION = 'EP-GATE-EXECUTION-BINDING-v1';
|
|
8
|
-
|
|
9
|
-
const MAX_JSON_DEPTH = 64;
|
|
10
|
-
const MAX_JSON_NODES = 50000;
|
|
11
|
-
const MAX_JSON_STRING_BYTES = 1024 * 1024;
|
|
12
|
-
|
|
13
|
-
function validUnicodeString(value) {
|
|
14
|
-
for (let i = 0; i < value.length; i++) {
|
|
15
|
-
const unit = value.charCodeAt(i);
|
|
16
|
-
if (unit >= 0xd800 && unit <= 0xdbff) {
|
|
17
|
-
const next = value.charCodeAt(++i);
|
|
18
|
-
if (!(next >= 0xdc00 && next <= 0xdfff)) return false;
|
|
19
|
-
} else if (unit >= 0xdc00 && unit <= 0xdfff) return false;
|
|
20
|
-
}
|
|
21
|
-
return true;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function isPlainObject(value) {
|
|
25
|
-
if (value === null || typeof value !== 'object' || Array.isArray(value)) return false;
|
|
26
|
-
const prototype = Object.getPrototypeOf(value);
|
|
27
|
-
return prototype === Object.prototype || prototype === null;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Match the hardened EP canonical JSON safety profile: valid Unicode strings,
|
|
32
|
-
* booleans, null, safe integers, dense arrays, and plain data objects. Object
|
|
33
|
-
* identity is unique across the graph, so cycles and aliases both refuse.
|
|
34
|
-
*/
|
|
35
|
-
function assertCanonicalJson(value) {
|
|
36
|
-
const stack = [{ value, depth: 0 }];
|
|
37
|
-
const seen = new WeakSet();
|
|
38
|
-
let nodes = 0;
|
|
39
|
-
let stringBytes = 0;
|
|
40
|
-
|
|
41
|
-
while (stack.length) {
|
|
42
|
-
const current = stack.pop();
|
|
43
|
-
if (++nodes > MAX_JSON_NODES || current.depth > MAX_JSON_DEPTH) {
|
|
44
|
-
throw new TypeError('value exceeds the EP canonical JSON resource profile');
|
|
45
|
-
}
|
|
46
|
-
const v = current.value;
|
|
47
|
-
if (v === null || typeof v === 'boolean') continue;
|
|
48
|
-
if (typeof v === 'string') {
|
|
49
|
-
if (!validUnicodeString(v)) throw new TypeError('value contains invalid Unicode');
|
|
50
|
-
stringBytes += Buffer.byteLength(v, 'utf8');
|
|
51
|
-
if (stringBytes > MAX_JSON_STRING_BYTES) throw new TypeError('value exceeds the EP canonical JSON string limit');
|
|
52
|
-
continue;
|
|
53
|
-
}
|
|
54
|
-
if (typeof v === 'number') {
|
|
55
|
-
if (!Number.isSafeInteger(v)) throw new TypeError('value contains a non-safe integer');
|
|
56
|
-
continue;
|
|
57
|
-
}
|
|
58
|
-
if (!Array.isArray(v) && !isPlainObject(v)) {
|
|
59
|
-
throw new TypeError('value contains a non-plain JSON object');
|
|
60
|
-
}
|
|
61
|
-
if (seen.has(v)) throw new TypeError('value contains a cycle or alias');
|
|
62
|
-
seen.add(v);
|
|
63
|
-
|
|
64
|
-
if (Array.isArray(v)) {
|
|
65
|
-
const ownKeys = Reflect.ownKeys(v);
|
|
66
|
-
if (ownKeys.length !== v.length + 1 || !ownKeys.includes('length')) {
|
|
67
|
-
throw new TypeError('value contains a sparse or extended array');
|
|
68
|
-
}
|
|
69
|
-
for (let i = 0; i < v.length; i++) {
|
|
70
|
-
const descriptor = Object.getOwnPropertyDescriptor(v, String(i));
|
|
71
|
-
if (!descriptor || descriptor.enumerable !== true || !Object.hasOwn(descriptor, 'value')) {
|
|
72
|
-
throw new TypeError('value contains a sparse or accessor array');
|
|
73
|
-
}
|
|
74
|
-
stack.push({ value: descriptor.value, depth: current.depth + 1 });
|
|
75
|
-
}
|
|
76
|
-
continue;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
for (const key of Reflect.ownKeys(v)) {
|
|
80
|
-
if (typeof key !== 'string' || !validUnicodeString(key)) {
|
|
81
|
-
throw new TypeError('value contains a non-JSON object key');
|
|
82
|
-
}
|
|
83
|
-
const descriptor = Object.getOwnPropertyDescriptor(v, key);
|
|
84
|
-
if (!descriptor || descriptor.enumerable !== true || !Object.hasOwn(descriptor, 'value')) {
|
|
85
|
-
throw new TypeError('value contains a non-data JSON property');
|
|
86
|
-
}
|
|
87
|
-
stringBytes += Buffer.byteLength(key, 'utf8');
|
|
88
|
-
if (stringBytes > MAX_JSON_STRING_BYTES) throw new TypeError('value exceeds the EP canonical JSON string limit');
|
|
89
|
-
stack.push({ value: descriptor.value, depth: current.depth + 1 });
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
function canonicalizeValidated(v) {
|
|
95
|
-
if (v === null) return 'null';
|
|
96
|
-
if (Array.isArray(v)) return `[${v.map(canonicalizeValidated).join(',')}]`;
|
|
97
|
-
if (typeof v === 'object') {
|
|
98
|
-
return `{${Object.keys(v).sort().map((k) => JSON.stringify(k) + ':' + canonicalizeValidated(v[k])).join(',')}}`;
|
|
99
|
-
}
|
|
100
|
-
return JSON.stringify(v);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
export function canonicalize(v) {
|
|
104
|
-
assertCanonicalJson(v);
|
|
105
|
-
return canonicalizeValidated(v);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
export function hashCanonical(v) {
|
|
109
|
-
return crypto.createHash('sha256').update(canonicalize(v)).digest('hex');
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
function fieldValue(container, field) {
|
|
113
|
-
if (!isPlainObject(container)) return { state: 'invalid' };
|
|
114
|
-
const descriptor = Object.getOwnPropertyDescriptor(container, field);
|
|
115
|
-
if (!descriptor) return { state: 'missing' };
|
|
116
|
-
if (descriptor.enumerable !== true || !Object.hasOwn(descriptor, 'value')) return { state: 'invalid' };
|
|
117
|
-
if (descriptor.value === null) return { state: 'missing' };
|
|
118
|
-
try {
|
|
119
|
-
assertCanonicalJson(descriptor.value);
|
|
120
|
-
return { state: 'valid', value: descriptor.value };
|
|
121
|
-
} catch {
|
|
122
|
-
return { state: 'invalid' };
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
function equalValue(a, b) {
|
|
127
|
-
return canonicalize(a) === canonicalize(b);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
export function materialFieldsFor(requirement) {
|
|
131
|
-
const fields = requirement?.execution_binding?.required_fields;
|
|
132
|
-
return Array.isArray(fields) ? [...new Set(fields.filter(Boolean))] : [];
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* Verifies that the signed claim and the executor-observed mutation fields
|
|
137
|
-
* match for the action pack. The executor must pass `observedAction` from the
|
|
138
|
-
* system of record, not from the agent request body.
|
|
139
|
-
*/
|
|
140
|
-
export function verifyExecutionBinding({ requirement, receipt, observedAction } = {}) {
|
|
141
|
-
const requiredFields = materialFieldsFor(requirement);
|
|
142
|
-
if (requiredFields.length === 0) {
|
|
143
|
-
return { ok: true, required: false, required_fields: [], signed_hash: null, observed_hash: null };
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
const signed = receipt?.payload?.claim || {};
|
|
147
|
-
const observed = observedAction || {};
|
|
148
|
-
const missingSigned = [];
|
|
149
|
-
const missingObserved = [];
|
|
150
|
-
const invalidSigned = [];
|
|
151
|
-
const invalidObserved = [];
|
|
152
|
-
const mismatched = [];
|
|
153
|
-
const signedValues = {};
|
|
154
|
-
const observedValues = {};
|
|
155
|
-
|
|
156
|
-
for (const field of requiredFields) {
|
|
157
|
-
const expected = fieldValue(signed, field);
|
|
158
|
-
const actual = fieldValue(observed, field);
|
|
159
|
-
if (expected.state === 'missing') {
|
|
160
|
-
missingSigned.push(field);
|
|
161
|
-
} else if (expected.state === 'invalid') {
|
|
162
|
-
invalidSigned.push(field);
|
|
163
|
-
} else {
|
|
164
|
-
signedValues[field] = expected.value;
|
|
165
|
-
}
|
|
166
|
-
if (actual.state === 'missing') {
|
|
167
|
-
missingObserved.push(field);
|
|
168
|
-
} else if (actual.state === 'invalid') {
|
|
169
|
-
invalidObserved.push(field);
|
|
170
|
-
} else {
|
|
171
|
-
observedValues[field] = actual.value;
|
|
172
|
-
}
|
|
173
|
-
if (expected.state === 'valid' && actual.state === 'valid'
|
|
174
|
-
&& !equalValue(expected.value, actual.value)) mismatched.push(field);
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
// Per-field checks do not see one object reused by two required fields.
|
|
178
|
-
// Validate the aggregate graphs before computing either digest.
|
|
179
|
-
try { assertCanonicalJson(signedValues); } catch {
|
|
180
|
-
for (const field of Object.keys(signedValues)) {
|
|
181
|
-
if (!invalidSigned.includes(field)) invalidSigned.push(field);
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
try { assertCanonicalJson(observedValues); } catch {
|
|
185
|
-
for (const field of Object.keys(observedValues)) {
|
|
186
|
-
if (!invalidObserved.includes(field)) invalidObserved.push(field);
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
return {
|
|
191
|
-
'@version': EXECUTION_BINDING_VERSION,
|
|
192
|
-
ok: missingSigned.length === 0 && missingObserved.length === 0
|
|
193
|
-
&& invalidSigned.length === 0 && invalidObserved.length === 0 && mismatched.length === 0,
|
|
194
|
-
required: true,
|
|
195
|
-
required_fields: requiredFields,
|
|
196
|
-
missing_signed_fields: missingSigned,
|
|
197
|
-
missing_observed_fields: missingObserved,
|
|
198
|
-
invalid_signed_fields: invalidSigned,
|
|
199
|
-
invalid_observed_fields: invalidObserved,
|
|
200
|
-
mismatched_fields: mismatched,
|
|
201
|
-
signed_hash: invalidSigned.length === 0 ? hashCanonical(signedValues) : null,
|
|
202
|
-
observed_hash: invalidObserved.length === 0 ? hashCanonical(observedValues) : null,
|
|
203
|
-
note: 'Executor MUST provide observedAction from the system of record; request-body fields are not a trust source.',
|
|
204
|
-
};
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
export default {
|
|
208
|
-
EXECUTION_BINDING_VERSION,
|
|
209
|
-
canonicalize,
|
|
210
|
-
hashCanonical,
|
|
211
|
-
materialFieldsFor,
|
|
212
|
-
verifyExecutionBinding,
|
|
213
|
-
};
|
|
2
|
+
// Compatibility entry point for the TypeScript execution-binding module.
|
|
3
|
+
export * from './dist/execution-binding.js';
|
|
4
|
+
export { default } from './dist/execution-binding.js';
|