@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/cli/verify.ts
DELETED
|
@@ -1,349 +0,0 @@
|
|
|
1
|
-
// `sess verify <file>`: the CLI face of decision-bundle verification. Offline only: it reads the
|
|
2
|
-
// bundle and the keys the caller trusts from local files, verifies with verifyDecisionBundle and
|
|
3
|
-
// prints a short summary (or the result object with --json). It opens no network connection and
|
|
4
|
-
// never prints the receipt, the disclosed content or any key material.
|
|
5
|
-
import { readFile, stat } from 'node:fs/promises';
|
|
6
|
-
import { parseArgs } from 'node:util';
|
|
7
|
-
import {
|
|
8
|
-
verifyDecisionBundle,
|
|
9
|
-
type DecisionBundleVerification,
|
|
10
|
-
type ExpectedBundleContext,
|
|
11
|
-
} from '../receipts/bundle.js';
|
|
12
|
-
import { BUNDLED_RENDERER_IDENTITIES } from '../receipts/renderers.js';
|
|
13
|
-
import { validateKeySet, verifyRendererIdentities } from '../receipts/verify.js';
|
|
14
|
-
|
|
15
|
-
/** Exit codes: valid, invalid bundle, usage or file error. */
|
|
16
|
-
export const EXIT = Object.freeze({ valid: 0, invalid: 1, usage: 2 });
|
|
17
|
-
|
|
18
|
-
/** A bundle file larger than this is not read (the bundle bound itself is 1 MiB canonical). */
|
|
19
|
-
const MAX_BUNDLE_FILE_BYTES = 16 * 1024 * 1024;
|
|
20
|
-
const MAX_KEYS_FILE_BYTES = 1024 * 1024;
|
|
21
|
-
const MAX_RENDERERS_FILE_BYTES = 64 * 1024;
|
|
22
|
-
|
|
23
|
-
export interface CliIo {
|
|
24
|
-
stdout: (text: string) => void;
|
|
25
|
-
stderr: (text: string) => void;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export const USAGE = `Usage: sess verify <bundle.json> --issuer <url> --keys <jwks.json> [options]
|
|
29
|
-
|
|
30
|
-
Checks a Sess decision bundle on this computer against the issuer and keys you trust.
|
|
31
|
-
Nothing is sent anywhere. Keys inside the bundle are never trusted.
|
|
32
|
-
|
|
33
|
-
Required:
|
|
34
|
-
--issuer <url> the receipt issuer you trust, for example https://api.sess.si
|
|
35
|
-
--keys <file> that issuer's public keys (a JWK Set) you obtained yourself
|
|
36
|
-
|
|
37
|
-
Expected context (optional; only what you pass is checked):
|
|
38
|
-
--audience <id> the workspace the decision must belong to
|
|
39
|
-
--workspace <id> the workspace ID
|
|
40
|
-
--session <id> the Session ID
|
|
41
|
-
--event <id> the Event ID
|
|
42
|
-
--revision <n> the Surface revision
|
|
43
|
-
--action <id> the action ID
|
|
44
|
-
|
|
45
|
-
Renderer identity (optional):
|
|
46
|
-
--renderers <file> the issuer's signed renderer list you obtained yourself
|
|
47
|
-
(<issuer>/.well-known/sess-renderer-identities.json); also checks that the
|
|
48
|
-
receipt's renderer identity is one this release knows or one on that list
|
|
49
|
-
|
|
50
|
-
Other:
|
|
51
|
-
--now <time> check at this RFC 3339 time instead of now
|
|
52
|
-
--json print the full result as JSON
|
|
53
|
-
-h, --help show this help
|
|
54
|
-
|
|
55
|
-
Exit status: 0 valid, 1 invalid, 2 usage or file error.
|
|
56
|
-
`;
|
|
57
|
-
|
|
58
|
-
class UsageError extends Error {}
|
|
59
|
-
|
|
60
|
-
const STRING_OPTIONS = [
|
|
61
|
-
'issuer',
|
|
62
|
-
'keys',
|
|
63
|
-
'audience',
|
|
64
|
-
'workspace',
|
|
65
|
-
'session',
|
|
66
|
-
'event',
|
|
67
|
-
'revision',
|
|
68
|
-
'action',
|
|
69
|
-
'now',
|
|
70
|
-
'renderers',
|
|
71
|
-
] as const;
|
|
72
|
-
|
|
73
|
-
const RFC3339 = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{1,9})?(?:Z|[+-]\d{2}:\d{2})$/;
|
|
74
|
-
|
|
75
|
-
/** Escapes characters a terminal could interpret (controls, bidi overrides, line separators). */
|
|
76
|
-
function safe(text: string): string {
|
|
77
|
-
return text.replace(
|
|
78
|
-
/[\u0000-\u001f\u007f-\u009f\u200e\u200f\u2028\u2029\u202a-\u202e\u2066-\u2069]/g,
|
|
79
|
-
(c) => `\\u${c.charCodeAt(0).toString(16).padStart(4, '0')}`,
|
|
80
|
-
);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
async function readBounded(path: string, max: number, what: string): Promise<Buffer> {
|
|
84
|
-
let size: number;
|
|
85
|
-
try {
|
|
86
|
-
const info = await stat(path);
|
|
87
|
-
if (!info.isFile()) throw new UsageError(`${what} is not a file: ${safe(path)}`);
|
|
88
|
-
size = info.size;
|
|
89
|
-
} catch (e) {
|
|
90
|
-
if (e instanceof UsageError) throw e;
|
|
91
|
-
throw new UsageError(`cannot read ${what}: ${safe(path)}`);
|
|
92
|
-
}
|
|
93
|
-
if (size > max) throw new UsageError(`${what} is larger than ${max} bytes: ${safe(path)}`);
|
|
94
|
-
try {
|
|
95
|
-
return await readFile(path);
|
|
96
|
-
} catch {
|
|
97
|
-
throw new UsageError(`cannot read ${what}: ${safe(path)}`);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
function parseJson(bytes: Buffer): { ok: true; value: unknown } | { ok: false } {
|
|
102
|
-
try {
|
|
103
|
-
const text = new TextDecoder('utf-8', { fatal: true }).decode(bytes);
|
|
104
|
-
return { ok: true, value: JSON.parse(text) as unknown };
|
|
105
|
-
} catch {
|
|
106
|
-
return { ok: false };
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
interface Parsed {
|
|
111
|
-
file: string;
|
|
112
|
-
issuer: string;
|
|
113
|
-
keysPath: string;
|
|
114
|
-
renderersPath: string | undefined;
|
|
115
|
-
context: ExpectedBundleContext | undefined;
|
|
116
|
-
now: Date | undefined;
|
|
117
|
-
json: boolean;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
function parse(argv: readonly string[]): Parsed | 'help' {
|
|
121
|
-
let parsed: ReturnType<typeof parseArgs>;
|
|
122
|
-
try {
|
|
123
|
-
parsed = parseArgs({
|
|
124
|
-
args: [...argv],
|
|
125
|
-
allowPositionals: true,
|
|
126
|
-
strict: true,
|
|
127
|
-
tokens: true,
|
|
128
|
-
options: {
|
|
129
|
-
...Object.fromEntries(STRING_OPTIONS.map((name) => [name, { type: 'string' as const }])),
|
|
130
|
-
json: { type: 'boolean' },
|
|
131
|
-
help: { type: 'boolean', short: 'h' },
|
|
132
|
-
},
|
|
133
|
-
});
|
|
134
|
-
} catch {
|
|
135
|
-
throw new UsageError('unknown option or missing option value');
|
|
136
|
-
}
|
|
137
|
-
const values = parsed.values as Record<string, string | boolean | undefined>;
|
|
138
|
-
if (values.help) return 'help';
|
|
139
|
-
// A trust setting given twice is ambiguous: refuse rather than pick one.
|
|
140
|
-
const seen = new Set<string>();
|
|
141
|
-
for (const token of parsed.tokens ?? []) {
|
|
142
|
-
if (token.kind !== 'option') continue;
|
|
143
|
-
if (seen.has(token.name)) throw new UsageError(`--${token.name} is given more than once`);
|
|
144
|
-
seen.add(token.name);
|
|
145
|
-
}
|
|
146
|
-
const [command, file, ...rest] = parsed.positionals;
|
|
147
|
-
if (command !== 'verify') throw new UsageError('the only command is: verify <bundle.json>');
|
|
148
|
-
if (file === undefined || rest.length > 0) throw new UsageError('give exactly one bundle file');
|
|
149
|
-
const str = (name: (typeof STRING_OPTIONS)[number]) => {
|
|
150
|
-
const v = values[name];
|
|
151
|
-
if (v === undefined) return undefined;
|
|
152
|
-
if (typeof v !== 'string' || v.length === 0) throw new UsageError(`--${name} needs a value`);
|
|
153
|
-
return v;
|
|
154
|
-
};
|
|
155
|
-
const issuer = str('issuer');
|
|
156
|
-
const keysPath = str('keys');
|
|
157
|
-
if (issuer === undefined || keysPath === undefined)
|
|
158
|
-
throw new UsageError('--issuer and --keys are required: verification only uses keys you trust');
|
|
159
|
-
const revisionText = str('revision');
|
|
160
|
-
let revision: number | undefined;
|
|
161
|
-
if (revisionText !== undefined) {
|
|
162
|
-
revision = Number(revisionText);
|
|
163
|
-
if (!/^[1-9][0-9]*$/.test(revisionText) || !Number.isSafeInteger(revision))
|
|
164
|
-
throw new UsageError('--revision must be a positive integer');
|
|
165
|
-
}
|
|
166
|
-
const nowText = str('now');
|
|
167
|
-
let now: Date | undefined;
|
|
168
|
-
if (nowText !== undefined) {
|
|
169
|
-
now = new Date(nowText);
|
|
170
|
-
if (!RFC3339.test(nowText) || Number.isNaN(now.getTime()))
|
|
171
|
-
throw new UsageError('--now must be an RFC 3339 time, for example 2026-10-09T12:05:00Z');
|
|
172
|
-
}
|
|
173
|
-
const context: ExpectedBundleContext = {};
|
|
174
|
-
const audience = str('audience');
|
|
175
|
-
const workspaceId = str('workspace');
|
|
176
|
-
const sessionId = str('session');
|
|
177
|
-
const eventId = str('event');
|
|
178
|
-
const actionId = str('action');
|
|
179
|
-
if (audience !== undefined) context.audience = audience;
|
|
180
|
-
if (workspaceId !== undefined) context.workspaceId = workspaceId;
|
|
181
|
-
if (sessionId !== undefined) context.sessionId = sessionId;
|
|
182
|
-
if (eventId !== undefined) context.eventId = eventId;
|
|
183
|
-
if (revision !== undefined) context.revision = revision;
|
|
184
|
-
if (actionId !== undefined) context.actionId = actionId;
|
|
185
|
-
return {
|
|
186
|
-
file,
|
|
187
|
-
issuer,
|
|
188
|
-
keysPath,
|
|
189
|
-
renderersPath: str('renderers'),
|
|
190
|
-
context: Object.keys(context).length > 0 ? context : undefined,
|
|
191
|
-
now,
|
|
192
|
-
json: values.json === true,
|
|
193
|
-
};
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
const REASON_TEXT: Record<string, string> = {
|
|
197
|
-
malformed: 'the file is not a bundle-v1 decision bundle',
|
|
198
|
-
oversized: 'the bundle exceeds a bundle-v1 size bound',
|
|
199
|
-
unsupported_version: 'the bundle uses a later format version',
|
|
200
|
-
receipt_malformed: 'the receipt cannot be read',
|
|
201
|
-
algorithm: 'the receipt does not use the EdDSA algorithm',
|
|
202
|
-
missing_kid: 'the receipt names no key',
|
|
203
|
-
unknown_kid: 'the receipt was signed with a key you do not trust',
|
|
204
|
-
type: 'the receipt has the wrong type',
|
|
205
|
-
signature: 'the signature does not verify with the key you trust',
|
|
206
|
-
issuer: 'the receipt is from another issuer',
|
|
207
|
-
audience: 'the receipt belongs to another workspace',
|
|
208
|
-
expired: 'the receipt has expired',
|
|
209
|
-
claims: 'the receipt claims are not valid receipt-v1 claims',
|
|
210
|
-
not_yet_valid: 'the receipt is dated in the future',
|
|
211
|
-
surface_mismatch: 'the disclosed Surface is not the one the receipt signed',
|
|
212
|
-
input_mismatch: 'the disclosed input is not the one the receipt signed',
|
|
213
|
-
context_mismatch: 'the decision is not the one you expected',
|
|
214
|
-
renderer: 'the receipt names a renderer identity that is neither known nor on the signed list',
|
|
215
|
-
};
|
|
216
|
-
|
|
217
|
-
/** With --renderers: the receipt's renderer identity and whether it is known or listed. */
|
|
218
|
-
interface RendererCheck {
|
|
219
|
-
identity: string | null;
|
|
220
|
-
status: 'listed' | 'unlisted' | 'not_checked';
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
function summary(r: DecisionBundleVerification, renderer?: RendererCheck): string {
|
|
224
|
-
const lines: string[] = [];
|
|
225
|
-
const row = (label: string, value: string) => lines.push(` ${label.padEnd(11)} ${value}`);
|
|
226
|
-
const context =
|
|
227
|
-
r.context.status === 'mismatch'
|
|
228
|
-
? `mismatch: ${r.context.mismatches.join(', ')}`
|
|
229
|
-
: r.context.status === 'unchecked'
|
|
230
|
-
? 'unchecked (no expected context given)'
|
|
231
|
-
: r.context.checked.length > 0
|
|
232
|
-
? `${r.context.status}: ${r.context.checked.join(', ')}`
|
|
233
|
-
: r.context.status;
|
|
234
|
-
if (r.valid && r.receipt) {
|
|
235
|
-
const c = r.receipt;
|
|
236
|
-
lines.push('Valid decision bundle.');
|
|
237
|
-
row('Accepted', `${safe(c.accepted_at)}, action ${safe(c.action.id)} on revision ${c.surface.revision}`);
|
|
238
|
-
row('Session', `${safe(c.session_id)} (event ${safe(c.event_id)}, workspace ${safe(c.workspace_id)})`);
|
|
239
|
-
row('Issuer', `${safe(c.issuer)} (key ${safe(c.key_id)})`);
|
|
240
|
-
row('Actor', "someone holding the Session's human link (not a verified person)");
|
|
241
|
-
row('Content', 'verified');
|
|
242
|
-
row('Context', context);
|
|
243
|
-
row('Purpose', r.application_purpose);
|
|
244
|
-
row('Expires', safe(c.expires_at));
|
|
245
|
-
if (renderer) row('Renderer', `${safe(c.surface.renderer_version)} (listed)`);
|
|
246
|
-
} else {
|
|
247
|
-
lines.push(`Invalid decision bundle: ${r.reason}`);
|
|
248
|
-
lines.push(` ${REASON_TEXT[r.reason ?? ''] ?? ''}`);
|
|
249
|
-
row('Content', r.content);
|
|
250
|
-
row('Context', context);
|
|
251
|
-
if (renderer?.identity) row('Renderer', `${safe(renderer.identity)} (${renderer.status})`);
|
|
252
|
-
}
|
|
253
|
-
row('Checked at', r.checked_at);
|
|
254
|
-
return `${lines.join('\n')}\n`;
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
/**
|
|
258
|
-
* JSON with every terminal-sensitive character escaped; parses to the same value. JSON.stringify
|
|
259
|
-
* already escapes C0 controls inside strings, and the only raw characters it can leave are inside
|
|
260
|
-
* strings, where a \u escape denotes the same character.
|
|
261
|
-
*/
|
|
262
|
-
function safeJson(value: unknown): string {
|
|
263
|
-
const text = JSON.stringify(value, null, 2).replace(
|
|
264
|
-
/[\u007f-\u009f\u200e\u200f\u2028\u2029\u202a-\u202e\u2066-\u2069]/g,
|
|
265
|
-
(c) => `\\u${c.charCodeAt(0).toString(16).padStart(4, '0')}`,
|
|
266
|
-
);
|
|
267
|
-
return `${text}\n`;
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
/** Runs `sess <argv>`; resolves to the exit code. Never throws. */
|
|
271
|
-
export async function runCli(argv: readonly string[], io: CliIo): Promise<number> {
|
|
272
|
-
try {
|
|
273
|
-
const parsed = parse(argv);
|
|
274
|
-
if (parsed === 'help') {
|
|
275
|
-
io.stdout(USAGE);
|
|
276
|
-
return EXIT.valid;
|
|
277
|
-
}
|
|
278
|
-
const keyBytes = await readBounded(parsed.keysPath, MAX_KEYS_FILE_BYTES, 'the --keys file');
|
|
279
|
-
const keyJson = parseJson(keyBytes);
|
|
280
|
-
let trustedKeys;
|
|
281
|
-
try {
|
|
282
|
-
if (!keyJson.ok) throw new Error('not JSON');
|
|
283
|
-
trustedKeys = validateKeySet(keyJson.value);
|
|
284
|
-
} catch {
|
|
285
|
-
throw new UsageError(
|
|
286
|
-
'the --keys file must be a JWK Set of public Ed25519 keys with unique kids (private keys are refused)',
|
|
287
|
-
);
|
|
288
|
-
}
|
|
289
|
-
// Decision 0009 Amendment 13: the issuer's signed renderer list, trusted only through its
|
|
290
|
-
// signature under the keys above.
|
|
291
|
-
let listed: readonly string[] | undefined;
|
|
292
|
-
if (parsed.renderersPath !== undefined) {
|
|
293
|
-
const listJson = parseJson(
|
|
294
|
-
await readBounded(parsed.renderersPath, MAX_RENDERERS_FILE_BYTES, 'the --renderers file'),
|
|
295
|
-
);
|
|
296
|
-
try {
|
|
297
|
-
if (!listJson.ok) throw new Error('not JSON');
|
|
298
|
-
listed = (
|
|
299
|
-
await verifyRendererIdentities(listJson.value as never, {
|
|
300
|
-
issuer: parsed.issuer,
|
|
301
|
-
keys: trustedKeys,
|
|
302
|
-
})
|
|
303
|
-
).identities.map((i) => i.renderer_version);
|
|
304
|
-
} catch {
|
|
305
|
-
throw new UsageError(
|
|
306
|
-
"the --renderers file must be the issuer's renderer list, signed with the keys you trust",
|
|
307
|
-
);
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
const bundleBytes = await readBounded(parsed.file, MAX_BUNDLE_FILE_BYTES, 'the bundle file');
|
|
311
|
-
const bundleJson = parseJson(bundleBytes);
|
|
312
|
-
let result: DecisionBundleVerification;
|
|
313
|
-
try {
|
|
314
|
-
// A file that is not JSON is not a bundle: the same facade reports it as malformed.
|
|
315
|
-
result = await verifyDecisionBundle(bundleJson.ok ? bundleJson.value : undefined, {
|
|
316
|
-
issuer: parsed.issuer,
|
|
317
|
-
trustedKeys,
|
|
318
|
-
...(parsed.context ? { expectedContext: parsed.context } : {}),
|
|
319
|
-
...(parsed.now ? { now: parsed.now } : {}),
|
|
320
|
-
});
|
|
321
|
-
} catch {
|
|
322
|
-
throw new UsageError('invalid verification options');
|
|
323
|
-
}
|
|
324
|
-
let renderer: RendererCheck | undefined;
|
|
325
|
-
if (listed !== undefined) {
|
|
326
|
-
const identity = result.receipt?.surface.renderer_version ?? null;
|
|
327
|
-
renderer =
|
|
328
|
-
identity === null
|
|
329
|
-
? { identity, status: 'not_checked' }
|
|
330
|
-
: {
|
|
331
|
-
identity,
|
|
332
|
-
status:
|
|
333
|
-
BUNDLED_RENDERER_IDENTITIES.includes(identity) || listed.includes(identity)
|
|
334
|
-
? 'listed'
|
|
335
|
-
: 'unlisted',
|
|
336
|
-
};
|
|
337
|
-
if (renderer.status === 'unlisted')
|
|
338
|
-
result = { ...result, valid: false, reason: 'renderer' as DecisionBundleVerification['reason'] };
|
|
339
|
-
}
|
|
340
|
-
io.stdout(
|
|
341
|
-
parsed.json ? safeJson(renderer ? { ...result, renderer } : result) : summary(result, renderer),
|
|
342
|
-
);
|
|
343
|
-
return result.valid ? EXIT.valid : EXIT.invalid;
|
|
344
|
-
} catch (e) {
|
|
345
|
-
const message = e instanceof UsageError ? e.message : 'unexpected error';
|
|
346
|
-
io.stderr(`sess: ${message}\nRun "sess --help" for usage.\n`);
|
|
347
|
-
return EXIT.usage;
|
|
348
|
-
}
|
|
349
|
-
}
|
package/src/client.ts
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import type { SessOptions } from './types.js';
|
|
2
|
-
import { Transport } from './transport.js';
|
|
3
|
-
import { Sessions } from './sessions.js';
|
|
4
|
-
import { WebhookResource } from './webhook-resource.js';
|
|
5
|
-
import { createReceiptResource } from './receipts/resource.js';
|
|
6
|
-
import { ask, type AskAnswer, type AskField, type AskHandle, type AskInput, type AskOptions } from './ask.js';
|
|
7
|
-
|
|
8
|
-
/** Server-side client. Construction has no network or installation side effects. */
|
|
9
|
-
export class Sess {
|
|
10
|
-
readonly sessions: Sessions;
|
|
11
|
-
readonly webhooks: WebhookResource;
|
|
12
|
-
readonly receipts: ReturnType<typeof createReceiptResource>;
|
|
13
|
-
|
|
14
|
-
constructor(options: SessOptions) {
|
|
15
|
-
const transport = new Transport(options);
|
|
16
|
-
this.sessions = new Sessions(transport);
|
|
17
|
-
this.webhooks = new WebhookResource(transport);
|
|
18
|
-
this.receipts = createReceiptResource({
|
|
19
|
-
baseUrl: transport.baseUrl,
|
|
20
|
-
receiptIssuer: transport.receiptIssuer,
|
|
21
|
-
expectedWorkspaceId: transport.expectedWorkspaceId,
|
|
22
|
-
operationTimeoutMs: transport.operationTimeoutMs,
|
|
23
|
-
retrieveSession: (id, requestOptions) => this.sessions.retrieve(id, requestOptions),
|
|
24
|
-
retrieveSurface: (id, revision, requestOptions) =>
|
|
25
|
-
this.sessions.retrieveSurface(id, revision, requestOptions),
|
|
26
|
-
publicGet: (path, requestOptions) =>
|
|
27
|
-
transport.request(
|
|
28
|
-
{
|
|
29
|
-
name: 'receipts.keys',
|
|
30
|
-
method: 'GET',
|
|
31
|
-
path,
|
|
32
|
-
status: 200,
|
|
33
|
-
retry: 'safe',
|
|
34
|
-
authenticated: false,
|
|
35
|
-
parse: (value) => value,
|
|
36
|
-
},
|
|
37
|
-
requestOptions,
|
|
38
|
-
),
|
|
39
|
-
});
|
|
40
|
-
Object.freeze(this);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Asks one question with simple typed fields. Invalid input fails before credential lookup or
|
|
45
|
-
* network access. The Session is created once with the whole interaction, and the returned
|
|
46
|
-
* handle carries the private link before any wait. `answer()` verifies the answer, checks that it
|
|
47
|
-
* is still current, completes the Session and returns the typed values.
|
|
48
|
-
*/
|
|
49
|
-
ask<const F extends readonly AskField[]>(
|
|
50
|
-
input: AskInput<F>,
|
|
51
|
-
options?: AskOptions,
|
|
52
|
-
): Promise<AskHandle<AskAnswer<F>>> {
|
|
53
|
-
return ask<AskAnswer<F>>(this.sessions, this.receipts, input, options);
|
|
54
|
-
}
|
|
55
|
-
}
|
package/src/errors.ts
DELETED
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
import { inspect } from 'node:util';
|
|
2
|
-
import type { CreateSessionResponse } from './types.js';
|
|
3
|
-
|
|
4
|
-
export type SessErrorKind =
|
|
5
|
-
| 'configuration'
|
|
6
|
-
| 'api'
|
|
7
|
-
| 'transport'
|
|
8
|
-
| 'timeout'
|
|
9
|
-
| 'aborted'
|
|
10
|
-
| 'protocol'
|
|
11
|
-
| 'unknown_outcome'
|
|
12
|
-
| 'receipt_verification'
|
|
13
|
-
| 'wait_ended';
|
|
14
|
-
export type WriteOutcome = 'not_sent' | 'rejected' | 'unknown';
|
|
15
|
-
export interface SessErrorDetails {
|
|
16
|
-
code?: string;
|
|
17
|
-
reason?: string;
|
|
18
|
-
status?: number;
|
|
19
|
-
requestId?: string;
|
|
20
|
-
operation?: string;
|
|
21
|
-
attempts?: number;
|
|
22
|
-
outcome?: WriteOutcome;
|
|
23
|
-
idempotencyKey?: string;
|
|
24
|
-
retryAfterMs?: number;
|
|
25
|
-
sessionId?: string;
|
|
26
|
-
revision?: number;
|
|
27
|
-
/** With a length error: a JSON pointer to the value that is too long, e.g. `/interaction/title`. */
|
|
28
|
-
field?: string;
|
|
29
|
-
/** With a length error: the most the value may have, in Unicode code points. */
|
|
30
|
-
limit?: number;
|
|
31
|
-
/** With a length error: the value's actual length, in Unicode code points. */
|
|
32
|
-
length?: number;
|
|
33
|
-
}
|
|
34
|
-
const recovery = new WeakMap<SessError, Readonly<CreateSessionResponse>>();
|
|
35
|
-
|
|
36
|
-
/** Stable error fields; messages never include response bodies or request secrets. */
|
|
37
|
-
export class SessError extends Error {
|
|
38
|
-
readonly kind: SessErrorKind;
|
|
39
|
-
readonly code?: string;
|
|
40
|
-
readonly reason?: string;
|
|
41
|
-
readonly status?: number;
|
|
42
|
-
readonly requestId?: string;
|
|
43
|
-
readonly operation?: string;
|
|
44
|
-
readonly attempts?: number;
|
|
45
|
-
readonly outcome?: WriteOutcome;
|
|
46
|
-
readonly idempotencyKey?: string;
|
|
47
|
-
readonly retryAfterMs?: number;
|
|
48
|
-
readonly sessionId?: string;
|
|
49
|
-
readonly revision?: number;
|
|
50
|
-
/**
|
|
51
|
-
* Set only when a value is too long (a 400 INVALID_SCHEMA from Sess, or INVALID_QUESTION from
|
|
52
|
-
* `ask`): a JSON pointer to it, e.g. `/interaction/title`. Nothing is ever truncated: shorten
|
|
53
|
-
* the value to at most `limit` characters and send again.
|
|
54
|
-
*/
|
|
55
|
-
readonly field?: string;
|
|
56
|
-
/** With `field`: the most the value may have, in Unicode code points (e.g. title 80). */
|
|
57
|
-
readonly limit?: number;
|
|
58
|
-
/** With `field`: the value's actual length, in Unicode code points. */
|
|
59
|
-
readonly length?: number;
|
|
60
|
-
|
|
61
|
-
constructor(kind: SessErrorKind, message: string, details: SessErrorDetails = {}) {
|
|
62
|
-
super(message);
|
|
63
|
-
this.name = 'SessError';
|
|
64
|
-
this.kind = kind;
|
|
65
|
-
Object.assign(this, details);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/** Explicit access only. A usable create body may accompany a defective header. */
|
|
69
|
-
getRecoveryResult(): Readonly<CreateSessionResponse> | undefined {
|
|
70
|
-
return recovery.get(this);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
toJSON(): object {
|
|
74
|
-
const {
|
|
75
|
-
name,
|
|
76
|
-
kind,
|
|
77
|
-
message,
|
|
78
|
-
code,
|
|
79
|
-
reason,
|
|
80
|
-
status,
|
|
81
|
-
requestId,
|
|
82
|
-
operation,
|
|
83
|
-
attempts,
|
|
84
|
-
outcome,
|
|
85
|
-
idempotencyKey,
|
|
86
|
-
retryAfterMs,
|
|
87
|
-
field,
|
|
88
|
-
limit,
|
|
89
|
-
length,
|
|
90
|
-
} = this;
|
|
91
|
-
return {
|
|
92
|
-
name,
|
|
93
|
-
kind,
|
|
94
|
-
message,
|
|
95
|
-
code,
|
|
96
|
-
reason,
|
|
97
|
-
status,
|
|
98
|
-
requestId,
|
|
99
|
-
operation,
|
|
100
|
-
attempts,
|
|
101
|
-
outcome,
|
|
102
|
-
idempotencyKey,
|
|
103
|
-
retryAfterMs,
|
|
104
|
-
field,
|
|
105
|
-
limit,
|
|
106
|
-
length,
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
[inspect.custom](): object {
|
|
111
|
-
return this.toJSON();
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/** Internal: the one-line fix every length error carries (field, limit, actual length, fix). */
|
|
116
|
-
export function lengthMessage(field: string, limit: number, length: number): string {
|
|
117
|
-
// The same sentences as Sess's 400 INVALID_SCHEMA (`field` is a JSON pointer); the title (the
|
|
118
|
-
// `sess.ask()` question) has its own.
|
|
119
|
-
if (field === '/interaction/title' || field === '/question')
|
|
120
|
-
return `The title can be at most ${limit} characters. Keep it to a short heading, about 40.`;
|
|
121
|
-
return `${field} is ${length} characters; the limit is ${limit}. Shorten it to at most ${limit} characters.`;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/** Internal: keep private creation capabilities out of inspection and serialization. */
|
|
125
|
-
export function attachRecovery(error: SessError, value: CreateSessionResponse): void {
|
|
126
|
-
recovery.set(error, Object.freeze({ ...value }));
|
|
127
|
-
}
|