@verax-ai/proxy 0.5.1 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/approvals.js +2 -1
- package/dist/index.d.ts +4 -3
- package/dist/index.js +3 -2
- package/dist/operator-removal-sync.d.ts +58 -0
- package/dist/operator-removal-sync.js +170 -0
- package/dist/policy.d.ts +28 -3
- package/dist/policy.js +43 -3
- package/dist/proxy.js +17 -5
- package/dist/reconcile.d.ts +7 -0
- package/dist/reconcile.js +49 -11
- package/dist/types.d.ts +14 -0
- package/package.json +1 -1
package/dist/approvals.js
CHANGED
|
@@ -8,6 +8,7 @@ import { join } from "node:path";
|
|
|
8
8
|
import { signDecisionRecord } from "@cedulon/core";
|
|
9
9
|
import { appendDurable, lookupDecisionByRef, lookupResolvedBy, noteResolution } from "./ledger.js";
|
|
10
10
|
import { effectDescriptor, sha256Canonical } from "./hash.js";
|
|
11
|
+
import { terminalText } from "./policy.js";
|
|
11
12
|
import { inputsLogFor } from "./inputs.js";
|
|
12
13
|
import { SerialQueue } from "./serial-queue.js";
|
|
13
14
|
function withStatus(cur, status, extra) {
|
|
@@ -444,7 +445,7 @@ export async function drainApprovalCommands(dir, apply) {
|
|
|
444
445
|
// Leave the processing name in place. The bytes stay either way.
|
|
445
446
|
}
|
|
446
447
|
const message = err instanceof Error ? err.message : String(err);
|
|
447
|
-
process.stderr.write(`verax-drain: unreadable ${file}: ${message}\n`);
|
|
448
|
+
process.stderr.write(`verax-drain: unreadable ${terminalText(file)}: ${terminalText(message)}\n`);
|
|
448
449
|
continue;
|
|
449
450
|
}
|
|
450
451
|
try {
|
package/dist/index.d.ts
CHANGED
|
@@ -1,22 +1,23 @@
|
|
|
1
1
|
export { createProxy, LedgerDenyUnrecorded } from "./proxy.ts";
|
|
2
2
|
export { effectDescriptor, sha256Canonical } from "./hash.ts";
|
|
3
3
|
export { samePublicKey, thirdPartyReceiptRejection } from "./third-party.ts";
|
|
4
|
-
export { loadPolicy, TERMINAL_CONTROL_CLASS } from "./policy.ts";
|
|
4
|
+
export { loadPolicy, TERMINAL_CONTROL_CLASS, DISPLAY_ESCAPE_CLASS, terminalText, terminalLines } from "./policy.ts";
|
|
5
5
|
export type { PolicyParseOptions } from "./policy.ts";
|
|
6
6
|
export { FileLedger, MemoryLedger, readLedgerTail, lockHolderAlive, lockState, lockBeatOf } from "./ledger.ts";
|
|
7
7
|
export { indexCoverage, listPieceFiles } from "./ledger-manifest.ts";
|
|
8
8
|
export { explain } from "./explain.ts";
|
|
9
9
|
export { approvePending, approvalsLogFor, createApprovalBudgetGuard, enqueueApprovalCommand, loadApprovalsFromDir, } from "./approvals.ts";
|
|
10
|
-
export { loadDecisionRefsFromDir, loadEffectsFromDir, parseCardCsv, parseChannelJsonl, reconcile } from "./reconcile.ts";
|
|
10
|
+
export { loadDecisionRefsFromDir, loadDecisionRequestHashesFromDir, loadEffectsFromDir, parseCardCsv, parseChannelJsonl, reconcile, } from "./reconcile.ts";
|
|
11
11
|
export { tenantKey } from "./tenant.ts";
|
|
12
12
|
export { diskProbe } from "./disk.ts";
|
|
13
13
|
export { checkpointsPath, readCheckpointFile } from "./checkpoints.ts";
|
|
14
14
|
export { signEffectAttestation } from "./ledger.ts";
|
|
15
15
|
export { spokenReason } from "./spoken-reason.ts";
|
|
16
|
+
export { OPERATOR_REMOVED_DIR, operatorRemovalMark } from "./operator-removal-sync.ts";
|
|
16
17
|
export type { ApprovalRow, ApproveResult } from "./approvals.ts";
|
|
17
18
|
export type { CardCsvOpts, ChannelRow, ReconcileReport } from "./reconcile.ts";
|
|
18
19
|
export type { FileLedgerOpts, LedgerCounts, LockHolder } from "./ledger.ts";
|
|
19
|
-
export type { ApprovalSignature, ControlSignature, RevokeInput } from "./types.ts";
|
|
20
|
+
export type { ApprovalSignature, ControlSignature, OperatorRemovalInput, RevokeInput } from "./types.ts";
|
|
20
21
|
export type { EffectSigner, ExplainChain, ExplainPair, ExplainFinding, ExplainOpts, ExplainResult, ExplainTrustRoot, ExplainWarning, ExtractWindow, Ledger, LedgerEffect, Policy, PolicyDecision, Principal, ProxyDeps, RecordSigner, ToolCall, ToolResult, WitnessClass, } from "./types.ts";
|
|
21
22
|
export { verifyLedger } from "./verify-ledger.ts";
|
|
22
23
|
export type { VerifyResult, VerifyOptions, VerifyTrust, VerifyControl, VerifyInputs, VerifyAnchors, } from "./verify-ledger.ts";
|
package/dist/index.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
export { createProxy, LedgerDenyUnrecorded } from "./proxy.js";
|
|
2
2
|
export { effectDescriptor, sha256Canonical } from "./hash.js";
|
|
3
3
|
export { samePublicKey, thirdPartyReceiptRejection } from "./third-party.js";
|
|
4
|
-
export { loadPolicy, TERMINAL_CONTROL_CLASS } from "./policy.js";
|
|
4
|
+
export { loadPolicy, TERMINAL_CONTROL_CLASS, DISPLAY_ESCAPE_CLASS, terminalText, terminalLines } from "./policy.js";
|
|
5
5
|
export { FileLedger, MemoryLedger, readLedgerTail, lockHolderAlive, lockState, lockBeatOf } from "./ledger.js";
|
|
6
6
|
export { indexCoverage, listPieceFiles } from "./ledger-manifest.js";
|
|
7
7
|
export { explain } from "./explain.js";
|
|
8
8
|
export { approvePending, approvalsLogFor, createApprovalBudgetGuard, enqueueApprovalCommand, loadApprovalsFromDir, } from "./approvals.js";
|
|
9
|
-
export { loadDecisionRefsFromDir, loadEffectsFromDir, parseCardCsv, parseChannelJsonl, reconcile } from "./reconcile.js";
|
|
9
|
+
export { loadDecisionRefsFromDir, loadDecisionRequestHashesFromDir, loadEffectsFromDir, parseCardCsv, parseChannelJsonl, reconcile, } from "./reconcile.js";
|
|
10
10
|
export { tenantKey } from "./tenant.js";
|
|
11
11
|
// The body writes checkpoints and signs its own effect attestations. Both were
|
|
12
12
|
// reached through a relative path into this package, which only exists inside
|
|
@@ -18,6 +18,7 @@ export { diskProbe } from "./disk.js";
|
|
|
18
18
|
export { checkpointsPath, readCheckpointFile } from "./checkpoints.js";
|
|
19
19
|
export { signEffectAttestation } from "./ledger.js";
|
|
20
20
|
export { spokenReason } from "./spoken-reason.js";
|
|
21
|
+
export { OPERATOR_REMOVED_DIR, operatorRemovalMark } from "./operator-removal-sync.js";
|
|
21
22
|
export { verifyLedger } from "./verify-ledger.js";
|
|
22
23
|
export { anchorEntryHash, appendAnchor, readAnchors } from "./anchors.js";
|
|
23
24
|
export { verifyReceipt } from "./transparency-receipt.js";
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { SignedDecisionRecord } from "@cedulon/core";
|
|
2
|
+
import type { LedgerWriter } from "./ledger.ts";
|
|
3
|
+
import type { InputsLog, RecordSigner } from "./types.ts";
|
|
4
|
+
/**
|
|
5
|
+
* Same directory as `packages/body/src/operator-credentials.ts`. The directory
|
|
6
|
+
* stays the switch: the body treats a passkey as removed when its mark is
|
|
7
|
+
* one of the names there, and it does not read what that name holds. This
|
|
8
|
+
* module keeps the ledger's account of that set of names.
|
|
9
|
+
*
|
|
10
|
+
* The ledger's list is replayed from its `verax.operator` records of effect
|
|
11
|
+
* class `verax.control`. A mark in the directory and not in that list is one
|
|
12
|
+
* `passkey-removed` record; a mark in that list and gone from the directory
|
|
13
|
+
* is one `passkey-removal-undone` record, because taking the mark away lets
|
|
14
|
+
* that passkey in again and nothing else would say so. Both are denies with
|
|
15
|
+
* no effect: a removal refuses, it does not let a call through, and a
|
|
16
|
+
* verifier that predates these records reads them as ordinary denies.
|
|
17
|
+
*
|
|
18
|
+
* A removal is not in the ledger until the body opens or the next call
|
|
19
|
+
* reaches the proxy. The command that writes the mark does not.
|
|
20
|
+
*
|
|
21
|
+
* A directory that cannot be listed is left unwritten. It is not an empty
|
|
22
|
+
* directory: the marks were not taken away, and a `passkey-removal-undone`
|
|
23
|
+
* record would say that they were.
|
|
24
|
+
*/
|
|
25
|
+
export declare const OPERATOR_REMOVED_DIR = "operator-removed";
|
|
26
|
+
/** The name a removed passkey's mark is filed under. An id can be longer than a file name may be. */
|
|
27
|
+
export declare function operatorRemovalMark(id: string): string;
|
|
28
|
+
/** `unlistable` is a directory we could not read. It is not an empty name list. */
|
|
29
|
+
type Listing = {
|
|
30
|
+
kind: "marks";
|
|
31
|
+
names: string[];
|
|
32
|
+
} | {
|
|
33
|
+
kind: "unlistable";
|
|
34
|
+
};
|
|
35
|
+
export type OperatorRemovalMemory = {
|
|
36
|
+
loaded: boolean;
|
|
37
|
+
stamp: Listing | null;
|
|
38
|
+
removed: Set<string>;
|
|
39
|
+
};
|
|
40
|
+
export declare function createOperatorRemovalMemory(): OperatorRemovalMemory;
|
|
41
|
+
/**
|
|
42
|
+
* True when the sorted mark names, or the fact that they cannot be listed,
|
|
43
|
+
* are unchanged since the last sync. A directory timestamp is not a stamp:
|
|
44
|
+
* the names are the list, and a timestamp can change without them.
|
|
45
|
+
*/
|
|
46
|
+
export declare function operatorRemovalUnchanged(stateDir: string, memory: OperatorRemovalMemory): boolean;
|
|
47
|
+
export declare function syncOperatorRemovalControl(opts: {
|
|
48
|
+
stateDir: string;
|
|
49
|
+
now: () => number;
|
|
50
|
+
nonce: () => string;
|
|
51
|
+
policyHash: string;
|
|
52
|
+
recordSigner: RecordSigner;
|
|
53
|
+
inputsLog: InputsLog;
|
|
54
|
+
decisions: () => Promise<SignedDecisionRecord[]>;
|
|
55
|
+
memory: OperatorRemovalMemory;
|
|
56
|
+
writer: LedgerWriter;
|
|
57
|
+
}): Promise<void>;
|
|
58
|
+
export {};
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { readdirSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { signDecisionRecord } from "@cedulon/core";
|
|
5
|
+
import { sha256Canonical } from "./hash.js";
|
|
6
|
+
/**
|
|
7
|
+
* Same directory as `packages/body/src/operator-credentials.ts`. The directory
|
|
8
|
+
* stays the switch: the body treats a passkey as removed when its mark is
|
|
9
|
+
* one of the names there, and it does not read what that name holds. This
|
|
10
|
+
* module keeps the ledger's account of that set of names.
|
|
11
|
+
*
|
|
12
|
+
* The ledger's list is replayed from its `verax.operator` records of effect
|
|
13
|
+
* class `verax.control`. A mark in the directory and not in that list is one
|
|
14
|
+
* `passkey-removed` record; a mark in that list and gone from the directory
|
|
15
|
+
* is one `passkey-removal-undone` record, because taking the mark away lets
|
|
16
|
+
* that passkey in again and nothing else would say so. Both are denies with
|
|
17
|
+
* no effect: a removal refuses, it does not let a call through, and a
|
|
18
|
+
* verifier that predates these records reads them as ordinary denies.
|
|
19
|
+
*
|
|
20
|
+
* A removal is not in the ledger until the body opens or the next call
|
|
21
|
+
* reaches the proxy. The command that writes the mark does not.
|
|
22
|
+
*
|
|
23
|
+
* A directory that cannot be listed is left unwritten. It is not an empty
|
|
24
|
+
* directory: the marks were not taken away, and a `passkey-removal-undone`
|
|
25
|
+
* record would say that they were.
|
|
26
|
+
*/
|
|
27
|
+
export const OPERATOR_REMOVED_DIR = "operator-removed";
|
|
28
|
+
const SUBJECT = "verax.operator";
|
|
29
|
+
const CONTROL_EFFECT = "verax.control";
|
|
30
|
+
const MARK_NAME = /^[0-9a-f]{64}$/;
|
|
31
|
+
/** The name a removed passkey's mark is filed under. An id can be longer than a file name may be. */
|
|
32
|
+
export function operatorRemovalMark(id) {
|
|
33
|
+
return createHash("sha256").update(id, "utf8").digest("hex");
|
|
34
|
+
}
|
|
35
|
+
export function createOperatorRemovalMemory() {
|
|
36
|
+
return { loaded: false, stamp: null, removed: new Set() };
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* True when the sorted mark names, or the fact that they cannot be listed,
|
|
40
|
+
* are unchanged since the last sync. A directory timestamp is not a stamp:
|
|
41
|
+
* the names are the list, and a timestamp can change without them.
|
|
42
|
+
*/
|
|
43
|
+
export function operatorRemovalUnchanged(stateDir, memory) {
|
|
44
|
+
const listing = listMarks(stateDir);
|
|
45
|
+
if (!sameListing(memory.stamp, listing))
|
|
46
|
+
return false;
|
|
47
|
+
// Nothing to reconcile until the names can be read. Opening the ledger on
|
|
48
|
+
// every such call would only repeat the same miss.
|
|
49
|
+
if (listing.kind === "unlistable")
|
|
50
|
+
return true;
|
|
51
|
+
return memory.loaded;
|
|
52
|
+
}
|
|
53
|
+
export async function syncOperatorRemovalControl(opts) {
|
|
54
|
+
const memory = opts.memory;
|
|
55
|
+
if (operatorRemovalUnchanged(opts.stateDir, memory))
|
|
56
|
+
return;
|
|
57
|
+
let listing = listMarks(opts.stateDir);
|
|
58
|
+
for (let attempt = 0; attempt < 3; attempt += 1) {
|
|
59
|
+
const after = listMarks(opts.stateDir);
|
|
60
|
+
if (sameListing(listing, after))
|
|
61
|
+
break;
|
|
62
|
+
listing = after;
|
|
63
|
+
}
|
|
64
|
+
if (listing.kind === "unlistable") {
|
|
65
|
+
// Remember the miss so the next call does not open the ledger again.
|
|
66
|
+
// Do not replay and do not write: an unlisted directory is not "no marks".
|
|
67
|
+
memory.stamp = listing;
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
if (!memory.loaded) {
|
|
71
|
+
memory.removed = await replay(opts);
|
|
72
|
+
memory.loaded = true;
|
|
73
|
+
}
|
|
74
|
+
try {
|
|
75
|
+
const inDir = new Set(listing.names);
|
|
76
|
+
for (const mark of listing.names) {
|
|
77
|
+
if (memory.removed.has(mark))
|
|
78
|
+
continue;
|
|
79
|
+
const atMs = opts.now();
|
|
80
|
+
await writeRemoval(opts, { action: "remove", mark, by: "unknown", via: "file", atMs });
|
|
81
|
+
memory.removed.add(mark);
|
|
82
|
+
}
|
|
83
|
+
for (const mark of [...memory.removed]) {
|
|
84
|
+
if (inDir.has(mark))
|
|
85
|
+
continue;
|
|
86
|
+
const atMs = opts.now();
|
|
87
|
+
await writeRemoval(opts, { action: "restore", mark, by: "unknown", via: "file", atMs });
|
|
88
|
+
memory.removed.delete(mark);
|
|
89
|
+
}
|
|
90
|
+
memory.stamp = listing;
|
|
91
|
+
}
|
|
92
|
+
catch (err) {
|
|
93
|
+
// A partial append is already on disk. Replay the ledger next time rather
|
|
94
|
+
// than trust a list that may be ahead of it or behind it.
|
|
95
|
+
memory.loaded = false;
|
|
96
|
+
throw err;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
function listMarks(stateDir) {
|
|
100
|
+
let names;
|
|
101
|
+
try {
|
|
102
|
+
names = readdirSync(join(stateDir, OPERATOR_REMOVED_DIR));
|
|
103
|
+
}
|
|
104
|
+
catch (err) {
|
|
105
|
+
// Missing means nothing has been removed. Any other error means the
|
|
106
|
+
// names cannot be known; the caller must not treat that as an empty list.
|
|
107
|
+
if (err.code === "ENOENT")
|
|
108
|
+
return { kind: "marks", names: [] };
|
|
109
|
+
return { kind: "unlistable" };
|
|
110
|
+
}
|
|
111
|
+
// The body matches the name and does not open it, so a mark counts whether
|
|
112
|
+
// it is a file or a directory, and any other name is ignored.
|
|
113
|
+
return { kind: "marks", names: names.filter((name) => MARK_NAME.test(name)).sort() };
|
|
114
|
+
}
|
|
115
|
+
function sameListing(a, b) {
|
|
116
|
+
if (a === null || a.kind !== b.kind)
|
|
117
|
+
return false;
|
|
118
|
+
if (a.kind === "unlistable" || b.kind === "unlistable")
|
|
119
|
+
return true;
|
|
120
|
+
if (a.names.length !== b.names.length)
|
|
121
|
+
return false;
|
|
122
|
+
for (let i = 0; i < a.names.length; i += 1) {
|
|
123
|
+
if (a.names[i] !== b.names[i])
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
return true;
|
|
127
|
+
}
|
|
128
|
+
async function replay(opts) {
|
|
129
|
+
const removed = new Set();
|
|
130
|
+
for (const decision of await opts.decisions()) {
|
|
131
|
+
if (decision.claims.subject !== SUBJECT || decision.claims.effectClass !== CONTROL_EFFECT)
|
|
132
|
+
continue;
|
|
133
|
+
const ref = decision.claims.ref;
|
|
134
|
+
if (typeof ref !== "string")
|
|
135
|
+
continue;
|
|
136
|
+
const row = (await opts.inputsLog.get(ref))?.operatorRemoval;
|
|
137
|
+
if (!row || typeof row.mark !== "string")
|
|
138
|
+
continue;
|
|
139
|
+
if (row.action === "remove")
|
|
140
|
+
removed.add(row.mark);
|
|
141
|
+
else if (row.action === "restore")
|
|
142
|
+
removed.delete(row.mark);
|
|
143
|
+
}
|
|
144
|
+
return removed;
|
|
145
|
+
}
|
|
146
|
+
async function writeRemoval(opts, operatorRemoval) {
|
|
147
|
+
const ref = opts.nonce();
|
|
148
|
+
const inputs = {
|
|
149
|
+
principal: { brain: "verax-proxy", scopes: [] },
|
|
150
|
+
inputs: [],
|
|
151
|
+
operatorRemoval,
|
|
152
|
+
};
|
|
153
|
+
const inputsHash = sha256Canonical(inputs);
|
|
154
|
+
await opts.inputsLog.append(ref, inputs);
|
|
155
|
+
await opts.writer.appendDecisionChained((prevRecordHash) => signDecisionRecord({
|
|
156
|
+
decider: "verax-proxy",
|
|
157
|
+
subject: SUBJECT,
|
|
158
|
+
requestHash: sha256Canonical(operatorRemoval),
|
|
159
|
+
policyHash: opts.policyHash,
|
|
160
|
+
inputsHash,
|
|
161
|
+
decision: "deny",
|
|
162
|
+
reasonCode: operatorRemoval.action === "remove" ? "passkey-removed" : "passkey-removal-undone",
|
|
163
|
+
ref,
|
|
164
|
+
effectHash: null,
|
|
165
|
+
effectClass: CONTROL_EFFECT,
|
|
166
|
+
timestampMs: operatorRemoval.atMs,
|
|
167
|
+
nonce: ref,
|
|
168
|
+
prevRecordHash,
|
|
169
|
+
}, opts.recordSigner.privateKeyPem, opts.recordSigner.publicKeyPem));
|
|
170
|
+
}
|
package/dist/policy.d.ts
CHANGED
|
@@ -47,11 +47,36 @@ export type PolicyParseOptions = {
|
|
|
47
47
|
unknownKeys?: "refuse" | "ignore";
|
|
48
48
|
};
|
|
49
49
|
/**
|
|
50
|
-
*
|
|
51
|
-
* C0 and C1), every format character (Cf: bidi, ZWJ/ZWNJ,
|
|
52
|
-
* line and paragraph separators (Zl, Zp).
|
|
50
|
+
* What the spend gate refuses in payee, reference and currency: every
|
|
51
|
+
* control (Cc, C0 and C1), every format character (Cf: bidi, ZWJ/ZWNJ,
|
|
52
|
+
* U+FEFF), and the line and paragraph separators (Zl, Zp).
|
|
53
53
|
*/
|
|
54
54
|
export declare const TERMINAL_CONTROL_CLASS = "\\p{Cc}\\p{Cf}\\p{Zl}\\p{Zp}";
|
|
55
|
+
/**
|
|
56
|
+
* What the three operator screens show as the text itself: the approve
|
|
57
|
+
* prompt, the pin listing, and the door page. Past control and format
|
|
58
|
+
* characters, a combining mark draws over the character before it, and an
|
|
59
|
+
* unassigned or private-use code point has no glyph to rely on. A surrogate
|
|
60
|
+
* is not a character. Text for the inside of a character class: the
|
|
61
|
+
* spend-gate class, plus default-ignorable code points, variation selectors,
|
|
62
|
+
* the braille blank (U+2800), every space separator except U+0020, combining
|
|
63
|
+
* marks (M), unassigned code points (Cn), private-use code points (Co), and
|
|
64
|
+
* surrogates (Cs).
|
|
65
|
+
* Turkish and Latin letters arrive as one code point (NFC) and are left as
|
|
66
|
+
* they are. Writing that uses combining marks (Hindi, Thai, and the like)
|
|
67
|
+
* is shown escaped on these screens. The bytes that were recorded do not
|
|
68
|
+
* change.
|
|
69
|
+
*/
|
|
70
|
+
export declare const DISPLAY_ESCAPE_CLASS: string;
|
|
71
|
+
/**
|
|
72
|
+
* Printed form of text this process did not write. Same `\uXXXX` per UTF-16
|
|
73
|
+
* unit as the pin listing, but no added quotes and no doubled backslash: a
|
|
74
|
+
* Windows path in a diagnostic line stays readable. Escape only at the
|
|
75
|
+
* write. The thrown message is what the ledger and the agent still receive.
|
|
76
|
+
*/
|
|
77
|
+
export declare function terminalText(text: string): string;
|
|
78
|
+
/** Line breaks the body wrote on purpose stay; every other class member is escaped. */
|
|
79
|
+
export declare function terminalLines(text: string): string;
|
|
55
80
|
export declare function parsePolicyDocument(json: unknown, options?: PolicyParseOptions): PolicyDocument;
|
|
56
81
|
export declare function ruleTextHash(text: string): string;
|
|
57
82
|
export declare function loadPolicy(json: unknown, options?: PolicyParseOptions): Policy;
|
package/dist/policy.js
CHANGED
|
@@ -169,11 +169,51 @@ function asSpend(raw, id, options) {
|
|
|
169
169
|
return spend;
|
|
170
170
|
}
|
|
171
171
|
/**
|
|
172
|
-
*
|
|
173
|
-
* C0 and C1), every format character (Cf: bidi, ZWJ/ZWNJ,
|
|
174
|
-
* line and paragraph separators (Zl, Zp).
|
|
172
|
+
* What the spend gate refuses in payee, reference and currency: every
|
|
173
|
+
* control (Cc, C0 and C1), every format character (Cf: bidi, ZWJ/ZWNJ,
|
|
174
|
+
* U+FEFF), and the line and paragraph separators (Zl, Zp).
|
|
175
175
|
*/
|
|
176
176
|
export const TERMINAL_CONTROL_CLASS = "\\p{Cc}\\p{Cf}\\p{Zl}\\p{Zp}";
|
|
177
|
+
/**
|
|
178
|
+
* What the three operator screens show as the text itself: the approve
|
|
179
|
+
* prompt, the pin listing, and the door page. Past control and format
|
|
180
|
+
* characters, a combining mark draws over the character before it, and an
|
|
181
|
+
* unassigned or private-use code point has no glyph to rely on. A surrogate
|
|
182
|
+
* is not a character. Text for the inside of a character class: the
|
|
183
|
+
* spend-gate class, plus default-ignorable code points, variation selectors,
|
|
184
|
+
* the braille blank (U+2800), every space separator except U+0020, combining
|
|
185
|
+
* marks (M), unassigned code points (Cn), private-use code points (Co), and
|
|
186
|
+
* surrogates (Cs).
|
|
187
|
+
* Turkish and Latin letters arrive as one code point (NFC) and are left as
|
|
188
|
+
* they are. Writing that uses combining marks (Hindi, Thai, and the like)
|
|
189
|
+
* is shown escaped on these screens. The bytes that were recorded do not
|
|
190
|
+
* change.
|
|
191
|
+
*/
|
|
192
|
+
export const DISPLAY_ESCAPE_CLASS = `${TERMINAL_CONTROL_CLASS}\\p{Default_Ignorable_Code_Point}\\p{Variation_Selector}` +
|
|
193
|
+
"\\u2800\\u00A0\\u1680\\u2000-\\u200A\\u202F\\u205F\\u3000\\p{M}\\p{Cn}\\p{Co}\\p{Cs}";
|
|
194
|
+
const DISPLAY_ESCAPE = new RegExp(`[${DISPLAY_ESCAPE_CLASS}]`, "gu");
|
|
195
|
+
function escapeDisplayUnit(ch) {
|
|
196
|
+
const cp = ch.codePointAt(0) ?? 0;
|
|
197
|
+
if (cp <= 0xffff)
|
|
198
|
+
return `\\u${cp.toString(16).padStart(4, "0")}`;
|
|
199
|
+
const rest = cp - 0x10000;
|
|
200
|
+
const high = 0xd800 + (rest >> 10);
|
|
201
|
+
const low = 0xdc00 + (rest & 0x3ff);
|
|
202
|
+
return `\\u${high.toString(16).padStart(4, "0")}\\u${low.toString(16).padStart(4, "0")}`;
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Printed form of text this process did not write. Same `\uXXXX` per UTF-16
|
|
206
|
+
* unit as the pin listing, but no added quotes and no doubled backslash: a
|
|
207
|
+
* Windows path in a diagnostic line stays readable. Escape only at the
|
|
208
|
+
* write. The thrown message is what the ledger and the agent still receive.
|
|
209
|
+
*/
|
|
210
|
+
export function terminalText(text) {
|
|
211
|
+
return text.replace(DISPLAY_ESCAPE, escapeDisplayUnit);
|
|
212
|
+
}
|
|
213
|
+
/** Line breaks the body wrote on purpose stay; every other class member is escaped. */
|
|
214
|
+
export function terminalLines(text) {
|
|
215
|
+
return text.split("\n").map((line) => terminalText(line)).join("\n");
|
|
216
|
+
}
|
|
177
217
|
const SPEND_CONTROL = new RegExp(`[${TERMINAL_CONTROL_CLASS}]`, "u");
|
|
178
218
|
function spendArgsInvalid(args) {
|
|
179
219
|
const keys = Object.keys(args);
|
package/dist/proxy.js
CHANGED
|
@@ -8,8 +8,10 @@ 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 { createOperatorRemovalMemory, operatorRemovalUnchanged, syncOperatorRemovalControl } from "./operator-removal-sync.js";
|
|
11
12
|
import { createRevokeMemory, revokeUnchanged, syncRevokeControl } from "./revoke-sync.js";
|
|
12
13
|
import { countedWork, hasPrimaryEffect, lookupDecisionByRef, lookupReauthByHash, lookupResolvedBy, noteResolution, noteTenantRef, } from "./ledger.js";
|
|
14
|
+
import { terminalText } from "./policy.js";
|
|
13
15
|
import { spokenReason } from "./spoken-reason.js";
|
|
14
16
|
import { tenantKey } from "./tenant.js";
|
|
15
17
|
function scopedClaimsRef(principal, raw) {
|
|
@@ -195,6 +197,7 @@ export function createProxy(deps) {
|
|
|
195
197
|
// sync reads control records once; this snapshot is updated in place.
|
|
196
198
|
const controlMemory = createControlMemory();
|
|
197
199
|
const revokeMemory = createRevokeMemory();
|
|
200
|
+
const operatorRemovalMemory = createOperatorRemovalMemory();
|
|
198
201
|
let controlSyncReported = false;
|
|
199
202
|
if (stateDir === null) {
|
|
200
203
|
process.stderr.write("verax-proxy: halt and disk limits are inactive without a ledger directory\n");
|
|
@@ -449,14 +452,19 @@ export function createProxy(deps) {
|
|
|
449
452
|
if (stateDir === null)
|
|
450
453
|
return;
|
|
451
454
|
// 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.
|
|
455
|
+
// snapshot too, or every call would read the whole ledger again. It must
|
|
456
|
+
// not keep a passkey removal out of the ledger either, so removal sync
|
|
457
|
+
// runs before revoke.
|
|
453
458
|
let haltSynced = false;
|
|
454
459
|
try {
|
|
455
|
-
if (controlUnchanged(stateDir, controlMemory) &&
|
|
460
|
+
if (controlUnchanged(stateDir, controlMemory) &&
|
|
461
|
+
revokeUnchanged(stateDir, revokeMemory) &&
|
|
462
|
+
operatorRemovalUnchanged(stateDir, operatorRemovalMemory))
|
|
456
463
|
return;
|
|
457
464
|
const run = async (writer) => {
|
|
458
|
-
//
|
|
459
|
-
// and resume records, which the
|
|
465
|
+
// The snapshots load from one read. Halt runs, then removal, then
|
|
466
|
+
// revoke. Halt writes only halt and resume records, which the
|
|
467
|
+
// removal and revoke replays do not read.
|
|
460
468
|
let read = null;
|
|
461
469
|
const shared = {
|
|
462
470
|
stateDir,
|
|
@@ -470,6 +478,7 @@ export function createProxy(deps) {
|
|
|
470
478
|
};
|
|
471
479
|
await syncHaltControl({ ...shared, memory: controlMemory });
|
|
472
480
|
haltSynced = true;
|
|
481
|
+
await syncOperatorRemovalControl({ ...shared, memory: operatorRemovalMemory });
|
|
473
482
|
await syncRevokeControl({ ...shared, memory: revokeMemory });
|
|
474
483
|
};
|
|
475
484
|
const host = deps.ledger;
|
|
@@ -489,6 +498,9 @@ export function createProxy(deps) {
|
|
|
489
498
|
// unloaded so the next call reads again, and keep going: the halt switch
|
|
490
499
|
// and the rate limit still fail closed on their own path. One line per
|
|
491
500
|
// failure streak, so the admission retry in the same call does not repeat it.
|
|
501
|
+
// A finished removal sync stays valid after a later revoke failure, and
|
|
502
|
+
// an unfinished one either never loaded or cleared itself, so this catch
|
|
503
|
+
// leaves removal memory alone.
|
|
492
504
|
if (!haltSynced)
|
|
493
505
|
controlMemory.loaded = false;
|
|
494
506
|
revokeMemory.loaded = false;
|
|
@@ -496,7 +508,7 @@ export function createProxy(deps) {
|
|
|
496
508
|
return;
|
|
497
509
|
controlSyncReported = true;
|
|
498
510
|
const message = (err instanceof Error ? err.message : String(err)).replace(/[\r\n]+/g, " ");
|
|
499
|
-
process.stderr.write(`verax-proxy: control sync failed: ${message}\n`);
|
|
511
|
+
process.stderr.write(`verax-proxy: control sync failed: ${terminalText(message)}\n`);
|
|
500
512
|
}
|
|
501
513
|
}
|
|
502
514
|
return {
|
package/dist/reconcile.d.ts
CHANGED
|
@@ -69,6 +69,11 @@ export declare function parseCardCsv(text: string, opts: CardCsvOpts): ParsedCar
|
|
|
69
69
|
export declare function parseChannelJsonl(text: string): ChannelRow[];
|
|
70
70
|
/** Decision refs, read the same way `verifyLedger` lists decision files. */
|
|
71
71
|
export declare function loadDecisionRefsFromDir(stateDir: string): Set<string>;
|
|
72
|
+
/**
|
|
73
|
+
* requestHash on each decision, keyed by that decision's ref.
|
|
74
|
+
* Same files as `loadDecisionRefsFromDir`, still without the ledger lock.
|
|
75
|
+
*/
|
|
76
|
+
export declare function loadDecisionRequestHashesFromDir(stateDir: string): Map<string, string>;
|
|
72
77
|
export declare function loadEffectsFromDir(stateDir: string): LedgerEffect[];
|
|
73
78
|
export declare function reconcile(channelRows: readonly ChannelRow[], effects: readonly LedgerEffect[], opts?: {
|
|
74
79
|
toleranceMs?: number;
|
|
@@ -81,4 +86,6 @@ export declare function reconcile(channelRows: readonly ChannelRow[], effects: r
|
|
|
81
86
|
descriptorsByPayee?: Readonly<Record<string, readonly string[]>>;
|
|
82
87
|
/** Effect refs that have a decision. Absent means none: an effect is not a match. */
|
|
83
88
|
decisionRefs?: ReadonlySet<string>;
|
|
89
|
+
/** Effect ref to the requestHash on the signed decision of that ref. */
|
|
90
|
+
decisionRequestHashes?: ReadonlyMap<string, string>;
|
|
84
91
|
}): ReconcileReport;
|
package/dist/reconcile.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { createHash } from "node:crypto";
|
|
2
2
|
import { existsSync, readFileSync } from "node:fs";
|
|
3
3
|
import { join } from "node:path";
|
|
4
|
+
import { sha256Canonical } from "./hash.js";
|
|
4
5
|
import { readLedgerManifest, requireLedgerPiecePath } from "./ledger-manifest.js";
|
|
5
6
|
function asChannelRow(raw, line) {
|
|
6
7
|
if (raw === null || typeof raw !== "object") {
|
|
@@ -221,9 +222,8 @@ function decisionPaths(dir) {
|
|
|
221
222
|
const tek = join(dir, "decisions.jsonl");
|
|
222
223
|
return existsSync(tek) ? [tek] : [];
|
|
223
224
|
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
const refs = new Set();
|
|
225
|
+
function readDecisionLines(stateDir) {
|
|
226
|
+
const claims = [];
|
|
227
227
|
for (const path of decisionPaths(stateDir)) {
|
|
228
228
|
let text = "";
|
|
229
229
|
try {
|
|
@@ -238,12 +238,35 @@ export function loadDecisionRefsFromDir(stateDir) {
|
|
|
238
238
|
if (line === "")
|
|
239
239
|
continue;
|
|
240
240
|
const row = JSON.parse(line);
|
|
241
|
-
|
|
242
|
-
refs.add(row.claims.ref);
|
|
241
|
+
claims.push(row.claims ?? {});
|
|
243
242
|
}
|
|
244
243
|
}
|
|
244
|
+
return claims;
|
|
245
|
+
}
|
|
246
|
+
/** Decision refs, read the same way `verifyLedger` lists decision files. */
|
|
247
|
+
export function loadDecisionRefsFromDir(stateDir) {
|
|
248
|
+
const refs = new Set();
|
|
249
|
+
for (const claims of readDecisionLines(stateDir)) {
|
|
250
|
+
if (typeof claims.ref === "string")
|
|
251
|
+
refs.add(claims.ref);
|
|
252
|
+
}
|
|
245
253
|
return refs;
|
|
246
254
|
}
|
|
255
|
+
/**
|
|
256
|
+
* requestHash on each decision, keyed by that decision's ref.
|
|
257
|
+
* Same files as `loadDecisionRefsFromDir`, still without the ledger lock.
|
|
258
|
+
*/
|
|
259
|
+
export function loadDecisionRequestHashesFromDir(stateDir) {
|
|
260
|
+
const hashes = new Map();
|
|
261
|
+
for (const claims of readDecisionLines(stateDir)) {
|
|
262
|
+
if (typeof claims.ref !== "string")
|
|
263
|
+
continue;
|
|
264
|
+
if (typeof claims.requestHash !== "string" || claims.requestHash === "")
|
|
265
|
+
continue;
|
|
266
|
+
hashes.set(claims.ref, claims.requestHash);
|
|
267
|
+
}
|
|
268
|
+
return hashes;
|
|
269
|
+
}
|
|
247
270
|
export function loadEffectsFromDir(stateDir) {
|
|
248
271
|
try {
|
|
249
272
|
const text = readFileSync(join(stateDir, "effects.jsonl"), "utf8");
|
|
@@ -289,8 +312,23 @@ function approvalForEffect(approvals, effectRef) {
|
|
|
289
312
|
return undefined;
|
|
290
313
|
return approvals.find((r) => r.allowRef === effectRef || r.ref === effectRef);
|
|
291
314
|
}
|
|
292
|
-
|
|
315
|
+
/** The row may lend amount, currency and payee only when it still hashes to the signed request. */
|
|
316
|
+
function boundApproval(effect, approvals, requestHashes) {
|
|
317
|
+
if (!requestHashes)
|
|
318
|
+
return undefined;
|
|
319
|
+
const signed = requestHashes.get(effect.row.ref);
|
|
320
|
+
if (typeof signed !== "string" || signed === "")
|
|
321
|
+
return undefined;
|
|
293
322
|
const snap = approvalForEffect(approvals, effect.row.ref);
|
|
323
|
+
if (!snap)
|
|
324
|
+
return undefined;
|
|
325
|
+
const rowHash = sha256Canonical({ name: snap.subject, arguments: snap.args });
|
|
326
|
+
if (rowHash !== signed)
|
|
327
|
+
return undefined;
|
|
328
|
+
return snap;
|
|
329
|
+
}
|
|
330
|
+
function effectAmount(effect, approvals, requestHashes) {
|
|
331
|
+
const snap = boundApproval(effect, approvals, requestHashes);
|
|
294
332
|
const amountMinor = snap && typeof snap.args.amountMinor === "number" ? snap.args.amountMinor : undefined;
|
|
295
333
|
const currency = snap && typeof snap.args.currency === "string" ? snap.args.currency : undefined;
|
|
296
334
|
return { amountMinor, currency };
|
|
@@ -315,10 +353,10 @@ function amountGate(row, fromSnap) {
|
|
|
315
353
|
}
|
|
316
354
|
return "match";
|
|
317
355
|
}
|
|
318
|
-
function descriptorsFor(effect, approvals, byPayee) {
|
|
356
|
+
function descriptorsFor(effect, approvals, byPayee, requestHashes) {
|
|
319
357
|
if (!byPayee)
|
|
320
358
|
return undefined;
|
|
321
|
-
const snap =
|
|
359
|
+
const snap = boundApproval(effect, approvals, requestHashes);
|
|
322
360
|
const payee = snap && typeof snap.args.payee === "string" ? snap.args.payee : undefined;
|
|
323
361
|
if (!payee)
|
|
324
362
|
return undefined;
|
|
@@ -392,7 +430,7 @@ export function reconcile(channelRows, effects, opts) {
|
|
|
392
430
|
ghost.push(asGhost(marked, effects));
|
|
393
431
|
continue;
|
|
394
432
|
}
|
|
395
|
-
const fromSnap = effectAmount(effects[idx], opts?.approvals);
|
|
433
|
+
const fromSnap = effectAmount(effects[idx], opts?.approvals, opts?.decisionRequestHashes);
|
|
396
434
|
const gate = amountGate(row, fromSnap);
|
|
397
435
|
if (gate === "unknown") {
|
|
398
436
|
ghost.push(asGhost(marked, effects, "amount-unknown"));
|
|
@@ -418,7 +456,7 @@ export function reconcile(channelRows, effects, opts) {
|
|
|
418
456
|
return false;
|
|
419
457
|
if (Math.abs(row.occurredAtMs - e.row.timestampMs) > rowTol)
|
|
420
458
|
return false;
|
|
421
|
-
const stamps = descriptorsFor(e, opts?.approvals, opts?.descriptorsByPayee);
|
|
459
|
+
const stamps = descriptorsFor(e, opts?.approvals, opts?.descriptorsByPayee, opts?.decisionRequestHashes);
|
|
422
460
|
if (stamps) {
|
|
423
461
|
const text = row.descriptor ?? "";
|
|
424
462
|
if (!descriptorHits(text, stamps)) {
|
|
@@ -426,7 +464,7 @@ export function reconcile(channelRows, effects, opts) {
|
|
|
426
464
|
return false;
|
|
427
465
|
}
|
|
428
466
|
}
|
|
429
|
-
const fromSnap = effectAmount(e, opts?.approvals);
|
|
467
|
+
const fromSnap = effectAmount(e, opts?.approvals, opts?.decisionRequestHashes);
|
|
430
468
|
const gate = amountGate(row, fromSnap);
|
|
431
469
|
if (gate === "unknown") {
|
|
432
470
|
unknownNear = true;
|
package/dist/types.d.ts
CHANGED
|
@@ -157,6 +157,19 @@ export type RevokeInput = {
|
|
|
157
157
|
via: "http" | "file";
|
|
158
158
|
atMs: number;
|
|
159
159
|
};
|
|
160
|
+
/**
|
|
161
|
+
* One change to the `operator-removed` directory copied into the ledger.
|
|
162
|
+
* `remove` is a mark that appeared there; `restore` is one that was in the
|
|
163
|
+
* ledger's list and is gone from the directory, which lets that passkey in
|
|
164
|
+
* again.
|
|
165
|
+
*/
|
|
166
|
+
export type OperatorRemovalInput = {
|
|
167
|
+
action: "remove" | "restore";
|
|
168
|
+
mark: string;
|
|
169
|
+
by: string;
|
|
170
|
+
via: "file";
|
|
171
|
+
atMs: number;
|
|
172
|
+
};
|
|
160
173
|
/**
|
|
161
174
|
* The operator's own WebAuthn assertion for one HTTP approval.
|
|
162
175
|
* `authenticatorData`, `clientDataJSON` and `signature` are base64url.
|
|
@@ -191,6 +204,7 @@ export type DecisionInputs = {
|
|
|
191
204
|
};
|
|
192
205
|
control?: ControlInput;
|
|
193
206
|
revoke?: RevokeInput;
|
|
207
|
+
operatorRemoval?: OperatorRemovalInput;
|
|
194
208
|
};
|
|
195
209
|
/** 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`. */
|
|
196
210
|
export type ApprovalChannel = "cli" | "cli-script" | "http";
|