@openvtc/trust-tasks 0.12.17 → 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/README.md +27 -3
- package/dist/_runtime/canonical.d.ts +40 -0
- package/dist/_runtime/canonical.d.ts.map +1 -0
- package/dist/_runtime/canonical.js +147 -0
- package/dist/_runtime/canonical.js.map +1 -0
- package/dist/_runtime/consume.d.ts +108 -2
- package/dist/_runtime/consume.d.ts.map +1 -1
- package/dist/_runtime/consume.js +174 -8
- package/dist/_runtime/consume.js.map +1 -1
- package/dist/_runtime/document.d.ts +17 -4
- package/dist/_runtime/document.d.ts.map +1 -1
- package/dist/_runtime/document.js +17 -4
- package/dist/_runtime/document.js.map +1 -1
- package/dist/_runtime/freshness.d.ts +105 -0
- package/dist/_runtime/freshness.d.ts.map +1 -0
- package/dist/_runtime/freshness.js +142 -0
- package/dist/_runtime/freshness.js.map +1 -0
- package/dist/_runtime/index.d.ts +4 -1
- package/dist/_runtime/index.d.ts.map +1 -1
- package/dist/_runtime/index.js +4 -1
- package/dist/_runtime/index.js.map +1 -1
- package/dist/_runtime/replay.d.ts +176 -0
- package/dist/_runtime/replay.d.ts.map +1 -0
- package/dist/_runtime/replay.js +147 -0
- package/dist/_runtime/replay.js.map +1 -0
- package/package.json +1 -1
- package/src/_runtime/canonical.ts +159 -0
- package/src/_runtime/consume.ts +267 -10
- package/src/_runtime/document.ts +17 -4
- package/src/_runtime/freshness.ts +183 -0
- package/src/_runtime/index.ts +30 -0
- package/src/_runtime/replay.ts +250 -0
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Duplicate-execution protection — SPEC §7.2 item 11, §8.4, §10.1.
|
|
3
|
+
*
|
|
4
|
+
* Mirrors `replay.rs` in trust-tasks-rs, check for check.
|
|
5
|
+
*
|
|
6
|
+
* # The rule
|
|
7
|
+
*
|
|
8
|
+
* §7.2 item 11 is normative and unconditional for a *consequential Trust
|
|
9
|
+
* Task*: once a consumer has accepted a document with a given `id` for
|
|
10
|
+
* execution, receiving that same document again **MUST NOT** cause the
|
|
11
|
+
* consequential effect a second time, and receiving a *different* document
|
|
12
|
+
* under the same `id` **MUST** be rejected with `idConflict`.
|
|
13
|
+
*
|
|
14
|
+
* §8.4 is the same mechanism from the producer's end: a retry is a bit-for-bit
|
|
15
|
+
* identical resend, and it is safe *precisely because* item 11 obliges the
|
|
16
|
+
* consumer to absorb it. Every transport binding in this repo delegates replay
|
|
17
|
+
* defence to the consumer — `bindings/https/0.2` §5 says "Freshness / replay:
|
|
18
|
+
* None", and the DIDComm and TSP bindings say the same — so if the consumer
|
|
19
|
+
* does not do it, nobody does, and an ordinary mediator redelivery grants an
|
|
20
|
+
* ACL entry twice by accident. §10.1: "The rule deliberately does not
|
|
21
|
+
* distinguish a hostile replay from a legitimate transport retry, because at
|
|
22
|
+
* the document layer the two are indistinguishable."
|
|
23
|
+
*
|
|
24
|
+
* # The key
|
|
25
|
+
*
|
|
26
|
+
* The `id` **alone** — "Transport request identifiers, transport message
|
|
27
|
+
* identifiers, and execution handles **MUST NOT** substitute" — plus a digest
|
|
28
|
+
* of the canonical serialization, because "an `id` alone cannot distinguish
|
|
29
|
+
* the retry it must absorb from the conflict it must reject".
|
|
30
|
+
*
|
|
31
|
+
* {@link documentDigest} hashes {@link canonicalJson} of the whole document
|
|
32
|
+
* rather than the octets as received: a re-indented body or a member order
|
|
33
|
+
* chosen by an intermediary would otherwise make a legitimate §8.4 retry look
|
|
34
|
+
* like a *different* document.
|
|
35
|
+
*
|
|
36
|
+
* The digest covers the **entire** document, `proof` included. That differs
|
|
37
|
+
* deliberately from the §4.9.3 *task digest*, computed over
|
|
38
|
+
* `JCS(document ∖ proof)`, and the spec spells the distinction out: "Item 11
|
|
39
|
+
* and §8.4 ask *which serialization arrived*, so a re-signed `proof` over
|
|
40
|
+
* identical content makes a different document — that is the `idConflict`
|
|
41
|
+
* case, and the distinction is the whole point of the rule."
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
import { canonicalJson, sha256Hex } from "./canonical.js";
|
|
45
|
+
import type { TrustTaskDocument } from "./document.js";
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* The content identity of a document, per SPEC §7.2's keying paragraph:
|
|
49
|
+
* SHA-256 over the canonical serialization of the whole document, as hex.
|
|
50
|
+
*
|
|
51
|
+
* Consumer-local. Never on the wire, and **not** the §4.9.3 task digest.
|
|
52
|
+
*/
|
|
53
|
+
export function documentDigest<P>(doc: TrustTaskDocument<P>): string {
|
|
54
|
+
return sha256Hex(canonicalJson(doc));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** What a {@link ReplayGuard} says about a document offered for execution. */
|
|
58
|
+
export type ReplayVerdict =
|
|
59
|
+
/** Not seen before. The caller may execute. */
|
|
60
|
+
| { kind: "fresh" }
|
|
61
|
+
/**
|
|
62
|
+
* Already accepted under the *same* digest — a §8.4 retry, or a replay. The
|
|
63
|
+
* caller **MUST NOT** execute again.
|
|
64
|
+
*/
|
|
65
|
+
| {
|
|
66
|
+
kind: "duplicate";
|
|
67
|
+
/** The response the first execution produced, where one was retained. */
|
|
68
|
+
priorResponse?: unknown;
|
|
69
|
+
/** Whether that first execution is still running. */
|
|
70
|
+
inFlight: boolean;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Already accepted under a *different* digest. §7.2 item 11 requires
|
|
74
|
+
* `idConflict`, and requires that this not be treated as a retry.
|
|
75
|
+
*/
|
|
76
|
+
| { kind: "conflict" };
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* The consumer-side record that makes SPEC §7.2 item 11 true.
|
|
80
|
+
*
|
|
81
|
+
* An interface, so a deployment can back it with Redis, Postgres, or anything
|
|
82
|
+
* that survives a process restart. {@link InMemoryReplayGuard} is the default
|
|
83
|
+
* and is correct for a single-process consumer; it is **not** correct behind a
|
|
84
|
+
* load balancer, where two replicas would each accept the same document once.
|
|
85
|
+
*/
|
|
86
|
+
export interface ReplayGuard {
|
|
87
|
+
/**
|
|
88
|
+
* Claim `id` for execution on behalf of a document with identity `digest`.
|
|
89
|
+
*
|
|
90
|
+
* `retainUntil` is the instant past which the record may be dropped —
|
|
91
|
+
* {@link recordExpiry}, which SPEC §7.2 makes the same instant as the end of
|
|
92
|
+
* the consumer's willingness to execute the document. An implementation
|
|
93
|
+
* SHOULD treat a record whose `retainUntil` has passed as absent, so the key
|
|
94
|
+
* is released rather than conflicting forever with a document nobody would
|
|
95
|
+
* execute.
|
|
96
|
+
*
|
|
97
|
+
* Implementations **MUST** make claim-and-record atomic with respect to
|
|
98
|
+
* concurrent calls: two simultaneous deliveries of the same document must
|
|
99
|
+
* not both receive `fresh`. That is the whole guarantee.
|
|
100
|
+
*
|
|
101
|
+
* Throwing means the record could not be consulted. {@link consumeInbound}
|
|
102
|
+
* fails closed on that, mapping it to `unavailable` with `retryable: true` —
|
|
103
|
+
* a consumer that cannot establish whether a document is a duplicate has not
|
|
104
|
+
* satisfied item 11, and executing anyway is the double execution the rule
|
|
105
|
+
* forbids.
|
|
106
|
+
*/
|
|
107
|
+
claim(
|
|
108
|
+
id: string,
|
|
109
|
+
digest: string,
|
|
110
|
+
retainUntil: number | undefined,
|
|
111
|
+
now: number,
|
|
112
|
+
): Promise<ReplayVerdict> | ReplayVerdict;
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Attach the response a completed execution produced, so a later duplicate
|
|
116
|
+
* can be answered with it per §7.2 (*Disposition of a duplicate*) rather
|
|
117
|
+
* than merely absorbed in silence.
|
|
118
|
+
*
|
|
119
|
+
* Optional: a guard that records nothing still satisfies item 11 — the
|
|
120
|
+
* effect does not happen twice — and is the right shape for a
|
|
121
|
+
* fire-and-forget specification, which has no response to return.
|
|
122
|
+
*/
|
|
123
|
+
recordResponse?(id: string, response: unknown): Promise<void> | void;
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Release a claim whose execution is not to stand — for example a refusal
|
|
127
|
+
* the consumer marked `retryable`, where §8.4 has just invited the producer
|
|
128
|
+
* to re-send the same bytes. Without this, that invited retry would come
|
|
129
|
+
* back as an absorbed duplicate carrying the same failure forever.
|
|
130
|
+
*/
|
|
131
|
+
release?(id: string, digest: string): Promise<void> | void;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** How a consumer applies SPEC §7.2 item 11 in {@link consumeInbound}. */
|
|
135
|
+
export type ReplayPolicy =
|
|
136
|
+
/** Apply item 11 using this guard. Correct for any consequential spec. */
|
|
137
|
+
| { kind: "guard"; guard: ReplayGuard }
|
|
138
|
+
/**
|
|
139
|
+
* Keep no duplicate-execution record. Conformant **only** where the task is
|
|
140
|
+
* not consequential (§2), or where the specification "explicitly declares
|
|
141
|
+
* repeated execution safe and intended" — a property of the operation, not
|
|
142
|
+
* of the consumer's convenience.
|
|
143
|
+
*/
|
|
144
|
+
| { kind: "notConsequential" };
|
|
145
|
+
|
|
146
|
+
interface Entry {
|
|
147
|
+
digest: string;
|
|
148
|
+
retainUntil: number | undefined;
|
|
149
|
+
response: unknown;
|
|
150
|
+
completed: boolean;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* A bounded, in-process {@link ReplayGuard}: an LRU map from `id` to the
|
|
155
|
+
* digest accepted under it, its retention deadline, and the response it
|
|
156
|
+
* produced.
|
|
157
|
+
*
|
|
158
|
+
* **Suitable when** one process is the sole consumer for the `recipient` VID
|
|
159
|
+
* it serves and losing the record on restart is acceptable.
|
|
160
|
+
*
|
|
161
|
+
* **Not suitable when** the consumer is replicated: two replicas hold separate
|
|
162
|
+
* maps, so a document accepted by replica A is `fresh` at replica B and the
|
|
163
|
+
* effect happens twice — the exact failure item 11 exists to prevent.
|
|
164
|
+
* Replicated deployments MUST back the guard with a shared store.
|
|
165
|
+
*
|
|
166
|
+
* Eviction is by capacity as well as by `retainUntil`: a burst of distinct
|
|
167
|
+
* documents can push an older record out before its deadline, and a replay
|
|
168
|
+
* arriving after that would be accepted. Size the capacity above the number of
|
|
169
|
+
* distinct documents the widest acceptance window can hold.
|
|
170
|
+
*
|
|
171
|
+
* `Map` preserves insertion order, which is what makes the LRU a delete-then-
|
|
172
|
+
* reinsert rather than a second index.
|
|
173
|
+
*/
|
|
174
|
+
export class InMemoryReplayGuard implements ReplayGuard {
|
|
175
|
+
readonly #entries = new Map<string, Entry>();
|
|
176
|
+
readonly #capacity: number;
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* @param capacity Maximum records retained. Must be positive: a guard that
|
|
180
|
+
* retains nothing answers `fresh` to everything, which is a silent total
|
|
181
|
+
* defeat of item 11 rather than a visible misconfiguration.
|
|
182
|
+
*/
|
|
183
|
+
constructor(capacity = 10_000) {
|
|
184
|
+
if (!Number.isInteger(capacity) || capacity < 1) {
|
|
185
|
+
throw new RangeError("InMemoryReplayGuard capacity must be a positive integer");
|
|
186
|
+
}
|
|
187
|
+
this.#capacity = capacity;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** Number of records currently retained. For tests and metrics. */
|
|
191
|
+
get size(): number {
|
|
192
|
+
return this.#entries.size;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** Drop every record whose `retainUntil` has passed. */
|
|
196
|
+
purgeExpired(now: number): void {
|
|
197
|
+
for (const [id, entry] of this.#entries) {
|
|
198
|
+
if (entry.retainUntil !== undefined && entry.retainUntil <= now) this.#entries.delete(id);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
claim(id: string, digest: string, retainUntil: number | undefined, now: number): ReplayVerdict {
|
|
203
|
+
const existing = this.#entries.get(id);
|
|
204
|
+
|
|
205
|
+
// An expired record is treated as absent: the consumer would refuse the
|
|
206
|
+
// document under §7.2 item 4 or the acceptance window anyway, so holding
|
|
207
|
+
// the key would only manufacture a permanent `idConflict` for an `id`
|
|
208
|
+
// nobody can use.
|
|
209
|
+
if (existing !== undefined && existing.retainUntil !== undefined && existing.retainUntil <= now) {
|
|
210
|
+
this.#entries.delete(id);
|
|
211
|
+
} else if (existing !== undefined) {
|
|
212
|
+
if (existing.digest !== digest) {
|
|
213
|
+
// A conflicting document is not a *use* of the record, so it does not
|
|
214
|
+
// refresh recency — otherwise a flood of conflicts could pin an entry
|
|
215
|
+
// and evict live ones.
|
|
216
|
+
return { kind: "conflict" };
|
|
217
|
+
}
|
|
218
|
+
this.#entries.delete(id);
|
|
219
|
+
this.#entries.set(id, existing);
|
|
220
|
+
return existing.response === undefined
|
|
221
|
+
? { kind: "duplicate", inFlight: !existing.completed }
|
|
222
|
+
: { kind: "duplicate", priorResponse: existing.response, inFlight: !existing.completed };
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
this.#entries.set(id, { digest, retainUntil, response: undefined, completed: false });
|
|
226
|
+
while (this.#entries.size > this.#capacity) {
|
|
227
|
+
const oldest = this.#entries.keys().next();
|
|
228
|
+
if (oldest.done === true) break;
|
|
229
|
+
this.#entries.delete(oldest.value);
|
|
230
|
+
}
|
|
231
|
+
return { kind: "fresh" };
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
recordResponse(id: string, response: unknown): void {
|
|
235
|
+
const entry = this.#entries.get(id);
|
|
236
|
+
if (entry === undefined) return;
|
|
237
|
+
entry.response = response;
|
|
238
|
+
entry.completed = true;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
release(id: string, digest: string): void {
|
|
242
|
+
const entry = this.#entries.get(id);
|
|
243
|
+
// Only release the claim this digest made, and only while it is
|
|
244
|
+
// unfinished: a concurrent arrival that legitimately holds the key must
|
|
245
|
+
// not have it taken away by another document's cleanup.
|
|
246
|
+
if (entry !== undefined && entry.digest === digest && !entry.completed) {
|
|
247
|
+
this.#entries.delete(id);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|