@sess.si/sdk 0.3.0 → 0.5.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 +42 -0
- package/README.md +21 -18
- package/dist/ask.d.ts +5 -5
- package/dist/ask.js +6 -6
- package/dist/cli/verify.d.ts +6 -2
- package/dist/cli/verify.js +12 -5
- package/dist/commerce/builders.d.ts +2 -2
- package/dist/commerce/builders.js +4 -4
- package/dist/commerce/items.d.ts +3 -3
- package/dist/commerce/items.js +2 -2
- package/dist/errors.d.ts +8 -2
- package/dist/generated/openapi.d.ts +6388 -2539
- package/dist/internal/version.d.ts +1 -1
- package/dist/internal/version.js +1 -1
- package/dist/receipts/bundle.d.ts +15 -9
- package/dist/receipts/bundle.js +21 -4
- package/dist/receipts/envelope.d.ts +60 -15
- package/dist/receipts/envelope.js +150 -45
- package/dist/receipts/index.d.ts +1 -1
- package/dist/receipts/json.d.ts +13 -0
- package/dist/receipts/json.js +21 -0
- package/dist/receipts/shared-title.d.ts +7 -0
- package/dist/receipts/shared-title.js +127 -0
- package/dist/receipts/verify.d.ts +6 -1
- package/dist/receipts/verify.js +29 -4
- package/dist/types.d.ts +17 -16
- package/dist/webhooks/index.d.ts +17 -1
- package/dist/webhooks/index.js +20 -0
- package/package.json +1 -1
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/** This package's version, sent in the User-Agent header. Tests check it against package.json. */
|
|
2
|
-
export declare const SDK_VERSION = "0.
|
|
2
|
+
export declare const SDK_VERSION = "0.5.0";
|
package/dist/internal/version.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/** This package's version, sent in the User-Agent header. Tests check it against package.json. */
|
|
2
|
-
export const SDK_VERSION = '0.
|
|
2
|
+
export const SDK_VERSION = '0.5.0';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type RevisionEnvelopeFull } from './envelope.js';
|
|
2
2
|
import { type ReceiptKeySet } from './verify.js';
|
|
3
3
|
/** Bounds of a bundle (bundle-v1 §4). A bundle beyond any of them is `oversized`. */
|
|
4
4
|
export declare const BUNDLE_LIMITS: Readonly<{
|
|
@@ -31,8 +31,8 @@ export type DecisionBundle = DecisionBundleV1 | DecisionBundleV2;
|
|
|
31
31
|
/** A bundle-v2 object: bundle-v1's members plus `revision_envelope`, exactly. */
|
|
32
32
|
export interface DecisionBundleV2 extends Omit<DecisionBundleV1, 'version'> {
|
|
33
33
|
version: 2;
|
|
34
|
-
/** The revision envelope the receipt v2 commits to (receipt-v2 §2). */
|
|
35
|
-
revision_envelope:
|
|
34
|
+
/** The revision envelope the receipt v2 commits to (receipt-v2 §2), in its full form. */
|
|
35
|
+
revision_envelope: RevisionEnvelopeFull;
|
|
36
36
|
}
|
|
37
37
|
/** A bundle-v1 object: exactly these members. */
|
|
38
38
|
export interface DecisionBundleV1 {
|
|
@@ -53,9 +53,14 @@ export interface DecisionBundleV1 {
|
|
|
53
53
|
/** Receipt rejection codes (receipt-v1 §6; receipt-v2 §7 adds `revision`). */
|
|
54
54
|
type ReceiptRejection = 'malformed' | 'algorithm' | 'missing_kid' | 'unknown_kid' | 'type' | 'signature' | 'issuer' | 'audience' | 'expired' | 'claims'
|
|
55
55
|
/** Receipt v2: the revision envelope is missing, malformed, or of another digest or record. */
|
|
56
|
-
| 'revision'
|
|
56
|
+
| 'revision'
|
|
57
|
+
/**
|
|
58
|
+
* Newer than this release: a later Sess receipt `typ`, or a receipt v2 whose envelope has a
|
|
59
|
+
* later `sess_revision`. A bundle reports it as `unsupported_version`; nothing is released.
|
|
60
|
+
*/
|
|
61
|
+
| 'newer';
|
|
57
62
|
/** Why a bundle is invalid: the first check that failed (bundle-v1 §5, bundle-v2 §5). */
|
|
58
|
-
export type BundleRejection = 'malformed' | 'oversized' | 'unsupported_version' | 'receipt_malformed' | Exclude<ReceiptRejection, 'malformed'> | 'not_yet_valid' | 'surface_mismatch' | 'input_mismatch' | 'context_mismatch';
|
|
63
|
+
export type BundleRejection = 'malformed' | 'oversized' | 'unsupported_version' | 'receipt_malformed' | Exclude<ReceiptRejection, 'malformed' | 'newer'> | 'not_yet_valid' | 'surface_mismatch' | 'input_mismatch' | 'context_mismatch';
|
|
59
64
|
/** Application context from the caller's trusted configuration, never from the bundle. */
|
|
60
65
|
export interface ExpectedBundleContext {
|
|
61
66
|
/** The recipient the caller expects (the receipt's `aud`: the Session's workspace). */
|
|
@@ -103,11 +108,12 @@ export interface VerifiedReceiptSummary {
|
|
|
103
108
|
accepted_at: string;
|
|
104
109
|
expires_at: string;
|
|
105
110
|
/**
|
|
106
|
-
* bundle-v2 only: the verified revision envelope (absent for a bundle-v1
|
|
107
|
-
*
|
|
108
|
-
*
|
|
111
|
+
* bundle-v2 only: the verified revision envelope, in its full form (absent for a bundle-v1
|
|
112
|
+
* result). A face shows "Asked by" and `published_at` only from here: the most specific title,
|
|
113
|
+
* `titles.grant_title`, else `titles.target_title`, else `titles.owner_name`, each the owner's
|
|
114
|
+
* own words, never a verified identity.
|
|
109
115
|
*/
|
|
110
|
-
revision_envelope?:
|
|
116
|
+
revision_envelope?: RevisionEnvelopeFull;
|
|
111
117
|
}
|
|
112
118
|
/** The result every face reports (bundle-v1 §6, bundle-v2 §6). JSON-serializable. */
|
|
113
119
|
export interface DecisionBundleVerification {
|
package/dist/receipts/bundle.js
CHANGED
|
@@ -11,8 +11,8 @@
|
|
|
11
11
|
// trusted-key validation, revision-envelope reading and canonical JSON helpers.
|
|
12
12
|
import { createHash } from 'node:crypto';
|
|
13
13
|
import { decodeProtectedHeader, errors, importJWK, jwtVerify } from 'jose';
|
|
14
|
-
import { parseRevisionEnvelope, revisionHash } from './envelope.js';
|
|
15
|
-
import { canonicalJson, exactKeys, isRecord, jsonDigest } from './json.js';
|
|
14
|
+
import { parseFullRevisionEnvelope, parseRevisionEnvelope, revisionEnvelopeIsNewer, revisionHash, } from './envelope.js';
|
|
15
|
+
import { canonicalJson, exactKeys, isNewerReceiptTyp, isRecord, jsonDigest, RECEIPT_V2_ACTION_LABEL_MAX, } from './json.js';
|
|
16
16
|
import { validateKeySet } from './verify.js';
|
|
17
17
|
const BUNDLE_TYPE = 'sess.decision-bundle';
|
|
18
18
|
const BUNDLE_VERSION = 1;
|
|
@@ -97,6 +97,7 @@ function receiptV1Claims(p, version) {
|
|
|
97
97
|
exactKeys(a, ['id', 'label', 'summary']) &&
|
|
98
98
|
nonEmpty(a.id) &&
|
|
99
99
|
nonEmpty(a.label) &&
|
|
100
|
+
(version === 1 || [...a.label].length <= RECEIPT_V2_ACTION_LABEL_MAX) &&
|
|
100
101
|
typeof a.summary === 'string' &&
|
|
101
102
|
isRecord(act) &&
|
|
102
103
|
exactKeys(act, ['assurance', 'subject']) &&
|
|
@@ -130,6 +131,10 @@ async function verifyReceiptV1(jws, keys, options, envelope) {
|
|
|
130
131
|
catch {
|
|
131
132
|
throw new ReceiptV1Failure('malformed');
|
|
132
133
|
}
|
|
134
|
+
// A Sess format this release does not know is newer, before any other check: its key, its
|
|
135
|
+
// algorithm and its claims may all be ones this release has never seen.
|
|
136
|
+
if (isNewerReceiptTyp(header.typ))
|
|
137
|
+
throw new ReceiptV1Failure('newer');
|
|
133
138
|
if (header.alg !== RECEIPT_ALG)
|
|
134
139
|
throw new ReceiptV1Failure('algorithm');
|
|
135
140
|
if (typeof header.kid !== 'string' || header.kid.length === 0)
|
|
@@ -173,7 +178,10 @@ async function verifyReceiptV1(jws, keys, options, envelope) {
|
|
|
173
178
|
if (`sha256:${createHash('sha256').update(signed).digest('hex')}` !== jsonDigest(claims))
|
|
174
179
|
throw new ReceiptV1Failure('claims');
|
|
175
180
|
if (options.version === 2) {
|
|
176
|
-
// receipt-v2 §7: missing or null, malformed, of another digest, or of another record.
|
|
181
|
+
// receipt-v2 §7: missing or null, malformed, of another digest, or of another record. An
|
|
182
|
+
// envelope of a later sess_revision is newer, never read as this version.
|
|
183
|
+
if (envelope !== undefined && envelope !== null && revisionEnvelopeIsNewer(envelope))
|
|
184
|
+
throw new ReceiptV1Failure('newer');
|
|
177
185
|
const parsed = parseRevisionEnvelope(envelope);
|
|
178
186
|
if (parsed === null ||
|
|
179
187
|
revisionHash(parsed) !== claims.surface.revision_hash ||
|
|
@@ -412,8 +420,17 @@ export async function verifyDecisionBundle(bundle, options) {
|
|
|
412
420
|
return invalid('audience', {
|
|
413
421
|
context: { status: 'mismatch', checked: supplied, mismatches: ['audience'] },
|
|
414
422
|
});
|
|
423
|
+
// A receipt newer than this release (a later Sess typ, a later envelope) is a newer bundle:
|
|
424
|
+
// reported as unsupported_version, and nothing is released.
|
|
425
|
+
if (e.code === 'newer')
|
|
426
|
+
return invalid('unsupported_version');
|
|
415
427
|
return invalid(e.code === 'malformed' ? 'receipt_malformed' : e.code);
|
|
416
428
|
}
|
|
429
|
+
// A person's file carries the full form: a compact envelope is a QR code's, never a bundle's, so
|
|
430
|
+
// it is refused as `revision` even when its digest matches.
|
|
431
|
+
const verifiedEnvelope = v2 ? parseFullRevisionEnvelope(envelope) : null;
|
|
432
|
+
if (v2 && verifiedEnvelope === null)
|
|
433
|
+
return invalid('revision');
|
|
417
434
|
if (claims.iat > Math.floor(now.getTime() / 1000) + CLOCK_TOLERANCE_S)
|
|
418
435
|
return invalid('not_yet_valid');
|
|
419
436
|
if (claims.surface.hash !== jsonDigest(b.content.surface))
|
|
@@ -452,7 +469,7 @@ export async function verifyDecisionBundle(bundle, options) {
|
|
|
452
469
|
accepted_at: claims.accepted_at,
|
|
453
470
|
expires_at: new Date(claims.exp * 1000).toISOString(),
|
|
454
471
|
// bundle-v2: the envelope as verified (its digest is the receipt's revision_hash).
|
|
455
|
-
...(
|
|
472
|
+
...(verifiedEnvelope ? { revision_envelope: verifiedEnvelope } : {}),
|
|
456
473
|
},
|
|
457
474
|
};
|
|
458
475
|
}
|
|
@@ -1,30 +1,75 @@
|
|
|
1
|
-
/** The envelope's own schema version (`sess_revision`). */
|
|
2
|
-
export declare const REVISION_ENVELOPE_VERSION =
|
|
1
|
+
/** The envelope's own schema version (`sess_revision`); a higher one is newer. */
|
|
2
|
+
export declare const REVISION_ENVELOPE_VERSION = 2;
|
|
3
|
+
/** The registered address the Session started from, as at Start. */
|
|
4
|
+
export interface EnvelopeAgentTarget {
|
|
5
|
+
/** `tgt_…` */
|
|
6
|
+
id: string;
|
|
7
|
+
/** The Target's version bound at Start, a whole number from 1. */
|
|
8
|
+
version: number;
|
|
9
|
+
/** The address, `sess.si/a/{name}`: an owner-chosen public name, not a verified identity. */
|
|
10
|
+
name: string;
|
|
11
|
+
}
|
|
12
|
+
/** The three owner-chosen titles under their nonce. Each is 1–60 code points or null. */
|
|
13
|
+
export interface EnvelopeTitles {
|
|
14
|
+
nonce: string;
|
|
15
|
+
/** Null exactly when `grant_id` is null. */
|
|
16
|
+
grant_title: string | null;
|
|
17
|
+
/** Null when `agent_target` is null. */
|
|
18
|
+
target_title: string | null;
|
|
19
|
+
owner_name: string | null;
|
|
20
|
+
}
|
|
3
21
|
/**
|
|
4
|
-
* The
|
|
5
|
-
*
|
|
6
|
-
* label when it was published (both null for any other publisher, and for revisions published
|
|
7
|
-
* before receipt v2). They are the integrator's own key and word for it, never a person.
|
|
22
|
+
* The ten common members. The owner-chosen names in it (`agent_target.name` and, in the full form,
|
|
23
|
+
* the titles) are the owner's own words, signed by Sess as published, never verified identities.
|
|
8
24
|
*/
|
|
9
|
-
export interface
|
|
10
|
-
sess_revision:
|
|
11
|
-
session_id: string;
|
|
25
|
+
export interface RevisionEnvelopeCommon {
|
|
26
|
+
sess_revision: 2;
|
|
12
27
|
revision: number;
|
|
28
|
+
/** `ses_…` */
|
|
29
|
+
session_id: string;
|
|
13
30
|
/** The receipt's `surface.hash`: SHA-256 of the revision's normalized Surface document. */
|
|
14
31
|
surface_hash: string;
|
|
15
32
|
renderer_version: string;
|
|
16
33
|
/** When the revision was published: `YYYY-MM-DDTHH:MM:SS.mmmZ`. */
|
|
17
34
|
published_at: string;
|
|
35
|
+
/** The responsible owner key (`key_…`), or null. */
|
|
18
36
|
key_id: string | null;
|
|
19
|
-
/**
|
|
20
|
-
key_label: string | null;
|
|
21
|
-
/** Null in this version of Sess; an identifier is accepted. */
|
|
37
|
+
/** A Session grant (`sgr_…`), or null. */
|
|
22
38
|
grant_id: string | null;
|
|
39
|
+
/** The registered address the Session started from, or null. */
|
|
40
|
+
agent_target: EnvelopeAgentTarget | null;
|
|
41
|
+
/** The Ask a respondent's Session belongs to (`ask_…`), or null. */
|
|
42
|
+
ask_id: string | null;
|
|
43
|
+
}
|
|
44
|
+
/** The full form, with `titles`: every file a person keeps, and every API response. */
|
|
45
|
+
export interface RevisionEnvelopeFull extends RevisionEnvelopeCommon {
|
|
46
|
+
titles: EnvelopeTitles;
|
|
23
47
|
}
|
|
48
|
+
/** The compact form, with `titles_digest` in place of `titles` (a QR code's). */
|
|
49
|
+
export interface RevisionEnvelopeCompact extends RevisionEnvelopeCommon {
|
|
50
|
+
titles_digest: string;
|
|
51
|
+
}
|
|
52
|
+
/** The revision envelope beside a receipt v2, in either form. */
|
|
53
|
+
export type RevisionEnvelope = RevisionEnvelopeFull | RevisionEnvelopeCompact;
|
|
54
|
+
/**
|
|
55
|
+
* Whether a supplied value is an envelope of a later `sess_revision` than this reader knows: an
|
|
56
|
+
* object whose `sess_revision` is a whole number above 2. A verifier reports it as newer and
|
|
57
|
+
* releases nothing; it is never accepted. A lower or equal version is not newer.
|
|
58
|
+
*/
|
|
59
|
+
export declare function revisionEnvelopeIsNewer(v: unknown): boolean;
|
|
24
60
|
/**
|
|
25
|
-
* Reads a supplied envelope strictly: exactly the
|
|
26
|
-
*
|
|
61
|
+
* Reads a supplied envelope strictly: exactly the ten common members, each of its kind, and exactly
|
|
62
|
+
* one of `titles` (exactly its four members, each title under the shared title rule) or
|
|
63
|
+
* `titles_digest`; `titles.grant_title` null exactly when `grant_id` is null, and
|
|
64
|
+
* `titles.target_title` null when `agent_target` is null. Null for anything else: both forms,
|
|
65
|
+
* neither, an extra or missing member at either level, a key label, a `sess_revision` other than 2
|
|
66
|
+
* (see revisionEnvelopeIsNewer). The value returned is a copy in the form given.
|
|
27
67
|
*/
|
|
28
68
|
export declare function parseRevisionEnvelope(v: unknown): RevisionEnvelope | null;
|
|
29
|
-
/**
|
|
69
|
+
/** Reads a supplied envelope as a person's file carries it: the full form only. */
|
|
70
|
+
export declare function parseFullRevisionEnvelope(v: unknown): RevisionEnvelopeFull | null;
|
|
71
|
+
/**
|
|
72
|
+
* `revision_hash`: `sha256:<hex>` of the RFC 8785 canonical JSON of the compact form. A full form's
|
|
73
|
+
* `titles` is replaced by its digest first, so both forms of one revision give the same hash.
|
|
74
|
+
*/
|
|
30
75
|
export declare function revisionHash(envelope: RevisionEnvelope): string;
|
|
@@ -1,83 +1,188 @@
|
|
|
1
1
|
// The revision envelope (receipt-v2 §2, docs/spec/receipt-v2.md): the record of one published
|
|
2
|
-
// Surface revision that a receipt v2 commits to through `surface.revision_hash
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
// an envelope one accepts the other accepts.
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
2
|
+
// Surface revision that a receipt v2 commits to through `surface.revision_hash`. Sess computes it
|
|
3
|
+
// from the stored revision and returns it beside every receipt v2; a verifier recomputes the digest
|
|
4
|
+
// from the envelope it is given. It is read here exactly as the Sess verification core reads it
|
|
5
|
+
// (packages/security/src/revision-envelope.ts at the commit recorded in docs/spec/source.json:
|
|
6
|
+
// same members, types, identifier and title rules), so an envelope one accepts the other accepts.
|
|
7
|
+
//
|
|
8
|
+
// The format, `sess_revision: 2` (receipt-v2 §2, frozen): ten common members, then exactly one of
|
|
9
|
+
// - `titles` (the full form): `{nonce, grant_title, target_title, owner_name}`, exactly these four;
|
|
10
|
+
// - `titles_digest` (the compact form): the unpadded base64url SHA-256 of the RFC 8785 canonical
|
|
11
|
+
// JSON of `titles`, 43 characters.
|
|
12
|
+
// Both, or neither, or any other key at either level, is refused. `revision_hash` is the JCS
|
|
13
|
+
// SHA-256 of the compact form: a full form is first normalised by replacing `titles` with its
|
|
14
|
+
// digest, so both forms of one revision give the same hash. No key label is in the envelope.
|
|
15
|
+
//
|
|
16
|
+
// Forward compatibility (receipt-v2 §7, `newer`): an envelope whose `sess_revision` is a whole number above
|
|
17
|
+
// 2 is newer (revisionEnvelopeIsNewer), never read as this version; a verifier reports it as newer
|
|
18
|
+
// and releases nothing. The nine-member `sess_revision: 1` shape was never issued and is refused.
|
|
19
|
+
import { createHash } from 'node:crypto';
|
|
20
|
+
import { canonicalJson, isRecord, jsonDigest } from './json.js';
|
|
21
|
+
import { isStoredSharedTitle } from './shared-title.js';
|
|
22
|
+
/** The envelope's own schema version (`sess_revision`); a higher one is newer. */
|
|
23
|
+
export const REVISION_ENVELOPE_VERSION = 2;
|
|
10
24
|
/**
|
|
11
25
|
* The canonical `published_at` form: RFC 3339 UTC with exactly three fractional digits, as
|
|
12
26
|
* `Date.prototype.toISOString` writes it.
|
|
13
27
|
*/
|
|
14
28
|
const PUBLISHED_AT = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/;
|
|
15
29
|
const DIGEST = /^sha256:[0-9a-f]{64}$/;
|
|
30
|
+
/** `titles.nonce`: 16 bytes as unpadded base64url. */
|
|
31
|
+
const TITLES_NONCE = /^[A-Za-z0-9_-]{21}[AQgw]$/;
|
|
32
|
+
/** `titles_digest`: a 32-byte SHA-256 as unpadded base64url. */
|
|
33
|
+
const TITLES_DIGEST = /^[A-Za-z0-9_-]{42}[AEIMQUYcgkosw048]$/;
|
|
34
|
+
/** `agent_target.name`: the share-name rule (3 to 40 characters, a lowercase slug). */
|
|
35
|
+
const AGENT_TARGET_NAME = /^(?=.{3,40}$)[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/;
|
|
16
36
|
const ID_MAX = 200;
|
|
17
|
-
/** The key label's bound (at most 64 characters, possibly empty). */
|
|
18
|
-
const KEY_LABEL_MAX = 64;
|
|
19
37
|
const CONTROL = /[\u0000-\u001f\u007f-\u009f]/;
|
|
20
38
|
// A bearer credential is never envelope material: Sess owner and management keys, and the
|
|
21
39
|
// unprefixed 22- or 43-character human link token.
|
|
22
40
|
const CREDENTIAL = /^(?:sess_sk_[A-Za-z0-9_-]{43}|sess_mg_[A-Za-z0-9_-]{43}|[A-Za-z0-9_-]{22}|[A-Za-z0-9_-]{43})$/;
|
|
23
41
|
const MEMBERS = [
|
|
24
42
|
'sess_revision',
|
|
25
|
-
'session_id',
|
|
26
43
|
'revision',
|
|
44
|
+
'session_id',
|
|
27
45
|
'surface_hash',
|
|
28
46
|
'renderer_version',
|
|
29
47
|
'published_at',
|
|
30
48
|
'key_id',
|
|
31
|
-
'key_label',
|
|
32
49
|
'grant_id',
|
|
50
|
+
'agent_target',
|
|
51
|
+
'ask_id',
|
|
33
52
|
];
|
|
34
|
-
|
|
35
|
-
|
|
53
|
+
const TITLES = ['nonce', 'grant_title', 'target_title', 'owner_name'];
|
|
54
|
+
const AGENT_TARGET = ['id', 'version', 'name'];
|
|
55
|
+
function exactly(o, keys) {
|
|
56
|
+
const own = Object.keys(o);
|
|
57
|
+
return own.length === keys.length && keys.every((k) => Object.hasOwn(o, k));
|
|
36
58
|
}
|
|
37
|
-
function
|
|
38
|
-
return
|
|
59
|
+
function renderer(v) {
|
|
60
|
+
return typeof v === 'string' && v.length > 0 && v.length <= ID_MAX && !CONTROL.test(v) && !/\s/.test(v);
|
|
39
61
|
}
|
|
40
|
-
|
|
41
|
-
|
|
62
|
+
/** An opaque ID with its prefix and a non-empty body; never a bearer credential. */
|
|
63
|
+
function id(v, prefix) {
|
|
64
|
+
return (typeof v === 'string' &&
|
|
65
|
+
v.length > prefix.length &&
|
|
66
|
+
v.length <= ID_MAX &&
|
|
67
|
+
v.startsWith(prefix) &&
|
|
68
|
+
/^[A-Za-z0-9_-]+$/.test(v) &&
|
|
69
|
+
!CREDENTIAL.test(v) &&
|
|
70
|
+
!/sess_(?:sk|mg)_/.test(v));
|
|
42
71
|
}
|
|
72
|
+
const whole = (v, min) => Number.isSafeInteger(v) && v >= min;
|
|
43
73
|
function publishedAt(v) {
|
|
44
74
|
if (typeof v !== 'string' || !PUBLISHED_AT.test(v))
|
|
45
75
|
return false;
|
|
46
76
|
const t = new Date(v);
|
|
47
77
|
return !Number.isNaN(t.getTime()) && t.toISOString() === v;
|
|
48
78
|
}
|
|
79
|
+
function agentTarget(v) {
|
|
80
|
+
return (isRecord(v) &&
|
|
81
|
+
exactly(v, AGENT_TARGET) &&
|
|
82
|
+
id(v.id, 'tgt_') &&
|
|
83
|
+
whole(v.version, 1) &&
|
|
84
|
+
typeof v.name === 'string' &&
|
|
85
|
+
AGENT_TARGET_NAME.test(v.name));
|
|
86
|
+
}
|
|
87
|
+
const title = (v) => v === null || isStoredSharedTitle(v);
|
|
88
|
+
function titles(v) {
|
|
89
|
+
return (isRecord(v) &&
|
|
90
|
+
exactly(v, TITLES) &&
|
|
91
|
+
typeof v.nonce === 'string' &&
|
|
92
|
+
TITLES_NONCE.test(v.nonce) &&
|
|
93
|
+
title(v.grant_title) &&
|
|
94
|
+
title(v.target_title) &&
|
|
95
|
+
title(v.owner_name));
|
|
96
|
+
}
|
|
97
|
+
const RULES = {
|
|
98
|
+
sess_revision: (v) => v === REVISION_ENVELOPE_VERSION,
|
|
99
|
+
revision: (v) => whole(v, 1),
|
|
100
|
+
session_id: (v) => id(v, 'ses_'),
|
|
101
|
+
surface_hash: (v) => typeof v === 'string' && DIGEST.test(v),
|
|
102
|
+
renderer_version: renderer,
|
|
103
|
+
published_at: publishedAt,
|
|
104
|
+
key_id: (v) => v === null || id(v, 'key_'),
|
|
105
|
+
grant_id: (v) => v === null || id(v, 'sgr_'),
|
|
106
|
+
agent_target: (v) => v === null || agentTarget(v),
|
|
107
|
+
ask_id: (v) => v === null || id(v, 'ask_'),
|
|
108
|
+
};
|
|
49
109
|
/**
|
|
50
|
-
*
|
|
51
|
-
*
|
|
110
|
+
* `titles_digest`: the unpadded base64url SHA-256 of the UTF-8 bytes of the RFC 8785 canonical
|
|
111
|
+
* JSON of `titles`.
|
|
112
|
+
*/
|
|
113
|
+
function titlesDigest(t) {
|
|
114
|
+
return createHash('sha256').update(canonicalJson(t), 'utf8').digest('base64url');
|
|
115
|
+
}
|
|
116
|
+
function isFull(e) {
|
|
117
|
+
return Object.hasOwn(e, 'titles');
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Whether a supplied value is an envelope of a later `sess_revision` than this reader knows: an
|
|
121
|
+
* object whose `sess_revision` is a whole number above 2. A verifier reports it as newer and
|
|
122
|
+
* releases nothing; it is never accepted. A lower or equal version is not newer.
|
|
123
|
+
*/
|
|
124
|
+
export function revisionEnvelopeIsNewer(v) {
|
|
125
|
+
return (isRecord(v) &&
|
|
126
|
+
Number.isSafeInteger(v.sess_revision) &&
|
|
127
|
+
v.sess_revision > REVISION_ENVELOPE_VERSION);
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Reads a supplied envelope strictly: exactly the ten common members, each of its kind, and exactly
|
|
131
|
+
* one of `titles` (exactly its four members, each title under the shared title rule) or
|
|
132
|
+
* `titles_digest`; `titles.grant_title` null exactly when `grant_id` is null, and
|
|
133
|
+
* `titles.target_title` null when `agent_target` is null. Null for anything else: both forms,
|
|
134
|
+
* neither, an extra or missing member at either level, a key label, a `sess_revision` other than 2
|
|
135
|
+
* (see revisionEnvelopeIsNewer). The value returned is a copy in the form given.
|
|
52
136
|
*/
|
|
53
137
|
export function parseRevisionEnvelope(v) {
|
|
54
|
-
if (!isRecord(v)
|
|
138
|
+
if (!isRecord(v))
|
|
55
139
|
return null;
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
v.revision >= 1 &&
|
|
60
|
-
typeof v.surface_hash === 'string' &&
|
|
61
|
-
DIGEST.test(v.surface_hash) &&
|
|
62
|
-
plain(v.renderer_version) &&
|
|
63
|
-
publishedAt(v.published_at) &&
|
|
64
|
-
(v.key_id === null ? v.key_label === null : identifier(v.key_id) && label(v.key_label)) &&
|
|
65
|
-
(v.grant_id === null || identifier(v.grant_id));
|
|
66
|
-
if (!ok)
|
|
140
|
+
const full = Object.hasOwn(v, 'titles');
|
|
141
|
+
const compact = Object.hasOwn(v, 'titles_digest');
|
|
142
|
+
if (full === compact)
|
|
67
143
|
return null;
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
144
|
+
if (!exactly(v, [...MEMBERS, full ? 'titles' : 'titles_digest']))
|
|
145
|
+
return null;
|
|
146
|
+
const out = {};
|
|
147
|
+
for (const name of MEMBERS) {
|
|
148
|
+
if (!RULES[name](v[name]))
|
|
149
|
+
return null;
|
|
150
|
+
out[name] = v[name];
|
|
151
|
+
}
|
|
152
|
+
if (out.agent_target !== null)
|
|
153
|
+
out.agent_target = { ...out.agent_target };
|
|
154
|
+
if (full) {
|
|
155
|
+
if (!titles(v.titles))
|
|
156
|
+
return null;
|
|
157
|
+
out.titles = { ...v.titles };
|
|
158
|
+
const t = out.titles;
|
|
159
|
+
if ((out.grant_id === null) !== (t.grant_title === null))
|
|
160
|
+
return null;
|
|
161
|
+
if (out.agent_target === null && t.target_title !== null)
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
if (typeof v.titles_digest !== 'string' || !TITLES_DIGEST.test(v.titles_digest))
|
|
166
|
+
return null;
|
|
167
|
+
out.titles_digest = v.titles_digest;
|
|
168
|
+
}
|
|
169
|
+
return out;
|
|
170
|
+
}
|
|
171
|
+
/** Reads a supplied envelope as a person's file carries it: the full form only. */
|
|
172
|
+
export function parseFullRevisionEnvelope(v) {
|
|
173
|
+
const e = parseRevisionEnvelope(v);
|
|
174
|
+
return e !== null && isFull(e) ? e : null;
|
|
79
175
|
}
|
|
80
|
-
/**
|
|
176
|
+
/**
|
|
177
|
+
* `revision_hash`: `sha256:<hex>` of the RFC 8785 canonical JSON of the compact form. A full form's
|
|
178
|
+
* `titles` is replaced by its digest first, so both forms of one revision give the same hash.
|
|
179
|
+
*/
|
|
81
180
|
export function revisionHash(envelope) {
|
|
82
|
-
|
|
181
|
+
if (!isFull(envelope))
|
|
182
|
+
return jsonDigest(envelope);
|
|
183
|
+
const compact = {};
|
|
184
|
+
for (const name of MEMBERS)
|
|
185
|
+
compact[name] = envelope[name];
|
|
186
|
+
compact.titles_digest = titlesDigest(envelope.titles);
|
|
187
|
+
return jsonDigest(compact);
|
|
83
188
|
}
|
package/dist/receipts/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { verifyDecisionReceipt, verifyRendererIdentities } from './verify.js';
|
|
2
2
|
export { BUNDLED_RENDERER_IDENTITIES, BUNDLED_RENDERER_LIST, newerRendererList } from './renderers.js';
|
|
3
3
|
export type { RendererIdentitiesDocument, RendererIdentity, RendererList } from './renderers.js';
|
|
4
|
-
export type { RevisionEnvelope } from './envelope.js';
|
|
4
|
+
export type { EnvelopeAgentTarget, EnvelopeTitles, RevisionEnvelope, RevisionEnvelopeCompact, RevisionEnvelopeFull, } from './envelope.js';
|
|
5
5
|
export type { ReceiptClaims, ReceiptClaimsV1, ReceiptClaimsV2, ReceiptPublicKey, ReceiptKeySet, ReceiptKeyResolver, VerifyDecisionReceiptInput, VerifyDecisionReceiptOptions, } from './verify.js';
|
|
6
6
|
export type { DeepReadonly } from './json.js';
|
|
7
7
|
export { verifyDecisionBundle, BUNDLE_LIMITS } from './bundle.js';
|
package/dist/receipts/json.d.ts
CHANGED
|
@@ -4,6 +4,19 @@ export type DeepReadonly<T> = T extends readonly (infer U)[] ? readonly DeepRead
|
|
|
4
4
|
readonly [P in keyof T]: DeepReadonly<T[P]>;
|
|
5
5
|
} : T;
|
|
6
6
|
export declare function verificationError(code: string, message: string): SessError;
|
|
7
|
+
/**
|
|
8
|
+
* A receipt newer than this release (a later receipt type, or a later revision envelope): reported
|
|
9
|
+
* as newer, never as a verification failure, and nothing is returned with it.
|
|
10
|
+
*/
|
|
11
|
+
export declare function newerReceiptError(): SessError;
|
|
12
|
+
/**
|
|
13
|
+
* Whether a JWS `typ` names a Sess signed format this release does not know: a later receipt
|
|
14
|
+
* version or another Sess receipt type, such as a closing receipt (`sess-<name>+jwt`, other than
|
|
15
|
+
* the two receipt types read here). Any other unknown `typ` is not a Sess format.
|
|
16
|
+
*/
|
|
17
|
+
export declare function isNewerReceiptTyp(typ: unknown): boolean;
|
|
18
|
+
/** Receipt v2's action label bound in code points: a longer one is not a valid receipt v2. */
|
|
19
|
+
export declare const RECEIPT_V2_ACTION_LABEL_MAX = 24;
|
|
7
20
|
export declare function isRecord(value: unknown): value is Record<string, unknown>;
|
|
8
21
|
export declare function wellFormedUnicode(value: string): boolean;
|
|
9
22
|
export declare function exactKeys(value: Record<string, unknown>, keys: readonly string[]): boolean;
|
package/dist/receipts/json.js
CHANGED
|
@@ -1,9 +1,30 @@
|
|
|
1
1
|
import canonicalize from 'canonicalize';
|
|
2
2
|
import { createHash } from 'node:crypto';
|
|
3
3
|
import { SessError } from '../errors.js';
|
|
4
|
+
import { SDK_VERSION } from '../internal/version.js';
|
|
4
5
|
export function verificationError(code, message) {
|
|
5
6
|
return new SessError('receipt_verification', message, { code });
|
|
6
7
|
}
|
|
8
|
+
/**
|
|
9
|
+
* A receipt newer than this release (a later receipt type, or a later revision envelope): reported
|
|
10
|
+
* as newer, never as a verification failure, and nothing is returned with it.
|
|
11
|
+
*/
|
|
12
|
+
export function newerReceiptError() {
|
|
13
|
+
return new SessError('receipt_newer', `This receipt is newer than @sess.si/sdk ${SDK_VERSION}. Update @sess.si/sdk to check it. No answer data was returned.`, { code: 'RECEIPT_NEWER' });
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Whether a JWS `typ` names a Sess signed format this release does not know: a later receipt
|
|
17
|
+
* version or another Sess receipt type, such as a closing receipt (`sess-<name>+jwt`, other than
|
|
18
|
+
* the two receipt types read here). Any other unknown `typ` is not a Sess format.
|
|
19
|
+
*/
|
|
20
|
+
export function isNewerReceiptTyp(typ) {
|
|
21
|
+
return (typeof typ === 'string' &&
|
|
22
|
+
typ !== 'sess-receipt+jwt' &&
|
|
23
|
+
typ !== 'sess-receipt-2+jwt' &&
|
|
24
|
+
/^sess-[a-z0-9]+(?:-[a-z0-9]+)*\+jwt$/.test(typ));
|
|
25
|
+
}
|
|
26
|
+
/** Receipt v2's action label bound in code points: a longer one is not a valid receipt v2. */
|
|
27
|
+
export const RECEIPT_V2_ACTION_LABEL_MAX = 24;
|
|
7
28
|
export function isRecord(value) {
|
|
8
29
|
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
9
30
|
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** A title's bound, in code points. */
|
|
2
|
+
export declare const SHARED_TITLE_MAX = 60;
|
|
3
|
+
/**
|
|
4
|
+
* Whether `v` is a stored title: a string the shared title rule leaves exactly as it is (a fixed
|
|
5
|
+
* point of the rule, so non-empty, trimmed and within every rule above).
|
|
6
|
+
*/
|
|
7
|
+
export declare function isStoredSharedTitle(v: unknown): v is string;
|