@gadicc/loom 1.35.3 → 1.37.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.
Files changed (103) hide show
  1. package/README.md +66 -0
  2. package/THIRD_PARTY_NOTICES.md +12 -9
  3. package/esm/src/cli/externalSkills.d.ts.map +1 -1
  4. package/esm/src/cli/externalSkills.js +25 -20
  5. package/esm/src/cli/mod.d.ts +117 -1
  6. package/esm/src/cli/mod.d.ts.map +1 -1
  7. package/esm/src/cli/mod.js +1368 -44
  8. package/esm/src/cli/projectSkills.d.ts +10 -3
  9. package/esm/src/cli/projectSkills.d.ts.map +1 -1
  10. package/esm/src/cli/projectSkills.js +173 -49
  11. package/esm/src/cli/vercelBuildEnv.d.ts +28 -0
  12. package/esm/src/cli/vercelBuildEnv.d.ts.map +1 -1
  13. package/esm/src/cli/vercelBuildEnv.js +120 -30
  14. package/esm/src/next/secrets/mod.d.ts +108 -0
  15. package/esm/src/next/secrets/mod.d.ts.map +1 -0
  16. package/esm/src/next/secrets/mod.js +176 -0
  17. package/esm/src/secrets/encoding.d.ts +32 -0
  18. package/esm/src/secrets/encoding.d.ts.map +1 -0
  19. package/esm/src/secrets/encoding.js +87 -0
  20. package/esm/src/secrets/errors.d.ts +45 -0
  21. package/esm/src/secrets/errors.d.ts.map +1 -0
  22. package/esm/src/secrets/errors.js +39 -0
  23. package/esm/src/secrets/fingerprint.d.ts +43 -0
  24. package/esm/src/secrets/fingerprint.d.ts.map +1 -0
  25. package/esm/src/secrets/fingerprint.js +87 -0
  26. package/esm/src/secrets/frame.d.ts +34 -0
  27. package/esm/src/secrets/frame.d.ts.map +1 -0
  28. package/esm/src/secrets/frame.js +86 -0
  29. package/esm/src/secrets/keyring.d.ts +62 -0
  30. package/esm/src/secrets/keyring.d.ts.map +1 -0
  31. package/esm/src/secrets/keyring.js +160 -0
  32. package/esm/src/secrets/legacy.d.ts +60 -0
  33. package/esm/src/secrets/legacy.d.ts.map +1 -0
  34. package/esm/src/secrets/legacy.js +147 -0
  35. package/esm/src/secrets/maintenance.d.ts +173 -0
  36. package/esm/src/secrets/maintenance.d.ts.map +1 -0
  37. package/esm/src/secrets/maintenance.js +315 -0
  38. package/esm/src/secrets/mod.d.ts +6 -0
  39. package/esm/src/secrets/mod.d.ts.map +1 -0
  40. package/esm/src/secrets/mod.js +5 -0
  41. package/esm/src/secrets/secrets.d.ts +192 -0
  42. package/esm/src/secrets/secrets.d.ts.map +1 -0
  43. package/esm/src/secrets/secrets.js +528 -0
  44. package/package.json +50 -2
  45. package/script/src/cli/externalSkills.d.ts.map +1 -1
  46. package/script/src/cli/externalSkills.js +25 -20
  47. package/script/src/cli/mod.d.ts +117 -1
  48. package/script/src/cli/mod.d.ts.map +1 -1
  49. package/script/src/cli/mod.js +1372 -41
  50. package/script/src/cli/projectSkills.d.ts +10 -3
  51. package/script/src/cli/projectSkills.d.ts.map +1 -1
  52. package/script/src/cli/projectSkills.js +173 -49
  53. package/script/src/cli/vercelBuildEnv.d.ts +28 -0
  54. package/script/src/cli/vercelBuildEnv.d.ts.map +1 -1
  55. package/script/src/cli/vercelBuildEnv.js +121 -30
  56. package/script/src/next/secrets/mod.d.ts +108 -0
  57. package/script/src/next/secrets/mod.d.ts.map +1 -0
  58. package/script/src/next/secrets/mod.js +218 -0
  59. package/script/src/secrets/encoding.d.ts +32 -0
  60. package/script/src/secrets/encoding.d.ts.map +1 -0
  61. package/script/src/secrets/encoding.js +95 -0
  62. package/script/src/secrets/errors.d.ts +45 -0
  63. package/script/src/secrets/errors.d.ts.map +1 -0
  64. package/script/src/secrets/errors.js +45 -0
  65. package/script/src/secrets/fingerprint.d.ts +43 -0
  66. package/script/src/secrets/fingerprint.d.ts.map +1 -0
  67. package/script/src/secrets/fingerprint.js +92 -0
  68. package/script/src/secrets/frame.d.ts +34 -0
  69. package/script/src/secrets/frame.d.ts.map +1 -0
  70. package/script/src/secrets/frame.js +91 -0
  71. package/script/src/secrets/keyring.d.ts +62 -0
  72. package/script/src/secrets/keyring.d.ts.map +1 -0
  73. package/script/src/secrets/keyring.js +202 -0
  74. package/script/src/secrets/legacy.d.ts +60 -0
  75. package/script/src/secrets/legacy.d.ts.map +1 -0
  76. package/script/src/secrets/legacy.js +186 -0
  77. package/script/src/secrets/maintenance.d.ts +173 -0
  78. package/script/src/secrets/maintenance.d.ts.map +1 -0
  79. package/script/src/secrets/maintenance.js +323 -0
  80. package/script/src/secrets/mod.d.ts +6 -0
  81. package/script/src/secrets/mod.d.ts.map +1 -0
  82. package/script/src/secrets/mod.js +24 -0
  83. package/script/src/secrets/secrets.d.ts +192 -0
  84. package/script/src/secrets/secrets.d.ts.map +1 -0
  85. package/script/src/secrets/secrets.js +532 -0
  86. package/skills/loom/SKILL.md +79 -222
  87. package/skills/loom/references/dependency-upgrades.md +26 -0
  88. package/skills/loom/references/local-development.md +157 -0
  89. package/skills/loom/references/release-safety.md +118 -0
  90. package/skills/loom/references/release-trusted-sources.md +55 -0
  91. package/skills/loom/references/tools-and-skills.md +83 -0
  92. package/skills/loom-api-keys/SKILL.md +1 -1
  93. package/skills/loom-auth/SKILL.md +7 -60
  94. package/skills/loom-auth/references/secrets-and-rotation.md +61 -0
  95. package/skills/loom-db/SKILL.md +4 -79
  96. package/skills/loom-files/SKILL.md +4 -2
  97. package/skills/loom-files/references/provider-configuration.md +150 -0
  98. package/skills/loom-jobs/SKILL.md +9 -8
  99. package/skills/loom-lingui/SKILL.md +5 -3
  100. package/skills/loom-secrets/SKILL.md +266 -0
  101. package/skills/loom-torvalds-review/SKILL.md +9 -6
  102. package/skills/loom-torvalds-review/references/context-profiles.md +2 -1
  103. package/skills/upgrade-deps/SKILL.md +15 -7
@@ -0,0 +1,528 @@
1
+ import { assertWellFormed, decodeUtf8, encodeUtf8, fromBase64Url, pae, toBase64Url, } from "./encoding.js";
2
+ import { SecretsKeyError, SecretsKeyMismatchError, SecretUnreadableError, } from "./errors.js";
3
+ import { parseExpectedSecretKeys, secretKeyFingerprint, } from "./fingerprint.js";
4
+ import { decodeFrame, encodeFrame, FRAME_BLOCK_BYTES, } from "./frame.js";
5
+ import { isSecretKeyId, parseSecretKeyId, SECRETS_KEY_BYTES, } from "./keyring.js";
6
+ /** Prefix and version of the sealed string format. */
7
+ export const SEALED_FORMAT = "ls1";
8
+ /** Prefix and version of the keyed fingerprint format. */
9
+ export const KEYED_FINGERPRINT_FORMAT = "lm1";
10
+ const DOMAIN = "loom-secrets";
11
+ const NONCE_BYTES = 12;
12
+ const TAG_BYTES = 16;
13
+ const MAC_BYTES = 32;
14
+ // 32 bytes in unpadded base64url.
15
+ const MAC_TEXT_LENGTH = 43;
16
+ const PURPOSE_PATTERN = /^[a-z0-9]+(?:[.-][a-z0-9]+)*$/;
17
+ const MAX_PURPOSE_LENGTH = 64;
18
+ const MAX_KEY_ID_DIGITS = 9;
19
+ // Characters, not bytes: room for a payload a little over 1.5 MiB.
20
+ const DEFAULT_MAX_LENGTH = 2_097_152;
21
+ const DEFAULT_SKEW_SECONDS = 60;
22
+ function parseSealed(sealed, maxLength) {
23
+ if (typeof sealed !== "string" || sealed.length > maxLength) {
24
+ return null;
25
+ }
26
+ const parts = sealed.split(".");
27
+ if (parts.length !== 4 || parts[0] !== SEALED_FORMAT) {
28
+ return null;
29
+ }
30
+ const keyId = parseSecretKeyId(parts[1]);
31
+ const nonce = fromBase64Url(parts[2]);
32
+ const ciphertext = fromBase64Url(parts[3]);
33
+ if (keyId === null ||
34
+ nonce === null ||
35
+ nonce.length !== NONCE_BYTES ||
36
+ ciphertext === null ||
37
+ ciphertext.length < FRAME_BLOCK_BYTES + TAG_BYTES ||
38
+ (ciphertext.length - TAG_BYTES) % FRAME_BLOCK_BYTES !== 0) {
39
+ return null;
40
+ }
41
+ return { keyId, nonce, ciphertext };
42
+ }
43
+ function parseFingerprint(stored) {
44
+ // Bounded before splitting: the string may come from an untrusted row.
45
+ if (typeof stored !== "string" || stored.length > 64) {
46
+ return null;
47
+ }
48
+ const parts = stored.split(".");
49
+ if (parts.length !== 3 || parts[0] !== KEYED_FINGERPRINT_FORMAT) {
50
+ return null;
51
+ }
52
+ const keyId = parseSecretKeyId(parts[1]);
53
+ const tag = parts[2].length === MAC_TEXT_LENGTH
54
+ ? fromBase64Url(parts[2])
55
+ : null;
56
+ if (keyId === null || tag === null || tag.length !== MAC_BYTES) {
57
+ return null;
58
+ }
59
+ return { keyId, tag };
60
+ }
61
+ /**
62
+ * Validates a binding and flattens it to sorted `key, value` pieces.
63
+ *
64
+ * Callers do this once, before any `await`, so that a binding object mutated
65
+ * while an operation is pending cannot change what is authenticated.
66
+ */
67
+ function bindingPieces(binding, unbound) {
68
+ const prototype = binding !== null && typeof binding === "object"
69
+ ? Object.getPrototypeOf(binding)
70
+ : undefined;
71
+ // A Map, Date or class instance has no own enumerable entries and would
72
+ // otherwise pass for an empty binding.
73
+ if (prototype !== Object.prototype && prototype !== null) {
74
+ throw new TypeError("A secret binding must be a plain object of strings.");
75
+ }
76
+ const keys = Object.keys(binding).sort();
77
+ if (keys.length === 0 && !unbound) {
78
+ throw new TypeError("A secret binding is required. Declare the purpose `unbound` to seal without one.");
79
+ }
80
+ return keys.flatMap((key) => {
81
+ if (key === "") {
82
+ throw new TypeError("A secret binding key must not be empty.");
83
+ }
84
+ return [
85
+ assertWellFormed(key, "A secret binding key"),
86
+ assertWellFormed(binding[key], `Secret binding "${key}"`),
87
+ ];
88
+ });
89
+ }
90
+ function positiveSeconds(value, what) {
91
+ if (!Number.isSafeInteger(value) || value <= 0) {
92
+ throw new RangeError(`${what} must be a positive whole number of seconds.`);
93
+ }
94
+ return value;
95
+ }
96
+ /**
97
+ * Creates the sealing and opening operations over a keyring.
98
+ *
99
+ * Each purpose encrypts with its own key, derived with HKDF from the keyring's
100
+ * key, so a value sealed for one purpose cannot be opened as another. The
101
+ * format version, key version, purpose and binding are all authenticated.
102
+ */
103
+ export function createSecrets(options) {
104
+ const { keys } = options;
105
+ const now = options.now ?? Date.now;
106
+ const skewSeconds = options.skewSeconds ?? DEFAULT_SKEW_SECONDS;
107
+ if (!Number.isSafeInteger(skewSeconds) || skewSeconds < 0) {
108
+ throw new RangeError("skewSeconds must be a non-negative whole number.");
109
+ }
110
+ const subtle = globalThis.crypto.subtle;
111
+ const baseKeys = new Map();
112
+ // Copied and validated once, so later changes to the caller's array cannot
113
+ // loosen what was checked.
114
+ const expected = options.expectedKeys === undefined ? undefined : new Map(parseExpectedSecretKeys(options.expectedKeys).map((entry) => [entry.id, entry]));
115
+ /** The raw key for a version, checked against the expected-key list. */
116
+ async function checkedKey(keyId) {
117
+ const entry = expected?.get(keyId);
118
+ if (expected && entry === undefined) {
119
+ // An undeclared variable may be a reused number or a stray paste.
120
+ if (await keys.key(keyId) === null) {
121
+ return null;
122
+ }
123
+ throw new SecretsKeyError(`Key version ${keyId} is configured but not in the expected key list.`);
124
+ }
125
+ if (entry?.state === "retired") {
126
+ return null;
127
+ }
128
+ const raw = await keys.key(keyId);
129
+ if (raw === null) {
130
+ return null;
131
+ }
132
+ // A custom keyring is trusted for where a key comes from, not for its
133
+ // shape: a short key would seal and open without complaint.
134
+ if (!(raw instanceof Uint8Array) || raw.length !== SECRETS_KEY_BYTES) {
135
+ throw new SecretsKeyError(`Key version ${keyId} must be exactly ${SECRETS_KEY_BYTES} bytes.`);
136
+ }
137
+ if (entry && await secretKeyFingerprint(raw) !== entry.fingerprint) {
138
+ throw new SecretsKeyMismatchError(`Key version ${keyId} does not match its recorded fingerprint.`);
139
+ }
140
+ return raw;
141
+ }
142
+ /** HKDF input for a key version, imported once and kept non-extractable. */
143
+ function baseKey(keyId) {
144
+ let pending = baseKeys.get(keyId);
145
+ if (!pending) {
146
+ pending = checkedKey(keyId).then((raw) => raw === null
147
+ ? null
148
+ : subtle.importKey("raw", new Uint8Array(raw), "HKDF", false, [
149
+ "deriveKey",
150
+ ]));
151
+ baseKeys.set(keyId, pending);
152
+ // A failed or absent key is looked up again next time.
153
+ pending.then((key) => key === null && baseKeys.delete(keyId), () => baseKeys.delete(keyId));
154
+ }
155
+ return pending;
156
+ }
157
+ async function activeKeyId() {
158
+ const keyId = await keys.activeKeyId();
159
+ if (!isSecretKeyId(keyId)) {
160
+ throw new SecretsKeyError("The keyring's active key version is not a valid key version.");
161
+ }
162
+ // With a list, only the key recorded as active may encrypt, so a pointer
163
+ // left on, or moved to, a decrypt-only key is caught before it is used.
164
+ if (expected && expected.get(keyId)?.state !== "active") {
165
+ throw new SecretsKeyError(`Key version ${keyId} is the active pointer but is not the key recorded as active.`);
166
+ }
167
+ return keyId;
168
+ }
169
+ function report(event) {
170
+ try {
171
+ // A logging hook must not turn a failed open into a different error, or
172
+ // into an unhandled rejection that can end the process.
173
+ Promise.resolve(options.onFailure?.(event)).catch(() => { });
174
+ }
175
+ catch {
176
+ // Ignored for the same reason.
177
+ }
178
+ }
179
+ const secrets = {
180
+ async assertReady() {
181
+ const active = await activeKeyId();
182
+ const ids = expected
183
+ ? [...expected].filter(([, entry]) => entry.state !== "retired")
184
+ .map(([id]) => id)
185
+ : [active];
186
+ const checked = [];
187
+ for (const id of ids) {
188
+ const raw = await checkedKey(id);
189
+ if (raw === null) {
190
+ throw new SecretsKeyError(`Key version ${id} is not configured.`);
191
+ }
192
+ checked.push({
193
+ id,
194
+ // Already compared with the recorded value when there is one.
195
+ fingerprint: expected?.get(id)?.fingerprint ??
196
+ await secretKeyFingerprint(raw),
197
+ });
198
+ }
199
+ return {
200
+ verified: expected !== undefined,
201
+ activeKeyId: active,
202
+ keys: checked,
203
+ };
204
+ },
205
+ purpose(name, purposeOptions = {}) {
206
+ if (typeof name !== "string" ||
207
+ name.length > MAX_PURPOSE_LENGTH ||
208
+ !PURPOSE_PATTERN.test(name)) {
209
+ throw new TypeError("A secret purpose is lowercase letters and digits separated by dots or dashes, at most 64 characters.");
210
+ }
211
+ const unbound = purposeOptions.unbound === true;
212
+ const purposeTtl = purposeOptions.ttlSeconds === undefined
213
+ ? undefined
214
+ : positiveSeconds(purposeOptions.ttlSeconds, "A purpose ttlSeconds");
215
+ const maxLength = purposeOptions.maxLength ?? DEFAULT_MAX_LENGTH;
216
+ if (!Number.isSafeInteger(maxLength) || maxLength <= 0) {
217
+ throw new RangeError("A purpose maxLength must be a positive integer.");
218
+ }
219
+ const cipherKeys = new Map();
220
+ /** This purpose's AES-256-GCM key under one key version. */
221
+ function cipherKey(keyId) {
222
+ let pending = cipherKeys.get(keyId);
223
+ if (!pending) {
224
+ pending = baseKey(keyId).then((base) => base === null ? null : subtle.deriveKey({
225
+ name: "HKDF",
226
+ hash: "SHA-256",
227
+ salt: new Uint8Array(0),
228
+ info: pae([DOMAIN, SEALED_FORMAT, "key", name]),
229
+ }, base, { name: "AES-GCM", length: 256 }, false, ["encrypt", "decrypt"]));
230
+ cipherKeys.set(keyId, pending);
231
+ pending.then((key) => key === null && cipherKeys.delete(keyId), () => cipherKeys.delete(keyId));
232
+ }
233
+ return pending;
234
+ }
235
+ const macKeys = new Map();
236
+ /** This purpose's HMAC-SHA-256 key under one key version. */
237
+ function macKey(keyId) {
238
+ let pending = macKeys.get(keyId);
239
+ if (!pending) {
240
+ pending = baseKey(keyId).then((base) => base === null ? null : subtle.deriveKey({
241
+ name: "HKDF",
242
+ hash: "SHA-256",
243
+ salt: new Uint8Array(0),
244
+ // A different label from the encryption key, so the two keys
245
+ // are independent although they share a source.
246
+ info: pae([DOMAIN, SEALED_FORMAT, "mac", name]),
247
+ }, base, { name: "HMAC", hash: "SHA-256", length: MAC_BYTES * 8 }, false, ["sign", "verify"]));
248
+ macKeys.set(keyId, pending);
249
+ pending.then((key) => key === null && macKeys.delete(keyId), () => macKeys.delete(keyId));
250
+ }
251
+ return pending;
252
+ }
253
+ function macInput(text, binding) {
254
+ return pae([
255
+ DOMAIN,
256
+ SEALED_FORMAT,
257
+ "mac-input",
258
+ name,
259
+ assertWellFormed(text, "Fingerprint input"),
260
+ ...bindingPieces(binding, unbound),
261
+ ]);
262
+ }
263
+ function associatedData(keyId, pieces) {
264
+ return pae([
265
+ DOMAIN,
266
+ SEALED_FORMAT,
267
+ "aad",
268
+ String(keyId),
269
+ name,
270
+ ...pieces,
271
+ ]);
272
+ }
273
+ async function encryptFrame(frame, pieces) {
274
+ const keyId = await activeKeyId();
275
+ const key = await cipherKey(keyId);
276
+ if (key === null) {
277
+ throw new SecretsKeyError(`Active key version ${keyId} is not configured.`);
278
+ }
279
+ const nonce = globalThis.crypto.getRandomValues(new Uint8Array(NONCE_BYTES));
280
+ const ciphertext = await subtle.encrypt({
281
+ name: "AES-GCM",
282
+ iv: nonce,
283
+ additionalData: associatedData(keyId, pieces),
284
+ tagLength: TAG_BYTES * 8,
285
+ }, key, frame);
286
+ const sealed = `${SEALED_FORMAT}.${keyId}.${toBase64Url(nonce)}.${toBase64Url(new Uint8Array(ciphertext))}`;
287
+ // Measured as if the key version had the most digits it can have, so
288
+ // that a value sealed today can still be resealed under any later key.
289
+ const worstCase = sealed.length - String(keyId).length +
290
+ MAX_KEY_ID_DIGITS;
291
+ if (worstCase > maxLength) {
292
+ throw new RangeError(`The sealed value would exceed this purpose's maxLength of ${maxLength}.`);
293
+ }
294
+ return sealed;
295
+ }
296
+ function expiryFor(sealOptions) {
297
+ const requested = sealOptions?.ttlSeconds === undefined
298
+ ? undefined
299
+ : positiveSeconds(sealOptions.ttlSeconds, "ttlSeconds");
300
+ if (requested !== undefined && purposeTtl !== undefined &&
301
+ requested > purposeTtl) {
302
+ throw new RangeError("ttlSeconds may shorten the purpose lifetime, not extend it.");
303
+ }
304
+ const ttl = requested ?? purposeTtl;
305
+ if (ttl === undefined) {
306
+ return undefined;
307
+ }
308
+ // Rounded up so a value never lives less than the lifetime asked for.
309
+ const expiresAt = Math.ceil(now() / 1000) + ttl;
310
+ if (!Number.isSafeInteger(expiresAt)) {
311
+ throw new RangeError("The clock did not return a usable time.");
312
+ }
313
+ return expiresAt;
314
+ }
315
+ async function sealPayload(kind, payload, binding, sealOptions) {
316
+ // Everything taken from the caller is read here, before the first
317
+ // await, so later mutation of their objects cannot change the result.
318
+ const pieces = bindingPieces(binding, unbound);
319
+ const frame = encodeFrame({
320
+ kind,
321
+ payload: payload(),
322
+ expiresAt: expiryFor(sealOptions),
323
+ });
324
+ return await encryptFrame(frame, pieces);
325
+ }
326
+ /** Decrypts and checks a sealed string, returning the raw and parsed frame. */
327
+ async function openFrame(sealed, binding) {
328
+ // An invalid binding is a programming error, not an unreadable value.
329
+ const pieces = bindingPieces(binding, unbound);
330
+ const parsed = parseSealed(sealed, maxLength);
331
+ const fail = (reason) => {
332
+ report({ purpose: name, keyId: parsed?.keyId ?? null, reason });
333
+ throw new SecretUnreadableError(reason);
334
+ };
335
+ if (parsed === null) {
336
+ return fail("malformed");
337
+ }
338
+ let key;
339
+ try {
340
+ key = await cipherKey(parsed.keyId);
341
+ }
342
+ catch (error) {
343
+ return fail(error instanceof SecretsKeyMismatchError
344
+ ? "key-mismatch"
345
+ : "key-unavailable");
346
+ }
347
+ if (key === null) {
348
+ return fail("unknown-key");
349
+ }
350
+ let raw;
351
+ try {
352
+ raw = new Uint8Array(await subtle.decrypt({
353
+ name: "AES-GCM",
354
+ iv: parsed.nonce,
355
+ additionalData: associatedData(parsed.keyId, pieces),
356
+ tagLength: TAG_BYTES * 8,
357
+ }, key, parsed.ciphertext));
358
+ }
359
+ catch {
360
+ return fail("authentication");
361
+ }
362
+ const frame = decodeFrame(raw);
363
+ if (frame === null) {
364
+ return fail("malformed");
365
+ }
366
+ if (frame.expiresAt === undefined) {
367
+ if (purposeTtl !== undefined) {
368
+ return fail("malformed");
369
+ }
370
+ }
371
+ else {
372
+ const nowSeconds = Math.floor(now() / 1000);
373
+ // Written so that a clock returning NaN fails closed.
374
+ if (!(nowSeconds < frame.expiresAt)) {
375
+ return fail("expired");
376
+ }
377
+ // Nothing this purpose seals can expire later than this, so a later
378
+ // expiry means a wrong clock when sealing, or a different purpose
379
+ // configuration. Either way it must not be honoured.
380
+ if (purposeTtl !== undefined &&
381
+ frame.expiresAt > nowSeconds + purposeTtl + skewSeconds) {
382
+ return fail("malformed");
383
+ }
384
+ }
385
+ return { parsed, raw, frame, pieces };
386
+ }
387
+ async function openPayload(kind, sealed, binding) {
388
+ const { parsed, frame } = await openFrame(sealed, binding);
389
+ if (frame.kind !== kind) {
390
+ report({ purpose: name, keyId: parsed.keyId, reason: "malformed" });
391
+ throw new SecretUnreadableError("malformed");
392
+ }
393
+ return { parsed, payload: frame.payload };
394
+ }
395
+ function decodeText(parsed, payload) {
396
+ try {
397
+ return decodeUtf8(payload);
398
+ }
399
+ catch {
400
+ report({ purpose: name, keyId: parsed.keyId, reason: "malformed" });
401
+ throw new SecretUnreadableError("malformed");
402
+ }
403
+ }
404
+ /** `null` for an unreadable value; programming errors still throw. */
405
+ async function orNull(opening) {
406
+ try {
407
+ return await opening;
408
+ }
409
+ catch (error) {
410
+ if (error instanceof SecretUnreadableError) {
411
+ return null;
412
+ }
413
+ throw error;
414
+ }
415
+ }
416
+ const purpose = {
417
+ name,
418
+ seal(value, binding, sealOptions) {
419
+ return sealPayload("text", () => encodeUtf8(value, "A sealed value"), binding, sealOptions);
420
+ },
421
+ sealJson(value, binding, sealOptions) {
422
+ return sealPayload("json", () => {
423
+ const json = JSON.stringify(value);
424
+ if (json === undefined) {
425
+ throw new TypeError("The value cannot be represented as JSON.");
426
+ }
427
+ return encodeUtf8(json, "A sealed JSON value");
428
+ }, binding, sealOptions);
429
+ },
430
+ sealBytes(value, binding, sealOptions) {
431
+ return sealPayload("bytes", () => {
432
+ // `new Uint8Array(x)` accepts numbers, strings and other typed
433
+ // arrays and would silently seal something else.
434
+ if (!(value instanceof Uint8Array)) {
435
+ throw new TypeError("sealBytes needs a Uint8Array.");
436
+ }
437
+ return value;
438
+ }, binding, sealOptions);
439
+ },
440
+ async open(sealed, binding) {
441
+ const { parsed, payload } = await openPayload("text", sealed, binding);
442
+ return decodeText(parsed, payload);
443
+ },
444
+ async openJson(sealed, binding) {
445
+ const { parsed, payload } = await openPayload("json", sealed, binding);
446
+ const text = decodeText(parsed, payload);
447
+ try {
448
+ return JSON.parse(text);
449
+ }
450
+ catch {
451
+ // A SyntaxError message quotes its input, which is the plaintext.
452
+ report({ purpose: name, keyId: parsed.keyId, reason: "malformed" });
453
+ throw new SecretUnreadableError("malformed");
454
+ }
455
+ },
456
+ async openBytes(sealed, binding) {
457
+ return (await openPayload("bytes", sealed, binding)).payload;
458
+ },
459
+ tryOpen(sealed, binding) {
460
+ return orNull(purpose.open(sealed, binding));
461
+ },
462
+ tryOpenJson(sealed, binding) {
463
+ return orNull(purpose.openJson(sealed, binding).then((value) => ({ value })));
464
+ },
465
+ tryOpenBytes(sealed, binding) {
466
+ return orNull(purpose.openBytes(sealed, binding));
467
+ },
468
+ keyId(sealed) {
469
+ return parseSealed(sealed, maxLength)?.keyId ?? null;
470
+ },
471
+ async needsReseal(sealed) {
472
+ const keyId = parseSealed(sealed, maxLength)?.keyId;
473
+ return keyId !== undefined && keyId !== await activeKeyId();
474
+ },
475
+ async reseal(sealed, binding) {
476
+ const { parsed, raw, pieces } = await openFrame(sealed, binding);
477
+ if (parsed.keyId === await activeKeyId()) {
478
+ return sealed;
479
+ }
480
+ // The decrypted frame is encrypted again as it is, so the payload
481
+ // kind and the original expiry are preserved rather than renewed.
482
+ return await encryptFrame(new Uint8Array(raw), pieces);
483
+ },
484
+ async mac(text, binding, macOptions) {
485
+ // Read from the caller before the first await, as in `seal`.
486
+ const input = macInput(text, binding);
487
+ const requested = macOptions?.keyId;
488
+ if (requested !== undefined && !isSecretKeyId(requested)) {
489
+ throw new TypeError("keyId must be a key version.");
490
+ }
491
+ const keyId = requested ?? await activeKeyId();
492
+ const key = await macKey(keyId);
493
+ if (key === null) {
494
+ throw new SecretsKeyError(`Key version ${keyId} is not configured.`);
495
+ }
496
+ const tag = await subtle.sign("HMAC", key, input);
497
+ return `${KEYED_FINGERPRINT_FORMAT}.${keyId}.${toBase64Url(new Uint8Array(tag))}`;
498
+ },
499
+ async macMatches(stored, text, binding) {
500
+ const input = macInput(text, binding);
501
+ const parsed = parseFingerprint(stored);
502
+ if (parsed === null) {
503
+ report({ purpose: name, keyId: null, reason: "malformed" });
504
+ return false;
505
+ }
506
+ const key = await macKey(parsed.keyId);
507
+ if (key === null) {
508
+ report({
509
+ purpose: name,
510
+ keyId: parsed.keyId,
511
+ reason: "unknown-key",
512
+ });
513
+ return false;
514
+ }
515
+ // `verify` compares the tags without an early exit.
516
+ return await subtle.verify("HMAC", key, parsed.tag, input);
517
+ },
518
+ async macNeedsRefresh(stored) {
519
+ const keyId = parseFingerprint(stored)?.keyId;
520
+ return keyId !== undefined && keyId !== await activeKeyId();
521
+ },
522
+ assertReady: () => secrets.assertReady(),
523
+ };
524
+ return purpose;
525
+ },
526
+ };
527
+ return secrets;
528
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gadicc/loom",
3
- "version": "1.35.3",
3
+ "version": "1.37.0",
4
4
  "description": "Deno-first shared helpers for extracting common Next.js app code.",
5
5
  "homepage": "https://github.com/gadicc/loom#readme",
6
6
  "repository": {
@@ -79,6 +79,22 @@
79
79
  "import": "./esm/src/next/pwa/worker.js",
80
80
  "require": "./script/src/next/pwa/worker.js"
81
81
  },
82
+ "./src/next/secrets/mod.js": {
83
+ "import": "./esm/src/next/secrets/mod.js",
84
+ "require": "./script/src/next/secrets/mod.js"
85
+ },
86
+ "./src/secrets/legacy.js": {
87
+ "import": "./esm/src/secrets/legacy.js",
88
+ "require": "./script/src/secrets/legacy.js"
89
+ },
90
+ "./src/secrets/maintenance.js": {
91
+ "import": "./esm/src/secrets/maintenance.js",
92
+ "require": "./script/src/secrets/maintenance.js"
93
+ },
94
+ "./src/secrets/mod.js": {
95
+ "import": "./esm/src/secrets/mod.js",
96
+ "require": "./script/src/secrets/mod.js"
97
+ },
82
98
  "./client-data": {
83
99
  "types": "./esm/src/clientData/mod.d.ts",
84
100
  "import": "./esm/src/clientData/mod.js",
@@ -156,6 +172,21 @@
156
172
  "import": "./esm/src/mail/postal.js",
157
173
  "require": "./script/src/mail/postal.js"
158
174
  },
175
+ "./secrets": {
176
+ "types": "./esm/src/secrets/mod.d.ts",
177
+ "import": "./esm/src/secrets/mod.js",
178
+ "require": "./script/src/secrets/mod.js"
179
+ },
180
+ "./secrets/legacy": {
181
+ "types": "./esm/src/secrets/legacy.d.ts",
182
+ "import": "./esm/src/secrets/legacy.js",
183
+ "require": "./script/src/secrets/legacy.js"
184
+ },
185
+ "./secrets/maintenance": {
186
+ "types": "./esm/src/secrets/maintenance.d.ts",
187
+ "import": "./esm/src/secrets/maintenance.js",
188
+ "require": "./script/src/secrets/maintenance.js"
189
+ },
159
190
  "./jobs": {
160
191
  "types": "./esm/src/jobs/mod.d.ts",
161
192
  "import": "./esm/src/jobs/mod.js",
@@ -216,6 +247,11 @@
216
247
  "import": "./esm/src/next/files/mod.js",
217
248
  "require": "./script/src/next/files/mod.js"
218
249
  },
250
+ "./next/secrets": {
251
+ "types": "./esm/src/next/secrets/mod.d.ts",
252
+ "import": "./esm/src/next/secrets/mod.js",
253
+ "require": "./script/src/next/secrets/mod.js"
254
+ },
219
255
  "./next/mail": {
220
256
  "types": "./esm/src/next/mail/mod.d.ts",
221
257
  "import": "./esm/src/next/mail/mod.js",
@@ -244,7 +280,7 @@
244
280
  },
245
281
  "scripts": {},
246
282
  "loom": {
247
- "gitCommit": "4166c6a2650ce49339de1a3e1f84aef7513cc1ad"
283
+ "gitCommit": "3e8d72e229a00f56e1cd4f707b056c59f61df8b7"
248
284
  },
249
285
  "bin": {
250
286
  "loom": "script/bin/loom.js"
@@ -263,6 +299,15 @@
263
299
  "files": [
264
300
  "esm/src/files/mod.d.ts"
265
301
  ],
302
+ "secrets": [
303
+ "esm/src/secrets/mod.d.ts"
304
+ ],
305
+ "secrets/legacy": [
306
+ "esm/src/secrets/legacy.d.ts"
307
+ ],
308
+ "secrets/maintenance": [
309
+ "esm/src/secrets/maintenance.d.ts"
310
+ ],
266
311
  "files/*": [
267
312
  "esm/src/files/*.d.ts"
268
313
  ],
@@ -290,6 +335,9 @@
290
335
  "next/files": [
291
336
  "esm/src/next/files/mod.d.ts"
292
337
  ],
338
+ "next/secrets": [
339
+ "esm/src/next/secrets/mod.d.ts"
340
+ ],
293
341
  "next/mail": [
294
342
  "esm/src/next/mail/mod.d.ts"
295
343
  ],
@@ -1 +1 @@
1
- {"version":3,"file":"externalSkills.d.ts","sourceRoot":"","sources":["../../../src/src/cli/externalSkills.ts"],"names":[],"mappings":"AAAA,KAAK,WAAW,GAAG;IACjB,YAAY,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACtC,eAAe,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACzC,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB,CAAC;AAEF,2EAA2E;AAC3E,MAAM,MAAM,oBAAoB,GAAG;IACjC,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,SAAS,GAAG,YAAY,GAAG,SAAS,GAAG,UAAU,CAAC;CAC3D,CAAC;AAEF,sEAAsE;AACtE,MAAM,MAAM,mBAAmB,GAAG;IAChC,WAAW,EAAE,MAAM,CAAC;IACpB,GAAG,EAAE,MAAM,CAAC;IACZ,aAAa,EAAE,MAAM,CAAC;IACtB,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF,KAAK,iBAAiB,GAAG;IACvB,WAAW,EAAE,MAAM,CAAC;IACpB,eAAe,CAAC,EAAE,MAAM,EAAE,CAAC;IAC3B,qBAAqB,CAAC,EAAE,MAAM,EAAE,CAAC;IACjC,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;IACxB,OAAO,EAAE,oBAAoB,CAAC;IAC9B,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,mBAAmB,CAAC;IAC5B,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB,CAAC;AAEF,0DAA0D;AAC1D,MAAM,MAAM,aAAa,GAAG,IAAI,CAC9B,iBAAiB,EACf,iBAAiB,GACjB,uBAAuB,GACvB,cAAc,GACd,UAAU,CACb,CAAC;AA2NF,+EAA+E;AAC/E,wBAAgB,4BAA4B,CAC1C,WAAW,EAAE,WAAW,EACxB,iBAAiB,EAAE,GAAG,CAAC,MAAM,CAAC,EAC9B,WAAW,EAAE,OAAO,GACnB,aAAa,EAAE,CAqBjB;AAED,iEAAiE;AACjE,wBAAgB,oBAAoB,IAAI,aAAa,EAAE,CAUtD"}
1
+ {"version":3,"file":"externalSkills.d.ts","sourceRoot":"","sources":["../../../src/src/cli/externalSkills.ts"],"names":[],"mappings":"AAAA,KAAK,WAAW,GAAG;IACjB,YAAY,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACtC,eAAe,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACzC,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB,CAAC;AAEF,2EAA2E;AAC3E,MAAM,MAAM,oBAAoB,GAAG;IACjC,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,SAAS,GAAG,YAAY,GAAG,SAAS,GAAG,UAAU,CAAC;CAC3D,CAAC;AAEF,sEAAsE;AACtE,MAAM,MAAM,mBAAmB,GAAG;IAChC,WAAW,EAAE,MAAM,CAAC;IACpB,GAAG,EAAE,MAAM,CAAC;IACZ,aAAa,EAAE,MAAM,CAAC;IACtB,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF,KAAK,iBAAiB,GAAG;IACvB,WAAW,EAAE,MAAM,CAAC;IACpB,eAAe,CAAC,EAAE,MAAM,EAAE,CAAC;IAC3B,qBAAqB,CAAC,EAAE,MAAM,EAAE,CAAC;IACjC,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;IACxB,OAAO,EAAE,oBAAoB,CAAC;IAC9B,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,mBAAmB,CAAC;IAC5B,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB,CAAC;AAEF,0DAA0D;AAC1D,MAAM,MAAM,aAAa,GAAG,IAAI,CAC9B,iBAAiB,EACf,iBAAiB,GACjB,uBAAuB,GACvB,cAAc,GACd,UAAU,CACb,CAAC;AAsOF,+EAA+E;AAC/E,wBAAgB,4BAA4B,CAC1C,WAAW,EAAE,WAAW,EACxB,iBAAiB,EAAE,GAAG,CAAC,MAAM,CAAC,EAC9B,WAAW,EAAE,OAAO,GACnB,aAAa,EAAE,CAqBjB;AAED,iEAAiE;AACjE,wBAAgB,oBAAoB,IAAI,aAAa,EAAE,CAUtD"}