@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
|
@@ -0,0 +1,75 @@
|
|
|
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
|
+
}
|
|
21
|
+
/**
|
|
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.
|
|
24
|
+
*/
|
|
25
|
+
export interface RevisionEnvelopeCommon {
|
|
26
|
+
sess_revision: 2;
|
|
27
|
+
revision: number;
|
|
28
|
+
/** `ses_…` */
|
|
29
|
+
session_id: string;
|
|
30
|
+
/** The receipt's `surface.hash`: SHA-256 of the revision's normalized Surface document. */
|
|
31
|
+
surface_hash: string;
|
|
32
|
+
renderer_version: string;
|
|
33
|
+
/** When the revision was published: `YYYY-MM-DDTHH:MM:SS.mmmZ`. */
|
|
34
|
+
published_at: string;
|
|
35
|
+
/** The responsible owner key (`key_…`), or null. */
|
|
36
|
+
key_id: string | null;
|
|
37
|
+
/** A Session grant (`sgr_…`), or null. */
|
|
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;
|
|
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;
|
|
60
|
+
/**
|
|
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.
|
|
67
|
+
*/
|
|
68
|
+
export declare function parseRevisionEnvelope(v: unknown): RevisionEnvelope | null;
|
|
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
|
+
*/
|
|
75
|
+
export declare function revisionHash(envelope: RevisionEnvelope): string;
|
|
@@ -0,0 +1,188 @@
|
|
|
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`. 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;
|
|
24
|
+
/**
|
|
25
|
+
* The canonical `published_at` form: RFC 3339 UTC with exactly three fractional digits, as
|
|
26
|
+
* `Date.prototype.toISOString` writes it.
|
|
27
|
+
*/
|
|
28
|
+
const PUBLISHED_AT = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/;
|
|
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]+)*$/;
|
|
36
|
+
const ID_MAX = 200;
|
|
37
|
+
const CONTROL = /[\u0000-\u001f\u007f-\u009f]/;
|
|
38
|
+
// A bearer credential is never envelope material: Sess owner and management keys, and the
|
|
39
|
+
// unprefixed 22- or 43-character human link token.
|
|
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})$/;
|
|
41
|
+
const MEMBERS = [
|
|
42
|
+
'sess_revision',
|
|
43
|
+
'revision',
|
|
44
|
+
'session_id',
|
|
45
|
+
'surface_hash',
|
|
46
|
+
'renderer_version',
|
|
47
|
+
'published_at',
|
|
48
|
+
'key_id',
|
|
49
|
+
'grant_id',
|
|
50
|
+
'agent_target',
|
|
51
|
+
'ask_id',
|
|
52
|
+
];
|
|
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));
|
|
58
|
+
}
|
|
59
|
+
function renderer(v) {
|
|
60
|
+
return typeof v === 'string' && v.length > 0 && v.length <= ID_MAX && !CONTROL.test(v) && !/\s/.test(v);
|
|
61
|
+
}
|
|
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));
|
|
71
|
+
}
|
|
72
|
+
const whole = (v, min) => Number.isSafeInteger(v) && v >= min;
|
|
73
|
+
function publishedAt(v) {
|
|
74
|
+
if (typeof v !== 'string' || !PUBLISHED_AT.test(v))
|
|
75
|
+
return false;
|
|
76
|
+
const t = new Date(v);
|
|
77
|
+
return !Number.isNaN(t.getTime()) && t.toISOString() === v;
|
|
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
|
+
};
|
|
109
|
+
/**
|
|
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.
|
|
136
|
+
*/
|
|
137
|
+
export function parseRevisionEnvelope(v) {
|
|
138
|
+
if (!isRecord(v))
|
|
139
|
+
return null;
|
|
140
|
+
const full = Object.hasOwn(v, 'titles');
|
|
141
|
+
const compact = Object.hasOwn(v, 'titles_digest');
|
|
142
|
+
if (full === compact)
|
|
143
|
+
return null;
|
|
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;
|
|
175
|
+
}
|
|
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
|
+
*/
|
|
180
|
+
export function revisionHash(envelope) {
|
|
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);
|
|
188
|
+
}
|
package/dist/receipts/index.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
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 {
|
|
4
|
+
export type { EnvelopeAgentTarget, EnvelopeTitles, RevisionEnvelope, RevisionEnvelopeCompact, RevisionEnvelopeFull, } from './envelope.js';
|
|
5
|
+
export type { ReceiptClaims, ReceiptClaimsV1, ReceiptClaimsV2, ReceiptPublicKey, ReceiptKeySet, ReceiptKeyResolver, VerifyDecisionReceiptInput, VerifyDecisionReceiptOptions, } from './verify.js';
|
|
5
6
|
export type { DeepReadonly } from './json.js';
|
|
6
7
|
export { verifyDecisionBundle, BUNDLE_LIMITS } from './bundle.js';
|
|
7
|
-
export type { BundleContextField, BundleJwk, BundleRejection, DecisionBundle, DecisionBundleVerification, ExpectedBundleContext, VerifiedReceiptSummary, VerifyDecisionBundleOptions, } from './bundle.js';
|
|
8
|
+
export type { BundleContextField, BundleJwk, BundleRejection, DecisionBundle, DecisionBundleV1, DecisionBundleV2, DecisionBundleVerification, ExpectedBundleContext, VerifiedReceiptSummary, VerifyDecisionBundleOptions, } from './bundle.js';
|
|
8
9
|
export type { ReceiptResource, VerifyResponseInput, VerifiedResponse, VerificationRequestOptions, } from './resource.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
|
}
|
|
@@ -114,6 +114,9 @@ export function createReceiptResource(context) {
|
|
|
114
114
|
const claims = await verifyDecisionReceipt({
|
|
115
115
|
receipt: response.receipt,
|
|
116
116
|
...(rendererIdentities !== undefined ? { rendererIdentities } : {}),
|
|
117
|
+
// Receipt v2: the envelope returned beside it (the authorized record's own, compared
|
|
118
|
+
// above); a receipt v1 has none and the verifier does not read it.
|
|
119
|
+
revisionEnvelope: response.revision_envelope ?? null,
|
|
117
120
|
issuer,
|
|
118
121
|
audience: trustedWorkspace,
|
|
119
122
|
expected: {
|
|
@@ -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;
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
// The shared title rule (receipt-v2 §2: one line of plain text), as the revision-envelope reader
|
|
2
|
+
// applies it to every owner-chosen title a receipt v2 carries (`titles.grant_title`,
|
|
3
|
+
// `titles.target_title`, `titles.owner_name`): the frozen receipt v2 format says each title "is
|
|
4
|
+
// normalised by the shared title rule before storage, and stored as normalised", so a title this
|
|
5
|
+
// rule would change or refuse is not a valid envelope title. Ported from the Sess security core
|
|
6
|
+
// (packages/security/src/shared-title.ts at the commit recorded in docs/spec/source.json) with the
|
|
7
|
+
// same behaviour and the same refusal vectors (tests/security/receipts-v2.test.ts).
|
|
8
|
+
/** A title's bound, in code points. */
|
|
9
|
+
export const SHARED_TITLE_MAX = 60;
|
|
10
|
+
/**
|
|
11
|
+
* Characters owner text may not contain: C0/C1 controls other than the newline, the bidirectional
|
|
12
|
+
* embedding/override/isolate controls, zero-width and invisible format characters, and unpaired
|
|
13
|
+
* surrogates. A title refuses these and more (titleRefused).
|
|
14
|
+
*/
|
|
15
|
+
const FORBIDDEN_RANGES = [
|
|
16
|
+
[0x00, 0x09],
|
|
17
|
+
[0x0b, 0x1f],
|
|
18
|
+
[0x7f, 0x9f],
|
|
19
|
+
[0x61c, 0x61c],
|
|
20
|
+
[0x200b, 0x200f],
|
|
21
|
+
[0x2028, 0x202e],
|
|
22
|
+
[0x2060, 0x206f],
|
|
23
|
+
[0xd800, 0xdfff],
|
|
24
|
+
[0xfeff, 0xfeff],
|
|
25
|
+
[0xfff9, 0xfffb],
|
|
26
|
+
];
|
|
27
|
+
const codePoints = (text) => [...text].length;
|
|
28
|
+
/**
|
|
29
|
+
* The display title: one line of plain text, trimmed; no control or format character and none of
|
|
30
|
+
* FORBIDDEN_RANGES, except the joiners and the three flags titleRefused allows; at most
|
|
31
|
+
* SHARED_TITLE_MAX characters; at least one visible character (isVisible); null when empty.
|
|
32
|
+
*/
|
|
33
|
+
function normalizeSharedTitle(text) {
|
|
34
|
+
if (titleRefused([...text]))
|
|
35
|
+
return { ok: false };
|
|
36
|
+
const normalized = text.trim();
|
|
37
|
+
if (normalized === '')
|
|
38
|
+
return { ok: true, text: null };
|
|
39
|
+
if (codePoints(normalized) > SHARED_TITLE_MAX)
|
|
40
|
+
return { ok: false };
|
|
41
|
+
if (![...normalized].some(isVisible))
|
|
42
|
+
return { ok: false };
|
|
43
|
+
return { ok: true, text: normalized };
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Whether `v` is a stored title: a string the shared title rule leaves exactly as it is (a fixed
|
|
47
|
+
* point of the rule, so non-empty, trimmed and within every rule above).
|
|
48
|
+
*/
|
|
49
|
+
export function isStoredSharedTitle(v) {
|
|
50
|
+
if (typeof v !== 'string')
|
|
51
|
+
return false;
|
|
52
|
+
const r = normalizeSharedTitle(v);
|
|
53
|
+
return r.ok && r.text === v;
|
|
54
|
+
}
|
|
55
|
+
/** Every control (Cc, which covers CR and LF) and every format character (Cf). */
|
|
56
|
+
const TITLE_REFUSED = /[\p{Cc}\p{Cf}]/u;
|
|
57
|
+
const ZWNJ = 0x200c;
|
|
58
|
+
const ZWJ = 0x200d;
|
|
59
|
+
const BLACK_FLAG = 0x1f3f4;
|
|
60
|
+
/**
|
|
61
|
+
* The only tag sequences a title may hold: the three RGI subdivision flags, exactly (England,
|
|
62
|
+
* Scotland, Wales: U+1F3F4, the tag letters gbeng, gbsct or gbwls, U+E007F). Any other run of tag
|
|
63
|
+
* characters is refused: it could carry hidden ASCII text behind a plain black flag.
|
|
64
|
+
*/
|
|
65
|
+
const RGI_SUBDIVISION_FLAGS = [
|
|
66
|
+
[0x1f3f4, 0xe0067, 0xe0062, 0xe0065, 0xe006e, 0xe0067, 0xe007f],
|
|
67
|
+
[0x1f3f4, 0xe0067, 0xe0062, 0xe0073, 0xe0063, 0xe0074, 0xe007f],
|
|
68
|
+
[0x1f3f4, 0xe0067, 0xe0062, 0xe0077, 0xe006c, 0xe0073, 0xe007f],
|
|
69
|
+
];
|
|
70
|
+
/** The length of the RGI subdivision flag starting at `i`, or 0. */
|
|
71
|
+
const flagAt = (cps, i) => RGI_SUBDIVISION_FLAGS.find((f) => f.every((c, k) => cps[i + k] === c))?.length ?? 0;
|
|
72
|
+
const MARK = /\p{M}/u;
|
|
73
|
+
const VS15 = 0xfe0e;
|
|
74
|
+
const VS16 = 0xfe0f;
|
|
75
|
+
const EMOJI = /\p{Emoji}/u;
|
|
76
|
+
/**
|
|
77
|
+
* True when a title (as code points) has a character it may not hold. Every Cc and Cf, every
|
|
78
|
+
* default-ignorable code point and every FORBIDDEN_RANGES character is refused, with exactly three
|
|
79
|
+
* exceptions that real text needs:
|
|
80
|
+
* - one presentation selector (VS15, VS16) directly after an emoji character;
|
|
81
|
+
* - ZWNJ and ZWJ between two visible characters (the left neighbour is the base of the grapheme
|
|
82
|
+
* before the joiner: combining marks after it are skipped);
|
|
83
|
+
* - the three RGI subdivision flags, each as its exact code points.
|
|
84
|
+
*/
|
|
85
|
+
function titleRefused(chars) {
|
|
86
|
+
const cps = chars.map((ch) => ch.codePointAt(0));
|
|
87
|
+
for (let i = 0; i < cps.length; i++) {
|
|
88
|
+
const c = cps[i];
|
|
89
|
+
if (c === BLACK_FLAG) {
|
|
90
|
+
const flag = flagAt(cps, i);
|
|
91
|
+
if (flag > 0)
|
|
92
|
+
i += flag - 1; // the whole flag, its cancel tag included
|
|
93
|
+
continue; // a plain black flag is a symbol; tags after it fall to the Cf check
|
|
94
|
+
}
|
|
95
|
+
if (c === ZWNJ || c === ZWJ) {
|
|
96
|
+
let left = i - 1;
|
|
97
|
+
while (left >= 0 && MARK.test(chars[left]))
|
|
98
|
+
left--;
|
|
99
|
+
const before = chars[left];
|
|
100
|
+
const after = chars[i + 1];
|
|
101
|
+
if (before === undefined || after === undefined || !isVisible(before) || !isVisible(after))
|
|
102
|
+
return true;
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
if (c === VS15 || c === VS16) {
|
|
106
|
+
const base = chars[i - 1];
|
|
107
|
+
if (base === undefined || !EMOJI.test(base) || IGNORABLE.test(base))
|
|
108
|
+
return true;
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
if (IGNORABLE.test(chars[i]))
|
|
112
|
+
return true;
|
|
113
|
+
if (TITLE_REFUSED.test(chars[i]))
|
|
114
|
+
return true;
|
|
115
|
+
if (FORBIDDEN_RANGES.some(([lo, hi]) => c >= lo && c <= hi))
|
|
116
|
+
return true;
|
|
117
|
+
}
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* A visible character: a letter, number, symbol or punctuation (L, N, S, P) that is not a
|
|
122
|
+
* default-ignorable code point (the Hangul fillers are letters by category but draw blank) and not
|
|
123
|
+
* U+2800, the blank Braille pattern. Spaces, marks alone and private use do not count.
|
|
124
|
+
*/
|
|
125
|
+
const VISIBLE = /[\p{L}\p{N}\p{S}\p{P}]/u;
|
|
126
|
+
const IGNORABLE = /\p{Default_Ignorable_Code_Point}/u;
|
|
127
|
+
const isVisible = (ch) => VISIBLE.test(ch) && !IGNORABLE.test(ch) && ch.codePointAt(0) !== 0x2800;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { InteractiveSurfaceRevision } from '../types.js';
|
|
2
2
|
import { type DeepReadonly } from './json.js';
|
|
3
3
|
import { type RendererIdentitiesDocument, type RendererList } from './renderers.js';
|
|
4
|
+
import { type RevisionEnvelope } from './envelope.js';
|
|
4
5
|
export declare const MAX_RECEIPT_BYTES: number;
|
|
5
6
|
export declare const MAX_RECEIPT_KEY_SET_BYTES: number;
|
|
6
7
|
/** Public Ed25519 verification material; private JWK parameters are refused. */
|
|
@@ -23,7 +24,22 @@ export type ReceiptKeyResolver = (context: {
|
|
|
23
24
|
readonly issuer: string;
|
|
24
25
|
readonly signal?: AbortSignal;
|
|
25
26
|
}) => ReceiptKeySet | Promise<ReceiptKeySet>;
|
|
26
|
-
|
|
27
|
+
/** A verified receipt's claims: version 1, or version 2 (v1's plus `surface.revision_hash`). */
|
|
28
|
+
export type ReceiptClaims = ReceiptClaimsV1 | ReceiptClaimsV2;
|
|
29
|
+
/**
|
|
30
|
+
* Receipt v2 (receipt-v2 §3): the claims of receipt v1, unchanged, with `sess_receipt: 2` and
|
|
31
|
+
* `surface.revision_hash`, the SHA-256 of the revision envelope the receipt commits to.
|
|
32
|
+
*/
|
|
33
|
+
export interface ReceiptClaimsV2 extends Omit<ReceiptClaimsV1, 'sess_receipt' | 'surface'> {
|
|
34
|
+
sess_receipt: 2;
|
|
35
|
+
surface: {
|
|
36
|
+
revision: number;
|
|
37
|
+
hash: string;
|
|
38
|
+
renderer_version: string;
|
|
39
|
+
revision_hash: string;
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
export interface ReceiptClaimsV1 {
|
|
27
43
|
sess_receipt: 1;
|
|
28
44
|
iss: string;
|
|
29
45
|
aud: string;
|
|
@@ -83,6 +99,13 @@ export interface VerifyDecisionReceiptInput {
|
|
|
83
99
|
* only through its signature under the same trusted keys and issuer.
|
|
84
100
|
*/
|
|
85
101
|
rendererIdentities?: RendererIdentitiesDocument;
|
|
102
|
+
/**
|
|
103
|
+
* The revision envelope returned beside the receipt (`revision_envelope` on the accepted
|
|
104
|
+
* response, the Session event or the webhook message). Required for a receipt v2, which is
|
|
105
|
+
* refused with `REVISION_ENVELOPE_MISMATCH` without it or with any other envelope; not read for
|
|
106
|
+
* a receipt v1.
|
|
107
|
+
*/
|
|
108
|
+
revisionEnvelope?: RevisionEnvelope | null;
|
|
86
109
|
}
|
|
87
110
|
export interface VerifyDecisionReceiptOptions {
|
|
88
111
|
/** An explicit verification instant, useful for deliberate archival verification/tests. */
|
|
@@ -94,8 +117,20 @@ export interface VerifyDecisionReceiptOptions {
|
|
|
94
117
|
/** Validates and copies the whole set, not merely the key selected by the token. */
|
|
95
118
|
export declare function validateKeySet(raw: unknown): ReceiptKeySet;
|
|
96
119
|
/**
|
|
97
|
-
* Fully verifies a
|
|
98
|
-
*
|
|
120
|
+
* Fully verifies a receipt and its exact accepted record. Explicit keys make this function
|
|
121
|
+
* entirely offline. A valid receipt attests the accepted record, not business authority.
|
|
122
|
+
*
|
|
123
|
+
* The protected `typ` decides the version: `sess-receipt+jwt` is receipt v1, verified exactly as
|
|
124
|
+
* before (`revisionEnvelope` is not read); `sess-receipt-2+jwt` is receipt v2, verified only with
|
|
125
|
+
* the revision envelope it commits to. A receipt v2 whose envelope is missing or null, malformed,
|
|
126
|
+
* of another digest than `surface.revision_hash`, or naming another Session, revision, Surface
|
|
127
|
+
* hash or renderer identity is refused with `REVISION_ENVELOPE_MISMATCH`, after the issuer,
|
|
128
|
+
* audience, record, time and signature checks. The envelope is `sess_revision: 2`, in its full
|
|
129
|
+
* (`titles`) or compact (`titles_digest`) form; a receipt v2 action label is at most 24 code points.
|
|
130
|
+
*
|
|
131
|
+
* A receipt newer than this release, a later Sess receipt `typ` (checked before anything else in
|
|
132
|
+
* the receipt) or a receipt v2 whose envelope has a later `sess_revision`, is refused with kind
|
|
133
|
+
* `receipt_newer` and code `RECEIPT_NEWER`, never as a verification failure; nothing is returned.
|
|
99
134
|
*/
|
|
100
135
|
export declare function verifyDecisionReceipt(input: VerifyDecisionReceiptInput, options?: VerifyDecisionReceiptOptions): Promise<DeepReadonly<ReceiptClaims>>;
|
|
101
136
|
/**
|