@cogover/sdk 0.12.0 → 0.12.2
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/README.md +12 -9
- package/dist/crypto.js +182 -69
- package/dist/data.js +4 -1
- package/dist/email.js +5 -3
- package/dist/internal/base64.js +92 -28
- package/dist/internal/bridge.js +41 -3
- package/dist/internal/job-config.js +7 -1
- package/dist/internal/utf8.js +18 -53
- package/dist/locks.js +13 -3
- package/dist/logger.js +47 -1
- package/dist/org.js +14 -14
- package/dist/records.d.ts +13 -0
- package/dist/response.js +21 -1
- package/dist/schema.d.ts +1 -0
- package/dist/script.js +7 -3
- package/docs/en/api-reference/background-jobs.md +288 -0
- package/docs/en/api-reference/cryptography.md +321 -0
- package/docs/en/api-reference/fetch-and-secrets.md +184 -0
- package/docs/en/api-reference/inbound-webhooks.md +65 -0
- package/docs/en/api-reference/notifications-and-email.md +317 -0
- package/docs/en/api-reference/organization-structure.md +304 -0
- package/docs/en/api-reference/overview.md +198 -0
- package/docs/en/api-reference/push-messages.md +115 -0
- package/docs/en/api-reference/record-triggers.md +469 -0
- package/docs/en/api-reference/records.md +447 -0
- package/docs/en/api-reference/scripts-and-routes.md +354 -0
- package/docs/en/api-reference/state-and-locks.md +162 -0
- package/docs/en/usage-guide.md +17 -14
- package/package.json +1 -1
- package/docs/en/api-reference.md +0 -2944
package/README.md
CHANGED
|
@@ -110,9 +110,10 @@ Routes support GET, POST, PUT, PATCH, and DELETE. `request` exposes `method`, `p
|
|
|
110
110
|
decoded `params`, repeated-value-aware `query`, safe lower-case `headers`, and JSON
|
|
111
111
|
`body`. Authentication headers are never exposed. Plain return values are JSON with
|
|
112
112
|
HTTP 200. `response.json`, `text`, `bytes`, `empty`, and `redirect` provide explicit
|
|
113
|
-
status, safe response headers, and text/binary bodies. Missing routes
|
|
114
|
-
|
|
115
|
-
|
|
113
|
+
status, safe response headers, and text/binary bodies. Missing routes, including a
|
|
114
|
+
path registered only for other methods, return 404; methods other than these five
|
|
115
|
+
return 405. Existing `defineScript()` projects continue at `/`.
|
|
116
|
+
The complete router contract is included in `docs/en/api-reference/scripts-and-routes.md`.
|
|
116
117
|
|
|
117
118
|
## Record triggers
|
|
118
119
|
|
|
@@ -199,7 +200,7 @@ before-change trigger. Throw `RetryableError` for a temporary failure and Cogove
|
|
|
199
200
|
runs the handler again later, a limited number of times; other errors are logged and
|
|
200
201
|
not retried. Writes made by a trigger run triggers again, and Cogover ends such
|
|
201
202
|
chains after a bounded number of automated writes. The complete contract is included
|
|
202
|
-
in `docs/en/api-reference.md`.
|
|
203
|
+
in `docs/en/api-reference/record-triggers.md`.
|
|
203
204
|
|
|
204
205
|
## Background jobs
|
|
205
206
|
|
|
@@ -280,8 +281,9 @@ export default defineScript<Input>(async ({ input }) => {
|
|
|
280
281
|
});
|
|
281
282
|
```
|
|
282
283
|
|
|
283
|
-
V1 supports public HTTPS URLs on port 443
|
|
284
|
-
|
|
284
|
+
V1 supports public HTTPS URLs on port 443, or on another port once a Workspace
|
|
285
|
+
administrator approves that exact origin for the project, and the methods GET, HEAD,
|
|
286
|
+
POST, PUT, PATCH, and DELETE. Requests and buffered responses have platform limits. Redirects,
|
|
285
287
|
cookie jars, streaming, WebSocket, URL credentials, IP-literal hosts, custom proxy,
|
|
286
288
|
dispatcher, agent, DNS, and TLS settings are not supported. A failed mutating request
|
|
287
289
|
may already have reached the remote API, so use the remote API's idempotency mechanism
|
|
@@ -470,7 +472,8 @@ export default defineScript(async ({ state }) => {
|
|
|
470
472
|
```
|
|
471
473
|
|
|
472
474
|
Distributed locks provide project-scoped token leases for short critical sections.
|
|
473
|
-
`withLock` releases
|
|
475
|
+
`withLock` releases the lease after the callback and reports the callback's own error
|
|
476
|
+
if both fail; manual leases support renew/release and expose a
|
|
474
477
|
fencing token. A lease does not auto-renew and locks do not replace idempotency or
|
|
475
478
|
provide exactly-once execution.
|
|
476
479
|
|
|
@@ -613,8 +616,8 @@ Recommended `tsconfig.json` options:
|
|
|
613
616
|
## Additional documentation
|
|
614
617
|
|
|
615
618
|
The installed package includes an English usage guide at
|
|
616
|
-
`docs/en/usage-guide.md` and the complete API reference
|
|
617
|
-
`docs/en/api-reference.md`.
|
|
619
|
+
`docs/en/usage-guide.md` and the complete API reference in
|
|
620
|
+
`docs/en/api-reference/`, starting at `docs/en/api-reference/overview.md`.
|
|
618
621
|
|
|
619
622
|
## License
|
|
620
623
|
|
package/dist/crypto.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { CogoverApiError, ValidationError } from "./errors.js";
|
|
2
|
-
import { decodeBase64, encodeBase64 } from "./internal/base64.js";
|
|
2
|
+
import { decodeBase64, encodeBase64, encodeHex } from "./internal/base64.js";
|
|
3
3
|
import { callBridge } from "./internal/bridge.js";
|
|
4
|
-
import {
|
|
4
|
+
import { utf8ByteLength } from "./internal/utf8.js";
|
|
5
5
|
import { requireSecretName } from "./internal/validation.js";
|
|
6
6
|
import { isRecord } from "./internal/values.js";
|
|
7
7
|
const MAX_DATA_BYTES = 256 * 1024;
|
|
@@ -28,21 +28,38 @@ const AES_DECRYPT_OPTIONS = new Set(["mode", "iv", "tag", "aad", "encoding", "ou
|
|
|
28
28
|
const RSA_ENCRYPT_OPTIONS = new Set(["padding", "encoding"]);
|
|
29
29
|
const RSA_DECRYPT_OPTIONS = new Set(["padding", "encoding", "output"]);
|
|
30
30
|
const SIGNATURE_OPTIONS = new Set(["encoding", "signatureFormat"]);
|
|
31
|
+
/**
|
|
32
|
+
* Bytes that cross the bridge as base64. They are encoded only when the request is serialized, after the size
|
|
33
|
+
* check: base64 runs as guest code, so encoding data that is then refused would spend the invocation's CPU time
|
|
34
|
+
* and statement budget for nothing.
|
|
35
|
+
*/
|
|
36
|
+
class Base64Bytes {
|
|
37
|
+
bytes;
|
|
38
|
+
constructor(bytes) {
|
|
39
|
+
this.bytes = bytes;
|
|
40
|
+
}
|
|
41
|
+
toJSON() {
|
|
42
|
+
return encodeBase64(this.bytes);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
31
45
|
async function aesEncrypt(key, data, options) {
|
|
32
46
|
const label = "aesEncrypt()";
|
|
33
47
|
const opts = optionsObject(options, AES_ENCRYPT_OPTIONS, label);
|
|
34
48
|
const mode = aesMode(opts.mode, label);
|
|
35
49
|
const encoding = binaryEncoding(opts.encoding, label);
|
|
50
|
+
const keyPayload = aesKey(key, label);
|
|
51
|
+
const plain = bridgeData(data, `${label} data`);
|
|
36
52
|
const payload = {
|
|
37
53
|
algorithm: `AES-${mode}`,
|
|
38
|
-
key:
|
|
39
|
-
...namedData(
|
|
54
|
+
key: keyPayload,
|
|
55
|
+
...namedData(plain, "data"),
|
|
40
56
|
encoding,
|
|
41
57
|
};
|
|
58
|
+
let minimumBytes = plain.jsonBytes;
|
|
42
59
|
if (opts.iv !== undefined)
|
|
43
60
|
addBinary(payload, "iv", binaryInput(opts.iv, encoding, `${label} iv`, MAX_ENCODED_PARAMETER_CHARS));
|
|
44
61
|
if (opts.aad !== undefined)
|
|
45
|
-
addAad(payload, opts.aad, mode, label);
|
|
62
|
+
minimumBytes += addAad(payload, opts.aad, mode, label);
|
|
46
63
|
if (opts.separateTag !== undefined) {
|
|
47
64
|
if (typeof opts.separateTag !== "boolean")
|
|
48
65
|
throw new ValidationError(`${label} separateTag must be a boolean`);
|
|
@@ -51,7 +68,7 @@ async function aesEncrypt(key, data, options) {
|
|
|
51
68
|
if (opts.separateTag)
|
|
52
69
|
payload.separateTag = true;
|
|
53
70
|
}
|
|
54
|
-
const result = await callBridge("crypto.encrypt", payload);
|
|
71
|
+
const result = await callBridge("crypto.encrypt", payload, minimumBytes);
|
|
55
72
|
if (!isRecord(result) || typeof result.ciphertext !== "string" || typeof result.iv !== "string"
|
|
56
73
|
|| result.iv.length === 0 || (payload.separateTag === true) !== (typeof result.tag === "string")) {
|
|
57
74
|
throw new CogoverApiError("INVALID_BRIDGE_RESPONSE", "Encryption result is invalid");
|
|
@@ -70,31 +87,37 @@ async function aesDecrypt(key, ciphertext, options) {
|
|
|
70
87
|
const output = decryptOutput(opts.output, label);
|
|
71
88
|
if (opts.iv === undefined)
|
|
72
89
|
throw new ValidationError(`${label} options.iv is required`);
|
|
90
|
+
const keyPayload = aesKey(key, label);
|
|
91
|
+
const sealed = binaryInput(ciphertext, encoding, `${label} ciphertext`, MAX_ENCODED_CHARS);
|
|
73
92
|
const payload = {
|
|
74
93
|
algorithm: `AES-${mode}`,
|
|
75
|
-
key:
|
|
76
|
-
...namedBinary(
|
|
94
|
+
key: keyPayload,
|
|
95
|
+
...namedBinary(sealed, "data"),
|
|
77
96
|
...namedBinary(binaryInput(opts.iv, encoding, `${label} iv`, MAX_ENCODED_PARAMETER_CHARS), "iv"),
|
|
78
97
|
output: output === "bytes" ? "base64" : output,
|
|
79
98
|
};
|
|
99
|
+
let minimumBytes = sealed.jsonBytes;
|
|
80
100
|
if (opts.tag !== undefined) {
|
|
81
101
|
if (mode !== "GCM")
|
|
82
102
|
throw new ValidationError(`${label} tag requires mode "GCM"`);
|
|
83
103
|
addBinary(payload, "tag", binaryInput(opts.tag, encoding, `${label} tag`, MAX_ENCODED_PARAMETER_CHARS));
|
|
84
104
|
}
|
|
85
105
|
if (opts.aad !== undefined)
|
|
86
|
-
addAad(payload, opts.aad, mode, label);
|
|
87
|
-
return plaintext(await callBridge("crypto.decrypt", payload), output);
|
|
106
|
+
minimumBytes += addAad(payload, opts.aad, mode, label);
|
|
107
|
+
return plaintext(await callBridge("crypto.decrypt", payload, minimumBytes), output);
|
|
88
108
|
}
|
|
89
109
|
async function rsaEncrypt(publicKey, data, options) {
|
|
90
110
|
const label = "rsaEncrypt()";
|
|
91
111
|
const opts = optionsObject(options, RSA_ENCRYPT_OPTIONS, label);
|
|
112
|
+
const algorithm = `RSA-${rsaPadding(opts.padding, label)}`;
|
|
113
|
+
const key = asymmetricKey(publicKey, `${label} public key`);
|
|
114
|
+
const plain = bridgeData(data, `${label} data`);
|
|
92
115
|
const result = await callBridge("crypto.encrypt", {
|
|
93
|
-
algorithm
|
|
94
|
-
key
|
|
95
|
-
...namedData(
|
|
116
|
+
algorithm,
|
|
117
|
+
key,
|
|
118
|
+
...namedData(plain, "data"),
|
|
96
119
|
encoding: binaryEncoding(opts.encoding, label),
|
|
97
|
-
});
|
|
120
|
+
}, plain.jsonBytes);
|
|
98
121
|
if (!isRecord(result) || typeof result.ciphertext !== "string" || result.ciphertext.length === 0) {
|
|
99
122
|
throw new CogoverApiError("INVALID_BRIDGE_RESPONSE", "Encryption result is invalid");
|
|
100
123
|
}
|
|
@@ -105,25 +128,30 @@ async function rsaDecrypt(privateKey, ciphertext, options) {
|
|
|
105
128
|
const opts = optionsObject(options, RSA_DECRYPT_OPTIONS, label);
|
|
106
129
|
const encoding = binaryEncoding(opts.encoding, label);
|
|
107
130
|
const output = decryptOutput(opts.output, label);
|
|
131
|
+
const algorithm = `RSA-${rsaPadding(opts.padding, label)}`;
|
|
132
|
+
const key = asymmetricKey(privateKey, `${label} private key`);
|
|
133
|
+
const sealed = binaryInput(ciphertext, encoding, `${label} ciphertext`, MAX_ENCODED_CHARS);
|
|
108
134
|
return plaintext(await callBridge("crypto.decrypt", {
|
|
109
|
-
algorithm
|
|
110
|
-
key
|
|
111
|
-
...namedBinary(
|
|
135
|
+
algorithm,
|
|
136
|
+
key,
|
|
137
|
+
...namedBinary(sealed, "data"),
|
|
112
138
|
output: output === "bytes" ? "base64" : output,
|
|
113
|
-
}), output);
|
|
139
|
+
}, sealed.jsonBytes), output);
|
|
114
140
|
}
|
|
115
141
|
async function sign(algorithm, privateKey, data, options) {
|
|
116
142
|
const label = "sign()";
|
|
117
143
|
const signatureAlgorithm = requireSignatureAlgorithm(algorithm, label);
|
|
118
144
|
const opts = optionsObject(options, SIGNATURE_OPTIONS, label);
|
|
145
|
+
const key = asymmetricKey(privateKey, `${label} private key`);
|
|
146
|
+
const signed = bridgeData(data, `${label} data`);
|
|
119
147
|
const payload = {
|
|
120
148
|
algorithm: signatureAlgorithm,
|
|
121
|
-
key
|
|
122
|
-
...namedData(
|
|
149
|
+
key,
|
|
150
|
+
...namedData(signed, "data"),
|
|
123
151
|
encoding: binaryEncoding(opts.encoding, label),
|
|
124
152
|
};
|
|
125
153
|
addSignatureFormat(payload, opts.signatureFormat, signatureAlgorithm, label);
|
|
126
|
-
const result = await callBridge("crypto.sign", payload);
|
|
154
|
+
const result = await callBridge("crypto.sign", payload, signed.jsonBytes);
|
|
127
155
|
if (!isRecord(result) || typeof result.signature !== "string" || result.signature.length === 0) {
|
|
128
156
|
throw new CogoverApiError("INVALID_BRIDGE_RESPONSE", "Signature is invalid");
|
|
129
157
|
}
|
|
@@ -139,47 +167,46 @@ async function verify(algorithm, publicKey, data, signature, options) {
|
|
|
139
167
|
if (typeof signature !== "string" && !(signature instanceof Uint8Array)) {
|
|
140
168
|
throw new ValidationError(`${label} signature must be a string or a Uint8Array`);
|
|
141
169
|
}
|
|
142
|
-
// A signature usually comes from the party being verified, so one that is too long is simply invalid.
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
170
|
+
// A signature usually comes from the party being verified, so one that is too long is simply invalid. It is
|
|
171
|
+
// replaced by an empty one, which Cogover reports as invalid only after it has checked the key and algorithm.
|
|
172
|
+
const oversized = typeof signature === "string"
|
|
173
|
+
? signature.length > MAX_ENCODED_PARAMETER_CHARS * 2
|
|
174
|
+
: signature.length > 1024;
|
|
147
175
|
const payload = {
|
|
148
176
|
algorithm: signatureAlgorithm,
|
|
149
177
|
key,
|
|
150
178
|
...namedData(signed, "data"),
|
|
151
|
-
...namedBinary(
|
|
152
|
-
?
|
|
153
|
-
: { value: encodeBase64(signature), encoding: "base64" }, "signature"),
|
|
179
|
+
...namedBinary(oversized ? textInput("", "base64")
|
|
180
|
+
: typeof signature === "string" ? textInput(signature, encoding) : bytesInput(signature), "signature"),
|
|
154
181
|
};
|
|
155
182
|
addSignatureFormat(payload, opts.signatureFormat, signatureAlgorithm, label);
|
|
156
|
-
const result = await callBridge("crypto.verify", payload);
|
|
183
|
+
const result = await callBridge("crypto.verify", payload, signed.jsonBytes);
|
|
157
184
|
if (!isRecord(result) || typeof result.valid !== "boolean") {
|
|
158
185
|
throw new CogoverApiError("INVALID_BRIDGE_RESPONSE", "Verification result is invalid");
|
|
159
186
|
}
|
|
160
|
-
return result.valid;
|
|
187
|
+
return result.valid && !oversized;
|
|
161
188
|
}
|
|
162
189
|
/** Cryptographic helpers backed by Cogover's managed implementation. */
|
|
163
190
|
export const crypto = Object.freeze({
|
|
164
191
|
async sha256(data, encoding = "hex") {
|
|
165
|
-
const { data: text, dataEncoding } = bridgeData(data, "sha256() data");
|
|
192
|
+
const { data: text, dataEncoding, jsonBytes } = bridgeData(data, "sha256() data");
|
|
166
193
|
return digest(await callBridge("crypto.hash", {
|
|
167
194
|
algorithm: "SHA-256",
|
|
168
195
|
data: text,
|
|
169
196
|
dataEncoding,
|
|
170
197
|
encoding: digestEncoding(encoding, "sha256()"),
|
|
171
|
-
}));
|
|
198
|
+
}, jsonBytes));
|
|
172
199
|
},
|
|
173
200
|
async hmacSha256(key, data, encoding = "hex") {
|
|
174
|
-
const keyPayload = hmacKey(key);
|
|
175
|
-
const { data: text, dataEncoding } = bridgeData(data, "hmacSha256() data");
|
|
201
|
+
const { key: keyPayload, jsonBytes: keyBytes } = hmacKey(key);
|
|
202
|
+
const { data: text, dataEncoding, jsonBytes } = bridgeData(data, "hmacSha256() data");
|
|
176
203
|
return digest(await callBridge("crypto.hmac", {
|
|
177
204
|
algorithm: "SHA-256",
|
|
178
205
|
key: keyPayload,
|
|
179
206
|
data: text,
|
|
180
207
|
dataEncoding,
|
|
181
208
|
encoding: digestEncoding(encoding, "hmacSha256()"),
|
|
182
|
-
}));
|
|
209
|
+
}, keyBytes + jsonBytes));
|
|
183
210
|
},
|
|
184
211
|
async randomBytes(length) {
|
|
185
212
|
if (!Number.isInteger(length) || length < 1 || length > MAX_RANDOM_BYTES) {
|
|
@@ -193,23 +220,19 @@ export const crypto = Object.freeze({
|
|
|
193
220
|
return bytes;
|
|
194
221
|
},
|
|
195
222
|
async randomUUID() {
|
|
196
|
-
const bytes = await
|
|
223
|
+
const bytes = await uuidBytes();
|
|
197
224
|
bytes[6] = ((bytes[6] ?? 0) & 0x0f) | 0x40; // version 4
|
|
198
225
|
bytes[8] = ((bytes[8] ?? 0) & 0x3f) | 0x80; // RFC 4122 variant
|
|
199
|
-
const hex =
|
|
226
|
+
const hex = encodeHex(bytes);
|
|
200
227
|
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
201
228
|
},
|
|
202
229
|
timingSafeEqual(a, b) {
|
|
203
|
-
const left =
|
|
204
|
-
const right =
|
|
205
|
-
//
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
for (let index = 0; index < length; index++) {
|
|
210
|
-
mismatch |= (left[index] ?? 0) ^ (right[index] ?? 0);
|
|
211
|
-
}
|
|
212
|
-
return mismatch === 0;
|
|
230
|
+
const left = comparable(a, "timingSafeEqual() first value");
|
|
231
|
+
const right = comparable(b, "timingSafeEqual() second value");
|
|
232
|
+
// Well-formed strings are equal exactly when their UTF-8 encodings are, so two strings need no encoding.
|
|
233
|
+
return typeof left === "string" && typeof right === "string"
|
|
234
|
+
? constantTimeEqual(wellFormed(left), wellFormed(right))
|
|
235
|
+
: constantTimeEqual(binaryString(left), binaryString(right));
|
|
213
236
|
},
|
|
214
237
|
aesEncrypt,
|
|
215
238
|
aesDecrypt: aesDecrypt,
|
|
@@ -218,16 +241,21 @@ export const crypto = Object.freeze({
|
|
|
218
241
|
sign,
|
|
219
242
|
verify,
|
|
220
243
|
});
|
|
244
|
+
/** Characters of the padded base64 text of `byteLength` bytes. */
|
|
245
|
+
function base64Length(byteLength) {
|
|
246
|
+
return 4 * Math.ceil(byteLength / 3);
|
|
247
|
+
}
|
|
221
248
|
function bridgeData(data, label) {
|
|
222
249
|
if (typeof data === "string") {
|
|
223
|
-
|
|
250
|
+
const byteLength = utf8ByteLength(data);
|
|
251
|
+
if (byteLength > MAX_DATA_BYTES)
|
|
224
252
|
throw tooLarge(label);
|
|
225
|
-
return { data, dataEncoding: "utf8" };
|
|
253
|
+
return { data, dataEncoding: "utf8", jsonBytes: byteLength + 2 };
|
|
226
254
|
}
|
|
227
255
|
if (data instanceof Uint8Array) {
|
|
228
256
|
if (data.length > MAX_DATA_BYTES)
|
|
229
257
|
throw tooLarge(label);
|
|
230
|
-
return { data:
|
|
258
|
+
return { data: new Base64Bytes(data), dataEncoding: "base64", jsonBytes: base64Length(data.length) + 2 };
|
|
231
259
|
}
|
|
232
260
|
throw new ValidationError(`${label} must be a string or a Uint8Array`);
|
|
233
261
|
}
|
|
@@ -240,25 +268,34 @@ function namedBinary(input, name) {
|
|
|
240
268
|
function addBinary(payload, name, input) {
|
|
241
269
|
Object.assign(payload, namedBinary(input, name));
|
|
242
270
|
}
|
|
271
|
+
/** Text already in `encoding`; each UTF-16 code unit takes at least one byte. */
|
|
272
|
+
function textInput(value, encoding) {
|
|
273
|
+
return { value, encoding, jsonBytes: value.length + 2 };
|
|
274
|
+
}
|
|
275
|
+
function bytesInput(bytes) {
|
|
276
|
+
return { value: new Base64Bytes(bytes), encoding: "base64", jsonBytes: base64Length(bytes.length) + 2 };
|
|
277
|
+
}
|
|
243
278
|
/** A binary value given as text in `encoding`, or as bytes that cross the bridge as base64. */
|
|
244
279
|
function binaryInput(value, encoding, label, maxChars) {
|
|
245
280
|
if (typeof value === "string") {
|
|
246
281
|
if (value.length > maxChars)
|
|
247
282
|
throw new ValidationError(`${label} must not exceed ${maxChars} characters`);
|
|
248
|
-
return
|
|
283
|
+
return textInput(value, encoding);
|
|
249
284
|
}
|
|
250
285
|
if (value instanceof Uint8Array) {
|
|
251
|
-
|
|
252
|
-
if (text.length > maxChars)
|
|
286
|
+
if (base64Length(value.length) > maxChars)
|
|
253
287
|
throw new ValidationError(`${label} is too large`);
|
|
254
|
-
return
|
|
288
|
+
return bytesInput(value);
|
|
255
289
|
}
|
|
256
290
|
throw new ValidationError(`${label} must be a string or a Uint8Array`);
|
|
257
291
|
}
|
|
292
|
+
/** Adds `aad` and returns the bytes it takes in the JSON request at least. */
|
|
258
293
|
function addAad(payload, aad, mode, label) {
|
|
259
294
|
if (mode !== "GCM")
|
|
260
295
|
throw new ValidationError(`${label} aad requires mode "GCM"`);
|
|
261
|
-
|
|
296
|
+
const data = bridgeData(aad, `${label} aad`);
|
|
297
|
+
Object.assign(payload, namedData(data, "aad"));
|
|
298
|
+
return data.jsonBytes;
|
|
262
299
|
}
|
|
263
300
|
function optionsObject(options, allowed, label) {
|
|
264
301
|
if (options === undefined)
|
|
@@ -368,23 +405,25 @@ function plaintext(payload, output) {
|
|
|
368
405
|
function aesKeyLength(label) {
|
|
369
406
|
return new ValidationError(`${label} key must be 16, 24 or 32 bytes`);
|
|
370
407
|
}
|
|
408
|
+
/** The HMAC key payload and the bytes its value takes in the JSON request at least. */
|
|
371
409
|
function hmacKey(key) {
|
|
372
410
|
if (typeof key === "string") {
|
|
373
411
|
if (key.length === 0)
|
|
374
412
|
throw new ValidationError("hmacSha256() key must not be empty");
|
|
375
|
-
|
|
413
|
+
const byteLength = utf8ByteLength(key);
|
|
414
|
+
if (byteLength > MAX_DATA_BYTES)
|
|
376
415
|
throw tooLarge("hmacSha256() key");
|
|
377
|
-
return { value: key, encoding: "utf8" };
|
|
416
|
+
return { key: { value: key, encoding: "utf8" }, jsonBytes: byteLength + 2 };
|
|
378
417
|
}
|
|
379
418
|
if (key instanceof Uint8Array) {
|
|
380
419
|
if (key.length === 0)
|
|
381
420
|
throw new ValidationError("hmacSha256() key must not be empty");
|
|
382
421
|
if (key.length > MAX_DATA_BYTES)
|
|
383
422
|
throw tooLarge("hmacSha256() key");
|
|
384
|
-
return { value:
|
|
423
|
+
return { key: { value: new Base64Bytes(key), encoding: "base64" }, jsonBytes: base64Length(key.length) + 2 };
|
|
385
424
|
}
|
|
386
425
|
if (isRecord(key) && Object.keys(key).length === 1 && "secret" in key) {
|
|
387
|
-
return { secret: requireSecretName(key.secret, "hmacSha256() key.secret") };
|
|
426
|
+
return { key: { secret: requireSecretName(key.secret, "hmacSha256() key.secret") }, jsonBytes: 0 };
|
|
388
427
|
}
|
|
389
428
|
throw new ValidationError("hmacSha256() key must be a string, a Uint8Array or { secret: name }");
|
|
390
429
|
}
|
|
@@ -400,18 +439,92 @@ function digest(payload) {
|
|
|
400
439
|
}
|
|
401
440
|
return payload.digest;
|
|
402
441
|
}
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
442
|
+
/** Random bytes for UUIDs, fetched MAX_RANDOM_BYTES at a time: one capability call covers 64 UUIDs. */
|
|
443
|
+
let uuidPool = new Uint8Array(0);
|
|
444
|
+
let uuidPoolOffset = 0;
|
|
445
|
+
let uuidPoolRefill;
|
|
446
|
+
async function uuidBytes() {
|
|
447
|
+
while (uuidPoolOffset + 16 > uuidPool.length) {
|
|
448
|
+
// Concurrent calls share one refill; a failed refill is retried by the next call.
|
|
449
|
+
uuidPoolRefill ??= crypto.randomBytes(MAX_RANDOM_BYTES)
|
|
450
|
+
.then(bytes => {
|
|
451
|
+
uuidPool = bytes;
|
|
452
|
+
uuidPoolOffset = 0;
|
|
453
|
+
})
|
|
454
|
+
.finally(() => {
|
|
455
|
+
uuidPoolRefill = undefined;
|
|
456
|
+
});
|
|
457
|
+
await uuidPoolRefill;
|
|
458
|
+
}
|
|
459
|
+
const bytes = uuidPool.slice(uuidPoolOffset, uuidPoolOffset + 16);
|
|
460
|
+
uuidPool.fill(0, uuidPoolOffset, uuidPoolOffset + 16);
|
|
461
|
+
uuidPoolOffset += 16;
|
|
462
|
+
return bytes;
|
|
463
|
+
}
|
|
464
|
+
/** A timingSafeEqual argument after its type and size checks. */
|
|
465
|
+
function comparable(value, label) {
|
|
466
|
+
if (typeof value === "string") {
|
|
467
|
+
if (utf8ByteLength(value) > MAX_DATA_BYTES)
|
|
468
|
+
throw tooLarge(label);
|
|
469
|
+
return value;
|
|
470
|
+
}
|
|
471
|
+
if (value instanceof Uint8Array) {
|
|
472
|
+
if (value.length > MAX_DATA_BYTES)
|
|
473
|
+
throw tooLarge(label);
|
|
407
474
|
return value;
|
|
475
|
+
}
|
|
408
476
|
throw new ValidationError(`${label} must be a string or a Uint8Array`);
|
|
409
477
|
}
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
478
|
+
const LONE_SURROGATES = /[\ud800-\udbff](?![\udc00-\udfff])|(?<![\ud800-\udbff])[\udc00-\udfff]/g;
|
|
479
|
+
const BINARY_CHUNK = 8192;
|
|
480
|
+
/** The string with each lone surrogate replaced by U+FFFD, as its UTF-8 encoding does. */
|
|
481
|
+
function wellFormed(value) {
|
|
482
|
+
const toWellFormed = String.prototype.toWellFormed;
|
|
483
|
+
return typeof toWellFormed === "function" ? toWellFormed.call(value) : value.replace(LONE_SURROGATES, "\ufffd");
|
|
484
|
+
}
|
|
485
|
+
/**
|
|
486
|
+
* One character per byte: the UTF-8 encoding of a string, or the bytes themselves. Both conversions run as engine
|
|
487
|
+
* code; encodeURIComponent leaves only ASCII characters unescaped, and unescape turns each %XX back into one byte.
|
|
488
|
+
*/
|
|
489
|
+
function binaryString(value) {
|
|
490
|
+
if (typeof value === "string")
|
|
491
|
+
return unescape(encodeURIComponent(wellFormed(value)));
|
|
492
|
+
const parts = [];
|
|
493
|
+
for (let index = 0; index < value.length; index += BINARY_CHUNK) {
|
|
494
|
+
parts[parts.length] = String.fromCharCode.apply(null, value.subarray(index, index + BINARY_CHUNK));
|
|
495
|
+
}
|
|
496
|
+
return parts.join("");
|
|
497
|
+
}
|
|
498
|
+
/**
|
|
499
|
+
* Visits every code unit of the longer string, so the time depends only on the lengths, never on where the first
|
|
500
|
+
* difference is. Each loop turn compares 32 code units in one expression to stay within the statement budget; past
|
|
501
|
+
* the end of the shorter string charCodeAt returns NaN, which a bitwise operator reads as 0.
|
|
502
|
+
*/
|
|
503
|
+
function constantTimeEqual(left, right) {
|
|
504
|
+
let mismatch = left.length ^ right.length;
|
|
505
|
+
const length = Math.max(left.length, right.length);
|
|
506
|
+
let index = 0;
|
|
507
|
+
for (; index + 32 <= length; index += 32) {
|
|
508
|
+
mismatch |= (left.charCodeAt(index) ^ right.charCodeAt(index)) | (left.charCodeAt(index + 1) ^ right.charCodeAt(index + 1))
|
|
509
|
+
| (left.charCodeAt(index + 2) ^ right.charCodeAt(index + 2)) | (left.charCodeAt(index + 3) ^ right.charCodeAt(index + 3))
|
|
510
|
+
| (left.charCodeAt(index + 4) ^ right.charCodeAt(index + 4)) | (left.charCodeAt(index + 5) ^ right.charCodeAt(index + 5))
|
|
511
|
+
| (left.charCodeAt(index + 6) ^ right.charCodeAt(index + 6)) | (left.charCodeAt(index + 7) ^ right.charCodeAt(index + 7))
|
|
512
|
+
| (left.charCodeAt(index + 8) ^ right.charCodeAt(index + 8)) | (left.charCodeAt(index + 9) ^ right.charCodeAt(index + 9))
|
|
513
|
+
| (left.charCodeAt(index + 10) ^ right.charCodeAt(index + 10)) | (left.charCodeAt(index + 11) ^ right.charCodeAt(index + 11))
|
|
514
|
+
| (left.charCodeAt(index + 12) ^ right.charCodeAt(index + 12)) | (left.charCodeAt(index + 13) ^ right.charCodeAt(index + 13))
|
|
515
|
+
| (left.charCodeAt(index + 14) ^ right.charCodeAt(index + 14)) | (left.charCodeAt(index + 15) ^ right.charCodeAt(index + 15))
|
|
516
|
+
| (left.charCodeAt(index + 16) ^ right.charCodeAt(index + 16)) | (left.charCodeAt(index + 17) ^ right.charCodeAt(index + 17))
|
|
517
|
+
| (left.charCodeAt(index + 18) ^ right.charCodeAt(index + 18)) | (left.charCodeAt(index + 19) ^ right.charCodeAt(index + 19))
|
|
518
|
+
| (left.charCodeAt(index + 20) ^ right.charCodeAt(index + 20)) | (left.charCodeAt(index + 21) ^ right.charCodeAt(index + 21))
|
|
519
|
+
| (left.charCodeAt(index + 22) ^ right.charCodeAt(index + 22)) | (left.charCodeAt(index + 23) ^ right.charCodeAt(index + 23))
|
|
520
|
+
| (left.charCodeAt(index + 24) ^ right.charCodeAt(index + 24)) | (left.charCodeAt(index + 25) ^ right.charCodeAt(index + 25))
|
|
521
|
+
| (left.charCodeAt(index + 26) ^ right.charCodeAt(index + 26)) | (left.charCodeAt(index + 27) ^ right.charCodeAt(index + 27))
|
|
522
|
+
| (left.charCodeAt(index + 28) ^ right.charCodeAt(index + 28)) | (left.charCodeAt(index + 29) ^ right.charCodeAt(index + 29))
|
|
523
|
+
| (left.charCodeAt(index + 30) ^ right.charCodeAt(index + 30)) | (left.charCodeAt(index + 31) ^ right.charCodeAt(index + 31));
|
|
524
|
+
}
|
|
525
|
+
for (; index < length; index++)
|
|
526
|
+
mismatch |= left.charCodeAt(index) ^ right.charCodeAt(index);
|
|
527
|
+
return mismatch === 0;
|
|
415
528
|
}
|
|
416
529
|
function tooLarge(label) {
|
|
417
530
|
return new ValidationError(`${label} must not exceed ${MAX_DATA_BYTES} bytes`);
|
package/dist/data.js
CHANGED
|
@@ -109,7 +109,7 @@ function createObjectClient(slug, identity) {
|
|
|
109
109
|
}
|
|
110
110
|
try {
|
|
111
111
|
return {
|
|
112
|
-
id: requireText(item.id,
|
|
112
|
+
id: requireText(item.id, "record id"),
|
|
113
113
|
fields: requireFields(item.fields),
|
|
114
114
|
};
|
|
115
115
|
}
|
|
@@ -144,6 +144,9 @@ function createObjectClient(slug, identity) {
|
|
|
144
144
|
if (!Array.isArray(ids) || ids.length === 0) {
|
|
145
145
|
throw new ValidationError("deleteMany() requires at least one record id");
|
|
146
146
|
}
|
|
147
|
+
if (ids.length > MAX_BATCH_RECORDS) {
|
|
148
|
+
throw new ValidationError(`deleteMany() supports at most ${MAX_BATCH_RECORDS} record ids`);
|
|
149
|
+
}
|
|
147
150
|
return callBridge("records.deleteMany", {
|
|
148
151
|
identity,
|
|
149
152
|
objectSlug: slug,
|
package/dist/email.js
CHANGED
|
@@ -8,7 +8,8 @@ const MAX_RECIPIENTS = 50;
|
|
|
8
8
|
const MAX_SUBJECT_CHARS = 255;
|
|
9
9
|
const MAX_NAME_CHARS = 200;
|
|
10
10
|
const MAX_EMAIL_CHARS = 254;
|
|
11
|
-
// The
|
|
11
|
+
// The body is measured as it travels, JSON-encoded, so that with the rest of the message it fits one capability
|
|
12
|
+
// request (MAX_BRIDGE_REQUEST_BYTES).
|
|
12
13
|
const MAX_BODY_BYTES = 192 * 1024;
|
|
13
14
|
const MAX_ATTACHMENTS = 10;
|
|
14
15
|
const MAX_RECORD_EMAIL_FIELDS = 20;
|
|
@@ -80,8 +81,9 @@ function emailPayload(message) {
|
|
|
80
81
|
if ((html === undefined) === (text === undefined))
|
|
81
82
|
throw new ValidationError("Provide exactly one of html and text");
|
|
82
83
|
const body = (html ?? text);
|
|
83
|
-
|
|
84
|
-
|
|
84
|
+
// JSON.stringify adds two quotes; each quote, backslash or control character inside counts as its escape.
|
|
85
|
+
if (utf8ByteLength(JSON.stringify(body)) - 2 > MAX_BODY_BYTES) {
|
|
86
|
+
throw new ValidationError(`The email body must not exceed ${MAX_BODY_BYTES} bytes once JSON-encoded`);
|
|
85
87
|
}
|
|
86
88
|
if (html !== undefined)
|
|
87
89
|
payload.html = html;
|