|
1 | 1 | // SPDX-License-Identifier: Apache-2.0 |
2 | 2 |
|
3 | 3 | /** |
4 | | - * `dev.atrib/attribution` extension receipts (P049 draft, |
5 | | - * docs/adr-draft-p049-mcp-extension.md). |
| 4 | + * `dev.atrib/attribution` extension receipts (accepted as D141; extension |
| 5 | + * spec at docs/extensions/dev.atrib-attribution/v0.1.md, conformance at |
| 6 | + * spec/conformance/mcp-extension/). |
6 | 7 | * |
7 | 8 | * Behind an opt-in flag, the daemon client parses attestation receipts |
8 | 9 | * from tool results' `_meta["dev.atrib/attribution"]`: the propagation |
|
15 | 16 | * signed records and inclusion proofs, never from the receipt itself. |
16 | 17 | */ |
17 | 18 |
|
18 | | -import type { AtribRecord } from '@atrib/mcp' |
| 19 | +import { encodeToken, normalizeEventType, type AtribRecord } from '@atrib/mcp' |
| 20 | +import { recordHashRef } from './hashes.js' |
19 | 21 |
|
20 | 22 | export const ATTRIBUTION_EXTENSION_KEY = 'dev.atrib/attribution' |
21 | 23 |
|
@@ -87,3 +89,81 @@ export function parseAttributionReceiptBlock(meta: unknown): AttributionReceiptB |
87 | 89 | ? out |
88 | 90 | : null |
89 | 91 | } |
| 92 | + |
| 93 | +/** Outcome of checking a receipt against its attached signed record. */ |
| 94 | +export interface AttributionReceiptConsistency { |
| 95 | + /** True iff every receipt claim matches the attached record. */ |
| 96 | + receipt_valid: boolean |
| 97 | + /** Receipt fields whose claims contradict the attached record. */ |
| 98 | + mismatched_fields: string[] |
| 99 | + /** recordHashRef of the attached record (when a record is available). */ |
| 100 | + attached_record_hash?: string |
| 101 | + /** The receipt's claimed record_hash (when present). */ |
| 102 | + claimed_record_hash?: string |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * Check a receipt block's claims against the signed record they name |
| 107 | + * (the attached `block.record`, or a caller-retrieved record). Receipts |
| 108 | + * are advisory: a mismatch NEVER invalidates the tool result — it means |
| 109 | + * the receipt must not be trusted or cited (conformance: |
| 110 | + * spec/conformance/mcp-extension/cases/receipt--*.json). |
| 111 | + * |
| 112 | + * Compared claims: `receipt.record_hash` vs the record's canonical hash, |
| 113 | + * `token` vs encodeToken(record), and `creator_key` / `context_id` / |
| 114 | + * `chain_root` / `event_type` (short name or URI, normalized) vs the |
| 115 | + * record's fields. Absent receipt fields are not mismatches. |
| 116 | + */ |
| 117 | +export function checkAttributionReceiptConsistency( |
| 118 | + block: AttributionReceiptBlock, |
| 119 | + record?: AtribRecord, |
| 120 | +): AttributionReceiptConsistency { |
| 121 | + const attached = record ?? block.record |
| 122 | + const receipt = block.receipt |
| 123 | + const claimed = receipt?.record_hash |
| 124 | + if (!attached) { |
| 125 | + return { |
| 126 | + receipt_valid: false, |
| 127 | + mismatched_fields: ['record'], |
| 128 | + ...(claimed !== undefined ? { claimed_record_hash: claimed } : {}), |
| 129 | + } |
| 130 | + } |
| 131 | + const mismatched: string[] = [] |
| 132 | + let attachedHash: string | undefined |
| 133 | + let token: string | undefined |
| 134 | + try { |
| 135 | + attachedHash = recordHashRef(attached) |
| 136 | + token = encodeToken(attached) |
| 137 | + } catch { |
| 138 | + // A record that cannot be canonicalized/hashed cannot back a receipt. |
| 139 | + return { |
| 140 | + receipt_valid: false, |
| 141 | + mismatched_fields: ['record'], |
| 142 | + ...(claimed !== undefined ? { claimed_record_hash: claimed } : {}), |
| 143 | + } |
| 144 | + } |
| 145 | + if (claimed !== undefined && claimed !== attachedHash) mismatched.push('record_hash') |
| 146 | + if (block.token !== undefined && block.token !== token) mismatched.push('token') |
| 147 | + if (receipt?.creator_key !== undefined && receipt.creator_key !== attached.creator_key) { |
| 148 | + mismatched.push('creator_key') |
| 149 | + } |
| 150 | + if (receipt?.context_id !== undefined && receipt.context_id !== attached.context_id) { |
| 151 | + mismatched.push('context_id') |
| 152 | + } |
| 153 | + if (receipt?.chain_root !== undefined && receipt.chain_root !== attached.chain_root) { |
| 154 | + mismatched.push('chain_root') |
| 155 | + } |
| 156 | + if ( |
| 157 | + receipt?.event_type !== undefined && |
| 158 | + normalizeEventType(receipt.event_type) !== normalizeEventType(attached.event_type) |
| 159 | + ) { |
| 160 | + mismatched.push('event_type') |
| 161 | + } |
| 162 | + return { |
| 163 | + receipt_valid: mismatched.length === 0, |
| 164 | + mismatched_fields: mismatched, |
| 165 | + attached_record_hash: attachedHash, |
| 166 | + ...(claimed !== undefined ? { claimed_record_hash: claimed } : {}), |
| 167 | + } |
| 168 | +} |
| 169 | + |
0 commit comments