@sess.si/sdk 0.0.0-stage → 0.1.0-rc.4
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 +25 -0
- package/LICENSE +201 -0
- package/NOTICE +9 -0
- package/README.md +136 -2
- package/dist/client.d.ts +12 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +34 -0
- package/dist/client.js.map +1 -0
- package/dist/errors.d.ts +40 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +52 -0
- package/dist/errors.js.map +1 -0
- package/dist/generated/openapi.d.ts +2462 -0
- package/dist/generated/openapi.d.ts.map +1 -0
- package/dist/generated/openapi.js +2 -0
- package/dist/generated/openapi.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/internal/decode.d.ts +21 -0
- package/dist/internal/decode.d.ts.map +1 -0
- package/dist/internal/decode.js +333 -0
- package/dist/internal/decode.js.map +1 -0
- package/dist/internal/json.d.ts +4 -0
- package/dist/internal/json.d.ts.map +1 -0
- package/dist/internal/json.js +75 -0
- package/dist/internal/json.js.map +1 -0
- package/dist/internal/time.d.ts +11 -0
- package/dist/internal/time.d.ts.map +1 -0
- package/dist/internal/time.js +71 -0
- package/dist/internal/time.js.map +1 -0
- package/dist/polling.d.ts +3 -0
- package/dist/polling.d.ts.map +1 -0
- package/dist/polling.js +61 -0
- package/dist/polling.js.map +1 -0
- package/dist/receipts/index.d.ts +5 -0
- package/dist/receipts/index.d.ts.map +1 -0
- package/dist/receipts/index.js +2 -0
- package/dist/receipts/index.js.map +1 -0
- package/dist/receipts/json.d.ts +22 -0
- package/dist/receipts/json.d.ts.map +1 -0
- package/dist/receipts/json.js +152 -0
- package/dist/receipts/json.js.map +1 -0
- package/dist/receipts/key-cache.d.ts +28 -0
- package/dist/receipts/key-cache.d.ts.map +1 -0
- package/dist/receipts/key-cache.js +110 -0
- package/dist/receipts/key-cache.js.map +1 -0
- package/dist/receipts/resource.d.ts +39 -0
- package/dist/receipts/resource.d.ts.map +1 -0
- package/dist/receipts/resource.js +119 -0
- package/dist/receipts/resource.js.map +1 -0
- package/dist/receipts/verify.d.ts +90 -0
- package/dist/receipts/verify.d.ts.map +1 -0
- package/dist/receipts/verify.js +294 -0
- package/dist/receipts/verify.js.map +1 -0
- package/dist/sessions.d.ts +19 -0
- package/dist/sessions.d.ts.map +1 -0
- package/dist/sessions.js +145 -0
- package/dist/sessions.js.map +1 -0
- package/dist/transport.d.ts +29 -0
- package/dist/transport.d.ts.map +1 -0
- package/dist/transport.js +447 -0
- package/dist/transport.js.map +1 -0
- package/dist/types.d.ts +149 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +9 -0
- package/dist/types.js.map +1 -0
- package/dist/webhook-resource.d.ts +13 -0
- package/dist/webhook-resource.d.ts.map +1 -0
- package/dist/webhook-resource.js +52 -0
- package/dist/webhook-resource.js.map +1 -0
- package/dist/webhooks/index.d.ts +22 -0
- package/dist/webhooks/index.d.ts.map +1 -0
- package/dist/webhooks/index.js +184 -0
- package/dist/webhooks/index.js.map +1 -0
- package/package.json +74 -4
- package/src/client.ts +41 -0
- package/src/errors.ts +96 -0
- package/src/generated/openapi.ts +2578 -0
- package/src/index.ts +4 -0
- package/src/internal/decode.ts +317 -0
- package/src/internal/json.ts +64 -0
- package/src/internal/time.ts +85 -0
- package/src/polling.ts +77 -0
- package/src/receipts/index.ts +16 -0
- package/src/receipts/json.ts +168 -0
- package/src/receipts/key-cache.ts +143 -0
- package/src/receipts/resource.ts +227 -0
- package/src/receipts/verify.ts +468 -0
- package/src/sessions.ts +227 -0
- package/src/transport.ts +524 -0
- package/src/types.ts +135 -0
- package/src/webhook-resource.ts +81 -0
- package/src/webhooks/index.ts +246 -0
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { Transport } from './transport.js';
|
|
2
|
+
import {
|
|
3
|
+
decodeWebhook,
|
|
4
|
+
decodeWebhookRegister,
|
|
5
|
+
decodeWebhookRotate,
|
|
6
|
+
decodeWebhookVerify,
|
|
7
|
+
} from './internal/decode.js';
|
|
8
|
+
import type {
|
|
9
|
+
ApiResult,
|
|
10
|
+
RequestOptions,
|
|
11
|
+
WebhookRegisterRequest,
|
|
12
|
+
WebhookRegisterResponse,
|
|
13
|
+
WebhookResponse,
|
|
14
|
+
WebhookRotateResponse,
|
|
15
|
+
WebhookVerifyResponse,
|
|
16
|
+
} from './types.js';
|
|
17
|
+
|
|
18
|
+
export class WebhookResource {
|
|
19
|
+
#transport: Transport;
|
|
20
|
+
constructor(transport: Transport) {
|
|
21
|
+
this.#transport = transport;
|
|
22
|
+
}
|
|
23
|
+
/** Replaces the workspace endpoint. Store any returned signing secret privately. */
|
|
24
|
+
register(
|
|
25
|
+
body: WebhookRegisterRequest,
|
|
26
|
+
options?: RequestOptions,
|
|
27
|
+
): Promise<ApiResult<WebhookRegisterResponse>> {
|
|
28
|
+
return this.#transport.request(
|
|
29
|
+
{
|
|
30
|
+
name: 'webhooks.register',
|
|
31
|
+
method: 'PUT',
|
|
32
|
+
path: '/v1/webhook',
|
|
33
|
+
body,
|
|
34
|
+
status: 201,
|
|
35
|
+
retry: 'never',
|
|
36
|
+
parse: decodeWebhookRegister,
|
|
37
|
+
},
|
|
38
|
+
options,
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
retrieve(options?: RequestOptions): Promise<ApiResult<WebhookResponse>> {
|
|
42
|
+
return this.#transport.request(
|
|
43
|
+
{
|
|
44
|
+
name: 'webhooks.retrieve',
|
|
45
|
+
method: 'GET',
|
|
46
|
+
path: '/v1/webhook',
|
|
47
|
+
status: 200,
|
|
48
|
+
retry: 'safe',
|
|
49
|
+
parse: decodeWebhook,
|
|
50
|
+
},
|
|
51
|
+
options,
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
verifyEndpoint(options?: RequestOptions): Promise<ApiResult<WebhookVerifyResponse>> {
|
|
55
|
+
return this.#transport.request(
|
|
56
|
+
{
|
|
57
|
+
name: 'webhooks.verifyEndpoint',
|
|
58
|
+
method: 'POST',
|
|
59
|
+
path: '/v1/webhook/verify',
|
|
60
|
+
status: 200,
|
|
61
|
+
retry: 'never',
|
|
62
|
+
parse: decodeWebhookVerify,
|
|
63
|
+
},
|
|
64
|
+
options,
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
/** Once-returned secret; never automatically retried. */
|
|
68
|
+
rotateSecret(options?: RequestOptions): Promise<ApiResult<WebhookRotateResponse>> {
|
|
69
|
+
return this.#transport.request(
|
|
70
|
+
{
|
|
71
|
+
name: 'webhooks.rotateSecret',
|
|
72
|
+
method: 'POST',
|
|
73
|
+
path: '/v1/webhook/secrets/rotate',
|
|
74
|
+
status: 201,
|
|
75
|
+
retry: 'never',
|
|
76
|
+
parse: decodeWebhookRotate,
|
|
77
|
+
},
|
|
78
|
+
options,
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import { Webhook } from 'standardwebhooks';
|
|
2
|
+
import { SessError } from '../errors.js';
|
|
3
|
+
import type { components } from '../generated/openapi.js';
|
|
4
|
+
import {
|
|
5
|
+
checkedInstant,
|
|
6
|
+
deepFreeze,
|
|
7
|
+
exactKeys,
|
|
8
|
+
isRecord,
|
|
9
|
+
snapshotJson,
|
|
10
|
+
wellFormedUnicode,
|
|
11
|
+
type DeepReadonly,
|
|
12
|
+
} from '../receipts/json.js';
|
|
13
|
+
|
|
14
|
+
export type WebhookEvent = components['schemas']['WebhookDelivery'];
|
|
15
|
+
export type WebhookChallenge = components['schemas']['WebhookVerification'];
|
|
16
|
+
export type VerifiedWebhook = DeepReadonly<WebhookEvent | WebhookChallenge>;
|
|
17
|
+
export type WebhookHeaders = Headers | Readonly<Record<string, string | readonly string[] | undefined>>;
|
|
18
|
+
|
|
19
|
+
export interface VerifyWebhookOptions {
|
|
20
|
+
/** Workspace-specific secrets from registration/rotation; store outside source control. */
|
|
21
|
+
secret?: string;
|
|
22
|
+
/** During rotation, either trusted secret can authenticate the overlapping signatures. */
|
|
23
|
+
secrets?: readonly string[];
|
|
24
|
+
/** 0–300 seconds; the maintained Standard Webhooks verifier sets the outer 300-second bound. */
|
|
25
|
+
toleranceSeconds?: number;
|
|
26
|
+
/** Defaults to 1 MiB; can be tightened, never increased beyond 1 MiB. */
|
|
27
|
+
maxBodyBytes?: number;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const MAX_BODY_BYTES = 1024 * 1024;
|
|
31
|
+
const MAX_HEADER_BYTES = 8192;
|
|
32
|
+
const MAX_SIGNATURES = 8;
|
|
33
|
+
const MAX_SECRETS = 4;
|
|
34
|
+
const SECRET = /^whsec_[A-Za-z0-9+/]{43}=$/;
|
|
35
|
+
const EVENT_ID = /^evt_[0-9A-HJKMNP-TV-Z]{26}$/;
|
|
36
|
+
const SESSION_ID = /^ses_[0-9A-HJKMNP-TV-Z]{26}$/;
|
|
37
|
+
|
|
38
|
+
function failure(code: string, message: string): SessError {
|
|
39
|
+
return new SessError('protocol', message, { code, operation: 'webhooks.verify' });
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function headersOf(headers: WebhookHeaders): Record<string, string> {
|
|
43
|
+
const wanted = new Set(['webhook-id', 'webhook-timestamp', 'webhook-signature']);
|
|
44
|
+
const result: Record<string, string> = {};
|
|
45
|
+
const seen = new Set<string>();
|
|
46
|
+
let bytes = 0;
|
|
47
|
+
const entries: Iterable<[string, unknown]> =
|
|
48
|
+
headers instanceof Headers ? headers.entries() : Object.entries(headers);
|
|
49
|
+
for (const [rawName, value] of entries) {
|
|
50
|
+
const name = rawName.toLowerCase();
|
|
51
|
+
if (!wanted.has(name)) continue; // sess-webhook-key-ids never grants authorization.
|
|
52
|
+
if (seen.has(name) || typeof value !== 'string' || /[\u0000-\u001f\u007f]/.test(value)) {
|
|
53
|
+
throw failure('WEBHOOK_INVALID_HEADERS', 'Webhook authentication headers are duplicated or malformed.');
|
|
54
|
+
}
|
|
55
|
+
seen.add(name);
|
|
56
|
+
bytes += Buffer.byteLength(name) + Buffer.byteLength(value);
|
|
57
|
+
if (bytes > MAX_HEADER_BYTES)
|
|
58
|
+
throw failure('WEBHOOK_INVALID_HEADERS', 'Webhook authentication headers exceed the safety limit.');
|
|
59
|
+
result[name] = value;
|
|
60
|
+
}
|
|
61
|
+
const id = result['webhook-id'];
|
|
62
|
+
const timestamp = result['webhook-timestamp'];
|
|
63
|
+
const signatures = result['webhook-signature'];
|
|
64
|
+
if (
|
|
65
|
+
!id ||
|
|
66
|
+
!/^[A-Za-z0-9._-]{1,128}$/.test(id) ||
|
|
67
|
+
!timestamp ||
|
|
68
|
+
!/^(?:0|[1-9][0-9]{0,11})$/.test(timestamp) ||
|
|
69
|
+
!signatures ||
|
|
70
|
+
signatures.length > 2048
|
|
71
|
+
) {
|
|
72
|
+
throw failure('WEBHOOK_INVALID_HEADERS', 'Webhook authentication headers are missing or malformed.');
|
|
73
|
+
}
|
|
74
|
+
const candidates = signatures.split(' ');
|
|
75
|
+
if (
|
|
76
|
+
candidates.length > MAX_SIGNATURES ||
|
|
77
|
+
candidates.some((candidate) => !/^v[1-9][0-9]*,[A-Za-z0-9+/]{43}=$/.test(candidate))
|
|
78
|
+
) {
|
|
79
|
+
throw failure(
|
|
80
|
+
'WEBHOOK_INVALID_HEADERS',
|
|
81
|
+
'Webhook signatures must use bounded, space-separated versioned signatures.',
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
// Headers combines repeated lines with a comma; exact grammar above rejects that form.
|
|
85
|
+
return result;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Authenticates exact raw UTF-8 bytes before parsing JSON. This is transport authentication,
|
|
90
|
+
* not decision-receipt verification or authorization to perform application business work.
|
|
91
|
+
*/
|
|
92
|
+
export function verifyWebhook(
|
|
93
|
+
rawBody: Uint8Array | string,
|
|
94
|
+
headers: WebhookHeaders,
|
|
95
|
+
options: VerifyWebhookOptions,
|
|
96
|
+
): VerifiedWebhook {
|
|
97
|
+
if (!options || !headers)
|
|
98
|
+
throw new SessError('configuration', 'Webhook verification requires headers and trusted-secret options.');
|
|
99
|
+
const tolerance = options?.toleranceSeconds ?? 300;
|
|
100
|
+
const maxBytes = options?.maxBodyBytes ?? MAX_BODY_BYTES;
|
|
101
|
+
if (
|
|
102
|
+
!Number.isFinite(tolerance) ||
|
|
103
|
+
tolerance < 0 ||
|
|
104
|
+
tolerance > 300 ||
|
|
105
|
+
!Number.isSafeInteger(maxBytes) ||
|
|
106
|
+
maxBytes < 1 ||
|
|
107
|
+
maxBytes > MAX_BODY_BYTES
|
|
108
|
+
) {
|
|
109
|
+
throw new SessError(
|
|
110
|
+
'configuration',
|
|
111
|
+
'Webhook tolerance must be 0–300 seconds and its byte limit 1–1048576.',
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
if ((options.secret === undefined) === (options.secrets === undefined)) {
|
|
115
|
+
throw new SessError(
|
|
116
|
+
'configuration',
|
|
117
|
+
'Provide exactly one webhook secret or a trusted rotation secret list.',
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
const secrets = options.secrets === undefined ? [options.secret] : [...options.secrets];
|
|
121
|
+
if (
|
|
122
|
+
secrets.length === 0 ||
|
|
123
|
+
secrets.length > MAX_SECRETS ||
|
|
124
|
+
secrets.some(
|
|
125
|
+
(secret) =>
|
|
126
|
+
typeof secret !== 'string' ||
|
|
127
|
+
!SECRET.test(secret) ||
|
|
128
|
+
Buffer.from(secret.slice(6), 'base64').length !== 32 ||
|
|
129
|
+
Buffer.from(secret.slice(6), 'base64').toString('base64') !== secret.slice(6),
|
|
130
|
+
)
|
|
131
|
+
) {
|
|
132
|
+
throw new SessError('configuration', 'Use the workspace webhook signing secret returned by SESS.');
|
|
133
|
+
}
|
|
134
|
+
let bytes: Buffer;
|
|
135
|
+
if (typeof rawBody === 'string') {
|
|
136
|
+
if (!wellFormedUnicode(rawBody))
|
|
137
|
+
throw failure('WEBHOOK_INVALID_ENCODING', 'Webhook bodies must be exact well-formed UTF-8.');
|
|
138
|
+
if (Buffer.byteLength(rawBody, 'utf8') > maxBytes)
|
|
139
|
+
throw failure('WEBHOOK_BODY_TOO_LARGE', 'Webhook body exceeds the configured byte limit.');
|
|
140
|
+
bytes = Buffer.from(rawBody, 'utf8');
|
|
141
|
+
} else if (rawBody instanceof Uint8Array) {
|
|
142
|
+
if (rawBody.byteLength > maxBytes)
|
|
143
|
+
throw failure('WEBHOOK_BODY_TOO_LARGE', 'Webhook body exceeds the configured byte limit.');
|
|
144
|
+
bytes = Buffer.from(rawBody);
|
|
145
|
+
} else {
|
|
146
|
+
throw failure('WEBHOOK_RAW_BODY_REQUIRED', 'Provide the original request bytes before JSON parsing.');
|
|
147
|
+
}
|
|
148
|
+
let body: string;
|
|
149
|
+
try {
|
|
150
|
+
body = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true }).decode(bytes);
|
|
151
|
+
} catch {
|
|
152
|
+
throw failure('WEBHOOK_INVALID_ENCODING', 'Webhook bodies must contain valid UTF-8 bytes.');
|
|
153
|
+
}
|
|
154
|
+
// Standard Webhooks accepts strings/Buffers and internally decodes text. This round trip
|
|
155
|
+
// guarantees that decoding cannot replace malformed bytes before signature verification.
|
|
156
|
+
if (!Buffer.from(body, 'utf8').equals(bytes))
|
|
157
|
+
throw failure('WEBHOOK_INVALID_ENCODING', 'Webhook UTF-8 decoding changed its signed bytes.');
|
|
158
|
+
const verifiedHeaders = headersOf(headers);
|
|
159
|
+
const timestamp = Number(verifiedHeaders['webhook-timestamp']);
|
|
160
|
+
if (Math.abs(Math.floor(Date.now() / 1000) - timestamp) > tolerance) {
|
|
161
|
+
throw failure(
|
|
162
|
+
'WEBHOOK_TIMESTAMP_OUT_OF_RANGE',
|
|
163
|
+
'Webhook timestamp is outside the configured replay tolerance.',
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
let authenticated = false;
|
|
167
|
+
for (const secret of secrets) {
|
|
168
|
+
try {
|
|
169
|
+
new Webhook(secret as string).verify(bytes, verifiedHeaders, { jsonParse: false });
|
|
170
|
+
authenticated = true;
|
|
171
|
+
break;
|
|
172
|
+
} catch {
|
|
173
|
+
// Neither dependency error messages nor secret/signature material reaches callers.
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
if (!authenticated)
|
|
177
|
+
throw failure(
|
|
178
|
+
'WEBHOOK_INVALID_SIGNATURE',
|
|
179
|
+
'Webhook authentication failed for the trusted workspace secrets.',
|
|
180
|
+
);
|
|
181
|
+
let parsed: unknown;
|
|
182
|
+
try {
|
|
183
|
+
parsed = JSON.parse(body);
|
|
184
|
+
} catch {
|
|
185
|
+
throw failure('WEBHOOK_INVALID_PAYLOAD', 'The authenticated webhook body is not JSON.');
|
|
186
|
+
}
|
|
187
|
+
let value: unknown;
|
|
188
|
+
try {
|
|
189
|
+
value = snapshotJson(parsed);
|
|
190
|
+
} catch {
|
|
191
|
+
throw failure('WEBHOOK_INVALID_PAYLOAD', 'The authenticated webhook body is not supported JSON.');
|
|
192
|
+
}
|
|
193
|
+
if (!isRecord(value))
|
|
194
|
+
throw failure('WEBHOOK_INVALID_PAYLOAD', 'The authenticated webhook payload must be an object.');
|
|
195
|
+
const id = verifiedHeaders['webhook-id'];
|
|
196
|
+
if (value.type === 'sess.webhook.verification') {
|
|
197
|
+
if (
|
|
198
|
+
!exactKeys(value, ['type', 'challenge']) ||
|
|
199
|
+
typeof value.challenge !== 'string' ||
|
|
200
|
+
Buffer.byteLength(value.challenge, 'utf8') > 1024
|
|
201
|
+
) {
|
|
202
|
+
throw failure(
|
|
203
|
+
'WEBHOOK_INVALID_CHALLENGE',
|
|
204
|
+
'The webhook challenge is malformed or exceeds its safety limit.',
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
return deepFreeze(value) as DeepReadonly<WebhookChallenge>;
|
|
208
|
+
}
|
|
209
|
+
if (
|
|
210
|
+
value.type !== 'response.accepted' ||
|
|
211
|
+
!exactKeys(value, [
|
|
212
|
+
'type',
|
|
213
|
+
'event_id',
|
|
214
|
+
'session_id',
|
|
215
|
+
'sequence',
|
|
216
|
+
'revision',
|
|
217
|
+
'action_id',
|
|
218
|
+
'data',
|
|
219
|
+
'accepted_at',
|
|
220
|
+
'receipt',
|
|
221
|
+
]) ||
|
|
222
|
+
typeof value.event_id !== 'string' ||
|
|
223
|
+
!EVENT_ID.test(value.event_id) ||
|
|
224
|
+
value.event_id !== id ||
|
|
225
|
+
typeof value.session_id !== 'string' ||
|
|
226
|
+
!SESSION_ID.test(value.session_id) ||
|
|
227
|
+
!Number.isSafeInteger(value.sequence) ||
|
|
228
|
+
(value.sequence as number) < 1 ||
|
|
229
|
+
!Number.isSafeInteger(value.revision) ||
|
|
230
|
+
(value.revision as number) < 1 ||
|
|
231
|
+
typeof value.action_id !== 'string' ||
|
|
232
|
+
!/^[a-z][a-z0-9_]{0,31}$/.test(value.action_id) ||
|
|
233
|
+
!isRecord(value.data) ||
|
|
234
|
+
checkedInstant(value.accepted_at) === undefined ||
|
|
235
|
+
(value.receipt !== null &&
|
|
236
|
+
(typeof value.receipt !== 'string' ||
|
|
237
|
+
value.receipt.length > 64 * 1024 ||
|
|
238
|
+
!/^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+$/.test(value.receipt)))
|
|
239
|
+
) {
|
|
240
|
+
throw failure(
|
|
241
|
+
'WEBHOOK_INVALID_PAYLOAD',
|
|
242
|
+
'The accepted webhook event is malformed or does not match its transport identity.',
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
return deepFreeze(value) as unknown as DeepReadonly<WebhookEvent>;
|
|
246
|
+
}
|