@sess.si/sdk 0.2.0 → 0.4.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 +59 -0
- package/README.md +9 -7
- package/dist/cli/verify.d.ts +6 -2
- package/dist/cli/verify.js +16 -8
- package/dist/errors.d.ts +7 -1
- package/dist/internal/decode.js +15 -1
- package/dist/internal/version.d.ts +1 -1
- package/dist/internal/version.js +1 -1
- package/dist/receipts/bundle.d.ts +35 -9
- package/dist/receipts/bundle.js +80 -28
- package/dist/receipts/envelope.d.ts +75 -0
- package/dist/receipts/envelope.js +188 -0
- package/dist/receipts/index.d.ts +3 -2
- package/dist/receipts/json.d.ts +13 -0
- package/dist/receipts/json.js +21 -0
- package/dist/receipts/resource.js +3 -0
- package/dist/receipts/shared-title.d.ts +7 -0
- package/dist/receipts/shared-title.js +127 -0
- package/dist/receipts/verify.d.ts +38 -3
- package/dist/receipts/verify.js +78 -10
- package/dist/types.d.ts +26 -4
- package/dist/webhooks/index.d.ts +26 -2
- package/dist/webhooks/index.js +25 -0
- package/package.json +1 -1
package/dist/receipts/verify.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { compactVerify, importJWK } from 'jose';
|
|
2
|
-
import { abortError, canonicalJson, checkedInstant, deepFreeze, exactKeys, isRecord, jsonDigest, snapshotJson, verificationError, waitWithSignal, } from './json.js';
|
|
2
|
+
import { abortError, canonicalJson, checkedInstant, deepFreeze, exactKeys, isNewerReceiptTyp, isRecord, jsonDigest, newerReceiptError, RECEIPT_V2_ACTION_LABEL_MAX, snapshotJson, verificationError, waitWithSignal, } from './json.js';
|
|
3
3
|
import { BUNDLED_RENDERER_IDENTITIES, verifyRendererList, } from './renderers.js';
|
|
4
|
+
import { parseRevisionEnvelope, revisionEnvelopeIsNewer, revisionHash, } from './envelope.js';
|
|
4
5
|
export const MAX_RECEIPT_BYTES = 64 * 1024;
|
|
5
6
|
export const MAX_RECEIPT_KEY_SET_BYTES = 256 * 1024;
|
|
6
7
|
const MAX_KEYS = 64;
|
|
@@ -9,6 +10,10 @@ const SESSION_ID = /^ses_[0-9A-HJKMNP-TV-Z]{26}$/;
|
|
|
9
10
|
const EVENT_ID = /^evt_[0-9A-HJKMNP-TV-Z]{26}$/;
|
|
10
11
|
const ACTION_ID = /^[a-z][a-z0-9_]{0,31}$/;
|
|
11
12
|
const DIGEST = /^sha256:[0-9a-f]{64}$/;
|
|
13
|
+
/** Protected `typ` of a receipt v1 (receipt-v1); it names version 1 only. */
|
|
14
|
+
const RECEIPT_TYP_V1 = 'sess-receipt+jwt';
|
|
15
|
+
/** Protected `typ` of a receipt v2 (receipt-v2); it names version 2 only. */
|
|
16
|
+
const RECEIPT_TYP_V2 = 'sess-receipt-2+jwt';
|
|
12
17
|
function identifier(value) {
|
|
13
18
|
return (typeof value === 'string' &&
|
|
14
19
|
value.length > 0 &&
|
|
@@ -87,13 +92,15 @@ function headerOf(encoded) {
|
|
|
87
92
|
throw verificationError('UNSUPPORTED_HEADER', 'Duplicate receipt header fields are not supported.');
|
|
88
93
|
if (parsed.alg !== 'EdDSA')
|
|
89
94
|
throw verificationError('UNSUPPORTED_ALGORITHM', 'The receipt must use the supported EdDSA algorithm.');
|
|
90
|
-
|
|
95
|
+
// The typ names the version: sess-receipt+jwt is receipt v1 only, sess-receipt-2+jwt v2 only.
|
|
96
|
+
const version = parsed.typ === RECEIPT_TYP_V1 ? 1 : parsed.typ === RECEIPT_TYP_V2 ? 2 : null;
|
|
97
|
+
if (version === null)
|
|
91
98
|
throw verificationError('UNSUPPORTED_TYPE', 'The receipt has an unsupported protected type.');
|
|
92
|
-
return parsed;
|
|
99
|
+
return { ...parsed, version };
|
|
93
100
|
}
|
|
94
|
-
function claimsOf(raw) {
|
|
95
|
-
if (!isRecord(raw) || raw.sess_receipt !==
|
|
96
|
-
throw verificationError('UNSUPPORTED_RECEIPT_VERSION', '
|
|
101
|
+
function claimsOf(raw, version) {
|
|
102
|
+
if (!isRecord(raw) || raw.sess_receipt !== version) {
|
|
103
|
+
throw verificationError('UNSUPPORTED_RECEIPT_VERSION', 'The receipt format version does not match its protected type (versions 1 and 2 are supported).');
|
|
97
104
|
}
|
|
98
105
|
const top = [
|
|
99
106
|
'sess_receipt',
|
|
@@ -125,7 +132,10 @@ function claimsOf(raw) {
|
|
|
125
132
|
raw.exp <= raw.iat ||
|
|
126
133
|
checkedInstant(raw.accepted_at) === undefined ||
|
|
127
134
|
!isRecord(surface) ||
|
|
128
|
-
!exactKeys(surface,
|
|
135
|
+
!exactKeys(surface, version === 2
|
|
136
|
+
? ['revision', 'hash', 'renderer_version', 'revision_hash']
|
|
137
|
+
: ['revision', 'hash', 'renderer_version']) ||
|
|
138
|
+
(version === 2 && (typeof surface.revision_hash !== 'string' || !DIGEST.test(surface.revision_hash))) ||
|
|
129
139
|
!Number.isSafeInteger(surface.revision) ||
|
|
130
140
|
surface.revision < 1 ||
|
|
131
141
|
typeof surface.hash !== 'string' ||
|
|
@@ -135,6 +145,7 @@ function claimsOf(raw) {
|
|
|
135
145
|
!identifier(action.id) ||
|
|
136
146
|
typeof action.label !== 'string' ||
|
|
137
147
|
action.label.length === 0 ||
|
|
148
|
+
(version === 2 && [...action.label].length > RECEIPT_V2_ACTION_LABEL_MAX) ||
|
|
138
149
|
typeof action.summary !== 'string' ||
|
|
139
150
|
typeof raw.input_hash !== 'string' ||
|
|
140
151
|
!DIGEST.test(raw.input_hash) ||
|
|
@@ -157,8 +168,20 @@ function claimsOf(raw) {
|
|
|
157
168
|
return raw;
|
|
158
169
|
}
|
|
159
170
|
/**
|
|
160
|
-
* Fully verifies a
|
|
161
|
-
*
|
|
171
|
+
* Fully verifies a receipt and its exact accepted record. Explicit keys make this function
|
|
172
|
+
* entirely offline. A valid receipt attests the accepted record, not business authority.
|
|
173
|
+
*
|
|
174
|
+
* The protected `typ` decides the version: `sess-receipt+jwt` is receipt v1, verified exactly as
|
|
175
|
+
* before (`revisionEnvelope` is not read); `sess-receipt-2+jwt` is receipt v2, verified only with
|
|
176
|
+
* the revision envelope it commits to. A receipt v2 whose envelope is missing or null, malformed,
|
|
177
|
+
* of another digest than `surface.revision_hash`, or naming another Session, revision, Surface
|
|
178
|
+
* hash or renderer identity is refused with `REVISION_ENVELOPE_MISMATCH`, after the issuer,
|
|
179
|
+
* audience, record, time and signature checks. The envelope is `sess_revision: 2`, in its full
|
|
180
|
+
* (`titles`) or compact (`titles_digest`) form; a receipt v2 action label is at most 24 code points.
|
|
181
|
+
*
|
|
182
|
+
* A receipt newer than this release, a later Sess receipt `typ` (checked before anything else in
|
|
183
|
+
* the receipt) or a receipt v2 whose envelope has a later `sess_revision`, is refused with kind
|
|
184
|
+
* `receipt_newer` and code `RECEIPT_NEWER`, never as a verification failure; nothing is returned.
|
|
162
185
|
*/
|
|
163
186
|
export async function verifyDecisionReceipt(input, options = {}) {
|
|
164
187
|
// Capture all caller-owned data and options before a resolver can yield control.
|
|
@@ -178,6 +201,14 @@ export async function verifyDecisionReceipt(input, options = {}) {
|
|
|
178
201
|
const expected = snapshotJson(input.expected);
|
|
179
202
|
const acceptedInput = snapshotJson(input.input);
|
|
180
203
|
const interaction = snapshotJson(input.interaction);
|
|
204
|
+
// Captured now (before a resolver can yield), judged only for a receipt v2 once its signature holds.
|
|
205
|
+
let suppliedEnvelope;
|
|
206
|
+
try {
|
|
207
|
+
suppliedEnvelope = input.revisionEnvelope == null ? null : snapshotJson(input.revisionEnvelope);
|
|
208
|
+
}
|
|
209
|
+
catch {
|
|
210
|
+
suppliedEnvelope = MALFORMED_ENVELOPE;
|
|
211
|
+
}
|
|
181
212
|
const resolver = input.keyResolver;
|
|
182
213
|
const keys = input.keys === undefined ? undefined : validateKeySet(input.keys);
|
|
183
214
|
const rendererList = input.rendererIdentities === undefined ? undefined : snapshotJson(input.rendererIdentities);
|
|
@@ -211,6 +242,10 @@ export async function verifyDecisionReceipt(input, options = {}) {
|
|
|
211
242
|
throw verificationError('INVALID_VERIFICATION_INPUT', 'Verification requires an accepted record and its complete normalized immutable interaction.');
|
|
212
243
|
}
|
|
213
244
|
const parts = receipt.split('.');
|
|
245
|
+
// A Sess format this release does not know is newer, before any other check: its key, its
|
|
246
|
+
// algorithm and its claims may all be ones this release has never seen. Nothing is returned.
|
|
247
|
+
if (declaresNewerTyp(parts[0]))
|
|
248
|
+
throw newerReceiptError();
|
|
214
249
|
const header = headerOf(parts[0]);
|
|
215
250
|
let raw;
|
|
216
251
|
const payloadText = decodeSegment(parts[1]);
|
|
@@ -220,7 +255,7 @@ export async function verifyDecisionReceipt(input, options = {}) {
|
|
|
220
255
|
catch {
|
|
221
256
|
throw verificationError('MALFORMED_RECEIPT', 'The receipt payload is not JSON.');
|
|
222
257
|
}
|
|
223
|
-
const claims = claimsOf(snapshotJson(raw));
|
|
258
|
+
const claims = claimsOf(snapshotJson(raw), header.version);
|
|
224
259
|
if (canonicalJson(claims) !== payloadText) {
|
|
225
260
|
throw verificationError('NON_CANONICAL_PAYLOAD', 'The signed receipt payload must be RFC 8785 canonical JSON.');
|
|
226
261
|
}
|
|
@@ -278,6 +313,8 @@ export async function verifyDecisionReceipt(input, options = {}) {
|
|
|
278
313
|
throw abortError(signal);
|
|
279
314
|
throw verificationError('INVALID_SIGNATURE', 'The receipt signature did not verify with the trusted Ed25519 key.');
|
|
280
315
|
}
|
|
316
|
+
if (claims.sess_receipt === 2)
|
|
317
|
+
checkRevisionEnvelope(claims, suppliedEnvelope);
|
|
281
318
|
// Decision 0009 Amendment 13: the renderer identity is one this release bundles, or one on the
|
|
282
319
|
// issuer's list that verifies under the same trusted keys.
|
|
283
320
|
const renderer = claims.surface.renderer_version;
|
|
@@ -294,6 +331,37 @@ export async function verifyDecisionReceipt(input, options = {}) {
|
|
|
294
331
|
}
|
|
295
332
|
return deepFreeze(claims);
|
|
296
333
|
}
|
|
334
|
+
const MALFORMED_ENVELOPE = Symbol('malformed revision envelope');
|
|
335
|
+
/** Whether the (unverified) protected header names a later Sess format; never throws. */
|
|
336
|
+
function declaresNewerTyp(encoded) {
|
|
337
|
+
try {
|
|
338
|
+
const parsed = JSON.parse(decodeSegment(encoded));
|
|
339
|
+
return isRecord(parsed) && isNewerReceiptTyp(parsed.typ);
|
|
340
|
+
}
|
|
341
|
+
catch {
|
|
342
|
+
return false;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* Receipt v2: the supplied envelope must be the one the receipt commits to (receipt-v2 §7). An
|
|
347
|
+
* envelope of a later `sess_revision` is newer: never accepted, and nothing is returned.
|
|
348
|
+
*/
|
|
349
|
+
function checkRevisionEnvelope(claims, supplied) {
|
|
350
|
+
if (supplied === null)
|
|
351
|
+
throw verificationError('REVISION_ENVELOPE_MISMATCH', 'A version 2 receipt is verified only with its revision envelope, and none was supplied.');
|
|
352
|
+
if (revisionEnvelopeIsNewer(supplied))
|
|
353
|
+
throw newerReceiptError();
|
|
354
|
+
const envelope = parseRevisionEnvelope(supplied);
|
|
355
|
+
if (envelope === null)
|
|
356
|
+
throw verificationError('REVISION_ENVELOPE_MISMATCH', 'The revision envelope is malformed.');
|
|
357
|
+
if (revisionHash(envelope) !== claims.surface.revision_hash)
|
|
358
|
+
throw verificationError('REVISION_ENVELOPE_MISMATCH', 'The revision envelope is not the one the receipt commits to.');
|
|
359
|
+
if (envelope.session_id !== claims.session_id ||
|
|
360
|
+
envelope.revision !== claims.surface.revision ||
|
|
361
|
+
envelope.surface_hash !== claims.surface.hash ||
|
|
362
|
+
envelope.renderer_version !== claims.surface.renderer_version)
|
|
363
|
+
throw verificationError('REVISION_ENVELOPE_MISMATCH', 'The revision envelope names another Session, revision, Surface or renderer.');
|
|
364
|
+
}
|
|
297
365
|
/** Resolves a kid to a trusted key: from the explicit set, or the trusted resolver (validated). */
|
|
298
366
|
function trustedKeyLookup(keys, resolver, issuer, signal) {
|
|
299
367
|
return async (kid) => {
|
package/dist/types.d.ts
CHANGED
|
@@ -1,19 +1,41 @@
|
|
|
1
1
|
import type { components } from './generated/openapi.js';
|
|
2
|
+
import type { RevisionEnvelope } from './receipts/envelope.js';
|
|
2
3
|
/** Unmodified types generated from the reviewed public HTTP contract. */
|
|
3
4
|
export type { components, paths, operations } from './generated/openapi.js';
|
|
5
|
+
export type { EnvelopeAgentTarget, EnvelopeTitles, RevisionEnvelope, RevisionEnvelopeCompact, RevisionEnvelopeFull, } from './receipts/envelope.js';
|
|
4
6
|
type Schemas = components['schemas'];
|
|
5
7
|
export type RawCreateSessionRequest = Schemas['CreateSessionRequest'];
|
|
6
8
|
export type RawPublishSessionRequest = Schemas['PatchSessionRequest'];
|
|
7
9
|
export type CreateSessionResponse = Schemas['CreateSessionResponse'];
|
|
8
10
|
export type DisplaySession = Schemas['SessionResponse'];
|
|
9
|
-
|
|
11
|
+
/**
|
|
12
|
+
* Receipt v2 (receipt-v2 §2): `revision_envelope` is written by hand beside the generated
|
|
13
|
+
* contract types. It is present beside a receipt v2 and absent beside a receipt v1.
|
|
14
|
+
*/
|
|
15
|
+
interface RevisionEnvelopeMember {
|
|
16
|
+
/**
|
|
17
|
+
* The revision envelope the receipt v2 commits to (`surface.revision_hash`): when the revision
|
|
18
|
+
* was published, the responsible API key's ID, the registered address or Ask the Session came
|
|
19
|
+
* from, and the titles its owner chose (`sess_revision: 2`). `receipts.verifyResponse` verifies
|
|
20
|
+
* the receipt with it. Absent beside a receipt v1. Read it only from a verified result.
|
|
21
|
+
*/
|
|
22
|
+
revision_envelope?: RevisionEnvelope;
|
|
23
|
+
}
|
|
24
|
+
type GeneratedInteractiveSession = Schemas['InteractiveSessionResponse'];
|
|
25
|
+
type GeneratedSessionEvents = Schemas['SessionEventsResponse'];
|
|
26
|
+
export type AcceptedResponse = GeneratedInteractiveSession['responses'][number] & RevisionEnvelopeMember;
|
|
27
|
+
export type InteractiveSession = Omit<GeneratedInteractiveSession, 'responses'> & {
|
|
28
|
+
responses: AcceptedResponse[];
|
|
29
|
+
};
|
|
10
30
|
export type Session = DisplaySession | InteractiveSession;
|
|
11
31
|
export type DisplaySurfaceRevision = Schemas['SessionResponse'];
|
|
12
32
|
export type InteractiveSurfaceRevision = Schemas['InteractiveSurfaceResponse'];
|
|
13
33
|
export type SurfaceRevision = DisplaySurfaceRevision | InteractiveSurfaceRevision;
|
|
14
|
-
|
|
15
|
-
export type
|
|
16
|
-
export type
|
|
34
|
+
/** One Event of `GET /v1/sessions/{id}/events`; `revision_envelope` on `response.accepted` only. */
|
|
35
|
+
export type SessionEvent = GeneratedSessionEvents['events'][number] & RevisionEnvelopeMember;
|
|
36
|
+
export type SessionEvents = Omit<GeneratedSessionEvents, 'events'> & {
|
|
37
|
+
events: SessionEvent[];
|
|
38
|
+
};
|
|
17
39
|
/**
|
|
18
40
|
* The Sess link's state at the time of a Session read (Decision 0009 Amendment 10): `live` takes
|
|
19
41
|
* answers; `lapsed` is past its deadline but the person can still ask for more time, so the
|
package/dist/webhooks/index.d.ts
CHANGED
|
@@ -1,8 +1,32 @@
|
|
|
1
1
|
import type { components } from '../generated/openapi.js';
|
|
2
2
|
import { type DeepReadonly } from '../receipts/json.js';
|
|
3
|
-
|
|
3
|
+
import type { RevisionEnvelope } from '../receipts/envelope.js';
|
|
4
|
+
/**
|
|
5
|
+
* A `response.accepted` delivery. A receipt v2's message also carries `revision_envelope` beside
|
|
6
|
+
* `receipt` (receipt-v2 §10); a receipt v1's message has no such member.
|
|
7
|
+
*/
|
|
8
|
+
export type WebhookEvent = components['schemas']['WebhookDelivery'] & {
|
|
9
|
+
/** Only for a respondent of a Sess Ask: the Ask's ID (`ask_…`). */
|
|
10
|
+
ask_id?: string;
|
|
11
|
+
/** Beside a receipt v2 only: the revision envelope the receipt commits to. */
|
|
12
|
+
revision_envelope?: RevisionEnvelope;
|
|
13
|
+
};
|
|
14
|
+
/** An `ask.closed` delivery: a Sess Ask closed. Its `event_id` equals the `webhook-id` header. */
|
|
15
|
+
export interface AskClosed {
|
|
16
|
+
type: 'ask.closed';
|
|
17
|
+
/** Stable across retries and redeliveries: deduplicate on it. */
|
|
18
|
+
event_id: string;
|
|
19
|
+
/** The Ask's ID (`ask_…`). */
|
|
20
|
+
ask_id: string;
|
|
21
|
+
/** `max`: the last place was started; `owner`: you closed it. */
|
|
22
|
+
closed_by: 'max' | 'owner';
|
|
23
|
+
/** How many people had started when it closed. */
|
|
24
|
+
started: number;
|
|
25
|
+
/** When it closed (ISO 8601, milliseconds, UTC). */
|
|
26
|
+
closed_at: string;
|
|
27
|
+
}
|
|
4
28
|
export type WebhookChallenge = components['schemas']['WebhookVerification'];
|
|
5
|
-
export type VerifiedWebhook = DeepReadonly<WebhookEvent | WebhookChallenge>;
|
|
29
|
+
export type VerifiedWebhook = DeepReadonly<WebhookEvent | AskClosed | WebhookChallenge>;
|
|
6
30
|
export type WebhookHeaders = Headers | Readonly<Record<string, string | readonly string[] | undefined>>;
|
|
7
31
|
export interface VerifyWebhookOptions {
|
|
8
32
|
/** Workspace-specific secrets from registration/rotation; store outside source control. */
|
package/dist/webhooks/index.js
CHANGED
|
@@ -8,6 +8,7 @@ const MAX_SECRETS = 4;
|
|
|
8
8
|
const SECRET = /^whsec_[A-Za-z0-9+/]{43}=$/;
|
|
9
9
|
const EVENT_ID = /^evt_[0-9A-HJKMNP-TV-Z]{26}$/;
|
|
10
10
|
const SESSION_ID = /^ses_[0-9A-HJKMNP-TV-Z]{26}$/;
|
|
11
|
+
const ASK_ID = /^ask_[0-9A-HJKMNP-TV-Z]{26}$/;
|
|
11
12
|
function failure(code, message) {
|
|
12
13
|
return new SessError('protocol', message, { code, operation: 'webhooks.verify' });
|
|
13
14
|
}
|
|
@@ -148,6 +149,26 @@ export function verifyWebhook(rawBody, headers, options) {
|
|
|
148
149
|
}
|
|
149
150
|
return deepFreeze(value);
|
|
150
151
|
}
|
|
152
|
+
if (value.type === 'ask.closed') {
|
|
153
|
+
if (!exactKeys(value, ['type', 'event_id', 'ask_id', 'closed_by', 'started', 'closed_at']) ||
|
|
154
|
+
typeof value.event_id !== 'string' ||
|
|
155
|
+
!EVENT_ID.test(value.event_id) ||
|
|
156
|
+
value.event_id !== id ||
|
|
157
|
+
typeof value.ask_id !== 'string' ||
|
|
158
|
+
!ASK_ID.test(value.ask_id) ||
|
|
159
|
+
(value.closed_by !== 'max' && value.closed_by !== 'owner') ||
|
|
160
|
+
!Number.isSafeInteger(value.started) ||
|
|
161
|
+
value.started < 0 ||
|
|
162
|
+
checkedInstant(value.closed_at) === undefined) {
|
|
163
|
+
throw failure('WEBHOOK_INVALID_PAYLOAD', 'The ask.closed webhook event is malformed or does not match its transport identity.');
|
|
164
|
+
}
|
|
165
|
+
return deepFreeze(value);
|
|
166
|
+
}
|
|
167
|
+
// A respondent of an Ask carries its `ask_id`; every other Session's message has no such member.
|
|
168
|
+
const withAsk = Object.hasOwn(value, 'ask_id');
|
|
169
|
+
// A receipt v2's message carries its revision envelope beside the receipt; a receipt v1's has
|
|
170
|
+
// exactly the members it always had. The envelope itself is judged by receipt verification.
|
|
171
|
+
const withEnvelope = Object.hasOwn(value, 'revision_envelope');
|
|
151
172
|
if (value.type !== 'response.accepted' ||
|
|
152
173
|
!exactKeys(value, [
|
|
153
174
|
'type',
|
|
@@ -159,7 +180,11 @@ export function verifyWebhook(rawBody, headers, options) {
|
|
|
159
180
|
'data',
|
|
160
181
|
'accepted_at',
|
|
161
182
|
'receipt',
|
|
183
|
+
...(withAsk ? ['ask_id'] : []),
|
|
184
|
+
...(withEnvelope ? ['revision_envelope'] : []),
|
|
162
185
|
]) ||
|
|
186
|
+
(withAsk && (typeof value.ask_id !== 'string' || !ASK_ID.test(value.ask_id))) ||
|
|
187
|
+
(withEnvelope && (!isRecord(value.revision_envelope) || typeof value.receipt !== 'string')) ||
|
|
163
188
|
typeof value.event_id !== 'string' ||
|
|
164
189
|
!EVENT_ID.test(value.event_id) ||
|
|
165
190
|
value.event_id !== id ||
|
package/package.json
CHANGED