@sess.si/sdk 0.1.0-rc.8 → 0.1.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/CHANGELOG.md +66 -9
- package/NOTICE +1 -1
- package/README.md +92 -16
- package/dist/ask.d.ts +0 -1
- package/dist/ask.js +0 -1
- package/dist/cli/main.d.ts +0 -1
- package/dist/cli/main.js +0 -1
- package/dist/cli/verify.d.ts +0 -1
- package/dist/cli/verify.js +0 -1
- package/dist/client.d.ts +0 -1
- package/dist/client.js +0 -1
- package/dist/commerce/builders.d.ts +99 -0
- package/dist/commerce/builders.js +208 -0
- package/dist/commerce/currency.d.ts +4 -0
- package/dist/commerce/currency.js +25 -0
- package/dist/commerce/items.d.ts +119 -0
- package/dist/commerce/items.js +280 -0
- package/dist/commerce/lint.d.ts +16 -0
- package/dist/commerce/lint.js +56 -0
- package/dist/errors.d.ts +5 -2
- package/dist/errors.js +15 -25
- package/dist/generated/openapi.d.ts +12 -13
- package/dist/generated/openapi.js +0 -1
- package/dist/index.d.ts +6 -1
- package/dist/index.js +3 -1
- package/dist/internal/decode.d.ts +0 -1
- package/dist/internal/decode.js +9 -2
- package/dist/internal/json.d.ts +0 -1
- package/dist/internal/json.js +0 -1
- package/dist/internal/time.d.ts +0 -1
- package/dist/internal/time.js +0 -1
- package/dist/internal/version.d.ts +2 -0
- package/dist/internal/version.js +2 -0
- package/dist/polling.d.ts +0 -1
- package/dist/polling.js +0 -1
- package/dist/qr/encoder.d.ts +0 -1
- package/dist/qr/encoder.js +0 -1
- package/dist/qr/index.d.ts +0 -1
- package/dist/qr/index.js +0 -1
- package/dist/receipts/bundle.d.ts +0 -1
- package/dist/receipts/bundle.js +0 -1
- package/dist/receipts/index.d.ts +0 -1
- package/dist/receipts/index.js +0 -1
- package/dist/receipts/json.d.ts +0 -1
- package/dist/receipts/json.js +0 -1
- package/dist/receipts/key-cache.d.ts +0 -1
- package/dist/receipts/key-cache.js +0 -1
- package/dist/receipts/renderers.d.ts +0 -1
- package/dist/receipts/renderers.js +1 -2
- package/dist/receipts/resource.d.ts +0 -1
- package/dist/receipts/resource.js +0 -1
- package/dist/receipts/verify.d.ts +0 -1
- package/dist/receipts/verify.js +0 -1
- package/dist/sessions.d.ts +0 -1
- package/dist/sessions.js +0 -1
- package/dist/transport.d.ts +0 -1
- package/dist/transport.js +35 -5
- package/dist/types.d.ts +56 -2
- package/dist/types.js +0 -1
- package/dist/webhook-resource.d.ts +0 -1
- package/dist/webhook-resource.js +0 -1
- package/dist/webhooks/index.d.ts +0 -1
- package/dist/webhooks/index.js +0 -1
- package/package.json +8 -3
- package/dist/ask.d.ts.map +0 -1
- package/dist/ask.js.map +0 -1
- package/dist/cli/main.d.ts.map +0 -1
- package/dist/cli/main.js.map +0 -1
- package/dist/cli/verify.d.ts.map +0 -1
- package/dist/cli/verify.js.map +0 -1
- package/dist/client.d.ts.map +0 -1
- package/dist/client.js.map +0 -1
- package/dist/errors.d.ts.map +0 -1
- package/dist/errors.js.map +0 -1
- package/dist/generated/openapi.d.ts.map +0 -1
- package/dist/generated/openapi.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/internal/decode.d.ts.map +0 -1
- package/dist/internal/decode.js.map +0 -1
- package/dist/internal/json.d.ts.map +0 -1
- package/dist/internal/json.js.map +0 -1
- package/dist/internal/time.d.ts.map +0 -1
- package/dist/internal/time.js.map +0 -1
- package/dist/polling.d.ts.map +0 -1
- package/dist/polling.js.map +0 -1
- package/dist/qr/encoder.d.ts.map +0 -1
- package/dist/qr/encoder.js.map +0 -1
- package/dist/qr/index.d.ts.map +0 -1
- package/dist/qr/index.js.map +0 -1
- package/dist/receipts/bundle.d.ts.map +0 -1
- package/dist/receipts/bundle.js.map +0 -1
- package/dist/receipts/index.d.ts.map +0 -1
- package/dist/receipts/index.js.map +0 -1
- package/dist/receipts/json.d.ts.map +0 -1
- package/dist/receipts/json.js.map +0 -1
- package/dist/receipts/key-cache.d.ts.map +0 -1
- package/dist/receipts/key-cache.js.map +0 -1
- package/dist/receipts/renderers.d.ts.map +0 -1
- package/dist/receipts/renderers.js.map +0 -1
- package/dist/receipts/resource.d.ts.map +0 -1
- package/dist/receipts/resource.js.map +0 -1
- package/dist/receipts/verify.d.ts.map +0 -1
- package/dist/receipts/verify.js.map +0 -1
- package/dist/sessions.d.ts.map +0 -1
- package/dist/sessions.js.map +0 -1
- package/dist/transport.d.ts.map +0 -1
- package/dist/transport.js.map +0 -1
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js.map +0 -1
- package/dist/webhook-resource.d.ts.map +0 -1
- package/dist/webhook-resource.js.map +0 -1
- package/dist/webhooks/index.d.ts.map +0 -1
- package/dist/webhooks/index.js.map +0 -1
- package/src/ask.ts +0 -683
- package/src/cli/main.ts +0 -8
- package/src/cli/verify.ts +0 -349
- package/src/client.ts +0 -55
- package/src/errors.ts +0 -127
- package/src/generated/openapi.ts +0 -8586
- package/src/index.ts +0 -5
- package/src/internal/decode.ts +0 -348
- package/src/internal/json.ts +0 -64
- package/src/internal/time.ts +0 -85
- package/src/polling.ts +0 -173
- package/src/qr/encoder.ts +0 -345
- package/src/qr/index.ts +0 -203
- package/src/receipts/bundle.ts +0 -547
- package/src/receipts/index.ts +0 -29
- package/src/receipts/json.ts +0 -168
- package/src/receipts/key-cache.ts +0 -143
- package/src/receipts/renderers.ts +0 -220
- package/src/receipts/resource.ts +0 -264
- package/src/receipts/verify.ts +0 -554
- package/src/sessions.ts +0 -238
- package/src/transport.ts +0 -566
- package/src/types.ts +0 -219
- package/src/webhook-resource.ts +0 -81
- package/src/webhooks/index.ts +0 -246
package/src/receipts/verify.ts
DELETED
|
@@ -1,554 +0,0 @@
|
|
|
1
|
-
import { compactVerify, importJWK } from 'jose';
|
|
2
|
-
import type { InteractiveSurfaceRevision } from '../types.js';
|
|
3
|
-
import {
|
|
4
|
-
abortError,
|
|
5
|
-
canonicalJson,
|
|
6
|
-
checkedInstant,
|
|
7
|
-
deepFreeze,
|
|
8
|
-
exactKeys,
|
|
9
|
-
isRecord,
|
|
10
|
-
jsonDigest,
|
|
11
|
-
snapshotJson,
|
|
12
|
-
verificationError,
|
|
13
|
-
waitWithSignal,
|
|
14
|
-
type DeepReadonly,
|
|
15
|
-
} from './json.js';
|
|
16
|
-
import {
|
|
17
|
-
BUNDLED_RENDERER_IDENTITIES,
|
|
18
|
-
verifyRendererList,
|
|
19
|
-
type RendererIdentitiesDocument,
|
|
20
|
-
type RendererList,
|
|
21
|
-
} from './renderers.js';
|
|
22
|
-
|
|
23
|
-
export const MAX_RECEIPT_BYTES = 64 * 1024;
|
|
24
|
-
export const MAX_RECEIPT_KEY_SET_BYTES = 256 * 1024;
|
|
25
|
-
const MAX_KEYS = 64;
|
|
26
|
-
const KID = /^[A-Za-z0-9._-]{1,64}$/;
|
|
27
|
-
const SESSION_ID = /^ses_[0-9A-HJKMNP-TV-Z]{26}$/;
|
|
28
|
-
const EVENT_ID = /^evt_[0-9A-HJKMNP-TV-Z]{26}$/;
|
|
29
|
-
const ACTION_ID = /^[a-z][a-z0-9_]{0,31}$/;
|
|
30
|
-
const DIGEST = /^sha256:[0-9a-f]{64}$/;
|
|
31
|
-
|
|
32
|
-
/** Public Ed25519 verification material; private JWK parameters are refused. */
|
|
33
|
-
export interface ReceiptPublicKey {
|
|
34
|
-
kty: 'OKP';
|
|
35
|
-
crv: 'Ed25519';
|
|
36
|
-
x: string;
|
|
37
|
-
kid: string;
|
|
38
|
-
alg?: 'EdDSA';
|
|
39
|
-
use?: 'sig';
|
|
40
|
-
key_ops?: readonly 'verify'[];
|
|
41
|
-
ext?: boolean;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export interface ReceiptKeySet {
|
|
45
|
-
keys: readonly ReceiptPublicKey[];
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/** A resolver is explicitly trusted application configuration, never JWT URL discovery. */
|
|
49
|
-
export type ReceiptKeyResolver = (context: {
|
|
50
|
-
readonly kid: string;
|
|
51
|
-
readonly issuer: string;
|
|
52
|
-
readonly signal?: AbortSignal;
|
|
53
|
-
}) => ReceiptKeySet | Promise<ReceiptKeySet>;
|
|
54
|
-
|
|
55
|
-
export interface ReceiptClaims {
|
|
56
|
-
sess_receipt: 1;
|
|
57
|
-
iss: string;
|
|
58
|
-
aud: string;
|
|
59
|
-
jti: string;
|
|
60
|
-
iat: number;
|
|
61
|
-
exp: number;
|
|
62
|
-
accepted_at: string;
|
|
63
|
-
workspace_id: string;
|
|
64
|
-
session_id: string;
|
|
65
|
-
step_id: string;
|
|
66
|
-
event_id: string;
|
|
67
|
-
/**
|
|
68
|
-
* `renderer_version`: the renderer identity the revision was shown under (Decision 0009
|
|
69
|
-
* Amendment 13): one this release bundles, or one on a list signed by the issuer's trusted keys.
|
|
70
|
-
*/
|
|
71
|
-
surface: { revision: number; hash: string; renderer_version: string };
|
|
72
|
-
action: { id: string; label: string; summary: string };
|
|
73
|
-
input_hash: string;
|
|
74
|
-
actor: { assurance: 'link_holder'; subject: string };
|
|
75
|
-
return_binding: { recipient: string; return_channel_id: null };
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export interface VerifyDecisionReceiptInput {
|
|
79
|
-
receipt: string | null;
|
|
80
|
-
/** Trusted protocol identity, independently configured. */
|
|
81
|
-
issuer: string;
|
|
82
|
-
/** Trusted resource workspace, never read from an unverified receipt. */
|
|
83
|
-
audience: string;
|
|
84
|
-
expected: {
|
|
85
|
-
sessionId: string;
|
|
86
|
-
eventId: string;
|
|
87
|
-
revision: number;
|
|
88
|
-
actionId: string;
|
|
89
|
-
acceptedAt: string;
|
|
90
|
-
};
|
|
91
|
-
input: Record<string, unknown>;
|
|
92
|
-
/** The complete normalized `interaction` from the immutable Surface endpoint. */
|
|
93
|
-
interaction: InteractiveSurfaceRevision['interaction'];
|
|
94
|
-
keys?: ReceiptKeySet;
|
|
95
|
-
keyResolver?: ReceiptKeyResolver;
|
|
96
|
-
/**
|
|
97
|
-
* The issuer's signed renderer-identity list (`/.well-known/sess-renderer-identities.json`),
|
|
98
|
-
* needed only for a receipt whose renderer identity this release does not bundle. It is trusted
|
|
99
|
-
* only through its signature under the same trusted keys and issuer.
|
|
100
|
-
*/
|
|
101
|
-
rendererIdentities?: RendererIdentitiesDocument;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
export interface VerifyDecisionReceiptOptions {
|
|
105
|
-
/** An explicit verification instant, useful for deliberate archival verification/tests. */
|
|
106
|
-
now?: Date;
|
|
107
|
-
/** Issued-time tolerance only, 0–60 seconds. Expiry never receives a grace interval. */
|
|
108
|
-
issuedAtToleranceSeconds?: number;
|
|
109
|
-
signal?: AbortSignal;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
function identifier(value: unknown): value is string {
|
|
113
|
-
return (
|
|
114
|
-
typeof value === 'string' &&
|
|
115
|
-
value.length > 0 &&
|
|
116
|
-
value.length <= 200 &&
|
|
117
|
-
!/[\s\u0000-\u001f\u007f-\u009f]/.test(value)
|
|
118
|
-
);
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/** Validates and copies the whole set, not merely the key selected by the token. */
|
|
122
|
-
export function validateKeySet(raw: unknown): ReceiptKeySet {
|
|
123
|
-
const value = snapshotJson(raw);
|
|
124
|
-
if (
|
|
125
|
-
!isRecord(value) ||
|
|
126
|
-
!exactKeys(value, ['keys']) ||
|
|
127
|
-
!Array.isArray(value.keys) ||
|
|
128
|
-
value.keys.length === 0 ||
|
|
129
|
-
value.keys.length > MAX_KEYS ||
|
|
130
|
-
Buffer.byteLength(canonicalJson(value), 'utf8') > MAX_RECEIPT_KEY_SET_BYTES
|
|
131
|
-
) {
|
|
132
|
-
throw verificationError(
|
|
133
|
-
'INVALID_KEY_SET',
|
|
134
|
-
'The trusted receipt key set is malformed or exceeds its safety limit.',
|
|
135
|
-
);
|
|
136
|
-
}
|
|
137
|
-
const seen = new Set<string>();
|
|
138
|
-
for (const key of value.keys) {
|
|
139
|
-
if (
|
|
140
|
-
!isRecord(key) ||
|
|
141
|
-
Object.keys(key).some(
|
|
142
|
-
(name) => !['kty', 'crv', 'x', 'kid', 'alg', 'use', 'key_ops', 'ext'].includes(name),
|
|
143
|
-
) ||
|
|
144
|
-
key.kty !== 'OKP' ||
|
|
145
|
-
key.crv !== 'Ed25519' ||
|
|
146
|
-
typeof key.kid !== 'string' ||
|
|
147
|
-
!KID.test(key.kid) ||
|
|
148
|
-
typeof key.x !== 'string' ||
|
|
149
|
-
!/^[A-Za-z0-9_-]{43}$/.test(key.x) ||
|
|
150
|
-
Buffer.from(key.x, 'base64url').length !== 32 ||
|
|
151
|
-
Buffer.from(key.x, 'base64url').toString('base64url') !== key.x ||
|
|
152
|
-
(key.alg !== undefined && key.alg !== 'EdDSA') ||
|
|
153
|
-
(key.use !== undefined && key.use !== 'sig') ||
|
|
154
|
-
(key.key_ops !== undefined &&
|
|
155
|
-
(!Array.isArray(key.key_ops) || key.key_ops.length !== 1 || key.key_ops[0] !== 'verify')) ||
|
|
156
|
-
(key.ext !== undefined && typeof key.ext !== 'boolean')
|
|
157
|
-
) {
|
|
158
|
-
throw verificationError(
|
|
159
|
-
'INVALID_PUBLIC_KEY',
|
|
160
|
-
'Receipt keys must be unambiguous public Ed25519 verification keys.',
|
|
161
|
-
);
|
|
162
|
-
}
|
|
163
|
-
if (seen.has(key.kid))
|
|
164
|
-
throw verificationError('DUPLICATE_KEY_ID', 'The trusted key set contains duplicate key IDs.');
|
|
165
|
-
seen.add(key.kid);
|
|
166
|
-
}
|
|
167
|
-
return deepFreeze(value) as unknown as ReceiptKeySet;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
function decodeSegment(segment: string): string {
|
|
171
|
-
const bytes = Buffer.from(segment, 'base64url');
|
|
172
|
-
if (bytes.toString('base64url') !== segment)
|
|
173
|
-
throw verificationError('MALFORMED_RECEIPT', 'The receipt is not canonical base64url.');
|
|
174
|
-
try {
|
|
175
|
-
return new TextDecoder('utf-8', { fatal: true, ignoreBOM: true }).decode(bytes);
|
|
176
|
-
} catch {
|
|
177
|
-
throw verificationError('MALFORMED_RECEIPT', 'The receipt does not contain valid UTF-8 JSON.');
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
/** Header fields have only string values; reject duplicate names before JOSE parsing. */
|
|
182
|
-
function headerOf(encoded: string): { alg: string; kid: string; typ: string } {
|
|
183
|
-
let parsed: unknown;
|
|
184
|
-
const text = decodeSegment(encoded);
|
|
185
|
-
try {
|
|
186
|
-
parsed = JSON.parse(text);
|
|
187
|
-
} catch {
|
|
188
|
-
throw verificationError('MALFORMED_RECEIPT', 'The receipt protected header is not JSON.');
|
|
189
|
-
}
|
|
190
|
-
if (
|
|
191
|
-
!isRecord(parsed) ||
|
|
192
|
-
!exactKeys(parsed, ['alg', 'kid', 'typ']) ||
|
|
193
|
-
typeof parsed.alg !== 'string' ||
|
|
194
|
-
typeof parsed.typ !== 'string' ||
|
|
195
|
-
typeof parsed.kid !== 'string' ||
|
|
196
|
-
!KID.test(parsed.kid)
|
|
197
|
-
) {
|
|
198
|
-
throw verificationError(
|
|
199
|
-
'UNSUPPORTED_HEADER',
|
|
200
|
-
'The receipt protected header has missing or unsupported fields.',
|
|
201
|
-
);
|
|
202
|
-
}
|
|
203
|
-
// Tokenize JSON strings, punctuation and whitespace. The exact shape above allows only
|
|
204
|
-
// {string:string,...}; six string tokens means precisely three names and three values.
|
|
205
|
-
const strings = text.match(/"(?:[^"\\\u0000-\u001f]|\\(?:["\\/bfnrt]|u[0-9a-fA-F]{4}))*"/g);
|
|
206
|
-
if (strings?.length !== 6)
|
|
207
|
-
throw verificationError('UNSUPPORTED_HEADER', 'Duplicate receipt header fields are not supported.');
|
|
208
|
-
if (parsed.alg !== 'EdDSA')
|
|
209
|
-
throw verificationError('UNSUPPORTED_ALGORITHM', 'The receipt must use the supported EdDSA algorithm.');
|
|
210
|
-
if (parsed.typ !== 'sess-receipt+jwt')
|
|
211
|
-
throw verificationError('UNSUPPORTED_TYPE', 'The receipt has an unsupported protected type.');
|
|
212
|
-
return parsed as { alg: string; kid: string; typ: string };
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
function claimsOf(raw: unknown): ReceiptClaims {
|
|
216
|
-
if (!isRecord(raw) || raw.sess_receipt !== 1) {
|
|
217
|
-
throw verificationError('UNSUPPORTED_RECEIPT_VERSION', 'Only receipt format version 1 is supported.');
|
|
218
|
-
}
|
|
219
|
-
const top = [
|
|
220
|
-
'sess_receipt',
|
|
221
|
-
'iss',
|
|
222
|
-
'aud',
|
|
223
|
-
'jti',
|
|
224
|
-
'iat',
|
|
225
|
-
'exp',
|
|
226
|
-
'accepted_at',
|
|
227
|
-
'workspace_id',
|
|
228
|
-
'session_id',
|
|
229
|
-
'step_id',
|
|
230
|
-
'event_id',
|
|
231
|
-
'surface',
|
|
232
|
-
'action',
|
|
233
|
-
'input_hash',
|
|
234
|
-
'actor',
|
|
235
|
-
'return_binding',
|
|
236
|
-
];
|
|
237
|
-
const surface = raw.surface;
|
|
238
|
-
const action = raw.action;
|
|
239
|
-
const actor = raw.actor;
|
|
240
|
-
const binding = raw.return_binding;
|
|
241
|
-
if (
|
|
242
|
-
!exactKeys(raw, top) ||
|
|
243
|
-
![raw.iss, raw.aud, raw.jti, raw.workspace_id, raw.session_id, raw.step_id, raw.event_id].every(
|
|
244
|
-
identifier,
|
|
245
|
-
) ||
|
|
246
|
-
!Number.isSafeInteger(raw.iat) ||
|
|
247
|
-
!Number.isSafeInteger(raw.exp) ||
|
|
248
|
-
(raw.iat as number) < 0 ||
|
|
249
|
-
(raw.exp as number) <= (raw.iat as number) ||
|
|
250
|
-
checkedInstant(raw.accepted_at) === undefined ||
|
|
251
|
-
!isRecord(surface) ||
|
|
252
|
-
!exactKeys(surface, ['revision', 'hash', 'renderer_version']) ||
|
|
253
|
-
!Number.isSafeInteger(surface.revision) ||
|
|
254
|
-
(surface.revision as number) < 1 ||
|
|
255
|
-
typeof surface.hash !== 'string' ||
|
|
256
|
-
!DIGEST.test(surface.hash) ||
|
|
257
|
-
!isRecord(action) ||
|
|
258
|
-
!exactKeys(action, ['id', 'label', 'summary']) ||
|
|
259
|
-
!identifier(action.id) ||
|
|
260
|
-
typeof action.label !== 'string' ||
|
|
261
|
-
action.label.length === 0 ||
|
|
262
|
-
typeof action.summary !== 'string' ||
|
|
263
|
-
typeof raw.input_hash !== 'string' ||
|
|
264
|
-
!DIGEST.test(raw.input_hash) ||
|
|
265
|
-
!isRecord(actor) ||
|
|
266
|
-
!exactKeys(actor, ['assurance', 'subject']) ||
|
|
267
|
-
!isRecord(binding) ||
|
|
268
|
-
!exactKeys(binding, ['recipient', 'return_channel_id'])
|
|
269
|
-
) {
|
|
270
|
-
throw verificationError('INVALID_CLAIMS', 'The receipt claims do not match the supported format.');
|
|
271
|
-
}
|
|
272
|
-
// Membership (bundled or on a signed list) is checked once the signature holds.
|
|
273
|
-
if (typeof surface.renderer_version !== 'string' || !identifier(surface.renderer_version)) {
|
|
274
|
-
throw verificationError('UNSUPPORTED_RENDERER', 'The receipt names an unsupported renderer version.');
|
|
275
|
-
}
|
|
276
|
-
if (actor.assurance !== 'link_holder' || !identifier(actor.subject)) {
|
|
277
|
-
throw verificationError('UNSUPPORTED_ACTOR', 'The receipt actor assurance or subject is unsupported.');
|
|
278
|
-
}
|
|
279
|
-
if (!identifier(binding.recipient) || binding.return_channel_id !== null) {
|
|
280
|
-
throw verificationError('RETURN_BINDING_MISMATCH', 'The receipt return binding is unsupported.');
|
|
281
|
-
}
|
|
282
|
-
return raw as unknown as ReceiptClaims;
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
/**
|
|
286
|
-
* Fully verifies a version-1 receipt and its exact accepted record. Explicit keys make this
|
|
287
|
-
* function entirely offline. A valid receipt attests the accepted record, not business authority.
|
|
288
|
-
*/
|
|
289
|
-
export async function verifyDecisionReceipt(
|
|
290
|
-
input: VerifyDecisionReceiptInput,
|
|
291
|
-
options: VerifyDecisionReceiptOptions = {},
|
|
292
|
-
): Promise<DeepReadonly<ReceiptClaims>> {
|
|
293
|
-
// Capture all caller-owned data and options before a resolver can yield control.
|
|
294
|
-
if (input?.receipt === null)
|
|
295
|
-
throw verificationError(
|
|
296
|
-
'LEGACY_RECEIPT_UNAVAILABLE',
|
|
297
|
-
'This accepted response has no verifiable receipt.',
|
|
298
|
-
);
|
|
299
|
-
if (
|
|
300
|
-
typeof input?.receipt !== 'string' ||
|
|
301
|
-
input.receipt.length > MAX_RECEIPT_BYTES ||
|
|
302
|
-
!/^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+$/.test(input.receipt)
|
|
303
|
-
) {
|
|
304
|
-
throw verificationError(
|
|
305
|
-
'MALFORMED_RECEIPT',
|
|
306
|
-
'The receipt is malformed or exceeds the 64 KiB safety limit.',
|
|
307
|
-
);
|
|
308
|
-
}
|
|
309
|
-
const receipt = input.receipt;
|
|
310
|
-
const issuer = input.issuer;
|
|
311
|
-
const audience = input.audience;
|
|
312
|
-
if (!identifier(issuer) || !identifier(audience)) {
|
|
313
|
-
throw verificationError(
|
|
314
|
-
'VERIFICATION_CONTEXT_MISSING',
|
|
315
|
-
'A trusted issuer and expected resource workspace are required.',
|
|
316
|
-
);
|
|
317
|
-
}
|
|
318
|
-
const expected = snapshotJson(input.expected);
|
|
319
|
-
const acceptedInput = snapshotJson(input.input);
|
|
320
|
-
const interaction = snapshotJson(input.interaction);
|
|
321
|
-
const resolver = input.keyResolver;
|
|
322
|
-
const keys = input.keys === undefined ? undefined : validateKeySet(input.keys);
|
|
323
|
-
const rendererList =
|
|
324
|
-
input.rendererIdentities === undefined ? undefined : snapshotJson(input.rendererIdentities);
|
|
325
|
-
if (
|
|
326
|
-
(keys === undefined) === (resolver === undefined) ||
|
|
327
|
-
(resolver !== undefined && typeof resolver !== 'function')
|
|
328
|
-
) {
|
|
329
|
-
throw verificationError(
|
|
330
|
-
'VERIFICATION_CONTEXT_MISSING',
|
|
331
|
-
'Provide exactly one trusted public key set or key resolver.',
|
|
332
|
-
);
|
|
333
|
-
}
|
|
334
|
-
const signal = options.signal;
|
|
335
|
-
const now =
|
|
336
|
-
options.now === undefined ? Date.now() : options.now instanceof Date ? options.now.getTime() : NaN;
|
|
337
|
-
const tolerance = options.issuedAtToleranceSeconds ?? 60;
|
|
338
|
-
if (!Number.isFinite(now) || !Number.isFinite(tolerance) || tolerance < 0 || tolerance > 60) {
|
|
339
|
-
throw verificationError(
|
|
340
|
-
'INVALID_VERIFICATION_OPTIONS',
|
|
341
|
-
'Use a valid verification time and issued-time tolerance between 0 and 60 seconds.',
|
|
342
|
-
);
|
|
343
|
-
}
|
|
344
|
-
if (signal?.aborted) throw abortError(signal);
|
|
345
|
-
if (
|
|
346
|
-
!isRecord(expected) ||
|
|
347
|
-
!SESSION_ID.test(expected.sessionId) ||
|
|
348
|
-
!EVENT_ID.test(expected.eventId) ||
|
|
349
|
-
!Number.isSafeInteger(expected.revision) ||
|
|
350
|
-
expected.revision < 1 ||
|
|
351
|
-
!ACTION_ID.test(expected.actionId) ||
|
|
352
|
-
checkedInstant(expected.acceptedAt) === undefined ||
|
|
353
|
-
!isRecord(acceptedInput) ||
|
|
354
|
-
!isRecord(interaction) ||
|
|
355
|
-
interaction.mode !== 'interactive' ||
|
|
356
|
-
interaction.response_policy !== 'first' ||
|
|
357
|
-
!Array.isArray(interaction.actions) ||
|
|
358
|
-
!isRecord(interaction.source) ||
|
|
359
|
-
interaction.source.kind !== 'sess_surface' ||
|
|
360
|
-
!Array.isArray(interaction.source.components)
|
|
361
|
-
) {
|
|
362
|
-
throw verificationError(
|
|
363
|
-
'INVALID_VERIFICATION_INPUT',
|
|
364
|
-
'Verification requires an accepted record and its complete normalized immutable interaction.',
|
|
365
|
-
);
|
|
366
|
-
}
|
|
367
|
-
const parts = receipt.split('.') as [string, string, string];
|
|
368
|
-
const header = headerOf(parts[0]);
|
|
369
|
-
let raw: unknown;
|
|
370
|
-
const payloadText = decodeSegment(parts[1]);
|
|
371
|
-
try {
|
|
372
|
-
raw = JSON.parse(payloadText);
|
|
373
|
-
} catch {
|
|
374
|
-
throw verificationError('MALFORMED_RECEIPT', 'The receipt payload is not JSON.');
|
|
375
|
-
}
|
|
376
|
-
const claims = claimsOf(snapshotJson(raw));
|
|
377
|
-
if (canonicalJson(claims) !== payloadText) {
|
|
378
|
-
throw verificationError(
|
|
379
|
-
'NON_CANONICAL_PAYLOAD',
|
|
380
|
-
'The signed receipt payload must be RFC 8785 canonical JSON.',
|
|
381
|
-
);
|
|
382
|
-
}
|
|
383
|
-
if (claims.iss !== issuer)
|
|
384
|
-
throw verificationError('ISSUER_MISMATCH', 'The receipt issuer does not match the trusted issuer.');
|
|
385
|
-
if (
|
|
386
|
-
claims.aud !== audience ||
|
|
387
|
-
claims.workspace_id !== audience ||
|
|
388
|
-
claims.return_binding.recipient !== audience
|
|
389
|
-
) {
|
|
390
|
-
throw verificationError(
|
|
391
|
-
'AUDIENCE_MISMATCH',
|
|
392
|
-
'The receipt does not belong to the expected resource workspace.',
|
|
393
|
-
);
|
|
394
|
-
}
|
|
395
|
-
if (
|
|
396
|
-
claims.session_id !== expected.sessionId ||
|
|
397
|
-
claims.event_id !== expected.eventId ||
|
|
398
|
-
claims.jti !== expected.eventId ||
|
|
399
|
-
claims.step_id !== `rev_${expected.revision}` ||
|
|
400
|
-
claims.surface.revision !== expected.revision
|
|
401
|
-
) {
|
|
402
|
-
throw verificationError(
|
|
403
|
-
'RECORD_BINDING_MISMATCH',
|
|
404
|
-
'The receipt does not bind the expected Session, event and revision.',
|
|
405
|
-
);
|
|
406
|
-
}
|
|
407
|
-
if (claims.actor.subject !== `${audience}:human_link:${expected.sessionId}`) {
|
|
408
|
-
throw verificationError(
|
|
409
|
-
'ACTOR_BINDING_MISMATCH',
|
|
410
|
-
'The receipt actor is not bound to this resource workspace and Session.',
|
|
411
|
-
);
|
|
412
|
-
}
|
|
413
|
-
const acceptedAt = checkedInstant(claims.accepted_at) as number;
|
|
414
|
-
if (claims.accepted_at !== expected.acceptedAt || Math.floor(acceptedAt / 1000) !== claims.iat) {
|
|
415
|
-
throw verificationError(
|
|
416
|
-
'ACCEPTED_TIME_MISMATCH',
|
|
417
|
-
'The receipt acceptance time does not match the accepted record.',
|
|
418
|
-
);
|
|
419
|
-
}
|
|
420
|
-
if (claims.exp * 1000 <= now)
|
|
421
|
-
throw verificationError('RECEIPT_EXPIRED', 'The receipt has expired at the verification time.');
|
|
422
|
-
if (acceptedAt > now + tolerance * 1000 || claims.iat * 1000 > now + tolerance * 1000) {
|
|
423
|
-
throw verificationError(
|
|
424
|
-
'RECEIPT_FROM_FUTURE',
|
|
425
|
-
'The receipt was issued after the allowed clock tolerance.',
|
|
426
|
-
);
|
|
427
|
-
}
|
|
428
|
-
const actions = interaction.actions.filter((action) => isRecord(action) && action.id === expected.actionId);
|
|
429
|
-
const action = actions[0];
|
|
430
|
-
if (
|
|
431
|
-
actions.length !== 1 ||
|
|
432
|
-
!action ||
|
|
433
|
-
claims.action.id !== expected.actionId ||
|
|
434
|
-
claims.action.label !== action.label ||
|
|
435
|
-
claims.action.summary !== (action.intent ?? '')
|
|
436
|
-
) {
|
|
437
|
-
throw verificationError(
|
|
438
|
-
'ACTION_BINDING_MISMATCH',
|
|
439
|
-
'The receipt action does not match its immutable Surface action.',
|
|
440
|
-
);
|
|
441
|
-
}
|
|
442
|
-
if (claims.surface.hash !== jsonDigest(interaction)) {
|
|
443
|
-
throw verificationError(
|
|
444
|
-
'SURFACE_HASH_MISMATCH',
|
|
445
|
-
'The receipt does not bind the complete immutable interaction.',
|
|
446
|
-
);
|
|
447
|
-
}
|
|
448
|
-
if (claims.input_hash !== jsonDigest(acceptedInput)) {
|
|
449
|
-
throw verificationError('INPUT_HASH_MISMATCH', 'The receipt does not bind the accepted typed input.');
|
|
450
|
-
}
|
|
451
|
-
const keyFor = trustedKeyLookup(keys, resolver, issuer, signal);
|
|
452
|
-
const publicKey = await keyFor(header.kid);
|
|
453
|
-
if (!publicKey)
|
|
454
|
-
throw verificationError('UNKNOWN_KEY_ID', 'The receipt signing key is not in the trusted key set.');
|
|
455
|
-
try {
|
|
456
|
-
const key = await waitWithSignal(
|
|
457
|
-
importJWK({ ...publicKey, key_ops: ['verify'], ext: false }, 'EdDSA'),
|
|
458
|
-
signal,
|
|
459
|
-
);
|
|
460
|
-
await waitWithSignal(compactVerify(receipt, key, { algorithms: ['EdDSA'] }), signal);
|
|
461
|
-
} catch {
|
|
462
|
-
if (signal?.aborted) throw abortError(signal);
|
|
463
|
-
throw verificationError(
|
|
464
|
-
'INVALID_SIGNATURE',
|
|
465
|
-
'The receipt signature did not verify with the trusted Ed25519 key.',
|
|
466
|
-
);
|
|
467
|
-
}
|
|
468
|
-
// Decision 0009 Amendment 13: the renderer identity is one this release bundles, or one on the
|
|
469
|
-
// issuer's list that verifies under the same trusted keys.
|
|
470
|
-
const renderer = claims.surface.renderer_version;
|
|
471
|
-
if (!BUNDLED_RENDERER_IDENTITIES.includes(renderer)) {
|
|
472
|
-
// A list proves the identities it holds; an identity it does not hold is unsupported, never
|
|
473
|
-
// accepted by fallback (an older list says nothing about what was added since).
|
|
474
|
-
const listed =
|
|
475
|
-
rendererList === undefined
|
|
476
|
-
? []
|
|
477
|
-
: (await verifyRendererList(rendererList, { issuer, keyFor, ...(signal ? { signal } : {}) }))
|
|
478
|
-
.identities;
|
|
479
|
-
if (!listed.some((identity) => identity.renderer_version === renderer)) {
|
|
480
|
-
throw verificationError(
|
|
481
|
-
'UNSUPPORTED_RENDERER',
|
|
482
|
-
'The receipt names a renderer identity that is not on the issuer’s signed list.',
|
|
483
|
-
);
|
|
484
|
-
}
|
|
485
|
-
}
|
|
486
|
-
return deepFreeze(claims);
|
|
487
|
-
}
|
|
488
|
-
|
|
489
|
-
/** Resolves a kid to a trusted key: from the explicit set, or the trusted resolver (validated). */
|
|
490
|
-
function trustedKeyLookup(
|
|
491
|
-
keys: ReceiptKeySet | undefined,
|
|
492
|
-
resolver: ReceiptKeyResolver | undefined,
|
|
493
|
-
issuer: string,
|
|
494
|
-
signal: AbortSignal | undefined,
|
|
495
|
-
): (kid: string) => Promise<ReceiptPublicKey | undefined> {
|
|
496
|
-
return async (kid) => {
|
|
497
|
-
let trusted = keys;
|
|
498
|
-
if (!trusted && resolver) {
|
|
499
|
-
let resolved: ReceiptKeySet;
|
|
500
|
-
try {
|
|
501
|
-
resolved = await waitWithSignal(
|
|
502
|
-
Promise.resolve(resolver(Object.freeze({ kid, issuer, ...(signal ? { signal } : {}) }))),
|
|
503
|
-
signal,
|
|
504
|
-
);
|
|
505
|
-
} catch {
|
|
506
|
-
if (signal?.aborted) throw abortError(signal);
|
|
507
|
-
// Callback failures may contain arbitrary application or upstream material,
|
|
508
|
-
// including caller-created SessError instances. Do not forward their fields.
|
|
509
|
-
throw verificationError(
|
|
510
|
-
'KEY_RESOLUTION_FAILED',
|
|
511
|
-
'The trusted receipt key resolver could not supply verification keys.',
|
|
512
|
-
);
|
|
513
|
-
}
|
|
514
|
-
trusted = validateKeySet(resolved);
|
|
515
|
-
}
|
|
516
|
-
return trusted?.keys.find((key) => key.kid === kid);
|
|
517
|
-
};
|
|
518
|
-
}
|
|
519
|
-
|
|
520
|
-
/**
|
|
521
|
-
* Verifies an issuer's signed renderer-identity list (Decision 0009 Amendment 13) under the keys
|
|
522
|
-
* you trust for that issuer: exactly one of `keys` or `keyResolver`. Offline with explicit keys.
|
|
523
|
-
* Returns the verified edition: `seq` (order lists by it; see newerRendererList), `iat` and the
|
|
524
|
-
* listed identities, in order.
|
|
525
|
-
*/
|
|
526
|
-
export async function verifyRendererIdentities(
|
|
527
|
-
document: RendererIdentitiesDocument,
|
|
528
|
-
context: {
|
|
529
|
-
issuer: string;
|
|
530
|
-
keys?: ReceiptKeySet;
|
|
531
|
-
keyResolver?: ReceiptKeyResolver;
|
|
532
|
-
signal?: AbortSignal;
|
|
533
|
-
},
|
|
534
|
-
): Promise<RendererList> {
|
|
535
|
-
const issuer = context?.issuer;
|
|
536
|
-
const resolver = context?.keyResolver;
|
|
537
|
-
const keys = context?.keys === undefined ? undefined : validateKeySet(context.keys);
|
|
538
|
-
if (
|
|
539
|
-
!identifier(issuer) ||
|
|
540
|
-
(keys === undefined) === (resolver === undefined) ||
|
|
541
|
-
(resolver !== undefined && typeof resolver !== 'function')
|
|
542
|
-
) {
|
|
543
|
-
throw verificationError(
|
|
544
|
-
'VERIFICATION_CONTEXT_MISSING',
|
|
545
|
-
'A trusted issuer and exactly one trusted public key set or key resolver are required.',
|
|
546
|
-
);
|
|
547
|
-
}
|
|
548
|
-
const signal = context.signal;
|
|
549
|
-
return await verifyRendererList(document, {
|
|
550
|
-
issuer,
|
|
551
|
-
keyFor: trustedKeyLookup(keys, resolver, issuer, signal),
|
|
552
|
-
...(signal ? { signal } : {}),
|
|
553
|
-
});
|
|
554
|
-
}
|