@sanning/proof 0.19.0 → 0.20.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 +30 -29
- package/dist/agent-record.d.ts +62 -0
- package/dist/agent-record.js +527 -0
- package/dist/agent-schemas.generated.d.ts +15 -0
- package/dist/agent-schemas.generated.js +5759 -0
- package/dist/case-file.d.ts +32 -89
- package/dist/case-file.js +41 -93
- package/dist/case.d.ts +343 -810
- package/dist/case.js +693 -1151
- package/dist/chain.d.ts +19 -47
- package/dist/chain.js +78 -205
- package/dist/claims.d.ts +211 -0
- package/dist/claims.js +1081 -0
- package/dist/cli.d.ts +45 -68
- package/dist/cli.js +618 -1022
- package/dist/compose.js +23 -65
- package/dist/crypto.d.ts +7 -8
- package/dist/crypto.js +60 -147
- package/dist/declaration.d.ts +68 -194
- package/dist/declaration.js +175 -518
- package/dist/environment.d.ts +9 -30
- package/dist/environment.js +17 -66
- package/dist/evidence.d.ts +119 -160
- package/dist/evidence.js +744 -1103
- package/dist/exit-codes.d.ts +11 -27
- package/dist/exit-codes.js +24 -59
- package/dist/file-match.d.ts +33 -79
- package/dist/file-match.js +33 -69
- package/dist/grading.d.ts +98 -135
- package/dist/grading.js +224 -380
- package/dist/handoff.d.ts +62 -160
- package/dist/handoff.js +153 -393
- package/dist/index.d.ts +14 -10
- package/dist/index.js +60 -167
- package/dist/json-schema.d.ts +29 -0
- package/dist/json-schema.js +298 -0
- package/dist/merkle.js +16 -28
- package/dist/message-parts.d.ts +56 -212
- package/dist/message-parts.js +142 -502
- package/dist/side-content.d.ts +11 -22
- package/dist/side-content.js +22 -65
- package/dist/types.d.ts +17 -0
- package/dist/types.js +19 -5
- package/dist/verifier.d.ts +40 -114
- package/dist/verifier.js +196 -535
- package/dist/witness-cache.d.ts +24 -45
- package/dist/witness-cache.js +38 -81
- package/dist/witness-lookup.d.ts +36 -57
- package/dist/witness-lookup.js +89 -139
- package/dist/zip-reader.d.ts +18 -33
- package/dist/zip-reader.js +19 -34
- package/package.json +11 -3
- package/src/agent-record.ts +608 -0
- package/src/agent-schemas.generated.ts +5764 -0
- package/src/case-file.ts +45 -109
- package/src/case.ts +848 -1322
- package/src/chain.ts +85 -224
- package/src/claims.ts +1253 -0
- package/src/cli.ts +714 -1176
- package/src/compose.ts +33 -79
- package/src/crypto.ts +60 -147
- package/src/declaration.ts +219 -656
- package/src/environment.ts +17 -65
- package/src/evidence.ts +958 -1381
- package/src/exit-codes.ts +24 -63
- package/src/file-match.ts +40 -89
- package/src/grading.ts +287 -421
- package/src/handoff.ts +175 -427
- package/src/index.ts +132 -177
- package/src/json-schema.ts +295 -0
- package/src/merkle.ts +16 -28
- package/src/message-parts.ts +155 -533
- package/src/side-content.ts +25 -68
- package/src/types.ts +55 -36
- package/src/verifier.ts +202 -526
- package/src/witness-cache.ts +45 -94
- package/src/witness-lookup.ts +107 -177
- package/src/zip-reader.ts +20 -38
- package/vectors/README.md +52 -34
- package/vectors/agent-record.json +2175 -0
- package/vectors/case-file-index.json +1 -1
- package/vectors/case-split.json +2 -2
- package/vectors/chain-reading.json +3 -3
- package/vectors/file-match.json +285 -148
- package/vectors/jcs-reader.json +6 -6
- package/vectors/pack-grading.json +102 -25
- package/vectors/sandbox-evidence.json +597 -580
- package/vectors/stamp-on-chain.json +18 -18
- package/vectors/witness-cache.json +49 -49
- package/dist/agent-proof.d.ts +0 -28
- package/dist/agent-proof.js +0 -226
- package/dist/run-adapters.d.ts +0 -10
- package/dist/run-adapters.js +0 -35
- package/dist/run.d.ts +0 -209
- package/dist/run.js +0 -1405
- package/src/agent-proof.ts +0 -264
- package/src/run-adapters.ts +0 -44
- package/src/run.ts +0 -1598
package/dist/message-parts.js
CHANGED
|
@@ -1,302 +1,118 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
* `
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
* `message_hash`, a failure section 4.1 already names, and the receiving
|
|
33
|
-
* side writes `outcome: "rejected"`. That is the ONE reading that moves,
|
|
34
|
-
* and it moves only for a receiver that holds the bytes;
|
|
35
|
-
* * every other deviation is named and none is silent. Three of them are
|
|
36
|
-
* REFUSALS of one supply, so a receiver's own decoding defect never becomes
|
|
37
|
-
* permanent evidence against an honest sender;
|
|
38
|
-
* * an inline part is ACCEPTED, and it is committed by the MESSAGE ITSELF
|
|
39
|
-
* (proof#157, ratified by Phil 2026-09-22). `message_hash` is SHA-256 over
|
|
40
|
-
* the canonical bytes of the message object, so a part carrying its content
|
|
41
|
-
* inside the message is already committed: alter that content and the
|
|
42
|
-
* message object changes, the hash changes, and the pair fails. None of the
|
|
43
|
-
* swapped-bytes attack applies, because that attack works only where a
|
|
44
|
-
* descriptor's hash and bytes travelling BESIDE the message are two
|
|
45
|
-
* separate things with nothing comparing them. Inline is not the risky
|
|
46
|
-
* shape, it is the safest one. Where an inline part ALSO carries a
|
|
47
|
-
* `content_hash` or a `content_length`, the receiver checks BOTH against
|
|
48
|
-
* the inline content and names a disagreement: one commitment governs, and
|
|
49
|
-
* a second one present and disagreeing is itself a finding.
|
|
50
|
-
*
|
|
51
|
-
* WHY THE HELD MESSAGE RATHER THAN A FINDING ON THE RECEIVING SIDE. A finding
|
|
52
|
-
* only the receiving side's own reader sees is the shape of every laundering
|
|
53
|
-
* path this section has closed: the party being audited publishes the pack, so
|
|
54
|
-
* a deviation that lives outside the committed record is a deviation that party
|
|
55
|
-
* chooses whether anyone reads. `message_hash` is the one value both sides
|
|
56
|
-
* publish and a reader already grades.
|
|
57
|
-
*
|
|
58
|
-
* WHAT DOES NOT MOVE. A receiver that never holds a part's bytes checks
|
|
59
|
-
* nothing, substitutes nothing and reads exactly as it did before this
|
|
60
|
-
* amendment: every file part is `PART_UNCHECKED`, and section 4.1 forbids
|
|
61
|
-
* treating an unchecked part as a checked one. No envelope field moves, no
|
|
62
|
-
* `spec_version` moves, no verdict word and no record-layer finding is coined.
|
|
63
|
-
*
|
|
64
|
-
* A REFUSAL IS SCOPED TO ITS PART, AND THE RECORD IS STILL WRITTEN (proof#156
|
|
65
|
-
* gate). The first cut of this module refused GLOBALLY: one refusal anywhere
|
|
66
|
-
* set `heldMessage` to null and dropped every substitution. That made the
|
|
67
|
-
* attack A18 exists to close launderable (a genuine swap on part 0 plus any
|
|
68
|
-
* refusal-triggering supply on part 1 wrote nothing at all), let a SENDER deny
|
|
69
|
-
* the receiver a record with an upper-case `content_hash` or a `"bytes": null`
|
|
70
|
-
* on an otherwise conformant descriptor, and contradicted section 4.1's own
|
|
71
|
-
* paragraph saying an uncheckable part is NAMED in the receiver's reading. So
|
|
72
|
-
* a problem with one part names that part, leaves the rest of the reading
|
|
73
|
-
* intact, and `heldMessage` is never null. A message-level problem is reserved
|
|
74
|
-
* for a message-level condition, and even then it produces a named result:
|
|
75
|
-
* `PARTS_NOT_AN_ARRAY`, and the entry-scoped `BYTES_FOR_UNKNOWN_PART`.
|
|
76
|
-
*
|
|
77
|
-
* WHAT THE CALLER STILL OWES, AND THE ONE FUNCTION THAT STATES IT.
|
|
78
|
-
* `substituted` decides whether a hand-off is `rejected`; it does NOT decide
|
|
79
|
-
* whether one is clean. A part that was refused reads `PART_UNCHECKED`,
|
|
80
|
-
* section 4.1 forbids a reader treating an unchecked part as a checked one,
|
|
81
|
-
* and a surface reporting a hand-off states how many parts were checked. A
|
|
82
|
-
* caller that wrote `accepted` on "nothing was substituted" alone would be
|
|
83
|
-
* calling a part it never established clean, which is the class this whole
|
|
84
|
-
* section keeps repairing.
|
|
85
|
-
*
|
|
86
|
-
* The sharp version, measured (proof#156 second gate, F5): deliver a tampered
|
|
87
|
-
* copy of a part ALONE and it is `part_mismatched`, substituted, and the pair
|
|
88
|
-
* fails on `message_hash`. Deliver the tampered copy AND the true one and it
|
|
89
|
-
* is `conflicting_part_bytes`, `part_unchecked`, nothing substituted, a
|
|
90
|
-
* `heldMessage` byte-identical to the sender's, and the pair VERIFIES. Adding
|
|
91
|
-
* data suppresses detection, and the refusal that names it lives only in
|
|
92
|
-
* `refusals`, which is a finding only the receiving side's own reader sees:
|
|
93
|
-
* the exact shape the paragraph above refuses to create.
|
|
94
|
-
* `PART_ENCODING_MISMATCH` has the same property.
|
|
95
|
-
*
|
|
96
|
-
* Until that round the obligation was PROSE IN THIS COMMENT with no mechanism,
|
|
97
|
-
* which is a rule a caller has to read to obey. `uncheckedHeldParts` is the
|
|
98
|
-
* mechanism's additive half: it names the parts the receiver HELD bytes for
|
|
99
|
-
* and did not check, so a caller writes `accepted` only where `substituted` is
|
|
100
|
-
* empty AND that list is empty, instead of re-deriving it from the refusals.
|
|
101
|
-
* It cannot make the mistake IMPOSSIBLE, because this module reads and does
|
|
102
|
-
* not write the record. What the record layer does with the list was
|
|
103
|
-
* proof#162, and Phil ratified it on 2026-09-22: `outcome: "accepted"` keeps
|
|
104
|
-
* meaning what it means today, and the count of held-but-unchecked parts
|
|
105
|
-
* rides in an ADDITIVE `context` key, REQUIRED when it is not zero. Its force
|
|
106
|
-
* is against SDKs through conformance vectors rather than against records
|
|
107
|
-
* through readers, since only the receiver knows what bytes it held, so THIS
|
|
108
|
-
* LIST IS WHAT EVERY SDK BUILT ON THE RULE WILL COUNT. A part missing from it
|
|
109
|
-
* is a part no record will ever mention (anchor#191 is the first caller).
|
|
110
|
-
*
|
|
111
|
-
* WHAT THIS MODULE DOES NOT DO. It does not canonicalize and it does not hash
|
|
112
|
-
* the message. It returns the message a receiver commits to, and the caller
|
|
113
|
-
* hashes that with the rule section 4.1 already states, so this module has no
|
|
114
|
-
* failure mode of its own for a message too deep or not serializable and there
|
|
115
|
-
* is no branch here where one kernel throws and the other returns.
|
|
2
|
+
* File parts of a hand-off message, and the message a receiver holding their
|
|
3
|
+
* bytes commits to (envelope-spec.md §4.1). Conformance:
|
|
4
|
+
* `test-vectors/handoff/parts/`.
|
|
5
|
+
*
|
|
6
|
+
* A file part is `kind: "file"` plus `content_hash` (SHA-256 of the exact
|
|
7
|
+
* bytes, 64 hex in either case) and `content_length` (a safe integer), with
|
|
8
|
+
* the bytes outside the message. A receiver handed a part's bytes must hash
|
|
9
|
+
* them. Where the digest or the length differs from the descriptor, the
|
|
10
|
+
* receiver commits to the message as it actually held it: that part's
|
|
11
|
+
* `content_hash` and `content_length` replaced by the held bytes' own. Its
|
|
12
|
+
* `message_hash` then differs from the sender's and the pair fails, so a
|
|
13
|
+
* transport that keeps the descriptor and swaps the bytes is caught in the
|
|
14
|
+
* committed record, not in a finding only the receiver's reader sees.
|
|
15
|
+
*
|
|
16
|
+
* An inline part (content inside the message) is committed by `message_hash`
|
|
17
|
+
* itself. If it also carries a `content_hash` or `content_length`, both are
|
|
18
|
+
* checked against the inline content and a disagreement is named.
|
|
19
|
+
*
|
|
20
|
+
* A refusal is scoped to its part and never stops the record being written:
|
|
21
|
+
* `heldMessage` is never null, so neither a sender nor a bad supply can deny
|
|
22
|
+
* the receiver a record or launder a swap on another part.
|
|
23
|
+
*
|
|
24
|
+
* `substituted` decides `rejected`; it does not make a hand-off clean. A caller
|
|
25
|
+
* writes `accepted` only when `substituted` and `uncheckedHeldParts` are both
|
|
26
|
+
* empty. Supplying a tampered copy alongside the true one reads
|
|
27
|
+
* `conflicting_part_bytes` with nothing substituted, so held-but-unchecked parts
|
|
28
|
+
* must be counted in the record.
|
|
29
|
+
*
|
|
30
|
+
* This module does not canonicalize or hash the message; the caller hashes
|
|
31
|
+
* `heldMessage` as §4.1 states.
|
|
116
32
|
*/
|
|
117
33
|
import { sha256Hex } from "./crypto.js";
|
|
118
|
-
/**
|
|
119
|
-
* The result of a part whose supplied bytes hash to its `content_hash` AND
|
|
120
|
-
* count to its `content_length`.
|
|
121
|
-
*/
|
|
34
|
+
/** The supplied bytes match both `content_hash` and `content_length`. */
|
|
122
35
|
export const PART_MATCHED = "part_matched";
|
|
123
36
|
/**
|
|
124
|
-
* The
|
|
125
|
-
*
|
|
126
|
-
* applies to this part, and it replaces BOTH values with the held bytes' own.
|
|
37
|
+
* The supplied bytes miss either value, and no respelling explains the digest.
|
|
38
|
+
* The held message replaces both values with the held bytes' own.
|
|
127
39
|
*/
|
|
128
40
|
export const PART_MISMATCHED = "part_mismatched";
|
|
129
|
-
/**
|
|
130
|
-
* A file part whose bytes were not supplied, or whose supply was REFUSED. It is
|
|
131
|
-
* not checked and it is not clean: section 4.1 forbids a reader treating it as
|
|
132
|
-
* checked.
|
|
133
|
-
*/
|
|
41
|
+
/** Bytes not supplied, or the supply was refused. Not checked, and not clean. */
|
|
134
42
|
export const PART_UNCHECKED = "part_unchecked";
|
|
135
43
|
/**
|
|
136
|
-
* A
|
|
137
|
-
*
|
|
138
|
-
* ITSELF: `message_hash` is SHA-256 over the canonical bytes of the message
|
|
139
|
-
* object, so altering that content changes the message, changes the hash, and
|
|
140
|
-
* fails the pair. Nothing is hashed against it here and nothing is substituted,
|
|
141
|
-
* because there is nothing travelling beside the message for the message to
|
|
142
|
-
* disagree with. That is why the swapped-bytes attack cannot reach this shape.
|
|
44
|
+
* A file part with its content inside the message, committed by
|
|
45
|
+
* `message_hash` itself. Nothing is hashed or substituted.
|
|
143
46
|
*/
|
|
144
47
|
export const PART_INLINE_BYTES = "part_inline_bytes";
|
|
145
48
|
/**
|
|
146
|
-
* An inline part whose `content_hash
|
|
147
|
-
*
|
|
148
|
-
*
|
|
149
|
-
* nothing is substituted; this names the SECOND claim that disagrees with it,
|
|
150
|
-
* which is a finding in its own right (proof#157).
|
|
151
|
-
*
|
|
152
|
-
* ONE name for both claims, exactly as `PART_MISMATCHED` is one name for both
|
|
153
|
-
* on a descriptor part. It was `part_inline_hash_mismatch` for one round of
|
|
154
|
-
* this unmerged PR, when only the hash was compared; a name that says "hash"
|
|
155
|
-
* about a result that also covers a length asserts more than its body does, so
|
|
156
|
-
* the name was re-cut with the rule (proof#156 second gate, F4).
|
|
157
|
-
*
|
|
158
|
-
* The check is deliberately permissive ACROSS readings: the raw UTF-8 of the
|
|
159
|
-
* value, its base64 decoding and its hex decoding are all tried, so this fires
|
|
160
|
-
* only where no reading agrees at all. It is not permissive BETWEEN claims: one
|
|
161
|
-
* reading has to satisfy all of them, because a hash that agrees under the
|
|
162
|
-
* base64 decoding beside a length that agrees under the raw text describes no
|
|
163
|
-
* blob that exists.
|
|
49
|
+
* An inline part whose `content_hash` or `content_length` disagrees with its
|
|
50
|
+
* inline content. The raw UTF-8, base64 and hex readings are all tried, but one
|
|
51
|
+
* reading must satisfy every claim present. Nothing is substituted.
|
|
164
52
|
*/
|
|
165
53
|
export const PART_INLINE_MISMATCHED = "part_inline_mismatched";
|
|
166
54
|
/**
|
|
167
55
|
* `kind: "file"` without a conformant `content_hash` and `content_length`. A
|
|
168
|
-
* sender's defect
|
|
169
|
-
* the receiver did nothing wrong by holding it.
|
|
56
|
+
* sender's defect, so it is named rather than refused.
|
|
170
57
|
*/
|
|
171
58
|
export const PART_MALFORMED_DESCRIPTOR = "part_malformed_descriptor";
|
|
172
|
-
/**
|
|
173
|
-
* Any other part. It is committed as ordinary JSON content and this rule checks
|
|
174
|
-
* nothing about it.
|
|
175
|
-
*/
|
|
59
|
+
/** Any other part: ordinary JSON content, not checked by this rule. */
|
|
176
60
|
export const PART_NOT_A_FILE = "part_not_a_file";
|
|
177
61
|
/**
|
|
178
|
-
*
|
|
179
|
-
*
|
|
180
|
-
*
|
|
181
|
-
* there establishes nothing. Where the entry names a part that EXISTS but is
|
|
182
|
-
* not checkable, the refusal carries that part's index and the part keeps the
|
|
183
|
-
* result it classified to; where it names no part at all, the refusal carries
|
|
184
|
-
* the entry position and no part. Neither stops the record being written.
|
|
62
|
+
* Refusal of one supplied entry: it names no part, or a part this rule cannot
|
|
63
|
+
* check. Carries that part's index when the part exists, else only the entry
|
|
64
|
+
* position.
|
|
185
65
|
*/
|
|
186
66
|
export const BYTES_FOR_UNKNOWN_PART = "bytes_for_unknown_part";
|
|
187
|
-
/**
|
|
188
|
-
* REFUSAL. Two DIFFERENT byte strings were supplied for one part. A receiver
|
|
189
|
-
* holding two answers has not established which one it held.
|
|
190
|
-
*/
|
|
67
|
+
/** Refusal: two different byte strings were supplied for one part. */
|
|
191
68
|
export const CONFLICTING_PART_BYTES = "conflicting_part_bytes";
|
|
192
69
|
/**
|
|
193
|
-
*
|
|
194
|
-
*
|
|
195
|
-
*
|
|
196
|
-
*
|
|
197
|
-
*
|
|
198
|
-
* attachment, a PEM body and a JWT all are). That is a coding defect in the
|
|
199
|
-
* receiver, not a tamper by the sender: a proto3 JSON transcoder re-encodes
|
|
200
|
-
* base64 URL-safe and unpadded while behaving honestly, and under the silence
|
|
201
|
-
* this amendment removes it produced a permanent tamper record against a sender
|
|
202
|
-
* who did nothing. Recording evidence out of a receiver's own bug is the one
|
|
203
|
-
* outcome worse than recording nothing, so BOTH directions are guarded
|
|
204
|
-
* (proof#156 gate): guarding only the decoding direction left the other half of
|
|
205
|
-
* one honest stack's behavior reading as a tamper.
|
|
70
|
+
* Refusal of one supplied entry: the bytes miss the descriptor, but a base64
|
|
71
|
+
* or hex respelling in either direction hashes to it. That is a receiver's
|
|
72
|
+
* coding defect (an undecoded supply, or a decoded PEM, DER or JWT the sender
|
|
73
|
+
* committed as text), not a sender's tamper, so it must not produce a tamper
|
|
74
|
+
* record.
|
|
206
75
|
*/
|
|
207
76
|
export const PART_ENCODING_MISMATCH = "part_encoding_mismatch";
|
|
208
77
|
/**
|
|
209
|
-
*
|
|
210
|
-
*
|
|
211
|
-
* reachable from the same transport family the motivating example comes from.
|
|
212
|
-
* Section 4.1 asks for one result per part; a message shaped like this has
|
|
213
|
-
* part-shaped content and no parts, and before proof#156's gate it produced
|
|
214
|
-
* zero readings and an accepted record. It is named rather than refused: the
|
|
215
|
-
* receiver did nothing wrong by holding it, and nothing in it is checked. A
|
|
216
|
-
* `parts` written as `null` is an ABSENT `parts` and is not this, on A14's
|
|
217
|
-
* ruling that an absent value and one written as `null` name one thing.
|
|
78
|
+
* Message-level: `parts` is present, not null and not an array (proto3 JSON
|
|
79
|
+
* encodes a map this way). Named, not refused. A `parts` of `null` is absent.
|
|
218
80
|
*/
|
|
219
81
|
export const PARTS_NOT_AN_ARRAY = "parts_not_an_array";
|
|
220
|
-
/**
|
|
221
|
-
* NOTE, not a refusal. One part supplied more than once with IDENTICAL bytes is
|
|
222
|
-
* one supply stated twice, and the part is checked normally. Different bytes for
|
|
223
|
-
* one part are `CONFLICTING_PART_BYTES`.
|
|
224
|
-
*/
|
|
82
|
+
/** Note, not a refusal: one part supplied more than once with identical bytes. */
|
|
225
83
|
export const PART_SUPPLIED_TWICE = "part_supplied_twice";
|
|
226
|
-
/**
|
|
227
|
-
* The order refusals are reported in, fixed so two kernels report one list
|
|
228
|
-
* rather than two orderings of it.
|
|
229
|
-
*/
|
|
84
|
+
/** The fixed order refusals are reported in. */
|
|
230
85
|
export const REFUSAL_ORDER = [
|
|
231
86
|
BYTES_FOR_UNKNOWN_PART,
|
|
232
87
|
CONFLICTING_PART_BYTES,
|
|
233
88
|
PART_ENCODING_MISMATCH,
|
|
234
89
|
];
|
|
235
|
-
/**
|
|
236
|
-
* The order message-level results are reported in, for the same reason as
|
|
237
|
-
* `REFUSAL_ORDER`.
|
|
238
|
-
*/
|
|
90
|
+
/** The fixed order message-level results are reported in. */
|
|
239
91
|
export const MESSAGE_RESULT_ORDER = [PARTS_NOT_AN_ARRAY];
|
|
240
92
|
/**
|
|
241
|
-
*
|
|
242
|
-
*
|
|
243
|
-
*
|
|
244
|
-
*
|
|
245
|
-
*
|
|
246
|
-
* not for these two: `PART_UNCHECKED` is a supply the rule refused to grade,
|
|
247
|
-
* and `PART_MALFORMED_DESCRIPTOR` is a part whose content lives BESIDE the
|
|
248
|
-
* message under a claim no receiver can use, so in both the receiver holds
|
|
249
|
-
* bytes that nothing in the committed record covers.
|
|
250
|
-
*
|
|
251
|
-
* The other three results a supply can be refused against are excluded, and
|
|
252
|
-
* excluded for a reason rather than by omission. `PART_INLINE_BYTES`,
|
|
253
|
-
* `PART_INLINE_MISMATCHED` and `PART_NOT_A_FILE` all carry their content
|
|
254
|
-
* INSIDE the message, which `message_hash` already commits on both sides:
|
|
255
|
-
* bytes supplied beside one of those grade nothing and hide nothing, because
|
|
256
|
-
* a reader of either pack reads the content itself. Counting them would make
|
|
257
|
-
* the caller's obligation fire on parts no attack can reach.
|
|
93
|
+
* Results that put a held part into `uncheckedHeldParts`: those whose content
|
|
94
|
+
* the message itself does not commit. Inline parts and non-file parts with no
|
|
95
|
+
* URL are excluded because `message_hash` already commits their content; a URL
|
|
96
|
+
* part (`isUrlPart`) counts, because the message commits the URL and not the
|
|
97
|
+
* file behind it.
|
|
258
98
|
*/
|
|
259
99
|
const HELD_UNCHECKED_RESULTS = [
|
|
260
100
|
PART_UNCHECKED,
|
|
261
101
|
PART_MALFORMED_DESCRIPTOR,
|
|
262
102
|
];
|
|
263
103
|
/**
|
|
264
|
-
* The
|
|
265
|
-
*
|
|
266
|
-
*
|
|
267
|
-
* inline spelling either reference kernel has read from a real producer.
|
|
268
|
-
* `content` and `data` were added on inference rather than on an observed
|
|
269
|
-
* wire shape, and sanning-io/anchor's `filePart()` and `file_part()` SDK
|
|
270
|
-
* helpers, the only producer this plane ships, confirm the inference was
|
|
271
|
-
* wrong: both emit a descriptor part, `kind: "file"` plus `content_hash` and
|
|
272
|
-
* `content_length`, and neither ever writes a `content` or `data` wire key
|
|
273
|
-
* for inline bytes. None is A2A 1.0.0's either: its `Part` has no `kind` and
|
|
274
|
-
* carries a file as `raw` or `url`, so a literal A2A part reads
|
|
275
|
-
* `part_not_a_file` (envelope-spec §4.1, A19). The one key present with a
|
|
276
|
-
* value that is not `null` is the inline content.
|
|
277
|
-
*
|
|
278
|
-
* A key present with `null` is an ABSENT inline value, on A14's ruling that an
|
|
279
|
-
* absent value and one written as `null` name one thing. That matters: a
|
|
280
|
-
* proto3 JSON encoder can write an unset field as `"bytes": null`, and reading
|
|
281
|
-
* that as inline made a conformant DESCRIPTOR part uncheckable
|
|
282
|
-
* (proof#156 gate). Such a part falls through to the descriptor rule, which is
|
|
283
|
-
* what it is.
|
|
104
|
+
* The key that makes a part carry its content inline (envelope-spec §4.1).
|
|
105
|
+
* `bytes` is A2A's spelling. A key present with `null` is absent, so a proto3
|
|
106
|
+
* `"bytes": null` on a descriptor part still reads as a descriptor.
|
|
284
107
|
*/
|
|
285
|
-
// Before A31 this list also carried `content` and `data`, and a conformant
|
|
286
|
-
// DESCRIPTOR part that happened to carry either key meaning something else (a
|
|
287
|
-
// filename, a caption) was silently reclassified as inline and never checked
|
|
288
|
-
// against supplied bytes: the swapped-bytes check A18 exists to run
|
|
289
|
-
// (proof#145) never ran on it. proof#163 is resolved by narrowing to `bytes`
|
|
290
|
-
// alone, closing that gap.
|
|
291
108
|
export const INLINE_BYTES_KEYS = ["bytes"];
|
|
292
109
|
const HEX64 = /^[0-9a-fA-F]{64}$/;
|
|
293
110
|
const HEX_CHARSET = /^[0-9a-fA-F]+$/;
|
|
294
111
|
const LOWER_HEX = "0123456789abcdef";
|
|
295
112
|
const UPPER_HEX = "0123456789ABCDEF";
|
|
296
113
|
/**
|
|
297
|
-
* The largest
|
|
298
|
-
*
|
|
299
|
-
* would read as two numbers.
|
|
114
|
+
* The largest exactly representable integer, the bound §4.1 puts on
|
|
115
|
+
* `event.sequence`. A larger `content_length` is malformed.
|
|
300
116
|
*/
|
|
301
117
|
export const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER;
|
|
302
118
|
const BASE64_STANDARD = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
@@ -308,19 +124,14 @@ function hasOwn(value, key) {
|
|
|
308
124
|
return Object.prototype.hasOwnProperty.call(value, key);
|
|
309
125
|
}
|
|
310
126
|
/**
|
|
311
|
-
* 64 hex characters
|
|
312
|
-
*
|
|
313
|
-
* upper case here would contradict a ratified decision and punish a producer
|
|
314
|
-
* who did nothing wrong. Every comparison against a digest lowercases both
|
|
315
|
-
* sides, which is what the other kernels already do.
|
|
127
|
+
* 64 hex characters in either case: the hex rule pins the charset, not the
|
|
128
|
+
* case. Every digest comparison lowercases both sides.
|
|
316
129
|
*/
|
|
317
130
|
function isHex64(value) {
|
|
318
131
|
return typeof value === "string" && HEX64.test(value);
|
|
319
132
|
}
|
|
320
133
|
/**
|
|
321
|
-
* A
|
|
322
|
-
* not a number. Zero is a legal length: the empty part is a real part, and a
|
|
323
|
-
* rule that read zero as "no length" would leave it unchecked in silence.
|
|
134
|
+
* A non-negative safe integer. Zero is legal: the empty part is a real part.
|
|
324
135
|
*/
|
|
325
136
|
function isSafeLength(value) {
|
|
326
137
|
return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
|
|
@@ -328,16 +139,9 @@ function isSafeLength(value) {
|
|
|
328
139
|
function isSafeIndex(value) {
|
|
329
140
|
return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
|
|
330
141
|
}
|
|
331
|
-
/**
|
|
332
|
-
* Distinguishes "no inline content" from an inline content of `null`, which
|
|
333
|
-
* A14's ruling says is the same thing but which a plain `null` could not be
|
|
334
|
-
* told apart from a legitimate value.
|
|
335
|
-
*/
|
|
336
142
|
const ABSENT = Symbol("absent");
|
|
337
143
|
/**
|
|
338
|
-
* The
|
|
339
|
-
* `INLINE_BYTES_KEYS` present with a value that is not `null`, so a part
|
|
340
|
-
* spelling it twice reads one way in both kernels.
|
|
144
|
+
* The first of `INLINE_BYTES_KEYS` present with a non-null value, or `ABSENT`.
|
|
341
145
|
*/
|
|
342
146
|
function inlineValue(part) {
|
|
343
147
|
for (const key of INLINE_BYTES_KEYS) {
|
|
@@ -348,13 +152,8 @@ function inlineValue(part) {
|
|
|
348
152
|
return ABSENT;
|
|
349
153
|
}
|
|
350
154
|
/**
|
|
351
|
-
*
|
|
352
|
-
*
|
|
353
|
-
* permissive: this exists only to decide whether a `content_hash` sitting
|
|
354
|
-
* BESIDE inline content agrees with it, and the message's own commitment
|
|
355
|
-
* governs either way, so the finding fires only where no reading agrees at all.
|
|
356
|
-
* A value that is not a string carries no readings, so a `content_hash` beside
|
|
357
|
-
* one has nothing to agree with.
|
|
155
|
+
* The raw UTF-8, base64 and hex readings of an inline string value. A
|
|
156
|
+
* non-string has no readings.
|
|
358
157
|
*/
|
|
359
158
|
function inlineReadings(value) {
|
|
360
159
|
if (typeof value !== "string")
|
|
@@ -370,21 +169,15 @@ function inlineReadings(value) {
|
|
|
370
169
|
return out;
|
|
371
170
|
}
|
|
372
171
|
/**
|
|
373
|
-
* The
|
|
374
|
-
*
|
|
375
|
-
*
|
|
376
|
-
* Shape only, and synchronous on purpose: the inline hash agreement needs
|
|
377
|
-
* SHA-256, which is async here and not in Python, so refining
|
|
378
|
-
* `PART_INLINE_BYTES` to `PART_INLINE_MISMATCHED` happens in the reading
|
|
379
|
-
* loop instead. Keeping it out of here is what lets `isFilePart` mean the same
|
|
380
|
-
* thing, and have the same signature, in both kernels.
|
|
172
|
+
* The result a part reads before supplied bytes are considered, or `null` for
|
|
173
|
+
* a checkable descriptor part. Synchronous so `isFilePart` stays synchronous;
|
|
174
|
+
* the inline hash check, which needs async SHA-256, runs in the reading loop.
|
|
381
175
|
*/
|
|
382
176
|
function classify(part) {
|
|
383
177
|
if (!isPlainObject(part))
|
|
384
178
|
return PART_NOT_A_FILE;
|
|
385
179
|
if (part.kind !== "file")
|
|
386
180
|
return PART_NOT_A_FILE;
|
|
387
|
-
// ACCEPTED, and committed by the message itself (proof#157).
|
|
388
181
|
if (inlineValue(part) !== ABSENT)
|
|
389
182
|
return PART_INLINE_BYTES;
|
|
390
183
|
if (!isHex64(part.content_hash))
|
|
@@ -394,28 +187,10 @@ function classify(part) {
|
|
|
394
187
|
return null;
|
|
395
188
|
}
|
|
396
189
|
/**
|
|
397
|
-
* Whether the
|
|
398
|
-
*
|
|
399
|
-
*
|
|
400
|
-
*
|
|
401
|
-
* was skipped for one round of this unmerged PR, on the argument that a reader
|
|
402
|
-
* holding the message can see a contradiction between the content and the
|
|
403
|
-
* claim from the message alone. That argument is true and it is also true of
|
|
404
|
-
* `content_hash`, which is compared: accepting it deletes the neighboring
|
|
405
|
-
* check. So the rule is the one section 4.1 already applies to a descriptor
|
|
406
|
-
* part, where a hash that misses and a count that misses reach one result: two
|
|
407
|
-
* claims about one blob, and reading one of them lets the other be false in
|
|
408
|
-
* silence.
|
|
409
|
-
*
|
|
410
|
-
* A claim that is ABSENT, or is not the shape the table names, is not a claim:
|
|
411
|
-
* it is not compared and it cannot disagree. A part writing neither agrees by
|
|
412
|
-
* default, because the message's own commitment governs and this only ever
|
|
413
|
-
* names a SECOND claim that contradicts it.
|
|
414
|
-
*
|
|
415
|
-
* ONE reading has to satisfy EVERY claim present, rather than each claim
|
|
416
|
-
* finding its own. Grading them separately would let a `content_hash` that
|
|
417
|
-
* agrees under the base64 decoding sit beside a `content_length` that agrees
|
|
418
|
-
* under the raw text, a pair no blob in the world satisfies, and call it clean.
|
|
190
|
+
* Whether the `content_hash` and `content_length` beside an inline part agree
|
|
191
|
+
* with its content. An absent or malformed claim is not compared. One reading
|
|
192
|
+
* must satisfy every claim present: a hash agreeing under base64 beside a
|
|
193
|
+
* length agreeing under raw text describes no real blob.
|
|
419
194
|
*/
|
|
420
195
|
async function inlineClaimsAgree(part) {
|
|
421
196
|
const wanted = isHex64(part.content_hash)
|
|
@@ -436,22 +211,15 @@ async function inlineClaimsAgree(part) {
|
|
|
436
211
|
return false;
|
|
437
212
|
}
|
|
438
213
|
/**
|
|
439
|
-
* Whether
|
|
440
|
-
*
|
|
441
|
-
* 4.1 states, in the order that keeps inline content out. An INLINE part is
|
|
442
|
-
* accepted and committed by the message itself (proof#157), so there is nothing
|
|
443
|
-
* here for supplied bytes to be checked against and this answers `false`.
|
|
214
|
+
* Whether bytes beside the message are checked against this part: true only
|
|
215
|
+
* for a conformant descriptor part, never for an inline one.
|
|
444
216
|
*/
|
|
445
217
|
export function isFilePart(part) {
|
|
446
218
|
return classify(part) === null;
|
|
447
219
|
}
|
|
448
220
|
/**
|
|
449
|
-
* The parts
|
|
450
|
-
*
|
|
451
|
-
* A message that carries no `parts` array carries no file parts, and that is
|
|
452
|
-
* not a deviation: it is the common case, a JSON-only message. Every supplied
|
|
453
|
-
* byte string then has no part to belong to and is refused, which is the
|
|
454
|
-
* reading section 4.1 asks for and not an exception.
|
|
221
|
+
* The message's `parts`, or none. A message without parts is the common
|
|
222
|
+
* JSON-only case; every supplied entry is then refused.
|
|
455
223
|
*/
|
|
456
224
|
function messageParts(message) {
|
|
457
225
|
if (!isPlainObject(message))
|
|
@@ -459,14 +227,7 @@ function messageParts(message) {
|
|
|
459
227
|
const parts = message.parts;
|
|
460
228
|
return Array.isArray(parts) ? [...parts] : [];
|
|
461
229
|
}
|
|
462
|
-
/**
|
|
463
|
-
* The message-level named results, before any part is read.
|
|
464
|
-
*
|
|
465
|
-
* One condition reaches this today: a `parts` that is present, is not null and
|
|
466
|
-
* is not an array. It gets a name rather than the silence it had (proof#156
|
|
467
|
-
* gate), because a message with part-shaped content and no parts is exactly the
|
|
468
|
-
* shape section 4.1's "one result per part" leaves nothing to say about.
|
|
469
|
-
*/
|
|
230
|
+
/** Message-level results, before any part is read. */
|
|
470
231
|
function messageResults(message) {
|
|
471
232
|
if (!isPlainObject(message))
|
|
472
233
|
return [];
|
|
@@ -493,28 +254,14 @@ function asAscii(data) {
|
|
|
493
254
|
return out;
|
|
494
255
|
}
|
|
495
256
|
/**
|
|
496
|
-
* Strict ASCII hex
|
|
497
|
-
* `bytes.fromhex` skips
|
|
498
|
-
*
|
|
499
|
-
*
|
|
500
|
-
* other.
|
|
501
|
-
*
|
|
502
|
-
* EVERY GUARD HERE AND IN `base64Decode` IS PINNED BY A CORPUS VECTOR CARRYING
|
|
503
|
-
* A HAND-WRITTEN EXPECTATION (proof#156 second gate, F3), named in the comment
|
|
504
|
-
* beside it. Until that round, six of the seven produced no signal when
|
|
505
|
-
* deleted: the whole ladder and the whole leg stayed green, so a third kernel
|
|
506
|
-
* that delegated to `atob` or `base64.b64decode` reproduced the entire corpus
|
|
507
|
-
* byte for byte, was conformant under `architecture.md` K3, and still put a
|
|
508
|
-
* record on the other side of `part_encoding_mismatch`. An agreement case
|
|
509
|
-
* cannot close that: the leg grades two kernels against each other, so a guard
|
|
510
|
-
* deleted from both is invisible to it. Only a hand-written `expected` block
|
|
511
|
-
* states which side of the boundary an input falls on.
|
|
257
|
+
* Strict ASCII hex, written out because library decoders differ (Python's
|
|
258
|
+
* `bytes.fromhex` skips whitespace), and a difference would move a record
|
|
259
|
+
* across `part_encoding_mismatch`. Each guard here and in `base64Decode` is
|
|
260
|
+
* pinned by a hand-written vector, named beside it, so deleting it goes red.
|
|
512
261
|
*/
|
|
513
262
|
function hexDecode(data) {
|
|
514
|
-
//
|
|
515
|
-
//
|
|
516
|
-
// `part_encoding_mismatch` where it owes `part_mismatched` and a
|
|
517
|
-
// substitution: `parts-decoder-hex-odd-length-01`.
|
|
263
|
+
// Odd length is not hex; otherwise "ab0" reads as 0xab 0x00
|
|
264
|
+
// (`parts-decoder-hex-odd-length-01`).
|
|
518
265
|
if (data.length === 0 || data.length % 2 !== 0)
|
|
519
266
|
return null;
|
|
520
267
|
const text = asAscii(data);
|
|
@@ -527,33 +274,13 @@ function hexDecode(data) {
|
|
|
527
274
|
return out;
|
|
528
275
|
}
|
|
529
276
|
/**
|
|
530
|
-
* Strict base64, standard
|
|
531
|
-
*
|
|
532
|
-
*
|
|
533
|
-
* and JavaScript's `atob` disagree about padding, about the URL-safe alphabet
|
|
534
|
-
* and about non-zero trailing bits, and each of those disagreements decides
|
|
535
|
-
* whether a receiver refuses its own decoding defect or records a tamper
|
|
536
|
-
* against an honest sender. The two alphabets are not mixed: a string carrying
|
|
537
|
-
* one character from each is neither spelling and decodes to nothing.
|
|
538
|
-
*
|
|
539
|
-
* TWO GUARDS THAT STOOD HERE WERE DELETED IN THE PROOF#156 SECOND GATE'S
|
|
540
|
-
* ROUND, because they could not change a decode for any input and so read as
|
|
541
|
-
* protection that was not there:
|
|
542
|
-
*
|
|
543
|
-
* * `pad > 0 && text.length % 4 !== 0` is the SAME PREDICATE as the
|
|
544
|
-
* `(body.length + pad) % 4` test below it. The strip loop above makes
|
|
545
|
-
* `text` exactly `body + "=".repeat(pad)`, so the two lengths are equal by
|
|
546
|
-
* construction and the two conditions are one condition written twice.
|
|
547
|
-
* Each masked the other, which is why deleting either alone changed
|
|
548
|
-
* nothing;
|
|
549
|
-
* * `body.includes("=")` is subsumed by the alphabet test three lines below
|
|
550
|
-
* it: `=` is in neither alphabet, so a body carrying one is refused there.
|
|
277
|
+
* Strict base64, standard or URL-safe (never mixed), padded or not, canonical
|
|
278
|
+
* only. Written out because `atob` and Python's `base64.b64decode` disagree on
|
|
279
|
+
* padding, the URL-safe alphabet and non-zero trailing bits.
|
|
551
280
|
*
|
|
552
|
-
*
|
|
553
|
-
*
|
|
554
|
-
*
|
|
555
|
-
* decodes move.
|
|
556
|
-
* The remaining guards each carry the vector that pins them.
|
|
281
|
+
* There is no separate `pad > 0 && text.length % 4 !== 0` or
|
|
282
|
+
* `body.includes("=")` guard: the first is the alignment test below, and `=`
|
|
283
|
+
* fails the alphabet test.
|
|
557
284
|
*/
|
|
558
285
|
function base64Decode(data) {
|
|
559
286
|
if (data.length === 0)
|
|
@@ -565,34 +292,21 @@ function base64Decode(data) {
|
|
|
565
292
|
body = body.slice(0, -1);
|
|
566
293
|
pad += 1;
|
|
567
294
|
}
|
|
568
|
-
// At most two padding characters
|
|
569
|
-
// aligned body followed by four `=` decode as though the padding were not
|
|
570
|
-
// there: `parts-decoder-base64-four-pad-chars-01`.
|
|
295
|
+
// At most two padding characters (`parts-decoder-base64-four-pad-chars-01`).
|
|
571
296
|
if (pad > 2)
|
|
572
297
|
return null;
|
|
573
298
|
const standard = [...body].every((c) => BASE64_STANDARD.includes(c));
|
|
574
299
|
const urlsafe = [...body].every((c) => BASE64_URLSAFE.includes(c));
|
|
575
|
-
// One alphabet or the other, never
|
|
576
|
-
//
|
|
577
|
-
// the URL-safe one and asks it for `/`, where `indexOf` answers -1 and
|
|
578
|
-
// garbles the arithmetic into `f7 f0 00`. TWO vectors pin that, one per
|
|
579
|
-
// shape of third kernel: `parts-decoder-base64-mixed-alphabet-01` commits
|
|
580
|
-
// the bytes a kernel that MERGES the alphabets reads, and
|
|
581
|
-
// `parts-decoder-base64-mixed-alphabet-02` commits the bytes this kernel's
|
|
582
|
-
// own -1 produces, which is the one that goes red HERE (proof#156 third
|
|
583
|
-
// gate, C2: deleting this line left all 2224 TypeScript tests green).
|
|
300
|
+
// One alphabet or the other, never both (`parts-decoder-base64-mixed-alphabet-01`
|
|
301
|
+
// and `-02`).
|
|
584
302
|
if (!standard && !urlsafe)
|
|
585
303
|
return null;
|
|
586
304
|
const alphabet = standard ? BASE64_STANDARD : BASE64_URLSAFE;
|
|
587
|
-
// A lone trailing character carries six bits
|
|
588
|
-
//
|
|
589
|
-
// characters whose low bits are zero could claim to be it:
|
|
590
|
-
// `parts-decoder-base64-single-character-01`.
|
|
305
|
+
// A lone trailing character carries six bits; otherwise "A" reads as the
|
|
306
|
+
// empty blob (`parts-decoder-base64-single-character-01`).
|
|
591
307
|
if (body.length % 4 === 1)
|
|
592
308
|
return null;
|
|
593
|
-
// Padding
|
|
594
|
-
// body followed by one `=` decode as though the `=` were not there:
|
|
595
|
-
// `parts-decoder-base64-padding-misaligned-01`.
|
|
309
|
+
// Padding must align the string (`parts-decoder-base64-padding-misaligned-01`).
|
|
596
310
|
if (pad > 0 && (body.length + pad) % 4 !== 0)
|
|
597
311
|
return null;
|
|
598
312
|
let bits = 0;
|
|
@@ -607,23 +321,15 @@ function base64Decode(data) {
|
|
|
607
321
|
bits = bits % 2 ** width;
|
|
608
322
|
}
|
|
609
323
|
}
|
|
610
|
-
// Canonical only:
|
|
611
|
-
//
|
|
612
|
-
// blob decode to one value. Both `atob` and `base64.b64decode` accept them,
|
|
613
|
-
// which is the sharpest reason this decoder is written out: deleting this
|
|
614
|
-
// line lets `"QR=="` read as `"A"`, so a receiver handed one of the sixteen
|
|
615
|
-
// non-canonical spellings of a one-byte part writes `part_encoding_mismatch`
|
|
616
|
-
// where it owes `part_mismatched` and a substitution:
|
|
617
|
-
// `parts-decoder-non-canonical-tail-01`.
|
|
324
|
+
// Canonical only: non-zero unused low bits would let two spellings decode to
|
|
325
|
+
// one blob, and `atob` accepts them (`parts-decoder-non-canonical-tail-01`).
|
|
618
326
|
if (width > 0 && bits !== 0)
|
|
619
327
|
return null;
|
|
620
328
|
return new Uint8Array(out);
|
|
621
329
|
}
|
|
622
330
|
/**
|
|
623
|
-
* The inverse of `base64Decode`, written out
|
|
624
|
-
*
|
|
625
|
-
* this value is compared against a digest a sender committed, so the two
|
|
626
|
-
* kernels have to emit the same characters.
|
|
331
|
+
* The inverse of `base64Decode`, written out so every implementation emits the
|
|
332
|
+
* same characters.
|
|
627
333
|
*/
|
|
628
334
|
function base64Encode(data, alphabet, pad) {
|
|
629
335
|
let out = "";
|
|
@@ -651,27 +357,10 @@ function asciiBytes(text) {
|
|
|
651
357
|
return out;
|
|
652
358
|
}
|
|
653
359
|
/**
|
|
654
|
-
* Every spelling of `data` an honest stack might have been handed
|
|
655
|
-
*
|
|
656
|
-
*
|
|
657
|
-
*
|
|
658
|
-
* encoded text, which is what a single-line DER attachment, a PEM body and a
|
|
659
|
-
* JWT each are: the part's bytes ARE the base64 text. Guarding only the
|
|
660
|
-
* decoding direction told that receiver it held a tamper and fired the
|
|
661
|
-
* substitution, which is the one outcome this module calls worse than recording
|
|
662
|
-
* nothing.
|
|
663
|
-
*
|
|
664
|
-
* The list is fixed and short, the same six spellings the decoder accepts, so
|
|
665
|
-
* the two kernels try the same candidates in the same order and the cost is
|
|
666
|
-
* linear in the length of the supply.
|
|
667
|
-
*
|
|
668
|
-
* A `data.length === 0` guard stood here and was deleted in the proof#156
|
|
669
|
-
* second gate's round, for the same reason as the two in `base64Decode`: it
|
|
670
|
-
* could not change an answer. This is reached only from `respellingExplains`,
|
|
671
|
-
* and only after `sha256(data)` has already been compared to `contentHash` and
|
|
672
|
-
* missed. Every spelling of the empty byte string is the empty byte string, so
|
|
673
|
-
* the one candidate it would have produced is the digest that just failed. An
|
|
674
|
-
* optimization written as a guard reads as protection that is not there.
|
|
360
|
+
* Every spelling of `data` an honest stack might have been handed: the
|
|
361
|
+
* encoding direction of the encoding guard. A receiver whose stack decoded a
|
|
362
|
+
* PEM, DER or JWT holds decoded bytes while the sender committed the text.
|
|
363
|
+
* No empty-input guard: an empty `data` already missed the digest.
|
|
675
364
|
*/
|
|
676
365
|
function respellings(data) {
|
|
677
366
|
return [
|
|
@@ -684,10 +373,8 @@ function respellings(data) {
|
|
|
684
373
|
];
|
|
685
374
|
}
|
|
686
375
|
/**
|
|
687
|
-
* Whether a base64 or hex respelling of `data`, in
|
|
688
|
-
* `contentHash
|
|
689
|
-
* the case is not (proof#135), so every comparison in this module is made on
|
|
690
|
-
* one spelling.
|
|
376
|
+
* Whether a base64 or hex respelling of `data`, in either direction, hashes to
|
|
377
|
+
* `contentHash` (lowercase).
|
|
691
378
|
*/
|
|
692
379
|
async function respellingExplains(data, contentHash) {
|
|
693
380
|
for (const decoded of [base64Decode(data), hexDecode(data)]) {
|
|
@@ -703,27 +390,15 @@ async function respellingExplains(data, contentHash) {
|
|
|
703
390
|
/**
|
|
704
391
|
* What a receiver commits to, given the message and the bytes it holds.
|
|
705
392
|
*
|
|
706
|
-
* `supplied`
|
|
707
|
-
*
|
|
708
|
-
*
|
|
709
|
-
*
|
|
710
|
-
*
|
|
711
|
-
*
|
|
712
|
-
* Where a part's bytes hash to something other than its `content_hash`, or
|
|
713
|
-
* where their count is not its `content_length`, the returned `heldMessage`
|
|
714
|
-
* carries that part's `content_hash` and `content_length` replaced by the held
|
|
715
|
-
* bytes' own. The receiver commits `event.message_hash` over THAT, so its
|
|
716
|
-
* record and the sender's disagree and the pair fails on `message_hash`.
|
|
717
|
-
*
|
|
718
|
-
* A refusal is scoped to the supplied entry it is about: the part it names is
|
|
719
|
-
* not checked and not substituted, every other part reads as it would have, and
|
|
720
|
-
* `heldMessage` is returned either way. Nothing a caller or a sender can do
|
|
721
|
-
* denies the receiver a record.
|
|
393
|
+
* `supplied` names each byte string by its part index. With none, every file
|
|
394
|
+
* part is `PART_UNCHECKED` and the held message is the message given. Where a
|
|
395
|
+
* part's bytes miss its hash or length, `heldMessage` carries the held bytes'
|
|
396
|
+
* own values for that part, so the pair fails on `message_hash`. A refusal is
|
|
397
|
+
* scoped to its entry; `heldMessage` is always returned.
|
|
722
398
|
*/
|
|
723
399
|
export async function commitHeldMessage(message, supplied = []) {
|
|
724
400
|
const parts = messageParts(message);
|
|
725
401
|
const classifications = parts.map(classify);
|
|
726
|
-
// The inline hash agreement, refined out of the shape pass because it hashes.
|
|
727
402
|
for (let i = 0; i < parts.length; i++) {
|
|
728
403
|
if (classifications[i] !== PART_INLINE_BYTES)
|
|
729
404
|
continue;
|
|
@@ -737,15 +412,13 @@ export async function commitHeldMessage(message, supplied = []) {
|
|
|
737
412
|
supplied.forEach((item, entry) => {
|
|
738
413
|
const index = item.part;
|
|
739
414
|
if (!isSafeIndex(index) || index >= parts.length) {
|
|
740
|
-
// No part of this message
|
|
741
|
-
// it still names itself rather than going quiet.
|
|
415
|
+
// No part of this message.
|
|
742
416
|
refusals.push({ name: BYTES_FOR_UNKNOWN_PART, part: null, entry });
|
|
743
417
|
return;
|
|
744
418
|
}
|
|
745
419
|
if (classifications[index] !== null) {
|
|
746
|
-
// A part that
|
|
747
|
-
//
|
|
748
|
-
// part it altered.
|
|
420
|
+
// A part that exists but cannot be checked. Scoped to that part, so one
|
|
421
|
+
// uncheckable part cannot hide an altered one.
|
|
749
422
|
refusals.push({ name: BYTES_FOR_UNKNOWN_PART, part: index, entry });
|
|
750
423
|
return;
|
|
751
424
|
}
|
|
@@ -785,10 +458,7 @@ export async function commitHeldMessage(message, supplied = []) {
|
|
|
785
458
|
const contentLength = part.content_length;
|
|
786
459
|
const digest = await sha256Hex(first.data);
|
|
787
460
|
if (digest === contentHash) {
|
|
788
|
-
//
|
|
789
|
-
// hash disagreement does (proof#156 gate). Before that, a part whose
|
|
790
|
-
// stated length the receiver could see was false read `part_matched`, so
|
|
791
|
-
// the receiver certified the part and committed the false length with it.
|
|
461
|
+
// A matching hash with a false length still substitutes.
|
|
792
462
|
if (first.data.length === contentLength) {
|
|
793
463
|
readings.push({ index, result: PART_MATCHED });
|
|
794
464
|
continue;
|
|
@@ -824,9 +494,7 @@ export async function commitHeldMessage(message, supplied = []) {
|
|
|
824
494
|
});
|
|
825
495
|
let held = message;
|
|
826
496
|
if (replacements.size > 0) {
|
|
827
|
-
// Rebuilt
|
|
828
|
-
// the caller's, and a reading that edits its input is a reading that
|
|
829
|
-
// changes what the caller commits to on the honest path too.
|
|
497
|
+
// Rebuilt, not mutated: the caller's message is the caller's.
|
|
830
498
|
const heldParts = parts.map((part, index) => {
|
|
831
499
|
const replacement = replacements.get(index);
|
|
832
500
|
if (replacement === undefined)
|
|
@@ -850,64 +518,36 @@ export async function commitHeldMessage(message, supplied = []) {
|
|
|
850
518
|
};
|
|
851
519
|
}
|
|
852
520
|
/**
|
|
853
|
-
* The parts the receiver
|
|
854
|
-
*
|
|
855
|
-
*
|
|
856
|
-
*
|
|
857
|
-
*
|
|
858
|
-
* is one of `HELD_UNCHECKED_RESULTS`: the receiver had bytes for that part,
|
|
859
|
-
* the rule declined to grade them, and NOTHING about that reaches the
|
|
860
|
-
* committed record.
|
|
861
|
-
*
|
|
862
|
-
* THE RULE, in one sentence, because a filter is not a rule. A part is in here
|
|
863
|
-
* where the receiver held bytes for it AND the message itself does not commit
|
|
864
|
-
* its content. `PART_UNCHECKED` is a refused supply;
|
|
865
|
-
* `PART_MALFORMED_DESCRIPTOR` is a part whose content travels BESIDE the
|
|
866
|
-
* message under a claim no receiver can use, so the receiver's bytes are
|
|
867
|
-
* exactly as ungraded and the record says as little. The first cut of this
|
|
868
|
-
* function omitted the second, which the third gate measured: a `kind: "file"`
|
|
869
|
-
* part with a non-conformant `content_hash` or `content_length`, bytes held,
|
|
870
|
-
* read `[]` here, so a caller following the rule this module prescribes wrote
|
|
871
|
-
* `accepted` over a part it held bytes for and never checked. That is the
|
|
872
|
-
* class the mechanism exists to close, and Phil named it in proof#162's
|
|
873
|
-
* ratification as the gap that decision inherits unless it is closed here.
|
|
521
|
+
* The parts the receiver held bytes for and did not check: a refusal names the
|
|
522
|
+
* part and its result is in `HELD_UNCHECKED_RESULTS`, or the part is a URL part
|
|
523
|
+
* that reads `PART_NOT_A_FILE` and the refusal is `BYTES_FOR_UNKNOWN_PART`. A
|
|
524
|
+
* caller writes `accepted` only when this and `substituted` are both empty, and
|
|
525
|
+
* records the positions and their count when it is not zero.
|
|
874
526
|
*
|
|
875
|
-
*
|
|
876
|
-
*
|
|
877
|
-
* their content INSIDE the message, and `message_hash` commits it on both
|
|
878
|
-
* sides: bytes supplied beside one of those grade nothing and hide nothing,
|
|
879
|
-
* because a reader of either pack reads the content itself. Naming them would
|
|
880
|
-
* fire the caller's obligation on parts the swapped-bytes attack cannot reach
|
|
881
|
-
* at all.
|
|
882
|
-
*
|
|
883
|
-
* Why it is worth a function rather than a note. Delivering a tampered copy of
|
|
884
|
-
* a part alone is `part_mismatched`, is substituted, and fails the pair on
|
|
885
|
-
* `message_hash`. Delivering the tampered copy AND the true one is
|
|
886
|
-
* `conflicting_part_bytes`, is never substituted, and the pair VERIFIES, so
|
|
887
|
-
* adding data suppresses detection and the only trace is a refusal the
|
|
888
|
-
* receiving side publishes at its own discretion.
|
|
889
|
-
*
|
|
890
|
-
* NOT the same as "this part reads `PART_UNCHECKED`". A receiver that holds no
|
|
891
|
-
* bytes at all reads every file part that way, and that is the COMMON CASE,
|
|
892
|
-
* unchanged by A18 and not a reason to withhold `accepted`. Only a part whose
|
|
893
|
-
* bytes the receiver actually held and could not grade is in here.
|
|
894
|
-
*
|
|
895
|
-
* What this does NOT do: it cannot stop a caller writing `accepted` anyway.
|
|
896
|
-
* This module reads and does not write the record. proof#162 ratified what the
|
|
897
|
-
* record layer does instead (Phil, 2026-09-22): `accepted` keeps its meaning
|
|
898
|
-
* and the COUNT of these parts rides in an additive `context` key, REQUIRED
|
|
899
|
-
* when it is not zero, enforced against SDKs through conformance vectors. So a
|
|
900
|
-
* part missing from this list is a part no record built on that rule will ever
|
|
901
|
-
* mention.
|
|
527
|
+
* Not the same as "reads `PART_UNCHECKED`": a receiver holding no bytes reads
|
|
528
|
+
* every file part that way, which is the common case.
|
|
902
529
|
*/
|
|
903
530
|
export function uncheckedHeldParts(reading) {
|
|
904
531
|
const unchecked = new Set(reading.parts
|
|
905
532
|
.filter((p) => HELD_UNCHECKED_RESULTS.includes(p.result))
|
|
906
533
|
.map((p) => p.index));
|
|
534
|
+
const held = reading.heldMessage;
|
|
535
|
+
const parts = isPlainObject(held) && Array.isArray(held.parts) ? held.parts : [];
|
|
536
|
+
const urlParts = new Set(reading.parts
|
|
537
|
+
.filter((p) => p.result === PART_NOT_A_FILE && p.index < parts.length && isUrlPart(parts[p.index]))
|
|
538
|
+
.map((p) => p.index));
|
|
907
539
|
const named = new Set();
|
|
908
540
|
for (const refusal of reading.refusals) {
|
|
909
|
-
if (refusal.part
|
|
541
|
+
if (refusal.part === null)
|
|
542
|
+
continue;
|
|
543
|
+
if (unchecked.has(refusal.part))
|
|
544
|
+
named.add(refusal.part);
|
|
545
|
+
if (urlParts.has(refusal.part) && refusal.name === BYTES_FOR_UNKNOWN_PART)
|
|
910
546
|
named.add(refusal.part);
|
|
911
547
|
}
|
|
912
548
|
return [...named].sort((a, b) => a - b);
|
|
913
549
|
}
|
|
550
|
+
/** A part with no `kind: "file"` whose `url` member is present and not `null`. */
|
|
551
|
+
function isUrlPart(part) {
|
|
552
|
+
return isPlainObject(part) && part.kind !== "file" && hasOwn(part, "url") && part.url !== null;
|
|
553
|
+
}
|