@tokenoftrust/storefront-runner 6.12.1 → 6.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.
|
@@ -29,52 +29,6 @@ const SYNTHETIC_DIGEST = "0".repeat(64);
|
|
|
29
29
|
const CONSENT_DAYS = 30;
|
|
30
30
|
|
|
31
31
|
type VaultKms = Parameters<typeof sealOrderContact>[1];
|
|
32
|
-
type GeneratedKey = Awaited<ReturnType<VaultKms["generateDataKey"]>>;
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* A KMS client that answers every repeat of a request from its first answer: a repeated data-key
|
|
36
|
-
* generation for the same context, or a decrypt of a blob it generated or already decrypted, makes no
|
|
37
|
-
* further KMS call. Key bytes are handed out as fresh copies because the vault zeroes what it imports;
|
|
38
|
-
* `clear` zeroes the copies it holds.
|
|
39
|
-
*/
|
|
40
|
-
function replayingKms(kms: VaultKms): { client: VaultKms; clear(): void } {
|
|
41
|
-
const generated = new Map<string, GeneratedKey>();
|
|
42
|
-
const decrypted = new Map<string, Uint8Array>();
|
|
43
|
-
const keep = (bytes: Uint8Array): Uint8Array => {
|
|
44
|
-
const copy = new Uint8Array(bytes);
|
|
45
|
-
bytes.fill(0);
|
|
46
|
-
return copy;
|
|
47
|
-
};
|
|
48
|
-
const client: VaultKms = {
|
|
49
|
-
async generateDataKey(ctx) {
|
|
50
|
-
const id = JSON.stringify(ctx);
|
|
51
|
-
let first = generated.get(id);
|
|
52
|
-
if (!first) {
|
|
53
|
-
const answer = await kms.generateDataKey(ctx);
|
|
54
|
-
first = { ...answer, plaintext: keep(answer.plaintext) };
|
|
55
|
-
generated.set(id, first);
|
|
56
|
-
decrypted.set(answer.ciphertextBlob, first.plaintext);
|
|
57
|
-
}
|
|
58
|
-
return { ...first, plaintext: new Uint8Array(first.plaintext) };
|
|
59
|
-
},
|
|
60
|
-
async decrypt(blob, ctx, keyArn) {
|
|
61
|
-
let first = decrypted.get(blob);
|
|
62
|
-
if (!first) {
|
|
63
|
-
first = keep(await kms.decrypt(blob, ctx, keyArn));
|
|
64
|
-
decrypted.set(blob, first);
|
|
65
|
-
}
|
|
66
|
-
return new Uint8Array(first);
|
|
67
|
-
},
|
|
68
|
-
};
|
|
69
|
-
return {
|
|
70
|
-
client,
|
|
71
|
-
clear() {
|
|
72
|
-
for (const bytes of decrypted.values()) bytes.fill(0);
|
|
73
|
-
generated.clear();
|
|
74
|
-
decrypted.clear();
|
|
75
|
-
},
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
32
|
|
|
79
33
|
export interface SyntheticOrderDeps {
|
|
80
34
|
/** Vault, consent and install reads. */
|
|
@@ -195,9 +149,8 @@ export async function bindTestProcessor(
|
|
|
195
149
|
|
|
196
150
|
/**
|
|
197
151
|
* Write one synthetic preview order, its source authority, and its sealed RFC-reserved recipient.
|
|
198
|
-
* The vault keys are
|
|
199
|
-
*
|
|
200
|
-
* the order exists.
|
|
152
|
+
* The vault keys are opened and stored before the order is written and the seal uses that key
|
|
153
|
+
* material, so a KMS failure refuses with nothing written and cannot arrive after the order exists.
|
|
201
154
|
*/
|
|
202
155
|
export async function createSyntheticOrder(
|
|
203
156
|
deps: SyntheticOrderDeps,
|
|
@@ -209,23 +162,9 @@ export async function createSyntheticOrder(
|
|
|
209
162
|
} catch {
|
|
210
163
|
return { ok: false, code: "vault-key-unavailable" };
|
|
211
164
|
}
|
|
212
|
-
const replaying = replayingKms(kms);
|
|
213
|
-
try {
|
|
214
|
-
return await createWithKeys(deps, scope, replaying.client);
|
|
215
|
-
} finally {
|
|
216
|
-
replaying.clear();
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
async function createWithKeys(
|
|
221
|
-
deps: SyntheticOrderDeps,
|
|
222
|
-
scope: SyntheticScope,
|
|
223
|
-
kms: VaultKms,
|
|
224
|
-
): Promise<SyntheticResult<{ orderId: string; stage: typeof SYNTHETIC_STAGE }>> {
|
|
225
165
|
const now = nowOf(deps);
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
}
|
|
166
|
+
const keys = await prepareOrderContactKeys(deps.db, kms, scope.tenantId, SYNTHETIC_STAGE, now);
|
|
167
|
+
if (!keys) return { ok: false, code: "vault-key-unavailable" };
|
|
229
168
|
const nowIso = now.toISOString();
|
|
230
169
|
const sourceOrderId = `${now.getTime()}${Math.floor(Math.random() * 1_000_000)}`;
|
|
231
170
|
const orderId = `synthetic-${sourceOrderId}`;
|
|
@@ -260,6 +199,7 @@ async function createWithKeys(
|
|
|
260
199
|
jti: `synthetic-seal-${orderId}`,
|
|
261
200
|
regulated: false,
|
|
262
201
|
now,
|
|
202
|
+
keys,
|
|
263
203
|
});
|
|
264
204
|
if (!sealed.ok) {
|
|
265
205
|
return { ok: false, code: sealed.reason === "vault-key-unavailable" ? "vault-key-unavailable" : "contact-refused" };
|
|
@@ -28,6 +28,7 @@ import {
|
|
|
28
28
|
deleteUnreferencedRetiredKeys,
|
|
29
29
|
keyForKid,
|
|
30
30
|
rotateDueKeys,
|
|
31
|
+
type ActiveDataKey,
|
|
31
32
|
} from "@/lib/customers/contactVault/dataKeys.js";
|
|
32
33
|
import { openField, sealField, type FieldBinding } from "@/lib/customers/contactVault/fieldEnvelope.js";
|
|
33
34
|
import { blindIndex, normalizeEmail, normalizePhone } from "@/lib/customers/contactVault/blindIndex.js";
|
|
@@ -351,6 +352,11 @@ export interface SealInput {
|
|
|
351
352
|
regulated: boolean;
|
|
352
353
|
now: Date;
|
|
353
354
|
policy?: RetentionPolicy;
|
|
355
|
+
/**
|
|
356
|
+
* Key material from {@link prepareOrderContactKeys}. The first attempt seals under it without
|
|
357
|
+
* reading the active key; a retry after a concurrent change reads the active key afresh.
|
|
358
|
+
*/
|
|
359
|
+
keys?: PreparedOrderContactKeys;
|
|
354
360
|
}
|
|
355
361
|
|
|
356
362
|
interface SealSnapshot extends OrderSnapshot {
|
|
@@ -394,29 +400,67 @@ function placedMonth(placedAt: Date): string {
|
|
|
394
400
|
return `${placedAt.getUTCFullYear()}${String(placedAt.getUTCMonth() + 1).padStart(2, "0")}`;
|
|
395
401
|
}
|
|
396
402
|
|
|
403
|
+
/** Order-contact key material opened before a caller's own writes; see {@link prepareOrderContactKeys}. */
|
|
404
|
+
export interface PreparedOrderContactKeys {
|
|
405
|
+
readonly order: ActiveDataKey;
|
|
406
|
+
readonly bidx: ActiveDataKey;
|
|
407
|
+
}
|
|
408
|
+
|
|
397
409
|
/**
|
|
398
|
-
*
|
|
399
|
-
* refuse before its own writes when the vault cannot seal.
|
|
400
|
-
* generated
|
|
401
|
-
*
|
|
402
|
-
*
|
|
410
|
+
* Open both order-contact keys for this tenant and stage and make them durable, for a caller that must
|
|
411
|
+
* refuse before its own writes when the vault cannot seal. Returns null when a key cannot be opened or
|
|
412
|
+
* generated, leaving no key row of its own behind. Otherwise any newly generated key is stored (the
|
|
413
|
+
* insert yields to an active key another writer stored first, which is opened instead) and the
|
|
414
|
+
* returned material goes to {@link sealOrderContact} as `keys`: the seal uses it without reading the
|
|
415
|
+
* active key or calling KMS for it, and a stored kid stays sealable after rotation retires it.
|
|
403
416
|
*/
|
|
404
417
|
export async function prepareOrderContactKeys(
|
|
405
418
|
db: Queryable, kms: KmsClient, tenantId: string, stage: VaultStage, now: Date,
|
|
406
|
-
): Promise<
|
|
407
|
-
const
|
|
419
|
+
): Promise<PreparedOrderContactKeys | null> {
|
|
420
|
+
const orderCtx = keyContext(tenantId, stage, ORDER_KEY);
|
|
421
|
+
const bidxCtx = keyContext(tenantId, stage, BIDX_KEY);
|
|
422
|
+
let order: ActiveDataKey;
|
|
423
|
+
let bidx: ActiveDataKey;
|
|
408
424
|
try {
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
const pending = opened.flatMap((key) => key.pending);
|
|
412
|
-
if (pending.length === 0) return true;
|
|
413
|
-
await requireBatch(db)(pending);
|
|
414
|
-
for (const ctx of contexts) await activeKey(db, kms, ctx, now);
|
|
415
|
-
return true;
|
|
425
|
+
order = await activeKey(db, kms, orderCtx, now);
|
|
426
|
+
bidx = await activeKey(db, kms, bidxCtx, now);
|
|
416
427
|
} catch (error) {
|
|
417
|
-
if (error instanceof VaultKeyUnavailableError) return
|
|
428
|
+
if (error instanceof VaultKeyUnavailableError) return null;
|
|
418
429
|
throw error;
|
|
419
430
|
}
|
|
431
|
+
const generated = ([[orderCtx, order], [bidxCtx, bidx]] as const).filter(([, key]) => key.pending.length > 0);
|
|
432
|
+
if (generated.length === 0) return { order, bidx };
|
|
433
|
+
await requireBatch(db)(generated.flatMap(([, key]) => key.pending));
|
|
434
|
+
try {
|
|
435
|
+
return { order: await storedKey(db, kms, orderCtx, order, now), bidx: await storedKey(db, kms, bidxCtx, bidx, now) };
|
|
436
|
+
} catch (error) {
|
|
437
|
+
if (!(error instanceof VaultKeyUnavailableError)) throw error;
|
|
438
|
+
await requireBatch(db)(generated.map(([ctx, key]) => unreferencedKeyCleanup(ctx, key.kid)));
|
|
439
|
+
return null;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
/** The key to seal under after storing `mine`: itself when its insert landed, else the active winner. */
|
|
444
|
+
async function storedKey(db: Queryable, kms: KmsClient, ctx: EncryptionContext, mine: ActiveDataKey, now: Date): Promise<ActiveDataKey> {
|
|
445
|
+
if (mine.pending.length === 0) return mine;
|
|
446
|
+
const active = await db.first<{ kid: string }>(
|
|
447
|
+
`SELECT kid FROM contact_vault_data_keys WHERE tenant_id = ? AND stage = ? AND purpose = ? AND retired_at IS NULL`,
|
|
448
|
+
ctx.tenantId, ctx.stage, ctx.purpose,
|
|
449
|
+
);
|
|
450
|
+
if (active?.kid === mine.kid) return { kid: mine.kid, key: mine.key, pending: [] };
|
|
451
|
+
return activeKey(db, kms, ctx, now);
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/** Removes `kid` when no order-contact record (or, for the blind-index key, contact message) names it. */
|
|
455
|
+
function unreferencedKeyCleanup(ctx: EncryptionContext, kid: string): QueryStatement {
|
|
456
|
+
const { tenantId, stage } = ctx;
|
|
457
|
+
return ctx.purpose === ORDER_KEY
|
|
458
|
+
? cleanupStatement(ctx, kid,
|
|
459
|
+
`SELECT 1 FROM order_contact_vault WHERE tenant_id = ? AND stage = ? AND order_kid = ?`, [tenantId, stage, kid])
|
|
460
|
+
: cleanupStatement(ctx, kid,
|
|
461
|
+
`SELECT 1 FROM order_contact_vault WHERE tenant_id = ? AND stage = ? AND bidx_kid = ?
|
|
462
|
+
UNION ALL SELECT 1 FROM contact_messages WHERE tenant_id = ? AND stage = ? AND bidx_kid = ?`,
|
|
463
|
+
[tenantId, stage, kid, tenantId, stage, kid]);
|
|
420
464
|
}
|
|
421
465
|
|
|
422
466
|
/** Seal one order's contact from a verified contact command. */
|
|
@@ -462,8 +506,8 @@ async function sealAttempt(db: Queryable, kms: KmsClient, input: SealInput, atte
|
|
|
462
506
|
|
|
463
507
|
const orderCtx = keyContext(tenantId, stage, ORDER_KEY);
|
|
464
508
|
const bidxCtx = keyContext(tenantId, stage, BIDX_KEY);
|
|
465
|
-
const orderKey = await activeKey(db, kms, orderCtx, now);
|
|
466
|
-
const bidxKey = await activeKey(db, kms, bidxCtx, now);
|
|
509
|
+
const orderKey = input.keys?.order ?? await activeKey(db, kms, orderCtx, now);
|
|
510
|
+
const bidxKey = input.keys?.bidx ?? await activeKey(db, kms, bidxCtx, now);
|
|
467
511
|
|
|
468
512
|
const factId = randomHex128();
|
|
469
513
|
// A group whose expiry has already passed is never written: only live groups are sealed.
|
|
@@ -544,16 +588,8 @@ async function sealAttempt(db: Queryable, kms: KmsClient, input: SealInput, atte
|
|
|
544
588
|
params: [tenantId, stage, issuer, jti, nowIso, ...vaultRowParams],
|
|
545
589
|
},
|
|
546
590
|
];
|
|
547
|
-
if (orderKey.pending.length > 0)
|
|
548
|
-
|
|
549
|
-
`SELECT 1 FROM order_contact_vault WHERE tenant_id = ? AND stage = ? AND order_kid = ?`, [tenantId, stage, orderKey.kid]));
|
|
550
|
-
}
|
|
551
|
-
if (bidxKey.pending.length > 0) {
|
|
552
|
-
statements.push(cleanupStatement(bidxCtx, bidxKey.kid,
|
|
553
|
-
`SELECT 1 FROM order_contact_vault WHERE tenant_id = ? AND stage = ? AND bidx_kid = ?
|
|
554
|
-
UNION ALL SELECT 1 FROM contact_messages WHERE tenant_id = ? AND stage = ? AND bidx_kid = ?`,
|
|
555
|
-
[tenantId, stage, bidxKey.kid, tenantId, stage, bidxKey.kid]));
|
|
556
|
-
}
|
|
591
|
+
if (orderKey.pending.length > 0) statements.push(unreferencedKeyCleanup(orderCtx, orderKey.kid));
|
|
592
|
+
if (bidxKey.pending.length > 0) statements.push(unreferencedKeyCleanup(bidxCtx, bidxKey.kid));
|
|
557
593
|
try {
|
|
558
594
|
await requireBatch(db)(statements);
|
|
559
595
|
} catch (error) {
|
|
@@ -569,7 +605,7 @@ async function sealAttempt(db: Queryable, kms: KmsClient, input: SealInput, atte
|
|
|
569
605
|
if (after) return after;
|
|
570
606
|
// Nothing committed: the order's retention snapshot or the active key changed underneath. Re-read
|
|
571
607
|
// and re-seal, bounded.
|
|
572
|
-
if (attempts > 1) return sealAttempt(db, kms, input, attempts - 1);
|
|
608
|
+
if (attempts > 1) return sealAttempt(db, kms, { ...input, keys: undefined }, attempts - 1);
|
|
573
609
|
throw new Error("contact-retention-changed");
|
|
574
610
|
}
|
|
575
611
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tokenoftrust/storefront-runner",
|
|
3
|
-
"version": "6.12.
|
|
3
|
+
"version": "6.12.2",
|
|
4
4
|
"license": "SEE LICENSE IN LICENSE",
|
|
5
5
|
"description": "World-shareable storefront runner: multi-tenant renderer on Astro/Cloudflare. No control plane.",
|
|
6
6
|
"packageManager": "pnpm@11.9.0",
|