@verax-ai/proxy 0.4.3 → 0.4.5
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/dist/control-sync.js +15 -1
- package/dist/explain.d.ts +3 -0
- package/dist/explain.js +39 -5
- package/dist/index.d.ts +3 -1
- package/dist/index.js +2 -0
- package/dist/ledger-manifest.js +6 -2
- package/dist/ledger.d.ts +3 -3
- package/dist/ledger.js +20 -12
- package/dist/proxy.js +37 -16
- package/dist/reconcile.js +3 -1
- package/dist/revoke-sync.d.ts +26 -0
- package/dist/revoke-sync.js +176 -0
- package/dist/third-party.d.ts +7 -0
- package/dist/third-party.js +53 -0
- package/dist/types.d.ts +43 -2
- package/dist/verify-ledger.d.ts +12 -0
- package/dist/verify-ledger.js +96 -7
- package/package.json +1 -1
package/dist/control-sync.js
CHANGED
|
@@ -23,6 +23,8 @@ import { sha256Canonical } from "./hash.js";
|
|
|
23
23
|
const HALT_SWITCH = "halted";
|
|
24
24
|
const HALT_HISTORY = "halt-history.jsonl";
|
|
25
25
|
const CONTROL_EFFECT = "verax.control";
|
|
26
|
+
/** A signature larger than this is not an assertion; the line is copied without it. */
|
|
27
|
+
const MAX_SIGNATURE_JSON = 8192;
|
|
26
28
|
export function createControlMemory() {
|
|
27
29
|
return {
|
|
28
30
|
loaded: false,
|
|
@@ -212,7 +214,18 @@ function parseHistoryLine(raw, line) {
|
|
|
212
214
|
if (typeof row.by !== "string" || row.by.length === 0 || row.by.length > 256)
|
|
213
215
|
return null;
|
|
214
216
|
const via = row.via === "http" ? "http" : row.via === "file" ? "file" : "cli";
|
|
215
|
-
|
|
217
|
+
const parsed = { action: row.action, atMs: row.atMs, by: row.by, via, line };
|
|
218
|
+
// The signature is copied as the line carries it, not judged here: a body
|
|
219
|
+
// that dropped a bad one would turn a forged line into a quiet unsigned one.
|
|
220
|
+
// `verax verify` decides whether it holds.
|
|
221
|
+
const signature = row.signature;
|
|
222
|
+
if (signature !== null &&
|
|
223
|
+
typeof signature === "object" &&
|
|
224
|
+
!Array.isArray(signature) &&
|
|
225
|
+
JSON.stringify(signature).length <= MAX_SIGNATURE_JSON) {
|
|
226
|
+
parsed.signature = signature;
|
|
227
|
+
}
|
|
228
|
+
return parsed;
|
|
216
229
|
}
|
|
217
230
|
function lastAction(lines) {
|
|
218
231
|
for (let i = lines.length - 1; i >= 0; i -= 1) {
|
|
@@ -315,6 +328,7 @@ async function writeControlAllow(opts, event, lineHash) {
|
|
|
315
328
|
by: event.by,
|
|
316
329
|
via: event.via,
|
|
317
330
|
line: event.line,
|
|
331
|
+
...(event.signature ? { signature: event.signature } : {}),
|
|
318
332
|
};
|
|
319
333
|
const bound = sha256Canonical(body);
|
|
320
334
|
const ref = await writeDecision(opts, {
|
package/dist/explain.d.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import type { ExplainOpts, ExplainResult, Ledger } from "./types.ts";
|
|
2
|
+
export declare function balancedSummary(guarantee: "unconditional" | "conditional", conditions: readonly string[], cedulonSummary: string): string;
|
|
3
|
+
/** Local deployment conditions can only narrow the audit's guarantee. */
|
|
4
|
+
export declare function guaranteeWithConditions(guarantee: ExplainResult["guarantee"], conditions: readonly string[]): ExplainResult["guarantee"];
|
|
2
5
|
/**
|
|
3
6
|
* Re-runs the Cedulon decision profile on the whole ledger. Finding codes
|
|
4
7
|
* are Cedulon's; this wrapper does not invent names. With no durable
|
package/dist/explain.js
CHANGED
|
@@ -5,6 +5,7 @@ import { sha256Canonical } from "./hash.js";
|
|
|
5
5
|
import { inputsLogFor } from "./inputs.js";
|
|
6
6
|
import { lookupResolvedBy } from "./ledger.js";
|
|
7
7
|
import { loadCheckpoints } from "./checkpoints.js";
|
|
8
|
+
import { samePublicKey, thirdPartyReceiptCoversRow } from "./third-party.js";
|
|
8
9
|
async function pairFor(ledger, decisions, record, inputsLog, approvals) {
|
|
9
10
|
const ref = record.claims.ref;
|
|
10
11
|
if (!ref)
|
|
@@ -53,21 +54,32 @@ function conditionName(f, extractPresented) {
|
|
|
53
54
|
// Name from whether an extract was handed to audit, not Cedulon detail text.
|
|
54
55
|
return extractPresented ? "extract unpinned" : "extract unsigned";
|
|
55
56
|
}
|
|
57
|
+
// A one-row extract does not state the period or the path it covers, so it cannot show completeness.
|
|
58
|
+
if (f.code === "unstated-audit-window")
|
|
59
|
+
return "audit window unstated";
|
|
60
|
+
if (f.code === "unstated-audit-scope")
|
|
61
|
+
return "audit scope unstated";
|
|
56
62
|
return null;
|
|
57
63
|
}
|
|
58
|
-
function balancedSummary(guarantee, conditions, cedulonSummary) {
|
|
64
|
+
export function balancedSummary(guarantee, conditions, cedulonSummary) {
|
|
59
65
|
if (conditions.length > 0) {
|
|
60
|
-
return `audit: balanced (
|
|
66
|
+
return `audit: balanced (conditional: ${conditions.join("; ")})`;
|
|
61
67
|
}
|
|
62
68
|
if (guarantee === "unconditional") {
|
|
63
69
|
return "audit: balanced (unconditional)";
|
|
64
70
|
}
|
|
71
|
+
// Cedulon's summary counts findings this wrapper may have dropped (window-coverage with no checkpoint),
|
|
72
|
+
// so a failing summary is never echoed inside a balanced one.
|
|
65
73
|
const why = cedulonSummary.replace(/^audit:\s*/i, "").trim();
|
|
66
|
-
if (why !== "" && why !== "balanced" && why !== "conditional") {
|
|
74
|
+
if (why !== "" && why !== "balanced" && why !== "conditional" && !/FAIL|finding\(s\)/.test(why)) {
|
|
67
75
|
return `audit: balanced (${guarantee}: ${why})`;
|
|
68
76
|
}
|
|
69
77
|
return `audit: balanced (${guarantee})`;
|
|
70
78
|
}
|
|
79
|
+
/** Local deployment conditions can only narrow the audit's guarantee. */
|
|
80
|
+
export function guaranteeWithConditions(guarantee, conditions) {
|
|
81
|
+
return conditions.length > 0 ? "conditional" : guarantee;
|
|
82
|
+
}
|
|
71
83
|
/**
|
|
72
84
|
* Re-runs the Cedulon decision profile on the whole ledger. Finding codes
|
|
73
85
|
* are Cedulon's; this wrapper does not invent names. With no durable
|
|
@@ -93,6 +105,13 @@ export async function explain(ledger, ref, opts) {
|
|
|
93
105
|
const issuerTrust = resolvedTrust.pin;
|
|
94
106
|
const pinSource = resolvedTrust.source;
|
|
95
107
|
const pinned = issuerTrust !== undefined;
|
|
108
|
+
const thirdPartyTrust = opts?.thirdPartyTrust;
|
|
109
|
+
const thirdPartyVerified = effect?.witnessClass === "third-party" && thirdPartyTrust !== undefined &&
|
|
110
|
+
thirdPartyReceiptCoversRow(effect.receipt, effect.row, thirdPartyTrust.publicKeyPem);
|
|
111
|
+
const issuerKeys = issuerTrust ? (typeof issuerTrust.publicKeyPem === "string"
|
|
112
|
+
? [issuerTrust.publicKeyPem] : issuerTrust.publicKeyPem) : [];
|
|
113
|
+
const sameParty = thirdPartyTrust !== undefined && [record.publicKeyPem, ...issuerKeys]
|
|
114
|
+
.some(key => samePublicKey(thirdPartyTrust.publicKeyPem, key));
|
|
96
115
|
const dir = ledger.dir;
|
|
97
116
|
const checkpoints = typeof dir === "string" ? loadCheckpoints(dir) : [];
|
|
98
117
|
const report = audit({
|
|
@@ -102,6 +121,9 @@ export async function explain(ledger, ref, opts) {
|
|
|
102
121
|
profile: DECISION_PROFILE,
|
|
103
122
|
...(issuerTrust ? { issuerTrust } : {}),
|
|
104
123
|
...(presentedExtract ? { extract: presentedExtract } : {}),
|
|
124
|
+
// On DECISION_PROFILE RailTrustPin pins the effect-extract signer.
|
|
125
|
+
...(thirdPartyVerified && presentedExtract === effect?.receipt
|
|
126
|
+
? { trust: { publicKeyPem: thirdPartyTrust.publicKeyPem } } : {}),
|
|
105
127
|
});
|
|
106
128
|
const dropWindow = checkpoints.length === 0;
|
|
107
129
|
const dropped = dropWindow
|
|
@@ -135,6 +157,18 @@ export async function explain(ledger, ref, opts) {
|
|
|
135
157
|
const conditions = [];
|
|
136
158
|
if (self)
|
|
137
159
|
conditions.push("self witness");
|
|
160
|
+
if (witnessClass === "same-org")
|
|
161
|
+
conditions.push("same-party witness");
|
|
162
|
+
if (witnessClass === "third-party") {
|
|
163
|
+
if (!thirdPartyTrust)
|
|
164
|
+
conditions.push("third-party witness unpinned");
|
|
165
|
+
else if (sameParty)
|
|
166
|
+
conditions.push("same-party witness");
|
|
167
|
+
else if (thirdPartyTrust.independent !== true || typeof thirdPartyTrust.party !== "string" || !thirdPartyTrust.party.trim())
|
|
168
|
+
conditions.push("witness independence unstated");
|
|
169
|
+
else if (!thirdPartyVerified)
|
|
170
|
+
conditions.push("third-party witness unpinned");
|
|
171
|
+
}
|
|
138
172
|
if (pinSource === "own-key")
|
|
139
173
|
conditions.push("issuer pinned to own key");
|
|
140
174
|
for (const w of applicableWarnings) {
|
|
@@ -173,10 +207,10 @@ export async function explain(ledger, ref, opts) {
|
|
|
173
207
|
(issuerMismatch && pinned ? 1 : 0) +
|
|
174
208
|
(inputsMismatch ? 1 : 0) +
|
|
175
209
|
(inputsMissing ? 1 : 0);
|
|
176
|
-
const guarantee = report.guarantee;
|
|
210
|
+
const guarantee = guaranteeWithConditions(report.guarantee, conditions);
|
|
177
211
|
const summary = balanced
|
|
178
212
|
? balancedSummary(guarantee, conditions, report.summary)
|
|
179
|
-
: `audit: ${Math.max(findingCount, 1)} finding(s) → FAIL`;
|
|
213
|
+
: `audit: ${Math.max(findingCount, 1)} finding(s) → FAIL${witnessClass === "third-party" && conditions.length > 0 ? ` (conditional: ${conditions.join("; ")})` : ""}`;
|
|
180
214
|
const warnings = applicableWarnings.filter((f) => f.id === "issuer" || f.id === "extract").map(asWarning);
|
|
181
215
|
if (inputsMissing) {
|
|
182
216
|
warnings.push({
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
export { createProxy, LedgerDenyUnrecorded } from "./proxy.ts";
|
|
2
|
+
export { effectDescriptor, sha256Canonical } from "./hash.ts";
|
|
3
|
+
export { samePublicKey, thirdPartyReceiptRejection } from "./third-party.ts";
|
|
2
4
|
export { loadPolicy, TERMINAL_CONTROL_CLASS } from "./policy.ts";
|
|
3
5
|
export { FileLedger, MemoryLedger, readLedgerTail } from "./ledger.ts";
|
|
4
6
|
export { indexCoverage, listPieceFiles } from "./ledger-manifest.ts";
|
|
@@ -13,7 +15,7 @@ export { spokenReason } from "./spoken-reason.ts";
|
|
|
13
15
|
export type { ApprovalRow, ApproveResult } from "./approvals.ts";
|
|
14
16
|
export type { CardCsvOpts, ChannelRow, ReconcileReport } from "./reconcile.ts";
|
|
15
17
|
export type { FileLedgerOpts, LedgerCounts } from "./ledger.ts";
|
|
16
|
-
export type { ApprovalSignature } from "./types.ts";
|
|
18
|
+
export type { ApprovalSignature, ControlSignature, RevokeInput } from "./types.ts";
|
|
17
19
|
export type { EffectSigner, ExplainChain, ExplainPair, ExplainFinding, ExplainOpts, ExplainResult, ExplainTrustRoot, ExplainWarning, ExtractWindow, Ledger, LedgerEffect, Policy, PolicyDecision, Principal, ProxyDeps, RecordSigner, ToolCall, ToolResult, WitnessClass, } from "./types.ts";
|
|
18
20
|
export { verifyLedger } from "./verify-ledger.ts";
|
|
19
21
|
export type { VerifyResult, VerifyOptions, VerifyTrust, VerifyControl, VerifyInputs, VerifyAnchors, } from "./verify-ledger.ts";
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
export { createProxy, LedgerDenyUnrecorded } from "./proxy.js";
|
|
2
|
+
export { effectDescriptor, sha256Canonical } from "./hash.js";
|
|
3
|
+
export { samePublicKey, thirdPartyReceiptRejection } from "./third-party.js";
|
|
2
4
|
export { loadPolicy, TERMINAL_CONTROL_CLASS } from "./policy.js";
|
|
3
5
|
export { FileLedger, MemoryLedger, readLedgerTail } from "./ledger.js";
|
|
4
6
|
export { indexCoverage, listPieceFiles } from "./ledger-manifest.js";
|
package/dist/ledger-manifest.js
CHANGED
|
@@ -201,13 +201,17 @@ export function parseIndexText(text) {
|
|
|
201
201
|
// The index is appended without fsync, so a crash can leave its last
|
|
202
202
|
// line half written. Such a line is skipped; the ref count below then
|
|
203
203
|
// falls short of the ledger's and the index is rebuilt from the pieces.
|
|
204
|
-
let
|
|
204
|
+
let parsed;
|
|
205
205
|
try {
|
|
206
|
-
|
|
206
|
+
parsed = JSON.parse(line);
|
|
207
207
|
}
|
|
208
208
|
catch {
|
|
209
209
|
continue;
|
|
210
210
|
}
|
|
211
|
+
// JSON that is not an object (null, an array, a number, a string) is not a row.
|
|
212
|
+
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed))
|
|
213
|
+
continue;
|
|
214
|
+
const raw = parsed;
|
|
211
215
|
if (typeof raw.ref !== "string" || typeof raw.piece !== "string")
|
|
212
216
|
continue;
|
|
213
217
|
rows.push({
|
package/dist/ledger.d.ts
CHANGED
|
@@ -14,7 +14,7 @@ export type PermissionCheck = "owner-only" | "not checked on this platform";
|
|
|
14
14
|
/** Appends that already hold the ledger queue. Calling the public methods from here deadlocks. */
|
|
15
15
|
export type LedgerWriter = {
|
|
16
16
|
appendDecisionChained(build: (prevRecordHash: string | null) => SignedDecisionRecord): Promise<void>;
|
|
17
|
-
appendEffect(row: EffectRow, witnessClass: WitnessClass, resultHash?: string): Promise<void>;
|
|
17
|
+
appendEffect(row: EffectRow, witnessClass: WitnessClass, resultHash?: string, externalReceipt?: SignedEffectExtract): Promise<void>;
|
|
18
18
|
};
|
|
19
19
|
/**
|
|
20
20
|
* A ledger file read from its end through the same seam the reads above use,
|
|
@@ -71,7 +71,7 @@ export declare class MemoryLedger implements Ledger {
|
|
|
71
71
|
private appendDecisionChainedUnlocked;
|
|
72
72
|
/** One turn of this ledger's queue: read a cursor and append without a second writer seeing the same cursor. */
|
|
73
73
|
exclusive<T>(fn: (writer: LedgerWriter) => Promise<T>): Promise<T>;
|
|
74
|
-
appendEffect(row: EffectRow, witnessClass?: WitnessClass, resultHash?: string): Promise<void>;
|
|
74
|
+
appendEffect(row: EffectRow, witnessClass?: WitnessClass, resultHash?: string, externalReceipt?: SignedEffectExtract): Promise<void>;
|
|
75
75
|
private appendEffectUnlocked;
|
|
76
76
|
decisions(): Promise<SignedDecisionRecord[]>;
|
|
77
77
|
effects(): Promise<LedgerEffect[]>;
|
|
@@ -195,7 +195,7 @@ export declare class FileLedger implements Ledger {
|
|
|
195
195
|
}>;
|
|
196
196
|
/** Effects stamped in [fromMs, toMs), read from the end of each overlapping piece. */
|
|
197
197
|
effectsWindow(fromMs: number, toMs: number): Promise<LedgerEffect[]>;
|
|
198
|
-
appendEffect(row: EffectRow, witnessClass?: WitnessClass, resultHash?: string): Promise<void>;
|
|
198
|
+
appendEffect(row: EffectRow, witnessClass?: WitnessClass, resultHash?: string, externalReceipt?: SignedEffectExtract): Promise<void>;
|
|
199
199
|
private appendEffectUnlocked;
|
|
200
200
|
decisions(): Promise<SignedDecisionRecord[]>;
|
|
201
201
|
effects(): Promise<LedgerEffect[]>;
|
package/dist/ledger.js
CHANGED
|
@@ -153,7 +153,12 @@ function asEffectRow(row) {
|
|
|
153
153
|
...(row.actor !== undefined ? { actor: row.actor } : {}),
|
|
154
154
|
};
|
|
155
155
|
}
|
|
156
|
-
function asStoredEffect(row, witnessClass, resultHash, signer) {
|
|
156
|
+
function asStoredEffect(row, witnessClass, resultHash, signer, externalReceipt) {
|
|
157
|
+
if (witnessClass === "third-party") {
|
|
158
|
+
// The downstream signed only the extract: resultHash is stored but unattested.
|
|
159
|
+
return { row: structuredClone(row), witnessClass, ...(externalReceipt ? { receipt: externalReceipt } : {}),
|
|
160
|
+
...(resultHash !== undefined ? { resultHash } : {}) };
|
|
161
|
+
}
|
|
157
162
|
const stored = {
|
|
158
163
|
row: asEffectRow(row),
|
|
159
164
|
witnessClass,
|
|
@@ -259,13 +264,13 @@ export class MemoryLedger {
|
|
|
259
264
|
async exclusive(fn) {
|
|
260
265
|
return this.q.enqueue(() => fn({
|
|
261
266
|
appendDecisionChained: (build) => this.appendDecisionChainedUnlocked(build),
|
|
262
|
-
appendEffect: (row, witnessClass, resultHash) => this.appendEffectUnlocked(row, witnessClass, resultHash),
|
|
267
|
+
appendEffect: (row, witnessClass, resultHash, externalReceipt) => this.appendEffectUnlocked(row, witnessClass, resultHash, externalReceipt),
|
|
263
268
|
}));
|
|
264
269
|
}
|
|
265
|
-
async appendEffect(row, witnessClass = DEFAULT_WITNESS, resultHash) {
|
|
266
|
-
return this.q.enqueue(async () => this.appendEffectUnlocked(row, witnessClass, resultHash));
|
|
270
|
+
async appendEffect(row, witnessClass = DEFAULT_WITNESS, resultHash, externalReceipt) {
|
|
271
|
+
return this.q.enqueue(async () => this.appendEffectUnlocked(row, witnessClass, resultHash, externalReceipt));
|
|
267
272
|
}
|
|
268
|
-
async appendEffectUnlocked(row, witnessClass, resultHash) {
|
|
273
|
+
async appendEffectUnlocked(row, witnessClass, resultHash, externalReceipt) {
|
|
269
274
|
const existing = this._effects.find((e) => e.row.ref === row.ref && e.row.effectClass !== "duplicate-effect");
|
|
270
275
|
if (existing && row.effectClass !== "duplicate-effect") {
|
|
271
276
|
this._effects.push(asStoredEffect({
|
|
@@ -277,7 +282,7 @@ export class MemoryLedger {
|
|
|
277
282
|
}, DEFAULT_WITNESS, resultHash, this.effectSigner));
|
|
278
283
|
throw new Error(`duplicate-effect:${row.ref}`);
|
|
279
284
|
}
|
|
280
|
-
this._effects.push(asStoredEffect(row, witnessClass, resultHash, this.effectSigner));
|
|
285
|
+
this._effects.push(asStoredEffect(row, witnessClass, resultHash, this.effectSigner, externalReceipt));
|
|
281
286
|
}
|
|
282
287
|
async decisions() {
|
|
283
288
|
return [...this._decisions];
|
|
@@ -835,7 +840,7 @@ export class FileLedger {
|
|
|
835
840
|
async exclusive(fn) {
|
|
836
841
|
return this.q.enqueue(() => fn({
|
|
837
842
|
appendDecisionChained: (build) => this.appendDecisionChainedUnlocked(build),
|
|
838
|
-
appendEffect: (row, witnessClass, resultHash) => this.appendEffectUnlocked(row, witnessClass, resultHash),
|
|
843
|
+
appendEffect: (row, witnessClass, resultHash, externalReceipt) => this.appendEffectUnlocked(row, witnessClass, resultHash, externalReceipt),
|
|
839
844
|
}));
|
|
840
845
|
}
|
|
841
846
|
/**
|
|
@@ -929,13 +934,13 @@ export class FileLedger {
|
|
|
929
934
|
}
|
|
930
935
|
return collected;
|
|
931
936
|
}
|
|
932
|
-
async appendEffect(row, witnessClass = DEFAULT_WITNESS, resultHash) {
|
|
937
|
+
async appendEffect(row, witnessClass = DEFAULT_WITNESS, resultHash, externalReceipt) {
|
|
933
938
|
return this.q.enqueue(async () => {
|
|
934
939
|
this.assertOwned();
|
|
935
|
-
return this.appendEffectUnlocked(row, witnessClass, resultHash);
|
|
940
|
+
return this.appendEffectUnlocked(row, witnessClass, resultHash, externalReceipt);
|
|
936
941
|
});
|
|
937
942
|
}
|
|
938
|
-
async appendEffectUnlocked(row, witnessClass = DEFAULT_WITNESS, resultHash) {
|
|
943
|
+
async appendEffectUnlocked(row, witnessClass = DEFAULT_WITNESS, resultHash, externalReceipt) {
|
|
939
944
|
this.assertOwned();
|
|
940
945
|
if (row.effectClass !== "duplicate-effect" && this.effectRefs.has(row.ref)) {
|
|
941
946
|
const dup = lineOf(asStoredEffect({
|
|
@@ -950,7 +955,7 @@ export class FileLedger {
|
|
|
950
955
|
this.activeEffectN += 1;
|
|
951
956
|
throw new Error(`duplicate-effect:${row.ref}`);
|
|
952
957
|
}
|
|
953
|
-
const line = lineOf(await this.storeEffect(row, witnessClass, resultHash));
|
|
958
|
+
const line = lineOf(await this.storeEffect(row, witnessClass, resultHash, externalReceipt));
|
|
954
959
|
await appendDurable(this.effectsPath, line);
|
|
955
960
|
await this.pulse("effects.jsonl", line, "effect");
|
|
956
961
|
this.activeEffectN += 1;
|
|
@@ -1020,7 +1025,10 @@ export class FileLedger {
|
|
|
1020
1025
|
lastEffectN: this.effectCount,
|
|
1021
1026
|
})}\n`, { encoding: "utf8", mode: 0o600 });
|
|
1022
1027
|
}
|
|
1023
|
-
async storeEffect(row, witnessClass, resultHash) {
|
|
1028
|
+
async storeEffect(row, witnessClass, resultHash, externalReceipt) {
|
|
1029
|
+
// Never ask the body or its same-org witness to sign an external receipt.
|
|
1030
|
+
if (witnessClass === "third-party")
|
|
1031
|
+
return asStoredEffect(row, witnessClass, resultHash, undefined, externalReceipt);
|
|
1024
1032
|
if (this.remoteWitness && row.effectClass !== "duplicate-effect") {
|
|
1025
1033
|
const remote = await this.remoteWitness(row, resultHash);
|
|
1026
1034
|
if (remote?.attestation && remote.receipt && remote.witnessClass && remote.witnessClass !== "self") {
|
package/dist/proxy.js
CHANGED
|
@@ -8,6 +8,7 @@ import { markEnded, markStarted, startedWithoutEnd } from "./in-flight-log.js";
|
|
|
8
8
|
import { effectDescriptor, sha256Canonical } from "./hash.js";
|
|
9
9
|
import { inputsLogFor } from "./inputs.js";
|
|
10
10
|
import { controlUnchanged, createControlMemory, syncHaltControl } from "./control-sync.js";
|
|
11
|
+
import { createRevokeMemory, revokeUnchanged, syncRevokeControl } from "./revoke-sync.js";
|
|
11
12
|
import { countedWork, hasPrimaryEffect, lookupDecisionByRef, lookupReauthByHash, lookupResolvedBy, noteResolution, noteTenantRef, } from "./ledger.js";
|
|
12
13
|
import { spokenReason } from "./spoken-reason.js";
|
|
13
14
|
import { tenantKey } from "./tenant.js";
|
|
@@ -193,6 +194,7 @@ export function createProxy(deps) {
|
|
|
193
194
|
// Single writer: after open, the ledger lock is this process. The first
|
|
194
195
|
// sync reads control records once; this snapshot is updated in place.
|
|
195
196
|
const controlMemory = createControlMemory();
|
|
197
|
+
const revokeMemory = createRevokeMemory();
|
|
196
198
|
let controlSyncReported = false;
|
|
197
199
|
if (stateDir === null) {
|
|
198
200
|
process.stderr.write("verax-proxy: halt and disk limits are inactive without a ledger directory\n");
|
|
@@ -308,8 +310,15 @@ export function createProxy(deps) {
|
|
|
308
310
|
timestampMs: deps.now(),
|
|
309
311
|
actor: principal.brain,
|
|
310
312
|
};
|
|
313
|
+
const { effectReceipt, ...normalResult } = result;
|
|
314
|
+
const signedRow = effectReceipt?.body?.effects?.[0];
|
|
315
|
+
const external = effectReceipt?.body?.effects?.length === 1 && signedRow?.ref === ref &&
|
|
316
|
+
signedRow.effectHash === dispatchedHash && signedRow.effectClass === dispatchedName;
|
|
311
317
|
try {
|
|
312
|
-
|
|
318
|
+
if (external)
|
|
319
|
+
await deps.ledger.appendEffect(signedRow, "third-party", sha256Canonical(normalResult), effectReceipt);
|
|
320
|
+
else
|
|
321
|
+
await deps.ledger.appendEffect(row, "self", sha256Canonical(normalResult));
|
|
313
322
|
effectRecorded.ok = true;
|
|
314
323
|
}
|
|
315
324
|
catch {
|
|
@@ -318,7 +327,7 @@ export function createProxy(deps) {
|
|
|
318
327
|
// stays false) and a restart answers outcome-unknown instead of running again.
|
|
319
328
|
return effectUnrecorded(ref);
|
|
320
329
|
}
|
|
321
|
-
return
|
|
330
|
+
return normalResult;
|
|
322
331
|
}
|
|
323
332
|
async function resolveInputs(call, principal, timestampMs) {
|
|
324
333
|
const declared = declaredInputs(call.arguments);
|
|
@@ -439,20 +448,30 @@ export function createProxy(deps) {
|
|
|
439
448
|
async function syncControlRecords() {
|
|
440
449
|
if (stateDir === null)
|
|
441
450
|
return;
|
|
451
|
+
// A revocation list that cannot be read must not throw away the halt
|
|
452
|
+
// snapshot too, or every call would read the whole ledger again.
|
|
453
|
+
let haltSynced = false;
|
|
442
454
|
try {
|
|
443
|
-
if (controlUnchanged(stateDir, controlMemory))
|
|
455
|
+
if (controlUnchanged(stateDir, controlMemory) && revokeUnchanged(stateDir, revokeMemory))
|
|
444
456
|
return;
|
|
445
|
-
const run = (writer) =>
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
457
|
+
const run = async (writer) => {
|
|
458
|
+
// Both snapshots load from one read. The halt sync writes only halt
|
|
459
|
+
// and resume records, which the revoke replay does not read.
|
|
460
|
+
let read = null;
|
|
461
|
+
const shared = {
|
|
462
|
+
stateDir,
|
|
463
|
+
now: deps.now,
|
|
464
|
+
nonce: deps.nonce,
|
|
465
|
+
policyHash: deps.policy.hash,
|
|
466
|
+
recordSigner: deps.recordSigner,
|
|
467
|
+
inputsLog,
|
|
468
|
+
decisions: () => (read ??= deps.ledger.decisions()),
|
|
469
|
+
writer,
|
|
470
|
+
};
|
|
471
|
+
await syncHaltControl({ ...shared, memory: controlMemory });
|
|
472
|
+
haltSynced = true;
|
|
473
|
+
await syncRevokeControl({ ...shared, memory: revokeMemory });
|
|
474
|
+
};
|
|
456
475
|
const host = deps.ledger;
|
|
457
476
|
if (typeof host.exclusive === "function") {
|
|
458
477
|
await host.exclusive((writer) => run(writer));
|
|
@@ -460,7 +479,7 @@ export function createProxy(deps) {
|
|
|
460
479
|
else {
|
|
461
480
|
await controlQueue.enqueue(() => run({
|
|
462
481
|
appendDecisionChained: (build) => deps.ledger.appendDecisionChained(build),
|
|
463
|
-
appendEffect: (row, witnessClass, resultHash) => deps.ledger.appendEffect(row, witnessClass, resultHash),
|
|
482
|
+
appendEffect: (row, witnessClass, resultHash, externalReceipt) => deps.ledger.appendEffect(row, witnessClass, resultHash, externalReceipt),
|
|
464
483
|
}));
|
|
465
484
|
}
|
|
466
485
|
controlSyncReported = false;
|
|
@@ -470,7 +489,9 @@ export function createProxy(deps) {
|
|
|
470
489
|
// unloaded so the next call reads again, and keep going: the halt switch
|
|
471
490
|
// and the rate limit still fail closed on their own path. One line per
|
|
472
491
|
// failure streak, so the admission retry in the same call does not repeat it.
|
|
473
|
-
|
|
492
|
+
if (!haltSynced)
|
|
493
|
+
controlMemory.loaded = false;
|
|
494
|
+
revokeMemory.loaded = false;
|
|
474
495
|
if (controlSyncReported)
|
|
475
496
|
return;
|
|
476
497
|
controlSyncReported = true;
|
package/dist/reconcile.js
CHANGED
|
@@ -41,7 +41,9 @@ function asChannelRow(raw, line) {
|
|
|
41
41
|
row.datePrecision = o.datePrecision;
|
|
42
42
|
return row;
|
|
43
43
|
}
|
|
44
|
-
|
|
44
|
+
// A raw ref (REF_RE in proxy.ts), or the scoped form `<tenantKey>:<raw>` the proxy writes
|
|
45
|
+
// when the principal carries an issuer, tenant or org; tenantKey is a SHA-256 hex digest.
|
|
46
|
+
const VERAX_REF = /verax:((?:[0-9a-f]{64}:)?[A-Za-z0-9][A-Za-z0-9._-]{0,63})/;
|
|
45
47
|
const CARD_MINUTE_TOLERANCE_MS = 120_000;
|
|
46
48
|
function parseCardDate(raw, format, tzOffsetMinutes) {
|
|
47
49
|
const t = raw.trim();
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { SignedDecisionRecord } from "@cedulon/core";
|
|
2
|
+
import type { LedgerWriter } from "./ledger.ts";
|
|
3
|
+
import type { InputsLog, RecordSigner } from "./types.ts";
|
|
4
|
+
type Stamp = {
|
|
5
|
+
size: number;
|
|
6
|
+
mtimeMs: number;
|
|
7
|
+
} | null;
|
|
8
|
+
export type RevokeMemory = {
|
|
9
|
+
loaded: boolean;
|
|
10
|
+
stamp: Stamp;
|
|
11
|
+
revoked: Set<string>;
|
|
12
|
+
};
|
|
13
|
+
export declare function createRevokeMemory(): RevokeMemory;
|
|
14
|
+
export declare function revokeUnchanged(stateDir: string, memory: RevokeMemory): boolean;
|
|
15
|
+
export declare function syncRevokeControl(opts: {
|
|
16
|
+
stateDir: string;
|
|
17
|
+
now: () => number;
|
|
18
|
+
nonce: () => string;
|
|
19
|
+
policyHash: string;
|
|
20
|
+
recordSigner: RecordSigner;
|
|
21
|
+
inputsLog: InputsLog;
|
|
22
|
+
decisions: () => Promise<SignedDecisionRecord[]>;
|
|
23
|
+
memory: RevokeMemory;
|
|
24
|
+
writer: LedgerWriter;
|
|
25
|
+
}): Promise<void>;
|
|
26
|
+
export {};
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { readFileSync, statSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { signDecisionRecord } from "@cedulon/core";
|
|
4
|
+
import { sha256Canonical } from "./hash.js";
|
|
5
|
+
/**
|
|
6
|
+
* Same file as `packages/body/src/revoke.ts` and the issuer's `POST /revoke`.
|
|
7
|
+
* The file stays the switch: the body refuses a listed jti whether or not the
|
|
8
|
+
* ledger has caught up. This module keeps the ledger's account of that list.
|
|
9
|
+
*
|
|
10
|
+
* The ledger's list is replayed from its `verax.revoke` records. A jti in the
|
|
11
|
+
* file and not in that list is one `token-revoked` record; a jti in that list
|
|
12
|
+
* and gone from the file is one `revoke-removed` record, because removing a
|
|
13
|
+
* line lets that token in again and nothing else would say so. Both are
|
|
14
|
+
* denies with no effect: a revocation refuses, it does not let a call through,
|
|
15
|
+
* and a verifier that predates these records reads them as ordinary denies.
|
|
16
|
+
*
|
|
17
|
+
* Like a CLI halt, a revocation is not in the ledger until the body opens,
|
|
18
|
+
* the halt doors sync, or the next call reaches the proxy. A call made with
|
|
19
|
+
* the revoked token is refused before the proxy and does not sync.
|
|
20
|
+
*/
|
|
21
|
+
const REVOKED = "revoked-jti.jsonl";
|
|
22
|
+
const SUBJECT = "verax.revoke";
|
|
23
|
+
const CONTROL_EFFECT = "verax.control";
|
|
24
|
+
const MAX_JTI = 256;
|
|
25
|
+
const MAX_BY = 256;
|
|
26
|
+
export function createRevokeMemory() {
|
|
27
|
+
return { loaded: false, stamp: null, revoked: new Set() };
|
|
28
|
+
}
|
|
29
|
+
export function revokeUnchanged(stateDir, memory) {
|
|
30
|
+
return memory.loaded && sameStamp(memory.stamp, fileStamp(stateDir));
|
|
31
|
+
}
|
|
32
|
+
export async function syncRevokeControl(opts) {
|
|
33
|
+
const memory = opts.memory;
|
|
34
|
+
if (revokeUnchanged(opts.stateDir, memory))
|
|
35
|
+
return;
|
|
36
|
+
let stamp = fileStamp(opts.stateDir);
|
|
37
|
+
let listed = [];
|
|
38
|
+
for (let attempt = 0; attempt < 3; attempt += 1) {
|
|
39
|
+
listed = readListed(opts.stateDir);
|
|
40
|
+
const after = fileStamp(opts.stateDir);
|
|
41
|
+
if (sameStamp(stamp, after))
|
|
42
|
+
break;
|
|
43
|
+
stamp = after;
|
|
44
|
+
}
|
|
45
|
+
if (!memory.loaded) {
|
|
46
|
+
memory.revoked = await replay(opts);
|
|
47
|
+
memory.loaded = true;
|
|
48
|
+
}
|
|
49
|
+
try {
|
|
50
|
+
const inFile = new Set();
|
|
51
|
+
for (const row of listed) {
|
|
52
|
+
if (inFile.has(row.jti))
|
|
53
|
+
continue;
|
|
54
|
+
inFile.add(row.jti);
|
|
55
|
+
if (memory.revoked.has(row.jti))
|
|
56
|
+
continue;
|
|
57
|
+
await writeRevoke(opts, {
|
|
58
|
+
action: "revoke",
|
|
59
|
+
jti: row.jti,
|
|
60
|
+
by: row.by,
|
|
61
|
+
via: row.via,
|
|
62
|
+
atMs: row.atMs ?? opts.now(),
|
|
63
|
+
});
|
|
64
|
+
memory.revoked.add(row.jti);
|
|
65
|
+
}
|
|
66
|
+
for (const jti of [...memory.revoked]) {
|
|
67
|
+
if (inFile.has(jti))
|
|
68
|
+
continue;
|
|
69
|
+
await writeRevoke(opts, { action: "unrevoke", jti, by: "unknown", via: "file", atMs: opts.now() });
|
|
70
|
+
memory.revoked.delete(jti);
|
|
71
|
+
}
|
|
72
|
+
memory.stamp = stamp;
|
|
73
|
+
}
|
|
74
|
+
catch (err) {
|
|
75
|
+
// A partial append is already on disk. Replay the ledger next time rather
|
|
76
|
+
// than trust a list that may be ahead of it or behind it.
|
|
77
|
+
memory.loaded = false;
|
|
78
|
+
throw err;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
function fileStamp(stateDir) {
|
|
82
|
+
try {
|
|
83
|
+
const st = statSync(join(stateDir, REVOKED));
|
|
84
|
+
return { size: st.size, mtimeMs: st.mtimeMs };
|
|
85
|
+
}
|
|
86
|
+
catch (err) {
|
|
87
|
+
if (err.code === "ENOENT")
|
|
88
|
+
return null;
|
|
89
|
+
throw err;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
function sameStamp(a, b) {
|
|
93
|
+
if (a === null || b === null)
|
|
94
|
+
return a === b;
|
|
95
|
+
return a.size === b.size && a.mtimeMs === b.mtimeMs;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Lines that name no usable jti are skipped here. The body's own check reads
|
|
99
|
+
* the same file and decides what such a line does to a token; this list only
|
|
100
|
+
* records jtis it can name.
|
|
101
|
+
*/
|
|
102
|
+
function readListed(stateDir) {
|
|
103
|
+
let text;
|
|
104
|
+
try {
|
|
105
|
+
text = readFileSync(join(stateDir, REVOKED), "utf8");
|
|
106
|
+
}
|
|
107
|
+
catch (err) {
|
|
108
|
+
if (err.code === "ENOENT")
|
|
109
|
+
return [];
|
|
110
|
+
throw err;
|
|
111
|
+
}
|
|
112
|
+
const out = [];
|
|
113
|
+
for (const raw of text.split("\n")) {
|
|
114
|
+
const line = raw.replace(/\r$/, "");
|
|
115
|
+
if (line.trim() === "")
|
|
116
|
+
continue;
|
|
117
|
+
let row;
|
|
118
|
+
try {
|
|
119
|
+
row = JSON.parse(line);
|
|
120
|
+
}
|
|
121
|
+
catch {
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (!row || typeof row !== "object")
|
|
125
|
+
continue;
|
|
126
|
+
if (typeof row.jti !== "string" || row.jti === "" || row.jti.length > MAX_JTI)
|
|
127
|
+
continue;
|
|
128
|
+
const named = typeof row.by === "string" && row.by !== "" && row.by.length <= MAX_BY;
|
|
129
|
+
const atMs = typeof row.atMs === "number" && Number.isFinite(row.atMs) ? row.atMs : null;
|
|
130
|
+
out.push({ jti: row.jti, by: named ? row.by : "unknown", via: named ? "http" : "file", atMs });
|
|
131
|
+
}
|
|
132
|
+
return out;
|
|
133
|
+
}
|
|
134
|
+
async function replay(opts) {
|
|
135
|
+
const revoked = new Set();
|
|
136
|
+
for (const decision of await opts.decisions()) {
|
|
137
|
+
if (decision.claims.subject !== SUBJECT || decision.claims.effectClass !== CONTROL_EFFECT)
|
|
138
|
+
continue;
|
|
139
|
+
const ref = decision.claims.ref;
|
|
140
|
+
if (typeof ref !== "string")
|
|
141
|
+
continue;
|
|
142
|
+
const row = (await opts.inputsLog.get(ref))?.revoke;
|
|
143
|
+
if (!row || typeof row.jti !== "string")
|
|
144
|
+
continue;
|
|
145
|
+
if (row.action === "revoke")
|
|
146
|
+
revoked.add(row.jti);
|
|
147
|
+
else if (row.action === "unrevoke")
|
|
148
|
+
revoked.delete(row.jti);
|
|
149
|
+
}
|
|
150
|
+
return revoked;
|
|
151
|
+
}
|
|
152
|
+
async function writeRevoke(opts, revoke) {
|
|
153
|
+
const ref = opts.nonce();
|
|
154
|
+
const inputs = {
|
|
155
|
+
principal: { brain: "verax-proxy", scopes: [] },
|
|
156
|
+
inputs: [],
|
|
157
|
+
revoke,
|
|
158
|
+
};
|
|
159
|
+
const inputsHash = sha256Canonical(inputs);
|
|
160
|
+
await opts.inputsLog.append(ref, inputs);
|
|
161
|
+
await opts.writer.appendDecisionChained((prevRecordHash) => signDecisionRecord({
|
|
162
|
+
decider: "verax-proxy",
|
|
163
|
+
subject: SUBJECT,
|
|
164
|
+
requestHash: sha256Canonical(revoke),
|
|
165
|
+
policyHash: opts.policyHash,
|
|
166
|
+
inputsHash,
|
|
167
|
+
decision: "deny",
|
|
168
|
+
reasonCode: revoke.action === "revoke" ? "token-revoked" : "revoke-removed",
|
|
169
|
+
ref,
|
|
170
|
+
effectHash: null,
|
|
171
|
+
effectClass: CONTROL_EFFECT,
|
|
172
|
+
timestampMs: revoke.atMs,
|
|
173
|
+
nonce: ref,
|
|
174
|
+
prevRecordHash,
|
|
175
|
+
}, opts.recordSigner.privateKeyPem, opts.recordSigner.publicKeyPem));
|
|
176
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type SignedEffectExtract } from "@cedulon/effect-extract";
|
|
2
|
+
/** Compare key material, not PEM whitespace or line endings. */
|
|
3
|
+
export declare function samePublicKey(a: string | null | undefined, b: string | null | undefined): boolean;
|
|
4
|
+
/** External receipts are meaningful only under a reader-supplied pin. */
|
|
5
|
+
export declare function thirdPartyReceiptCoversRow(receipt: SignedEffectExtract | undefined, row: unknown, key: string): boolean;
|
|
6
|
+
/** A valid signature must also describe this dispatch, not another call. */
|
|
7
|
+
export declare function thirdPartyReceiptRejection(value: unknown, key: string, ref: string | undefined, effectClass: string, effectHash: string): string | null;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { createPublicKey } from "node:crypto";
|
|
2
|
+
import { canonical } from "@cedulon/core";
|
|
3
|
+
import { verifyEffectExtract } from "@cedulon/effect-extract";
|
|
4
|
+
/** Compare key material, not PEM whitespace or line endings. */
|
|
5
|
+
export function samePublicKey(a, b) {
|
|
6
|
+
if (!a || !b)
|
|
7
|
+
return false;
|
|
8
|
+
try {
|
|
9
|
+
return createPublicKey(a).export({ type: "spki", format: "der" })
|
|
10
|
+
.equals(createPublicKey(b).export({ type: "spki", format: "der" }));
|
|
11
|
+
}
|
|
12
|
+
catch {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/** External receipts are meaningful only under a reader-supplied pin. */
|
|
17
|
+
export function thirdPartyReceiptCoversRow(receipt, row, key) {
|
|
18
|
+
try {
|
|
19
|
+
return !!receipt && createPublicKey(key).asymmetricKeyType === "ed25519" &&
|
|
20
|
+
verifyEffectExtract(receipt, key) && receipt.body.effects.length === 1 &&
|
|
21
|
+
canonical(receipt.body.effects[0]) === canonical(row);
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/** A valid signature must also describe this dispatch, not another call. */
|
|
28
|
+
export function thirdPartyReceiptRejection(value, key, ref, effectClass, effectHash) {
|
|
29
|
+
const receipt = value;
|
|
30
|
+
try {
|
|
31
|
+
if (!receipt || !verifyEffectExtract(receipt, key))
|
|
32
|
+
return "signature";
|
|
33
|
+
if (receipt.body.effects.length !== 1)
|
|
34
|
+
return "effect-count";
|
|
35
|
+
const row = receipt.body.effects[0];
|
|
36
|
+
if (!ref || row.ref !== ref)
|
|
37
|
+
return "ref";
|
|
38
|
+
if (row.effectClass !== effectClass)
|
|
39
|
+
return "effect-class";
|
|
40
|
+
if (row.effectHash !== effectHash)
|
|
41
|
+
return "effect-hash";
|
|
42
|
+
if (receipt.body.deciderId !== "verax-proxy")
|
|
43
|
+
return "decider";
|
|
44
|
+
if (receipt.body.windowStartMs !== row.timestampMs)
|
|
45
|
+
return "window-start";
|
|
46
|
+
if (receipt.body.windowEndMs !== row.timestampMs + 1)
|
|
47
|
+
return "window-end";
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
return "malformed-receipt";
|
|
52
|
+
}
|
|
53
|
+
}
|
package/dist/types.d.ts
CHANGED
|
@@ -24,6 +24,8 @@ export type ToolResult = {
|
|
|
24
24
|
}[];
|
|
25
25
|
isError: boolean;
|
|
26
26
|
claimedHash?: string;
|
|
27
|
+
/** Adapter-verified downstream extract. Side field: excluded from resultHash and the agent response. */
|
|
28
|
+
effectReceipt?: SignedEffectExtract;
|
|
27
29
|
};
|
|
28
30
|
export type PolicyDecision = {
|
|
29
31
|
decision: DecisionKind;
|
|
@@ -63,7 +65,7 @@ export type EffectAttestation = {
|
|
|
63
65
|
export type LedgerEffect = {
|
|
64
66
|
row: EffectRow;
|
|
65
67
|
witnessClass: WitnessClass;
|
|
66
|
-
/** Hash of the ToolResult (or thrown payload).
|
|
68
|
+
/** Hash of the normal ToolResult (or thrown payload). Unattested on third-party rows. */
|
|
67
69
|
resultHash?: string;
|
|
68
70
|
/** One-row extract signed at call time. */
|
|
69
71
|
receipt?: SignedEffectExtract;
|
|
@@ -89,7 +91,7 @@ export type ExtractWindow = {
|
|
|
89
91
|
export type Ledger = {
|
|
90
92
|
appendDecision(signed: SignedDecisionRecord): Promise<void>;
|
|
91
93
|
appendDecisionChained(build: (prevRecordHash: string | null) => SignedDecisionRecord): Promise<void>;
|
|
92
|
-
appendEffect(row: EffectRow, witnessClass?: WitnessClass, resultHash?: string): Promise<void>;
|
|
94
|
+
appendEffect(row: EffectRow, witnessClass?: WitnessClass, resultHash?: string, externalReceipt?: SignedEffectExtract): Promise<void>;
|
|
93
95
|
decisions(): Promise<SignedDecisionRecord[]>;
|
|
94
96
|
effects(): Promise<LedgerEffect[]>;
|
|
95
97
|
lastDecisionHash(): Promise<string | null>;
|
|
@@ -119,6 +121,39 @@ export type ControlInput = {
|
|
|
119
121
|
atMs: number;
|
|
120
122
|
line: number;
|
|
121
123
|
lineHash: string;
|
|
124
|
+
/**
|
|
125
|
+
* Copied as the history line carries it; `verax verify` judges it. Only an
|
|
126
|
+
* HTTP halt or resume made with a passkey assertion has one.
|
|
127
|
+
*/
|
|
128
|
+
signature?: ControlSignature;
|
|
129
|
+
};
|
|
130
|
+
/**
|
|
131
|
+
* The operator's WebAuthn assertion over one halt or resume. The challenge is
|
|
132
|
+
* rebuilt from `purpose` (`verax.halt` or `verax.resume`, from the record's
|
|
133
|
+
* subject) and `prev`, the SHA-256 of the history line before this one
|
|
134
|
+
* (`null` for the first line). A line is appended once, so a signature made
|
|
135
|
+
* for one tail of the history does not fit any later one.
|
|
136
|
+
*/
|
|
137
|
+
export type ControlSignature = {
|
|
138
|
+
credentialId: string;
|
|
139
|
+
sub: string;
|
|
140
|
+
authenticatorData: string;
|
|
141
|
+
clientDataJSON: string;
|
|
142
|
+
signature: string;
|
|
143
|
+
rpId: string;
|
|
144
|
+
prev: string | null;
|
|
145
|
+
};
|
|
146
|
+
/**
|
|
147
|
+
* One change to `revoked-jti.jsonl` copied into the ledger. `revoke` is a jti
|
|
148
|
+
* that appeared in the file; `unrevoke` is one that was in the ledger's list
|
|
149
|
+
* and is gone from the file, which lets that token in again.
|
|
150
|
+
*/
|
|
151
|
+
export type RevokeInput = {
|
|
152
|
+
action: "revoke" | "unrevoke";
|
|
153
|
+
jti: string;
|
|
154
|
+
by: string;
|
|
155
|
+
via: "http" | "file";
|
|
156
|
+
atMs: number;
|
|
122
157
|
};
|
|
123
158
|
/**
|
|
124
159
|
* The operator's own WebAuthn assertion for one HTTP approval.
|
|
@@ -153,6 +188,7 @@ export type DecisionInputs = {
|
|
|
153
188
|
signature?: ApprovalSignature;
|
|
154
189
|
};
|
|
155
190
|
control?: ControlInput;
|
|
191
|
+
revoke?: RevokeInput;
|
|
156
192
|
};
|
|
157
193
|
/** How an operator's approval reached the ledger: a person at `verax approve`, a non-interactive `--from-script` run, or the body's `/api/approve`. */
|
|
158
194
|
export type ApprovalChannel = "cli" | "cli-script" | "http";
|
|
@@ -195,6 +231,11 @@ export type ExplainWarning = {
|
|
|
195
231
|
detail: string;
|
|
196
232
|
};
|
|
197
233
|
export type ExplainOpts = {
|
|
234
|
+
thirdPartyTrust?: {
|
|
235
|
+
publicKeyPem: string;
|
|
236
|
+
party: string;
|
|
237
|
+
independent: boolean;
|
|
238
|
+
};
|
|
198
239
|
issuerTrust?: {
|
|
199
240
|
publicKeyPem: string | readonly string[];
|
|
200
241
|
source?: "env" | "own-key";
|
package/dist/verify-ledger.d.ts
CHANGED
|
@@ -15,6 +15,12 @@ export type VerifyResult = {
|
|
|
15
15
|
chainBreakAt: number | null;
|
|
16
16
|
effectsBound: number;
|
|
17
17
|
effectsOrphaned: number;
|
|
18
|
+
/**
|
|
19
|
+
* Refs of attested allows with no effect row that sit within the boundary
|
|
20
|
+
* allowance of the newest record: `boundary-deferred`, a warning. An older
|
|
21
|
+
* allow with no row is the problem `decision-without-effect <ref>`.
|
|
22
|
+
*/
|
|
23
|
+
effectsDeferred: string[];
|
|
18
24
|
trust: VerifyTrust;
|
|
19
25
|
/** Which key answered for `self` effect rows. Same honesty rules as `trust`. */
|
|
20
26
|
effectTrust: VerifyTrust;
|
|
@@ -82,11 +88,17 @@ export type VerifyOptions = {
|
|
|
82
88
|
publicKeyPem?: string;
|
|
83
89
|
/** Verify `self` effect rows against this key instead of the one those rows carry. */
|
|
84
90
|
effectPublicKeyPem?: string;
|
|
91
|
+
/** Out-of-band pins only. Third-party resultHash is never attested by the extract. */
|
|
92
|
+
thirdPartyPublicKeyPems?: string[];
|
|
85
93
|
/** Verify `same-org` effect rows against this key instead of the one those rows carry. */
|
|
86
94
|
witnessPublicKeyPem?: string;
|
|
87
95
|
/** Verify every checkpoint row against this key instead of one key taken from the file. */
|
|
88
96
|
checkpointPublicKeyPem?: string;
|
|
89
97
|
/** The Transparency Service key that receipts in `checkpoint-anchors.jsonl` are checked under. */
|
|
90
98
|
anchorPublicKeyPem?: string;
|
|
99
|
+
/** How close to the newest record an allow with no effect row is deferred rather than named. */
|
|
100
|
+
boundaryAllowanceMs?: number;
|
|
91
101
|
};
|
|
102
|
+
/** The core's default clock-skew allowance, which decision profile 6.1 applies unchanged. */
|
|
103
|
+
export declare const DEFAULT_BOUNDARY_ALLOWANCE_MS = 300000;
|
|
92
104
|
export declare function verifyLedger(dir: string, opts?: VerifyOptions): Promise<VerifyResult>;
|
package/dist/verify-ledger.js
CHANGED
|
@@ -17,6 +17,13 @@
|
|
|
17
17
|
* `{ ref, effectHash, witnessClass, resultHash }`, compared after
|
|
18
18
|
* decode, and the receipt's effect (`body.effects[0]`, every
|
|
19
19
|
* field the writer put there) must be that same row. A
|
|
20
|
+
* `third-party` row instead needs a one-row receipt verified
|
|
21
|
+
* under `thirdPartyPublicKeyPems` supplied by the reader, equal
|
|
22
|
+
* to the stored row and bound by ref and effectHash. No body
|
|
23
|
+
* attestation is required; its resultHash is unattested. Keys
|
|
24
|
+
* shared with record/effect/witness signers are refused. Without
|
|
25
|
+
* a third-party pin the row is named unchecked and is not bound.
|
|
26
|
+
* For self/same-org rows, a
|
|
20
27
|
* thrown call relaxes only the hash, and only for the tool that
|
|
21
28
|
* was allowed: some decision on that ref has `decision: "allow"`
|
|
22
29
|
* and this row's class is exactly that decision's `effectClass`
|
|
@@ -93,6 +100,7 @@
|
|
|
93
100
|
* copy they hold themselves.
|
|
94
101
|
*/
|
|
95
102
|
import { createPublicKey } from "node:crypto";
|
|
103
|
+
import { samePublicKey, thirdPartyReceiptCoversRow } from "./third-party.js";
|
|
96
104
|
import { existsSync, lstatSync, readFileSync } from "node:fs";
|
|
97
105
|
import { join } from "node:path";
|
|
98
106
|
import { checkpointHash, findCheckpointChainBreak, totalsFromDecisionRecords, verifyCheckpoint, verifyCheckpointUnderPin, } from "@cedulon/checkpoint";
|
|
@@ -344,7 +352,7 @@ function effectProblem(effect, ref) {
|
|
|
344
352
|
}
|
|
345
353
|
function firstReceiptKey(rows, sameOrg) {
|
|
346
354
|
for (const row of rows) {
|
|
347
|
-
if (
|
|
355
|
+
if (row.witnessClass !== (sameOrg ? "same-org" : "self"))
|
|
348
356
|
continue;
|
|
349
357
|
const pem = row.receipt?.publicKeyPem;
|
|
350
358
|
if (isPublicKeyPem(pem))
|
|
@@ -488,13 +496,18 @@ function indexLineProblems(text) {
|
|
|
488
496
|
let parsedOk = 0;
|
|
489
497
|
for (let i = 0; i < filled.length; i += 1) {
|
|
490
498
|
const last = i === filled.length - 1;
|
|
499
|
+
let parsed;
|
|
491
500
|
try {
|
|
492
|
-
JSON.parse(filled[i].line);
|
|
493
|
-
parsedOk += 1;
|
|
501
|
+
parsed = JSON.parse(filled[i].line);
|
|
494
502
|
}
|
|
495
503
|
catch {
|
|
496
504
|
if (!last || parsedOk === 0)
|
|
497
505
|
problems.push(`index line ${filled[i].n} is not JSON`);
|
|
506
|
+
continue;
|
|
507
|
+
}
|
|
508
|
+
parsedOk += 1;
|
|
509
|
+
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
510
|
+
problems.push(`index line ${filled[i].n} is not a JSON object`);
|
|
498
511
|
}
|
|
499
512
|
}
|
|
500
513
|
return problems;
|
|
@@ -560,6 +573,42 @@ function noteEffectCompleteness(effectRefs, bound, problems) {
|
|
|
560
573
|
}
|
|
561
574
|
return EFFECT_COMPLETENESS_CHECKED;
|
|
562
575
|
}
|
|
576
|
+
/** The core's default clock-skew allowance, which decision profile 6.1 applies unchanged. */
|
|
577
|
+
export const DEFAULT_BOUNDARY_ALLOWANCE_MS = 300_000;
|
|
578
|
+
/**
|
|
579
|
+
* Every attested allow expects an effect row under its ref (decision profile
|
|
580
|
+
* 6.1). This reads the signed records and the rows, never `index.jsonl`,
|
|
581
|
+
* which is unsigned. A ref with a primary row, bound or not, is not this
|
|
582
|
+
* finding: a row that fails to bind is already named for what is wrong with
|
|
583
|
+
* it. The writer appends the row after the call returns, so an allow within
|
|
584
|
+
* the allowance of the newest attested record may still be in flight; that
|
|
585
|
+
* one is `boundary-deferred` and does not change `ok`. An older allow with no
|
|
586
|
+
* row is `decision-without-effect`.
|
|
587
|
+
*/
|
|
588
|
+
function allowsWithoutEffect(attested, rowRefs, allowanceMs) {
|
|
589
|
+
let newest = -Infinity;
|
|
590
|
+
for (const r of attested) {
|
|
591
|
+
if (typeof r.claims.timestampMs === "number" && r.claims.timestampMs > newest)
|
|
592
|
+
newest = r.claims.timestampMs;
|
|
593
|
+
}
|
|
594
|
+
const findings = [];
|
|
595
|
+
const deferred = [];
|
|
596
|
+
const seen = new Set();
|
|
597
|
+
for (const r of attested) {
|
|
598
|
+
const ref = r.claims.ref;
|
|
599
|
+
if (r.claims.decision !== "allow" || typeof ref !== "string" || seen.has(ref))
|
|
600
|
+
continue;
|
|
601
|
+
seen.add(ref);
|
|
602
|
+
if (rowRefs.has(ref))
|
|
603
|
+
continue;
|
|
604
|
+
const t = r.claims.timestampMs;
|
|
605
|
+
if (typeof t === "number" && newest - t <= allowanceMs)
|
|
606
|
+
deferred.push(ref);
|
|
607
|
+
else
|
|
608
|
+
findings.push(ref);
|
|
609
|
+
}
|
|
610
|
+
return { findings, deferred };
|
|
611
|
+
}
|
|
563
612
|
function tailStatement(dir, records, checkpointPublicKeyPem, attested = records) {
|
|
564
613
|
const checkpointFile = checkpointsPath(dir);
|
|
565
614
|
const checkpointLink = symbolicLinkProblem(checkpointFile);
|
|
@@ -878,6 +927,7 @@ function unreadableResult(dir, problem) {
|
|
|
878
927
|
chainBreakAt: null,
|
|
879
928
|
effectsBound: 0,
|
|
880
929
|
effectsOrphaned: 0,
|
|
930
|
+
effectsDeferred: [],
|
|
881
931
|
trust,
|
|
882
932
|
effectTrust: trust,
|
|
883
933
|
witnessTrust: trust,
|
|
@@ -941,6 +991,7 @@ async function verifyLedgerUnchecked(dir, opts = {}) {
|
|
|
941
991
|
chainBreakAt: null,
|
|
942
992
|
effectsBound: 0,
|
|
943
993
|
effectsOrphaned: 0,
|
|
994
|
+
effectsDeferred: [],
|
|
944
995
|
trust: { source: "none", publicKeyPem: null, note: "no records, so no key was used" },
|
|
945
996
|
effectTrust: effectTrustOf(opts.effectPublicKeyPem?.trim() ?? "", null, 0),
|
|
946
997
|
witnessTrust: witnessTrustOf(opts.witnessPublicKeyPem?.trim() ?? "", null, 0),
|
|
@@ -1052,8 +1103,27 @@ async function verifyLedgerUnchecked(dir, opts = {}) {
|
|
|
1052
1103
|
const sameKeyTaken = firstReceiptKey(effectRows, true);
|
|
1053
1104
|
const effectKey = pinnedEffect !== "" ? pinnedEffect : selfKeyTaken;
|
|
1054
1105
|
const witnessKey = pinnedWitness !== "" ? pinnedWitness : sameKeyTaken;
|
|
1055
|
-
const selfCount = effectRows.filter((row) => row.witnessClass
|
|
1056
|
-
const sameCount = effectRows.
|
|
1106
|
+
const selfCount = effectRows.filter((row) => row.witnessClass === "self").length;
|
|
1107
|
+
const sameCount = effectRows.filter((row) => row.witnessClass === "same-org").length;
|
|
1108
|
+
const thirdPartyKeys = (opts.thirdPartyPublicKeyPems ?? []).filter((key) => {
|
|
1109
|
+
const roles = [
|
|
1110
|
+
["record", [anahtar, ...records.map(r => r.publicKeyPem)]],
|
|
1111
|
+
["effect", [effectKey, ...effectRows.filter(e => e.witnessClass === "self").map(e => e.receipt?.publicKeyPem)]],
|
|
1112
|
+
["witness", [witnessKey, ...effectRows.filter(e => e.witnessClass === "same-org").map(e => e.receipt?.publicKeyPem)]],
|
|
1113
|
+
];
|
|
1114
|
+
for (const [role, known] of roles) {
|
|
1115
|
+
if (known.some(pem => samePublicKey(key, pem))) {
|
|
1116
|
+
problems.push(`third-party key equals ${role} key; not accepted for third-party rows`);
|
|
1117
|
+
return false;
|
|
1118
|
+
}
|
|
1119
|
+
}
|
|
1120
|
+
const refusal = ed25519Refusal(key);
|
|
1121
|
+
if (refusal) {
|
|
1122
|
+
problems.push(`third-party key rejected: ${refusal}`);
|
|
1123
|
+
return false;
|
|
1124
|
+
}
|
|
1125
|
+
return true;
|
|
1126
|
+
});
|
|
1057
1127
|
const effectTrust = effectTrustOf(pinnedEffect, selfKeyTaken, selfCount);
|
|
1058
1128
|
const witnessTrust = witnessTrustOf(pinnedWitness, sameKeyTaken, sameCount);
|
|
1059
1129
|
if (pinnedEffect !== "" && pinnedWitness === "" && sameCount > 0) {
|
|
@@ -1075,6 +1145,12 @@ async function verifyLedgerUnchecked(dir, opts = {}) {
|
|
|
1075
1145
|
const effectHash = typeof e.row?.effectHash === "string" ? e.row.effectHash : null;
|
|
1076
1146
|
const effectClass = typeof e.row?.effectClass === "string" ? e.row.effectClass : "";
|
|
1077
1147
|
if (effectClass === "duplicate-effect") {
|
|
1148
|
+
// Duplicate refusals are body observations, never downstream receipts.
|
|
1149
|
+
if (e.witnessClass === "third-party") {
|
|
1150
|
+
effectsOrphaned += 1;
|
|
1151
|
+
problems.push(`third-party row ${ref ?? "(missing)"} cannot be a duplicate-effect refusal`);
|
|
1152
|
+
continue;
|
|
1153
|
+
}
|
|
1078
1154
|
const sig = typeof e.attestation?.coseHex === "string" ? e.attestation.coseHex : "";
|
|
1079
1155
|
const dupKey = `${ref ?? ""}\0${sig}`;
|
|
1080
1156
|
if (sig !== "" && duplicateSeen.has(dupKey)) {
|
|
@@ -1108,7 +1184,7 @@ async function verifyLedgerUnchecked(dir, opts = {}) {
|
|
|
1108
1184
|
primarySeen.add(ref);
|
|
1109
1185
|
const hashes = ref === null ? undefined : hashesByRef.get(ref);
|
|
1110
1186
|
const hashMatch = effectHash !== null && hashes?.has(effectHash) === true;
|
|
1111
|
-
if (effectClass.endsWith(":threw")) {
|
|
1187
|
+
if (e.witnessClass !== "third-party" && effectClass.endsWith(":threw")) {
|
|
1112
1188
|
const allowed = ref === null ? undefined : allowedClassesByRef.get(ref);
|
|
1113
1189
|
let thrownOk = false;
|
|
1114
1190
|
if (allowed) {
|
|
@@ -1132,7 +1208,17 @@ async function verifyLedgerUnchecked(dir, opts = {}) {
|
|
|
1132
1208
|
: `effect hash does not match its decision: ref ${ref}`);
|
|
1133
1209
|
continue;
|
|
1134
1210
|
}
|
|
1135
|
-
if (
|
|
1211
|
+
if (e.witnessClass === "third-party") {
|
|
1212
|
+
if (!thirdPartyKeys.some(key => thirdPartyReceiptCoversRow(e.receipt, e.row, key))) {
|
|
1213
|
+
effectsOrphaned += 1;
|
|
1214
|
+
problems.push((opts.thirdPartyPublicKeyPems?.length ?? 0) === 0
|
|
1215
|
+
? `third-party row ${ref ?? "(missing)"} unchecked: no pinned third-party key`
|
|
1216
|
+
: `third-party row ${ref ?? "(missing)"} receipt does not cover the row under an accepted pinned third-party key`);
|
|
1217
|
+
continue;
|
|
1218
|
+
}
|
|
1219
|
+
// Only the effect row is signed; resultHash has no third-party attestation.
|
|
1220
|
+
}
|
|
1221
|
+
else if (!effectSignatureCoversRow(e, e.witnessClass === "same-org" ? witnessKey : effectKey)) {
|
|
1136
1222
|
effectsOrphaned += 1;
|
|
1137
1223
|
problems.push(effectProblem(e, ref));
|
|
1138
1224
|
continue;
|
|
@@ -1141,6 +1227,8 @@ async function verifyLedgerUnchecked(dir, opts = {}) {
|
|
|
1141
1227
|
if (ref !== null)
|
|
1142
1228
|
boundPrimaryRefs.add(ref);
|
|
1143
1229
|
}
|
|
1230
|
+
const unbound = allowsWithoutEffect(attested, primarySeen, opts.boundaryAllowanceMs ?? DEFAULT_BOUNDARY_ALLOWANCE_MS);
|
|
1231
|
+
problems.push(...unbound.findings.map((ref) => `decision-without-effect ${ref}`));
|
|
1144
1232
|
const indexed = indexStatement(dir, refler);
|
|
1145
1233
|
const effectCompleteness = noteEffectCompleteness(indexed.effectRefs, boundPrimaryRefs, problems);
|
|
1146
1234
|
const inputs = inputsStatement(files.inputs, records, problems);
|
|
@@ -1160,6 +1248,7 @@ async function verifyLedgerUnchecked(dir, opts = {}) {
|
|
|
1160
1248
|
chainBreakAt,
|
|
1161
1249
|
effectsBound,
|
|
1162
1250
|
effectsOrphaned,
|
|
1251
|
+
effectsDeferred: unbound.deferred,
|
|
1163
1252
|
trust,
|
|
1164
1253
|
effectTrust,
|
|
1165
1254
|
witnessTrust,
|