@tearleads/client-sdk 0.1.240 → 0.1.242
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/data/documents/shared/sync/syncResponseAuthorization.js +12 -1
- package/data/documents/shared/sync/syncResponseAuthorization.js.map +1 -1
- package/data/documents/shared/sync/syncResponses.js +2 -2
- package/data/documents/shared/sync/syncResponses.js.map +1 -1
- package/data/keyingProjectionVerification/checkpoints/checkpointContext.js +12 -3
- package/data/keyingProjectionVerification/checkpoints/checkpointContext.js.map +1 -1
- package/data/keyingProjectionVerification/checkpoints/verifiedBundleBytes.js +73 -8
- package/data/keyingProjectionVerification/checkpoints/verifiedBundleBytes.js.map +1 -1
- package/data/keyingProjectionVerification/container/containerAncestorCitations.js +20 -4
- package/data/keyingProjectionVerification/container/containerAncestorCitations.js.map +1 -1
- package/data/keyingProjectionVerification/container/containerManifestVerification.js +43 -32
- package/data/keyingProjectionVerification/container/containerManifestVerification.js.map +1 -1
- package/data/keyingProjectionVerification/container/containerSignedParentReference.js +129 -0
- package/data/keyingProjectionVerification/container/containerSignedParentReference.js.map +1 -0
- package/data/keyingProjectionVerification/document/documentManifestVerification.js +28 -7
- package/data/keyingProjectionVerification/document/documentManifestVerification.js.map +1 -1
- package/data/persistence/verifiedCitations/bundleCitationRows.js.map +1 -1
- package/data/persistence/verifiedCitations/citationCollection.js +2 -2
- package/data/persistence/verifiedCitations/citationCollection.js.map +1 -1
- package/data/persistence/verifiedManifestBundlePersistence.d.ts +3 -2
- package/data/persistence/verifiedManifestBundlePersistence.js +1 -1
- package/data/persistence/verifiedManifestBundlePersistence.js.map +1 -1
- package/internal/api-client/ApiClient.d.ts +7 -7
- package/internal/api-client/client/apiClientContext.d.ts +1 -1
- package/internal/api-client/client/blobs.d.ts +1 -1
- package/internal/api-client/client/documents.d.ts +6 -6
- package/internal/api-client/verifiedManifestCitations.d.ts +4 -3
- package/internal/api-client/verifiedManifestCitations.js +3 -3
- package/internal/api-client/verifiedManifestCitations.js.map +1 -1
- package/internal/validators/operation/attachments.d.ts +2 -2
- package/internal/validators/operation/documentSync.d.ts +24 -24
- package/internal/validators/operation/writerProjections.d.ts +8 -8
- package/internal/validators/response/blob.d.ts +6 -6
- package/internal/validators/response/blob.js +12 -2
- package/internal/validators/response/blob.js.map +1 -1
- package/internal/validators/response/container.d.ts +4 -4
- package/internal/validators/response/documentMutation.d.ts +4 -4
- package/internal/validators/response/documentSyncSchema.d.ts +48 -48
- package/internal/validators/response/documentSyncSchema.js +1 -1
- package/internal/validators/response/documentSyncSchema.js.map +1 -1
- package/internal/validators/response/manifestBundleReferences.js +4 -3
- package/internal/validators/response/manifestBundleReferences.js.map +1 -1
- package/internal/validators/response/writerProjectionWire.d.ts +4 -4
- package/internal/validators/response/writerProjectionWire.js +8 -5
- package/internal/validators/response/writerProjectionWire.js.map +1 -1
- package/internal/validators/util/index.d.ts +1 -0
- package/internal/validators/util/index.js +1 -0
- package/internal/validators/util/index.js.map +1 -1
- package/internal/validators/util/servedManifestBundle.d.ts +71 -0
- package/internal/validators/util/servedManifestBundle.js +100 -0
- package/internal/validators/util/servedManifestBundle.js.map +1 -0
- package/package.json +1 -1
- package/workflows/blobs/hydration/decrypt.js +17 -1
- package/workflows/blobs/hydration/decrypt.js.map +1 -1
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import { KeyingVerificationError, principalPolicyMatchesReference, readKeyingCanonicalJson, verifyContainerAccessManifest, } from "../../../internal/crypto/index.js";
|
|
2
|
-
import { readCanonicalRecord } from "../../keying/canonicalJson.js";
|
|
3
2
|
import { assertCanonicalEqual, verifyAccessEventBundle, } from "../checkpoints/bundleVerification.js";
|
|
4
3
|
import { loadManifestCheckpointVerification, verifyCachedManifestCheckpoint, } from "../checkpoints/manifestCheckpointVerification.js";
|
|
5
|
-
import { recordVerifiedBundleBytes } from "../checkpoints/verifiedBundleBytes.js";
|
|
6
|
-
import { readKeyingVerificationShape } from "../error.js";
|
|
4
|
+
import { assertUnchangedSinceVerification, matchesVerifiedBundleBytes, recordVerifiedBundleBytes, } from "../checkpoints/verifiedBundleBytes.js";
|
|
7
5
|
import { collectReferencedPrincipalPolicies } from "../principalPolicy/principalPolicyVerification.js";
|
|
8
|
-
import { readAccessManifest
|
|
6
|
+
import { readAccessManifest } from "../readers.js";
|
|
9
7
|
import { assertCitedAncestorsDoNotRegress, resolveCitedAncestorPath, } from "./containerAncestorCitations.js";
|
|
8
|
+
import { assertSignedContainerParentPin, readContainerEventBodyParentReference, readSignedContainerParentReference, } from "./containerSignedParentReference.js";
|
|
10
9
|
async function collectManifestAuthorizationPolicies(input) {
|
|
11
10
|
const missingPrincipalHeads = input.referencedPrincipalHeads.filter((reference) => !input.authorizationEvidence.some((policy) => principalPolicyMatchesReference({ policy, reference })));
|
|
12
11
|
if (input.requireAuthorizationEvidence && missingPrincipalHeads.length > 0) {
|
|
@@ -29,7 +28,11 @@ async function collectManifestAuthorizationPolicies(input) {
|
|
|
29
28
|
export async function verifyContainerManifestBundle(input) {
|
|
30
29
|
const parentPath = await resolveContainerManifestVerificationParentPath(input);
|
|
31
30
|
const cached = input.verifiedByHash.get(input.bundle.manifestHash);
|
|
32
|
-
|
|
31
|
+
// A cache hit vouches for the bytes it verified: a copy that differs is
|
|
32
|
+
// verified afresh, so a warm cache neither vouches for bytes no
|
|
33
|
+
// verification saw nor refuses a copy a cold one would accept.
|
|
34
|
+
if (cached && matchesVerifiedBundleBytes(cached, input.bundle)) {
|
|
35
|
+
assertVerifiedState(input, cached);
|
|
33
36
|
assertContainerParentPathMatches({
|
|
34
37
|
label: input.label,
|
|
35
38
|
parentPath,
|
|
@@ -42,6 +45,9 @@ export async function verifyContainerManifestBundle(input) {
|
|
|
42
45
|
localCheckpoints: input.checkpointContext.localCheckpoints,
|
|
43
46
|
verifiedManifests: input.verifiedByHash,
|
|
44
47
|
});
|
|
48
|
+
// The caller reads this bundle on return, after the checkpoint read.
|
|
49
|
+
assertUnchangedSinceVerification(cached, input.bundle, input.label);
|
|
50
|
+
assertVerifiedState(input, cached);
|
|
45
51
|
}
|
|
46
52
|
return cached;
|
|
47
53
|
}
|
|
@@ -57,7 +63,10 @@ export async function verifyContainerManifestBundle(input) {
|
|
|
57
63
|
verifying.delete(input.bundle.manifestHash);
|
|
58
64
|
}
|
|
59
65
|
}
|
|
60
|
-
async function verifyFreshContainerManifestBundle(
|
|
66
|
+
async function verifyFreshContainerManifestBundle(served, parentPath) {
|
|
67
|
+
// Verify and record one copy taken before the first await, so the bytes
|
|
68
|
+
// recorded as verified are exactly the bytes verified.
|
|
69
|
+
const input = { ...served, bundle: structuredClone(served.bundle) };
|
|
61
70
|
const event = await verifyAccessEventBundle(input);
|
|
62
71
|
const manifest = readAccessManifest(input.bundle.manifest, `${input.label} manifest`);
|
|
63
72
|
const authorization = await resolveContainerManifestAuthorization(input, event, parentPath);
|
|
@@ -104,14 +113,20 @@ async function verifyFreshContainerManifestBundle(input, parentPath) {
|
|
|
104
113
|
if (!verified.ok) {
|
|
105
114
|
throw new KeyingVerificationError(verified.error.code, `${input.label} manifest verification failed: ${verified.error.message}`);
|
|
106
115
|
}
|
|
116
|
+
assertSignedContainerParentPin(input, verified.value);
|
|
117
|
+
assertVerifiedState(input, verified.value);
|
|
118
|
+
input.verifiedByHash.set(input.bundle.manifestHash, verified.value);
|
|
119
|
+
recordVerifiedBundleBytes(verified.value, input.bundle);
|
|
120
|
+
assertUnchangedSinceVerification(verified.value, served.bundle, input.label, input.bundle);
|
|
121
|
+
assertVerifiedState(served, verified.value);
|
|
122
|
+
return verified.value;
|
|
123
|
+
}
|
|
124
|
+
function assertVerifiedState(input, verified) {
|
|
107
125
|
assertCanonicalEqual({
|
|
108
126
|
actual: input.bundle.state,
|
|
109
|
-
expected: readKeyingCanonicalJson(verified.
|
|
127
|
+
expected: readKeyingCanonicalJson(verified.state, `${input.label} state`),
|
|
110
128
|
label: `${input.label} state`,
|
|
111
129
|
});
|
|
112
|
-
input.verifiedByHash.set(input.bundle.manifestHash, verified.value);
|
|
113
|
-
recordVerifiedBundleBytes(verified.value, input.bundle);
|
|
114
|
-
return verified.value;
|
|
115
130
|
}
|
|
116
131
|
/**
|
|
117
132
|
* The paths a container event is authorized against: its previous manifest
|
|
@@ -209,26 +224,14 @@ function assertContainerParentPathMatches(input) {
|
|
|
209
224
|
throw new KeyingVerificationError("object_mismatch", `${input.label} parent path mismatch`);
|
|
210
225
|
}
|
|
211
226
|
}
|
|
212
|
-
function readParentReference(value, label) {
|
|
213
|
-
return readKeyingVerificationShape(() => {
|
|
214
|
-
const record = readCanonicalRecord(value, label);
|
|
215
|
-
return {
|
|
216
|
-
parentContainerId: readRecordNullableString(record, "parentContainerId", label),
|
|
217
|
-
parentManifestHash: readRecordNullableString(record, "parentManifestHash", label),
|
|
218
|
-
};
|
|
219
|
-
});
|
|
220
|
-
}
|
|
221
|
-
function readContainerManifestParentReference(bundle, label) {
|
|
222
|
-
return readParentReference(bundle.state, `${label} state`);
|
|
223
|
-
}
|
|
224
|
-
/** The parent a create or move body pins, read from the signed body. */
|
|
225
|
-
function readContainerEventBodyParentReference(event, label) {
|
|
226
|
-
return readParentReference(event.body, `${label} event body`);
|
|
227
|
-
}
|
|
228
227
|
async function resolveContainerManifestVerificationParentPath(input) {
|
|
228
|
+
const resolvingParents = input.resolvingParents ?? new Set();
|
|
229
|
+
if (resolvingParents.has(input.bundle.manifestHash)) {
|
|
230
|
+
throw new KeyingVerificationError("object_mismatch", `${input.label} parent pins form a cycle`);
|
|
231
|
+
}
|
|
229
232
|
// Descendants keep the parent manifest hash they were created or moved under;
|
|
230
233
|
// a later parent share/rekey must not require rewriting descendant manifests.
|
|
231
|
-
const { parentContainerId, parentManifestHash } =
|
|
234
|
+
const { parentContainerId, parentManifestHash } = readSignedContainerParentReference(input);
|
|
232
235
|
if (parentContainerId === null || parentManifestHash === null) {
|
|
233
236
|
return [];
|
|
234
237
|
}
|
|
@@ -241,11 +244,19 @@ async function resolveContainerManifestVerificationParentPath(input) {
|
|
|
241
244
|
if (!parentBundle) {
|
|
242
245
|
return input.parentPath;
|
|
243
246
|
}
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
247
|
+
resolvingParents.add(input.bundle.manifestHash);
|
|
248
|
+
let parentParentPath;
|
|
249
|
+
try {
|
|
250
|
+
parentParentPath = await resolveContainerManifestVerificationParentPath({
|
|
251
|
+
...input,
|
|
252
|
+
bundle: parentBundle,
|
|
253
|
+
label: `${input.label} parent manifest`,
|
|
254
|
+
resolvingParents,
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
finally {
|
|
258
|
+
resolvingParents.delete(input.bundle.manifestHash);
|
|
259
|
+
}
|
|
249
260
|
const verifiedParent = await verifyContainerManifestBundle({
|
|
250
261
|
...input,
|
|
251
262
|
bundle: parentBundle,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"containerManifestVerification.js","sourceRoot":"","sources":["containerManifestVerification.ts"],"names":[],"mappings":"AACA,OAAO,EACL,uBAAuB,EACvB,+BAA+B,EAC/B,uBAAuB,EAEvB,6BAA6B,GAC9B,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AACjE,OAAO,EACL,oBAAoB,EACpB,uBAAuB,GACxB,MAAM,mCAAmC,CAAC;AAE3C,OAAO,EACL,kCAAkC,EAClC,8BAA8B,GAC/B,MAAM,+CAA+C,CAAC;AACvD,OAAO,EAAE,yBAAyB,EAAE,MAAM,oCAAoC,CAAC;AAC/E,OAAO,EAAE,2BAA2B,EAAE,MAAM,UAAU,CAAC;AACvD,OAAO,EAAE,kCAAkC,EAAE,MAAM,gDAAgD,CAAC;AACpG,OAAO,EAAE,kBAAkB,EAAE,wBAAwB,EAAE,MAAM,YAAY,CAAC;AAM1E,OAAO,EACL,gCAAgC,EAEhC,wBAAwB,GACzB,MAAM,8BAA8B,CAAC;AAEtC,KAAK,UAAU,oCAAoC,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cAAc;KACvB,CAAC,CAAC;IAEH,OAAO,6BAA6B,CAAC;QACnC,uBAAuB,EAAE,YAAY;QACrC,qBAAqB,EAAE,KAAK,CAAC,qBAAqB;QAClD,MAAM,EAAE,cAAc;QACtB,aAAa,EAAE,KAAK,CAAC,aAAa;QAClC,iBAAiB,EAAE,KAAK,CAAC,iBAAiB;QAC1C,sBAAsB,EAAE,KAAK;QAC7B,KAAK,EAAE,GAAG,KAAK,CAAC,KAAK,oBAAoB;QACzC,UAAU;QACV,oBAAoB,EAAE,KAAK,CAAC,oBAAoB;QAChD,cAAc,EAAE,KAAK,CAAC,cAAc;QACpC,4BAA4B,EAAE,KAAK,CAAC,4BAA4B;QAChE,cAAc,EAAE,KAAK,CAAC,cAAc;QACpC,SAAS,EAAE,KAAK,CAAC,SAAS;QAC1B,+BAA+B,EAAE,KAAK,CAAC,+BAA+B;KACvE,CAAC,CAAC;AACL,CAAC","sourcesContent":["import type { PrincipalPolicyAuthorization } from \"@tearleads/crypto\";\nimport {\n KeyingVerificationError,\n principalPolicyMatchesReference,\n readKeyingCanonicalJson,\n type VerifiedContainerAccessManifest,\n verifyContainerAccessManifest,\n} from \"@tearleads/crypto\";\nimport type { AccessManifestBundleWireResponse } from \"@tearleads/validators/response\";\nimport { readCanonicalRecord } from \"../../keying/canonicalJson\";\nimport {\n assertCanonicalEqual,\n verifyAccessEventBundle,\n} from \"../checkpoints/bundleVerification\";\nimport type { ProjectionCheckpointContext } from \"../checkpoints/checkpointContext\";\nimport {\n loadManifestCheckpointVerification,\n verifyCachedManifestCheckpoint,\n} from \"../checkpoints/manifestCheckpointVerification\";\nimport { recordVerifiedBundleBytes } from \"../checkpoints/verifiedBundleBytes\";\nimport { readKeyingVerificationShape } from \"../error\";\nimport { collectReferencedPrincipalPolicies } from \"../principalPolicy/principalPolicyVerification\";\nimport { readAccessManifest, readRecordNullableString } from \"../readers\";\nimport type {\n PrincipalPolicyCache,\n ProjectionUserKeyResolver,\n ReferencedPrincipalPolicyWarmer,\n} from \"../types\";\nimport {\n assertCitedAncestorsDoNotRegress,\n type CitedLineageInput,\n resolveCitedAncestorPath,\n} from \"./containerAncestorCitations\";\n\nasync function collectManifestAuthorizationPolicies(input: {\n readonly authorizationEvidence: readonly PrincipalPolicyAuthorization[];\n readonly checkpointContext: ProjectionCheckpointContext;\n readonly organizationId: string;\n readonly principalPolicyCache: PrincipalPolicyCache;\n readonly referencedPrincipalHeads: Parameters<\n typeof principalPolicyMatchesReference\n >[0][\"reference\"][];\n readonly requireAuthorizationEvidence: boolean;\n readonly resolveUserKey: ProjectionUserKeyResolver;\n readonly warmReferencedPrincipalPolicies?:\n | ReferencedPrincipalPolicyWarmer\n | undefined;\n}) {\n const missingPrincipalHeads = input.referencedPrincipalHeads.filter(\n (reference) =>\n !input.authorizationEvidence.some((policy) =>\n principalPolicyMatchesReference({ policy, reference }),\n ),\n );\n if (input.requireAuthorizationEvidence && missingPrincipalHeads.length > 0) {\n throw new KeyingVerificationError(\n \"missing_dependency\",\n \"Projection omits required principal policy evidence\",\n );\n }\n if (input.requireAuthorizationEvidence)\n return [...input.authorizationEvidence];\n return [\n ...input.authorizationEvidence,\n ...(await collectReferencedPrincipalPolicies({\n checkpointContext: input.checkpointContext,\n organizationId: input.organizationId,\n principalPolicyCache: input.principalPolicyCache,\n references: missingPrincipalHeads,\n resolveUserKey: input.resolveUserKey,\n warmReferencedPrincipalPolicies: input.warmReferencedPrincipalPolicies,\n })),\n ];\n}\n\nexport async function verifyContainerManifestBundle(input: {\n readonly authorizationMembership?: \"current\" | \"referenced\" | undefined;\n readonly authorizationEvidence?:\n | readonly PrincipalPolicyAuthorization[]\n | undefined;\n readonly bundle: AccessManifestBundleWireResponse;\n readonly bundlesByHash: ReadonlyMap<string, AccessManifestBundleWireResponse>;\n readonly checkpointContext: ProjectionCheckpointContext;\n readonly enforceLocalCheckpoint: boolean;\n readonly label: string;\n readonly parentPath: readonly VerifiedContainerAccessManifest[];\n readonly principalPolicyCache: PrincipalPolicyCache;\n readonly resolveUserKey: ProjectionUserKeyResolver;\n readonly requireAuthorizationEvidence?: boolean | undefined;\n readonly verifiedByHash: Map<string, VerifiedContainerAccessManifest>;\n // Manifest hashes whose verification is in progress up the call stack.\n // Previous manifests, pinned parents, and cited ancestors all recurse\n // before a bundle's own hash is checked, so served bundles that refer to\n // each other under claimed hashes would otherwise recurse without end.\n readonly verifying?: Set<string> | undefined;\n readonly warmReferencedPrincipalPolicies?:\n | ReferencedPrincipalPolicyWarmer\n | undefined;\n}): Promise<VerifiedContainerAccessManifest> {\n const parentPath =\n await resolveContainerManifestVerificationParentPath(input);\n const cached = input.verifiedByHash.get(input.bundle.manifestHash);\n if (cached) {\n assertContainerParentPathMatches({\n label: input.label,\n parentPath,\n verifiedManifest: cached,\n });\n if (input.enforceLocalCheckpoint) {\n await verifyCachedManifestCheckpoint({\n current: cached,\n execSql: input.checkpointContext.execSql,\n localCheckpoints: input.checkpointContext.localCheckpoints,\n verifiedManifests: input.verifiedByHash,\n });\n }\n return cached;\n }\n\n const verifying = input.verifying ?? new Set<string>();\n if (verifying.has(input.bundle.manifestHash)) {\n throw new KeyingVerificationError(\n \"object_mismatch\",\n `${input.label} refers back to a manifest whose verification it is part of`,\n );\n }\n verifying.add(input.bundle.manifestHash);\n try {\n return await verifyFreshContainerManifestBundle(\n { ...input, verifying },\n parentPath,\n );\n } finally {\n verifying.delete(input.bundle.manifestHash);\n }\n}\n\ntype CitedAncestorInput = Parameters<typeof verifyContainerManifestBundle>[0];\n\nasync function verifyFreshContainerManifestBundle(\n input: CitedAncestorInput,\n parentPath: readonly VerifiedContainerAccessManifest[],\n): Promise<VerifiedContainerAccessManifest> {\n const event = await verifyAccessEventBundle(input);\n const manifest = readAccessManifest(\n input.bundle.manifest,\n `${input.label} manifest`,\n );\n const authorization = await resolveContainerManifestAuthorization(\n input,\n event,\n parentPath,\n );\n const { citedAncestors, previousManifest, sourceAncestors } = authorization;\n const principalPolicies = await collectManifestAuthorizationPolicies({\n authorizationEvidence: input.authorizationEvidence ?? [],\n checkpointContext: input.checkpointContext,\n organizationId: event.event.organizationId,\n principalPolicyCache: input.principalPolicyCache,\n referencedPrincipalHeads: [\n ...authorization.referencedPrincipalHeads,\n ...manifest.referencedPrincipalHeads,\n ],\n requireAuthorizationEvidence: input.requireAuthorizationEvidence ?? false,\n resolveUserKey: input.resolveUserKey,\n warmReferencedPrincipalPolicies: input.warmReferencedPrincipalPolicies,\n });\n const checkpointVerification = input.enforceLocalCheckpoint\n ? await loadManifestCheckpointVerification({\n current: manifest,\n execSql: input.checkpointContext.execSql,\n localCheckpoints: input.checkpointContext.localCheckpoints,\n verifiedManifests: input.verifiedByHash,\n })\n : null;\n\n const verified = await verifyContainerAccessManifest({\n // Served manifests were committed under their signed policy references.\n // Current membership still gates new writes in the API verifier.\n authorizationMembership: input.authorizationMembership ?? \"referenced\",\n destinationParentContainerPath: citedAncestors,\n event,\n expectedManifestHash: input.bundle.manifestHash,\n manifest,\n parentContainerPath: citedAncestors,\n principalPolicies,\n ...(checkpointVerification ?? {}),\n ...(previousManifest\n ? {\n previousContainerPath: [...sourceAncestors, previousManifest],\n previousManifest,\n }\n : { previousManifest: null }),\n });\n if (!verified.ok) {\n throw new KeyingVerificationError(\n verified.error.code,\n `${input.label} manifest verification failed: ${verified.error.message}`,\n );\n }\n\n assertCanonicalEqual({\n actual: input.bundle.state,\n expected: readKeyingCanonicalJson(\n verified.value.state,\n `${input.label} state`,\n ),\n label: `${input.label} state`,\n });\n input.verifiedByHash.set(input.bundle.manifestHash, verified.value);\n recordVerifiedBundleBytes(verified.value, input.bundle);\n\n return verified.value;\n}\n\n/**\n * The paths a container event is authorized against: its previous manifest\n * and the ancestor heads its signed event cites, root to parent.\n */\nasync function resolveContainerManifestAuthorization(\n input: CitedAncestorInput,\n event: VerifiedContainerAccessManifest[\"event\"],\n parentPath: readonly VerifiedContainerAccessManifest[],\n) {\n const previousManifest =\n event.event.previousManifestHash === null\n ? null\n : await verifyPreviousContainerManifest({\n ...input,\n parentPath,\n previousManifestHash: event.event.previousManifestHash,\n });\n // Resolve the signed placement lineage, never the server-supplied wire state.\n // Rotations also resolve their new epoch's exact signed parent citation via\n // cited-ancestor verification below; that can differ from this previous path.\n const eventType = event.event.eventType;\n const { parentContainerId, parentManifestHash } =\n previousManifest && eventType !== \"container.move\"\n ? previousManifest.state\n : readContainerEventBodyParentReference(event, input.label);\n if (\n previousManifest === null &&\n parentManifestHash !== null &&\n !event.event.dependencyManifestHashes.includes(parentManifestHash)\n ) {\n throw new KeyingVerificationError(\n \"missing_dependency\",\n `${input.label} does not cite the parent manifest it was created under`,\n );\n }\n const lineage: CitedLineageInput = {\n bundlesByHash: input.bundlesByHash,\n label: input.label,\n verifiedByHash: input.verifiedByHash,\n };\n const citedAncestors = await resolveCitedAncestors(input, event, {\n parentContainerId,\n });\n assertCitedAncestorsDoNotRegress({\n ...lineage,\n citedAncestors,\n previousManifest,\n });\n // A move's source path leads to the previous manifest's parent, and the\n // move event cites that path too; the destination path is the one above.\n // The source path is held to the same no-regression rule.\n const sourceAncestors =\n eventType === \"container.move\" && previousManifest\n ? await resolveCitedAncestors(input, event, {\n parentContainerId: previousManifest.state.parentContainerId,\n })\n : citedAncestors;\n if (sourceAncestors !== citedAncestors) {\n assertCitedAncestorsDoNotRegress({\n ...lineage,\n citedAncestors: sourceAncestors,\n previousManifest,\n });\n }\n const ancestors = [\n ...parentPath,\n ...citedAncestors,\n ...(sourceAncestors === citedAncestors ? [] : sourceAncestors),\n ];\n return {\n citedAncestors,\n previousManifest,\n referencedPrincipalHeads: [\n ...ancestors.flatMap(\n (ancestor) => ancestor.state.referencedPrincipalHeads,\n ),\n ...(previousManifest?.state.referencedPrincipalHeads ?? []),\n ],\n sourceAncestors,\n };\n}\n\nfunction citedAncestorResolutionInput(input: CitedAncestorInput) {\n return {\n bundlesByHash: input.bundlesByHash,\n label: input.label,\n verifiedByHash: input.verifiedByHash,\n // A cited head older than the served one is history: authorize it at the\n // membership it referenced, as the previous manifest is.\n verifyHistoryBundle: (\n bundle: AccessManifestBundleWireResponse,\n label: string,\n ) =>\n verifyContainerManifestBundle({\n ...input,\n authorizationMembership: \"referenced\",\n bundle,\n enforceLocalCheckpoint: false,\n label,\n }),\n };\n}\n\nfunction resolveCitedAncestors(\n input: CitedAncestorInput,\n event: VerifiedContainerAccessManifest[\"event\"],\n reference: { readonly parentContainerId: string | null },\n): Promise<VerifiedContainerAccessManifest[]> {\n return resolveCitedAncestorPath({\n ...citedAncestorResolutionInput(input),\n event,\n parentContainerId: reference.parentContainerId,\n });\n}\n\nfunction assertContainerParentPathMatches(input: {\n readonly label: string;\n readonly parentPath: readonly VerifiedContainerAccessManifest[];\n readonly verifiedManifest: VerifiedContainerAccessManifest;\n}): void {\n const actualParentManifestHash =\n input.parentPath.at(-1)?.manifestHash ?? null;\n if (\n actualParentManifestHash !== input.verifiedManifest.state.parentManifestHash\n ) {\n throw new KeyingVerificationError(\n \"object_mismatch\",\n `${input.label} parent path mismatch`,\n );\n }\n}\n\ninterface ContainerParentReference {\n parentContainerId: string | null;\n parentManifestHash: string | null;\n}\n\nfunction readParentReference(\n value: unknown,\n label: string,\n): ContainerParentReference {\n return readKeyingVerificationShape(() => {\n const record = readCanonicalRecord(value, label);\n\n return {\n parentContainerId: readRecordNullableString(\n record,\n \"parentContainerId\",\n label,\n ),\n parentManifestHash: readRecordNullableString(\n record,\n \"parentManifestHash\",\n label,\n ),\n };\n });\n}\n\nfunction readContainerManifestParentReference(\n bundle: AccessManifestBundleWireResponse,\n label: string,\n): ContainerParentReference {\n return readParentReference(bundle.state, `${label} state`);\n}\n\n/** The parent a create or move body pins, read from the signed body. */\nfunction readContainerEventBodyParentReference(\n event: VerifiedContainerAccessManifest[\"event\"],\n label: string,\n): ContainerParentReference {\n return readParentReference(event.body, `${label} event body`);\n}\n\nasync function resolveContainerManifestVerificationParentPath(input: {\n readonly authorizationEvidence?:\n | readonly PrincipalPolicyAuthorization[]\n | undefined;\n readonly bundle: AccessManifestBundleWireResponse;\n readonly bundlesByHash: ReadonlyMap<string, AccessManifestBundleWireResponse>;\n readonly checkpointContext: ProjectionCheckpointContext;\n readonly label: string;\n readonly parentPath: readonly VerifiedContainerAccessManifest[];\n readonly principalPolicyCache: PrincipalPolicyCache;\n readonly resolveUserKey: ProjectionUserKeyResolver;\n readonly verifiedByHash: Map<string, VerifiedContainerAccessManifest>;\n readonly warmReferencedPrincipalPolicies?:\n | ReferencedPrincipalPolicyWarmer\n | undefined;\n}): Promise<readonly VerifiedContainerAccessManifest[]> {\n // Descendants keep the parent manifest hash they were created or moved under;\n // a later parent share/rekey must not require rewriting descendant manifests.\n const { parentContainerId, parentManifestHash } =\n readContainerManifestParentReference(input.bundle, input.label);\n if (parentContainerId === null || parentManifestHash === null) {\n return [];\n }\n\n const parentPathIndex = input.parentPath.findIndex(\n (manifest) =>\n manifest.state.containerId === parentContainerId &&\n manifest.manifestHash === parentManifestHash,\n );\n if (parentPathIndex >= 0) {\n return input.parentPath.slice(0, parentPathIndex + 1);\n }\n\n const parentBundle = input.bundlesByHash.get(parentManifestHash);\n if (!parentBundle) {\n return input.parentPath;\n }\n\n const parentParentPath = await resolveContainerManifestVerificationParentPath(\n {\n ...input,\n bundle: parentBundle,\n label: `${input.label} parent manifest`,\n },\n );\n const verifiedParent = await verifyContainerManifestBundle({\n ...input,\n bundle: parentBundle,\n enforceLocalCheckpoint: false,\n label: `${input.label} parent manifest`,\n parentPath: parentParentPath,\n });\n if (verifiedParent.state.containerId !== parentContainerId) {\n throw new KeyingVerificationError(\n \"object_mismatch\",\n `${input.label} parent manifest container mismatch`,\n );\n }\n\n return [...parentParentPath, verifiedParent];\n}\n\nasync function verifyPreviousContainerManifest(input: {\n readonly authorizationEvidence?:\n | readonly PrincipalPolicyAuthorization[]\n | undefined;\n readonly bundlesByHash: ReadonlyMap<string, AccessManifestBundleWireResponse>;\n readonly checkpointContext: ProjectionCheckpointContext;\n readonly label: string;\n readonly parentPath: readonly VerifiedContainerAccessManifest[];\n readonly principalPolicyCache: PrincipalPolicyCache;\n readonly previousManifestHash: string;\n readonly resolveUserKey: ProjectionUserKeyResolver;\n readonly requireAuthorizationEvidence?: boolean | undefined;\n readonly verifiedByHash: Map<string, VerifiedContainerAccessManifest>;\n readonly verifying?: Set<string> | undefined;\n readonly warmReferencedPrincipalPolicies?:\n | ReferencedPrincipalPolicyWarmer\n | undefined;\n}): Promise<VerifiedContainerAccessManifest> {\n const previousBundle = input.bundlesByHash.get(input.previousManifestHash);\n if (!previousBundle) {\n throw new KeyingVerificationError(\n \"missing_dependency\",\n `${input.label} previous manifest ${input.previousManifestHash} is missing`,\n );\n }\n const parentPath = await resolveContainerManifestVerificationParentPath({\n ...input,\n bundle: previousBundle,\n });\n\n return verifyContainerManifestBundle({\n authorizationMembership: \"referenced\",\n authorizationEvidence: input.authorizationEvidence,\n bundle: previousBundle,\n bundlesByHash: input.bundlesByHash,\n checkpointContext: input.checkpointContext,\n enforceLocalCheckpoint: false,\n label: `${input.label} previous manifest`,\n parentPath,\n principalPolicyCache: input.principalPolicyCache,\n resolveUserKey: input.resolveUserKey,\n requireAuthorizationEvidence: input.requireAuthorizationEvidence,\n verifiedByHash: input.verifiedByHash,\n verifying: input.verifying,\n warmReferencedPrincipalPolicies: input.warmReferencedPrincipalPolicies,\n });\n}\n"]}
|
|
1
|
+
{"version":3,"file":"containerManifestVerification.js","sourceRoot":"","sources":["containerManifestVerification.ts"],"names":[],"mappings":"AACA,OAAO,EACL,uBAAuB,EACvB,+BAA+B,EAC/B,uBAAuB,EAEvB,6BAA6B,GAC9B,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EACL,oBAAoB,EACpB,uBAAuB,GACxB,MAAM,mCAAmC,CAAC;AAE3C,OAAO,EACL,kCAAkC,EAClC,8BAA8B,GAC/B,MAAM,+CAA+C,CAAC;AACvD,OAAO,EACL,gCAAgC,EAChC,0BAA0B,EAC1B,yBAAyB,GAC1B,MAAM,oCAAoC,CAAC;AAC5C,OAAO,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K,CAAC,oBAAoB;QAChD,cAAc,EAAE,KAAK,CAAC,cAAc;QACpC,4BAA4B,EAAE,KAAK,CAAC,4BAA4B;QAChE,cAAc,EAAE,KAAK,CAAC,cAAc;QACpC,SAAS,EAAE,KAAK,CAAC,SAAS;QAC1B,+BAA+B,EAAE,KAAK,CAAC,+BAA+B;KACvE,CAAC,CAAC;AACL,CAAC","sourcesContent":["import type { PrincipalPolicyAuthorization } from \"@tearleads/crypto\";\nimport {\n KeyingVerificationError,\n principalPolicyMatchesReference,\n readKeyingCanonicalJson,\n type VerifiedContainerAccessManifest,\n verifyContainerAccessManifest,\n} from \"@tearleads/crypto\";\nimport type { AccessManifestBundleWireResponse } from \"@tearleads/validators/response\";\nimport {\n assertCanonicalEqual,\n verifyAccessEventBundle,\n} from \"../checkpoints/bundleVerification\";\nimport type { ProjectionCheckpointContext } from \"../checkpoints/checkpointContext\";\nimport {\n loadManifestCheckpointVerification,\n verifyCachedManifestCheckpoint,\n} from \"../checkpoints/manifestCheckpointVerification\";\nimport {\n assertUnchangedSinceVerification,\n matchesVerifiedBundleBytes,\n recordVerifiedBundleBytes,\n} from \"../checkpoints/verifiedBundleBytes\";\nimport { collectReferencedPrincipalPolicies } from \"../principalPolicy/principalPolicyVerification\";\nimport { readAccessManifest } from \"../readers\";\nimport type {\n PrincipalPolicyCache,\n ProjectionUserKeyResolver,\n ReferencedPrincipalPolicyWarmer,\n} from \"../types\";\nimport {\n assertCitedAncestorsDoNotRegress,\n type CitedLineageInput,\n resolveCitedAncestorPath,\n} from \"./containerAncestorCitations\";\nimport {\n assertSignedContainerParentPin,\n readContainerEventBodyParentReference,\n readSignedContainerParentReference,\n} from \"./containerSignedParentReference\";\n\nasync function collectManifestAuthorizationPolicies(input: {\n readonly authorizationEvidence: readonly PrincipalPolicyAuthorization[];\n readonly checkpointContext: ProjectionCheckpointContext;\n readonly organizationId: string;\n readonly principalPolicyCache: PrincipalPolicyCache;\n readonly referencedPrincipalHeads: Parameters<\n typeof principalPolicyMatchesReference\n >[0][\"reference\"][];\n readonly requireAuthorizationEvidence: boolean;\n readonly resolveUserKey: ProjectionUserKeyResolver;\n readonly warmReferencedPrincipalPolicies?:\n | ReferencedPrincipalPolicyWarmer\n | undefined;\n}) {\n const missingPrincipalHeads = input.referencedPrincipalHeads.filter(\n (reference) =>\n !input.authorizationEvidence.some((policy) =>\n principalPolicyMatchesReference({ policy, reference }),\n ),\n );\n if (input.requireAuthorizationEvidence && missingPrincipalHeads.length > 0) {\n throw new KeyingVerificationError(\n \"missing_dependency\",\n \"Projection omits required principal policy evidence\",\n );\n }\n if (input.requireAuthorizationEvidence)\n return [...input.authorizationEvidence];\n return [\n ...input.authorizationEvidence,\n ...(await collectReferencedPrincipalPolicies({\n checkpointContext: input.checkpointContext,\n organizationId: input.organizationId,\n principalPolicyCache: input.principalPolicyCache,\n references: missingPrincipalHeads,\n resolveUserKey: input.resolveUserKey,\n warmReferencedPrincipalPolicies: input.warmReferencedPrincipalPolicies,\n })),\n ];\n}\n\nexport async function verifyContainerManifestBundle(input: {\n readonly authorizationMembership?: \"current\" | \"referenced\" | undefined;\n readonly authorizationEvidence?:\n | readonly PrincipalPolicyAuthorization[]\n | undefined;\n readonly bundle: AccessManifestBundleWireResponse;\n readonly bundlesByHash: ReadonlyMap<string, AccessManifestBundleWireResponse>;\n readonly checkpointContext: ProjectionCheckpointContext;\n readonly enforceLocalCheckpoint: boolean;\n readonly label: string;\n readonly parentPath: readonly VerifiedContainerAccessManifest[];\n readonly principalPolicyCache: PrincipalPolicyCache;\n readonly resolveUserKey: ProjectionUserKeyResolver;\n readonly requireAuthorizationEvidence?: boolean | undefined;\n readonly verifiedByHash: Map<string, VerifiedContainerAccessManifest>;\n // Manifest hashes whose verification is in progress up the call stack.\n // Previous manifests, pinned parents, and cited ancestors all recurse\n // before a bundle's own hash is checked, so served bundles that refer to\n // each other under claimed hashes would otherwise recurse without end.\n readonly verifying?: Set<string> | undefined;\n readonly warmReferencedPrincipalPolicies?:\n | ReferencedPrincipalPolicyWarmer\n | undefined;\n}): Promise<VerifiedContainerAccessManifest> {\n const parentPath =\n await resolveContainerManifestVerificationParentPath(input);\n const cached = input.verifiedByHash.get(input.bundle.manifestHash);\n // A cache hit vouches for the bytes it verified: a copy that differs is\n // verified afresh, so a warm cache neither vouches for bytes no\n // verification saw nor refuses a copy a cold one would accept.\n if (cached && matchesVerifiedBundleBytes(cached, input.bundle)) {\n assertVerifiedState(input, cached);\n assertContainerParentPathMatches({\n label: input.label,\n parentPath,\n verifiedManifest: cached,\n });\n if (input.enforceLocalCheckpoint) {\n await verifyCachedManifestCheckpoint({\n current: cached,\n execSql: input.checkpointContext.execSql,\n localCheckpoints: input.checkpointContext.localCheckpoints,\n verifiedManifests: input.verifiedByHash,\n });\n // The caller reads this bundle on return, after the checkpoint read.\n assertUnchangedSinceVerification(cached, input.bundle, input.label);\n assertVerifiedState(input, cached);\n }\n return cached;\n }\n\n const verifying = input.verifying ?? new Set<string>();\n if (verifying.has(input.bundle.manifestHash)) {\n throw new KeyingVerificationError(\n \"object_mismatch\",\n `${input.label} refers back to a manifest whose verification it is part of`,\n );\n }\n verifying.add(input.bundle.manifestHash);\n try {\n return await verifyFreshContainerManifestBundle(\n { ...input, verifying },\n parentPath,\n );\n } finally {\n verifying.delete(input.bundle.manifestHash);\n }\n}\n\ntype CitedAncestorInput = Parameters<typeof verifyContainerManifestBundle>[0];\n\nasync function verifyFreshContainerManifestBundle(\n served: CitedAncestorInput,\n parentPath: readonly VerifiedContainerAccessManifest[],\n): Promise<VerifiedContainerAccessManifest> {\n // Verify and record one copy taken before the first await, so the bytes\n // recorded as verified are exactly the bytes verified.\n const input = { ...served, bundle: structuredClone(served.bundle) };\n const event = await verifyAccessEventBundle(input);\n const manifest = readAccessManifest(\n input.bundle.manifest,\n `${input.label} manifest`,\n );\n const authorization = await resolveContainerManifestAuthorization(\n input,\n event,\n parentPath,\n );\n const { citedAncestors, previousManifest, sourceAncestors } = authorization;\n const principalPolicies = await collectManifestAuthorizationPolicies({\n authorizationEvidence: input.authorizationEvidence ?? [],\n checkpointContext: input.checkpointContext,\n organizationId: event.event.organizationId,\n principalPolicyCache: input.principalPolicyCache,\n referencedPrincipalHeads: [\n ...authorization.referencedPrincipalHeads,\n ...manifest.referencedPrincipalHeads,\n ],\n requireAuthorizationEvidence: input.requireAuthorizationEvidence ?? false,\n resolveUserKey: input.resolveUserKey,\n warmReferencedPrincipalPolicies: input.warmReferencedPrincipalPolicies,\n });\n const checkpointVerification = input.enforceLocalCheckpoint\n ? await loadManifestCheckpointVerification({\n current: manifest,\n execSql: input.checkpointContext.execSql,\n localCheckpoints: input.checkpointContext.localCheckpoints,\n verifiedManifests: input.verifiedByHash,\n })\n : null;\n\n const verified = await verifyContainerAccessManifest({\n // Served manifests were committed under their signed policy references.\n // Current membership still gates new writes in the API verifier.\n authorizationMembership: input.authorizationMembership ?? \"referenced\",\n destinationParentContainerPath: citedAncestors,\n event,\n expectedManifestHash: input.bundle.manifestHash,\n manifest,\n parentContainerPath: citedAncestors,\n principalPolicies,\n ...(checkpointVerification ?? {}),\n ...(previousManifest\n ? {\n previousContainerPath: [...sourceAncestors, previousManifest],\n previousManifest,\n }\n : { previousManifest: null }),\n });\n if (!verified.ok) {\n throw new KeyingVerificationError(\n verified.error.code,\n `${input.label} manifest verification failed: ${verified.error.message}`,\n );\n }\n\n assertSignedContainerParentPin(input, verified.value);\n assertVerifiedState(input, verified.value);\n input.verifiedByHash.set(input.bundle.manifestHash, verified.value);\n recordVerifiedBundleBytes(verified.value, input.bundle);\n assertUnchangedSinceVerification(\n verified.value,\n served.bundle,\n input.label,\n input.bundle,\n );\n assertVerifiedState(served, verified.value);\n\n return verified.value;\n}\n\nfunction assertVerifiedState(\n input: Pick<CitedAncestorInput, \"bundle\" | \"label\">,\n verified: VerifiedContainerAccessManifest,\n): void {\n assertCanonicalEqual({\n actual: input.bundle.state,\n expected: readKeyingCanonicalJson(verified.state, `${input.label} state`),\n label: `${input.label} state`,\n });\n}\n\n/**\n * The paths a container event is authorized against: its previous manifest\n * and the ancestor heads its signed event cites, root to parent.\n */\nasync function resolveContainerManifestAuthorization(\n input: CitedAncestorInput,\n event: VerifiedContainerAccessManifest[\"event\"],\n parentPath: readonly VerifiedContainerAccessManifest[],\n) {\n const previousManifest =\n event.event.previousManifestHash === null\n ? null\n : await verifyPreviousContainerManifest({\n ...input,\n parentPath,\n previousManifestHash: event.event.previousManifestHash,\n });\n // Resolve the signed placement lineage, never the server-supplied wire state.\n // Rotations also resolve their new epoch's exact signed parent citation via\n // cited-ancestor verification below; that can differ from this previous path.\n const eventType = event.event.eventType;\n const { parentContainerId, parentManifestHash } =\n previousManifest && eventType !== \"container.move\"\n ? previousManifest.state\n : readContainerEventBodyParentReference(event, input.label);\n if (\n previousManifest === null &&\n parentManifestHash !== null &&\n !event.event.dependencyManifestHashes.includes(parentManifestHash)\n ) {\n throw new KeyingVerificationError(\n \"missing_dependency\",\n `${input.label} does not cite the parent manifest it was created under`,\n );\n }\n const lineage: CitedLineageInput = {\n bundlesByHash: input.bundlesByHash,\n label: input.label,\n verifiedByHash: input.verifiedByHash,\n };\n const citedAncestors = await resolveCitedAncestors(input, event, {\n parentContainerId,\n });\n assertCitedAncestorsDoNotRegress({\n ...lineage,\n citedAncestors,\n previousManifest,\n });\n // A move's source path leads to the previous manifest's parent, and the\n // move event cites that path too; the destination path is the one above.\n // The source path is held to the same no-regression rule.\n const sourceAncestors =\n eventType === \"container.move\" && previousManifest\n ? await resolveCitedAncestors(input, event, {\n parentContainerId: previousManifest.state.parentContainerId,\n })\n : citedAncestors;\n if (sourceAncestors !== citedAncestors) {\n assertCitedAncestorsDoNotRegress({\n ...lineage,\n citedAncestors: sourceAncestors,\n previousManifest,\n });\n }\n const ancestors = [\n ...parentPath,\n ...citedAncestors,\n ...(sourceAncestors === citedAncestors ? [] : sourceAncestors),\n ];\n return {\n citedAncestors,\n previousManifest,\n referencedPrincipalHeads: [\n ...ancestors.flatMap(\n (ancestor) => ancestor.state.referencedPrincipalHeads,\n ),\n ...(previousManifest?.state.referencedPrincipalHeads ?? []),\n ],\n sourceAncestors,\n };\n}\n\nfunction citedAncestorResolutionInput(input: CitedAncestorInput) {\n return {\n bundlesByHash: input.bundlesByHash,\n label: input.label,\n verifiedByHash: input.verifiedByHash,\n // A cited head older than the served one is history: authorize it at the\n // membership it referenced, as the previous manifest is.\n verifyHistoryBundle: (\n bundle: AccessManifestBundleWireResponse,\n label: string,\n ) =>\n verifyContainerManifestBundle({\n ...input,\n authorizationMembership: \"referenced\",\n bundle,\n enforceLocalCheckpoint: false,\n label,\n }),\n };\n}\n\nfunction resolveCitedAncestors(\n input: CitedAncestorInput,\n event: VerifiedContainerAccessManifest[\"event\"],\n reference: { readonly parentContainerId: string | null },\n): Promise<VerifiedContainerAccessManifest[]> {\n return resolveCitedAncestorPath({\n ...citedAncestorResolutionInput(input),\n event,\n parentContainerId: reference.parentContainerId,\n });\n}\n\nfunction assertContainerParentPathMatches(input: {\n readonly label: string;\n readonly parentPath: readonly VerifiedContainerAccessManifest[];\n readonly verifiedManifest: VerifiedContainerAccessManifest;\n}): void {\n const actualParentManifestHash =\n input.parentPath.at(-1)?.manifestHash ?? null;\n if (\n actualParentManifestHash !== input.verifiedManifest.state.parentManifestHash\n ) {\n throw new KeyingVerificationError(\n \"object_mismatch\",\n `${input.label} parent path mismatch`,\n );\n }\n}\n\nasync function resolveContainerManifestVerificationParentPath(input: {\n readonly authorizationEvidence?:\n | readonly PrincipalPolicyAuthorization[]\n | undefined;\n readonly bundle: AccessManifestBundleWireResponse;\n readonly bundlesByHash: ReadonlyMap<string, AccessManifestBundleWireResponse>;\n readonly checkpointContext: ProjectionCheckpointContext;\n readonly label: string;\n readonly parentPath: readonly VerifiedContainerAccessManifest[];\n readonly principalPolicyCache: PrincipalPolicyCache;\n // Bundles whose parent this chain of pins is already resolving.\n readonly resolvingParents?: Set<string> | undefined;\n readonly resolveUserKey: ProjectionUserKeyResolver;\n readonly verifiedByHash: Map<string, VerifiedContainerAccessManifest>;\n readonly warmReferencedPrincipalPolicies?:\n | ReferencedPrincipalPolicyWarmer\n | undefined;\n}): Promise<readonly VerifiedContainerAccessManifest[]> {\n const resolvingParents = input.resolvingParents ?? new Set<string>();\n if (resolvingParents.has(input.bundle.manifestHash)) {\n throw new KeyingVerificationError(\n \"object_mismatch\",\n `${input.label} parent pins form a cycle`,\n );\n }\n // Descendants keep the parent manifest hash they were created or moved under;\n // a later parent share/rekey must not require rewriting descendant manifests.\n const { parentContainerId, parentManifestHash } =\n readSignedContainerParentReference(input);\n if (parentContainerId === null || parentManifestHash === null) {\n return [];\n }\n\n const parentPathIndex = input.parentPath.findIndex(\n (manifest) =>\n manifest.state.containerId === parentContainerId &&\n manifest.manifestHash === parentManifestHash,\n );\n if (parentPathIndex >= 0) {\n return input.parentPath.slice(0, parentPathIndex + 1);\n }\n\n const parentBundle = input.bundlesByHash.get(parentManifestHash);\n if (!parentBundle) {\n return input.parentPath;\n }\n\n resolvingParents.add(input.bundle.manifestHash);\n let parentParentPath: readonly VerifiedContainerAccessManifest[];\n try {\n parentParentPath = await resolveContainerManifestVerificationParentPath({\n ...input,\n bundle: parentBundle,\n label: `${input.label} parent manifest`,\n resolvingParents,\n });\n } finally {\n resolvingParents.delete(input.bundle.manifestHash);\n }\n const verifiedParent = await verifyContainerManifestBundle({\n ...input,\n bundle: parentBundle,\n enforceLocalCheckpoint: false,\n label: `${input.label} parent manifest`,\n parentPath: parentParentPath,\n });\n if (verifiedParent.state.containerId !== parentContainerId) {\n throw new KeyingVerificationError(\n \"object_mismatch\",\n `${input.label} parent manifest container mismatch`,\n );\n }\n\n return [...parentParentPath, verifiedParent];\n}\n\nasync function verifyPreviousContainerManifest(input: {\n readonly authorizationEvidence?:\n | readonly PrincipalPolicyAuthorization[]\n | undefined;\n readonly bundlesByHash: ReadonlyMap<string, AccessManifestBundleWireResponse>;\n readonly checkpointContext: ProjectionCheckpointContext;\n readonly label: string;\n readonly parentPath: readonly VerifiedContainerAccessManifest[];\n readonly principalPolicyCache: PrincipalPolicyCache;\n readonly previousManifestHash: string;\n readonly resolveUserKey: ProjectionUserKeyResolver;\n readonly requireAuthorizationEvidence?: boolean | undefined;\n readonly verifiedByHash: Map<string, VerifiedContainerAccessManifest>;\n readonly verifying?: Set<string> | undefined;\n readonly warmReferencedPrincipalPolicies?:\n | ReferencedPrincipalPolicyWarmer\n | undefined;\n}): Promise<VerifiedContainerAccessManifest> {\n const previousBundle = input.bundlesByHash.get(input.previousManifestHash);\n if (!previousBundle) {\n throw new KeyingVerificationError(\n \"missing_dependency\",\n `${input.label} previous manifest ${input.previousManifestHash} is missing`,\n );\n }\n const parentPath = await resolveContainerManifestVerificationParentPath({\n ...input,\n bundle: previousBundle,\n });\n\n return verifyContainerManifestBundle({\n authorizationMembership: \"referenced\",\n authorizationEvidence: input.authorizationEvidence,\n bundle: previousBundle,\n bundlesByHash: input.bundlesByHash,\n checkpointContext: input.checkpointContext,\n enforceLocalCheckpoint: false,\n label: `${input.label} previous manifest`,\n parentPath,\n principalPolicyCache: input.principalPolicyCache,\n resolveUserKey: input.resolveUserKey,\n requireAuthorizationEvidence: input.requireAuthorizationEvidence,\n verifiedByHash: input.verifiedByHash,\n verifying: input.verifying,\n warmReferencedPrincipalPolicies: input.warmReferencedPrincipalPolicies,\n });\n}\n"]}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { KeyingVerificationError, } from "../../../internal/crypto/index.js";
|
|
2
|
+
import { isPlainObject } from "../../../internal/validators/isPlainObject.js";
|
|
3
|
+
import { readKeyingVerificationShape } from "../error.js";
|
|
4
|
+
import { readRecordNullableString, readRecordString } from "../readers.js";
|
|
5
|
+
function readRecord(value, label) {
|
|
6
|
+
if (!isPlainObject(value))
|
|
7
|
+
throw new Error(`${label} must be an object`);
|
|
8
|
+
return value;
|
|
9
|
+
}
|
|
10
|
+
/** The signed fields the parent pin follows; verification checks them all. */
|
|
11
|
+
function readSignedContainerEvent(bundle, label) {
|
|
12
|
+
return readKeyingVerificationShape(() => {
|
|
13
|
+
const event = readRecord(bundle.event.event, `${label} signed event`);
|
|
14
|
+
return {
|
|
15
|
+
eventType: readRecordString(event, "eventType", `${label} signed event`),
|
|
16
|
+
previousManifestHash: readRecordNullableString(event, "previousManifestHash", `${label} signed event`),
|
|
17
|
+
body: readRecord(bundle.event.body, `${label} event body`),
|
|
18
|
+
};
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
function readBodyHead(event, label) {
|
|
22
|
+
return readKeyingVerificationShape(() => readRecordNullableString(event.body, "parentManifestHash", `${label} event body`));
|
|
23
|
+
}
|
|
24
|
+
function readBodyParent(event, label) {
|
|
25
|
+
return {
|
|
26
|
+
parentContainerId: readKeyingVerificationShape(() => readRecordNullableString(event.body, "parentContainerId", `${label} event body`)),
|
|
27
|
+
parentManifestHash: readBodyHead(event, label),
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
function knownParent(state) {
|
|
31
|
+
return state
|
|
32
|
+
? {
|
|
33
|
+
parentContainerId: state.parentContainerId,
|
|
34
|
+
parentManifestHash: state.parentManifestHash,
|
|
35
|
+
}
|
|
36
|
+
: null;
|
|
37
|
+
}
|
|
38
|
+
// Each hash in one projection's bundles pins one parent, so a pass that
|
|
39
|
+
// resolves heads and then each of their predecessors reads each body once.
|
|
40
|
+
const resolvedByBundles = new WeakMap();
|
|
41
|
+
function nextBundle(input, event, walked) {
|
|
42
|
+
const previousHash = event.previousManifestHash;
|
|
43
|
+
if (previousHash === null) {
|
|
44
|
+
throw new KeyingVerificationError("missing_dependency", `${input.label} ${event.eventType} requires the previous container manifest`);
|
|
45
|
+
}
|
|
46
|
+
if (walked.has(previousHash)) {
|
|
47
|
+
throw new KeyingVerificationError("object_mismatch", `${input.label} refers back to a manifest whose verification it is part of`);
|
|
48
|
+
}
|
|
49
|
+
return previousHash;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Walks back to a pinned or already known parent: the base the pending
|
|
53
|
+
* events, newest first, fold onto.
|
|
54
|
+
*/
|
|
55
|
+
function walkToPinnedParent(input, known, resolved) {
|
|
56
|
+
const pending = [];
|
|
57
|
+
const walked = new Set();
|
|
58
|
+
let bundle = input.bundle;
|
|
59
|
+
for (;;) {
|
|
60
|
+
const event = readSignedContainerEvent(bundle, input.label);
|
|
61
|
+
if (event.eventType === "container.create" ||
|
|
62
|
+
event.eventType === "container.move") {
|
|
63
|
+
const base = readBodyParent(event, input.label);
|
|
64
|
+
resolved.set(bundle.manifestHash, base);
|
|
65
|
+
return { base, pending };
|
|
66
|
+
}
|
|
67
|
+
pending.push({ hash: bundle.manifestHash, event });
|
|
68
|
+
walked.add(bundle.manifestHash);
|
|
69
|
+
const previousHash = nextBundle(input, event, walked);
|
|
70
|
+
const base = known(previousHash);
|
|
71
|
+
if (base)
|
|
72
|
+
return { base, pending };
|
|
73
|
+
const previous = input.bundlesByHash.get(previousHash);
|
|
74
|
+
if (!previous) {
|
|
75
|
+
throw new KeyingVerificationError("missing_dependency", `${input.label} previous manifest ${previousHash} is missing`);
|
|
76
|
+
}
|
|
77
|
+
bundle = previous;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* The parent a container manifest pins, from signed bytes, before the
|
|
82
|
+
* manifest verifies: a create or move body names the parent and its head; a
|
|
83
|
+
* rekey or revoke keeps its predecessor's parent and re-cites a head in its
|
|
84
|
+
* body; a grant or recitation keeps both. A predecessor this pass already
|
|
85
|
+
* verified answers with its derived state, any other with its served signed
|
|
86
|
+
* body. Verification derives the state from the recursively verified
|
|
87
|
+
* predecessor and then requires this exact pin.
|
|
88
|
+
*/
|
|
89
|
+
export function readSignedContainerParentReference(input) {
|
|
90
|
+
const resolved = resolvedByBundles.get(input.bundlesByHash) ??
|
|
91
|
+
new Map();
|
|
92
|
+
resolvedByBundles.set(input.bundlesByHash, resolved);
|
|
93
|
+
const known = (hash) => knownParent(input.verifiedByHash.get(hash)?.state ?? resolved.get(hash));
|
|
94
|
+
const head = known(input.bundle.manifestHash);
|
|
95
|
+
if (head)
|
|
96
|
+
return head;
|
|
97
|
+
const walk = walkToPinnedParent(input, known, resolved);
|
|
98
|
+
let reference = walk.base;
|
|
99
|
+
for (const { hash, event } of walk.pending.reverse()) {
|
|
100
|
+
if (event.eventType === "container.rekey" ||
|
|
101
|
+
event.eventType === "container.revoke") {
|
|
102
|
+
reference = {
|
|
103
|
+
parentContainerId: reference.parentContainerId,
|
|
104
|
+
parentManifestHash: readBodyHead(event, input.label),
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
resolved.set(hash, reference);
|
|
108
|
+
}
|
|
109
|
+
return reference;
|
|
110
|
+
}
|
|
111
|
+
/** The parent path was resolved from the pin the derived state now proves. */
|
|
112
|
+
export function assertSignedContainerParentPin(input, verified) {
|
|
113
|
+
const pin = readSignedContainerParentReference(input);
|
|
114
|
+
if (pin.parentContainerId !== verified.state.parentContainerId ||
|
|
115
|
+
pin.parentManifestHash !== verified.state.parentManifestHash) {
|
|
116
|
+
throw new KeyingVerificationError("object_mismatch", `${input.label} parent pin differs from its derived state`);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
/** The parent a create or move body pins, read from its verified body. */
|
|
120
|
+
export function readContainerEventBodyParentReference(event, label) {
|
|
121
|
+
return readKeyingVerificationShape(() => {
|
|
122
|
+
const body = readRecord(event.body, `${label} event body`);
|
|
123
|
+
return {
|
|
124
|
+
parentContainerId: readRecordNullableString(body, "parentContainerId", `${label} event body`),
|
|
125
|
+
parentManifestHash: readRecordNullableString(body, "parentManifestHash", `${label} event body`),
|
|
126
|
+
};
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
//# sourceMappingURL=containerSignedParentReference.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"containerSignedParentReference.js","sourceRoot":"","sources":["containerSignedParentReference.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,uBAAuB,GAExB,MAAM,mBA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,KAAa;IAEb,OAAO,2BAA2B,CAAC,GAAG,EAAE;QACtC,MAAM,IAAI,GAAG,UAAU,CAAC,KAAK,CAAC,IAAI,EAAE,GAAG,KAAK,aAAa,CAAC,CAAC;QAC3D,OAAO;YACL,iBAAiB,EAAE,wBAAwB,CACzC,IAAI,EACJ,mBAAmB,EACnB,GAAG,KAAK,aAAa,CACtB;YACD,kBAAkB,EAAE,wBAAwB,CAC1C,IAAI,EACJ,oBAAoB,EACpB,GAAG,KAAK,aAAa,CACtB;SACF,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import {\n KeyingVerificationError,\n type VerifiedContainerAccessManifest,\n} from \"@tearleads/crypto\";\nimport { isPlainObject } from \"@tearleads/validators/isPlainObject\";\nimport type { AccessManifestBundleWireResponse } from \"@tearleads/validators/response\";\nimport { readKeyingVerificationShape } from \"../error\";\nimport { readRecordNullableString, readRecordString } from \"../readers\";\n\nexport interface ContainerParentReference {\n readonly parentContainerId: string | null;\n readonly parentManifestHash: string | null;\n}\n\ntype Bundles = ReadonlyMap<string, AccessManifestBundleWireResponse>;\n\ninterface SignedContainerEvent {\n readonly eventType: string;\n readonly previousManifestHash: string | null;\n readonly body: Record<string, unknown>;\n}\n\nfunction readRecord(value: unknown, label: string): Record<string, unknown> {\n if (!isPlainObject(value)) throw new Error(`${label} must be an object`);\n return value;\n}\n\n/** The signed fields the parent pin follows; verification checks them all. */\nfunction readSignedContainerEvent(\n bundle: AccessManifestBundleWireResponse,\n label: string,\n): SignedContainerEvent {\n return readKeyingVerificationShape(() => {\n const event = readRecord(bundle.event.event, `${label} signed event`);\n return {\n eventType: readRecordString(event, \"eventType\", `${label} signed event`),\n previousManifestHash: readRecordNullableString(\n event,\n \"previousManifestHash\",\n `${label} signed event`,\n ),\n body: readRecord(bundle.event.body, `${label} event body`),\n };\n });\n}\n\nfunction readBodyHead(event: SignedContainerEvent, label: string) {\n return readKeyingVerificationShape(() =>\n readRecordNullableString(\n event.body,\n \"parentManifestHash\",\n `${label} event body`,\n ),\n );\n}\n\nfunction readBodyParent(\n event: SignedContainerEvent,\n label: string,\n): ContainerParentReference {\n return {\n parentContainerId: readKeyingVerificationShape(() =>\n readRecordNullableString(\n event.body,\n \"parentContainerId\",\n `${label} event body`,\n ),\n ),\n parentManifestHash: readBodyHead(event, label),\n };\n}\n\nfunction knownParent(\n state: ContainerParentReference | undefined,\n): ContainerParentReference | null {\n return state\n ? {\n parentContainerId: state.parentContainerId,\n parentManifestHash: state.parentManifestHash,\n }\n : null;\n}\n\n// Each hash in one projection's bundles pins one parent, so a pass that\n// resolves heads and then each of their predecessors reads each body once.\nconst resolvedByBundles = new WeakMap<\n Bundles,\n Map<string, ContainerParentReference>\n>();\n\ntype PinInput = {\n readonly bundle: AccessManifestBundleWireResponse;\n readonly bundlesByHash: Bundles;\n readonly label: string;\n readonly verifiedByHash: ReadonlyMap<string, VerifiedContainerAccessManifest>;\n};\n\ninterface PendingEvent {\n readonly hash: string;\n readonly event: SignedContainerEvent;\n}\n\nfunction nextBundle(\n input: PinInput,\n event: SignedContainerEvent,\n walked: ReadonlySet<string>,\n): string {\n const previousHash = event.previousManifestHash;\n if (previousHash === null) {\n throw new KeyingVerificationError(\n \"missing_dependency\",\n `${input.label} ${event.eventType} requires the previous container manifest`,\n );\n }\n if (walked.has(previousHash)) {\n throw new KeyingVerificationError(\n \"object_mismatch\",\n `${input.label} refers back to a manifest whose verification it is part of`,\n );\n }\n return previousHash;\n}\n\n/**\n * Walks back to a pinned or already known parent: the base the pending\n * events, newest first, fold onto.\n */\nfunction walkToPinnedParent(\n input: PinInput,\n known: (hash: string) => ContainerParentReference | null,\n resolved: Map<string, ContainerParentReference>,\n) {\n const pending: PendingEvent[] = [];\n const walked = new Set<string>();\n let bundle = input.bundle;\n for (;;) {\n const event = readSignedContainerEvent(bundle, input.label);\n if (\n event.eventType === \"container.create\" ||\n event.eventType === \"container.move\"\n ) {\n const base = readBodyParent(event, input.label);\n resolved.set(bundle.manifestHash, base);\n return { base, pending };\n }\n pending.push({ hash: bundle.manifestHash, event });\n walked.add(bundle.manifestHash);\n const previousHash = nextBundle(input, event, walked);\n const base = known(previousHash);\n if (base) return { base, pending };\n const previous = input.bundlesByHash.get(previousHash);\n if (!previous) {\n throw new KeyingVerificationError(\n \"missing_dependency\",\n `${input.label} previous manifest ${previousHash} is missing`,\n );\n }\n bundle = previous;\n }\n}\n\n/**\n * The parent a container manifest pins, from signed bytes, before the\n * manifest verifies: a create or move body names the parent and its head; a\n * rekey or revoke keeps its predecessor's parent and re-cites a head in its\n * body; a grant or recitation keeps both. A predecessor this pass already\n * verified answers with its derived state, any other with its served signed\n * body. Verification derives the state from the recursively verified\n * predecessor and then requires this exact pin.\n */\nexport function readSignedContainerParentReference(\n input: PinInput,\n): ContainerParentReference {\n const resolved =\n resolvedByBundles.get(input.bundlesByHash) ??\n new Map<string, ContainerParentReference>();\n resolvedByBundles.set(input.bundlesByHash, resolved);\n const known = (hash: string) =>\n knownParent(input.verifiedByHash.get(hash)?.state ?? resolved.get(hash));\n\n const head = known(input.bundle.manifestHash);\n if (head) return head;\n const walk = walkToPinnedParent(input, known, resolved);\n let reference = walk.base;\n for (const { hash, event } of walk.pending.reverse()) {\n if (\n event.eventType === \"container.rekey\" ||\n event.eventType === \"container.revoke\"\n ) {\n reference = {\n parentContainerId: reference.parentContainerId,\n parentManifestHash: readBodyHead(event, input.label),\n };\n }\n resolved.set(hash, reference);\n }\n return reference;\n}\n\n/** The parent path was resolved from the pin the derived state now proves. */\nexport function assertSignedContainerParentPin(\n input: PinInput,\n verified: VerifiedContainerAccessManifest,\n): void {\n const pin = readSignedContainerParentReference(input);\n if (\n pin.parentContainerId !== verified.state.parentContainerId ||\n pin.parentManifestHash !== verified.state.parentManifestHash\n ) {\n throw new KeyingVerificationError(\n \"object_mismatch\",\n `${input.label} parent pin differs from its derived state`,\n );\n }\n}\n\n/** The parent a create or move body pins, read from its verified body. */\nexport function readContainerEventBodyParentReference(\n event: VerifiedContainerAccessManifest[\"event\"],\n label: string,\n): ContainerParentReference {\n return readKeyingVerificationShape(() => {\n const body = readRecord(event.body, `${label} event body`);\n return {\n parentContainerId: readRecordNullableString(\n body,\n \"parentContainerId\",\n `${label} event body`,\n ),\n parentManifestHash: readRecordNullableString(\n body,\n \"parentManifestHash\",\n `${label} event body`,\n ),\n };\n });\n}\n"]}
|
|
@@ -1,14 +1,24 @@
|
|
|
1
1
|
import { KeyingVerificationError, readKeyingCanonicalJson, verifyDocumentLinkSetManifest, } from "../../../internal/crypto/index.js";
|
|
2
2
|
import { addBundleByHash, assertCanonicalEqual, verifyAccessEventBundle, } from "../checkpoints/bundleVerification.js";
|
|
3
3
|
import { loadManifestCheckpointVerification, verifyCachedManifestCheckpoint, } from "../checkpoints/manifestCheckpointVerification.js";
|
|
4
|
-
import { recordVerifiedBundleBytes } from "../checkpoints/verifiedBundleBytes.js";
|
|
4
|
+
import { assertUnchangedSinceVerification, matchesVerifiedBundleBytes, recordVerifiedBundleBytes, } from "../checkpoints/verifiedBundleBytes.js";
|
|
5
5
|
import { readAccessManifest, readDocumentAccessEventBody } from "../readers.js";
|
|
6
6
|
import { resolveEventContainerPaths } from "./documentDependencyPaths.js";
|
|
7
7
|
import { collectDocumentManifestPrincipalPolicies, recordUsedDocumentContainerManifests, } from "./documentManifestPolicies.js";
|
|
8
8
|
import { requireVerifiedDocumentPredecessor } from "./documentManifestPredecessor.js";
|
|
9
|
+
function assertVerifiedState(input, verified) {
|
|
10
|
+
assertCanonicalEqual({
|
|
11
|
+
actual: input.bundle.state,
|
|
12
|
+
expected: readKeyingCanonicalJson(verified.state, `${input.label} state`),
|
|
13
|
+
label: `${input.label} state`,
|
|
14
|
+
});
|
|
15
|
+
}
|
|
9
16
|
export async function verifyDocumentManifestBundle(input) {
|
|
10
17
|
const cached = input.verifiedByHash.get(input.bundle.manifestHash);
|
|
11
|
-
|
|
18
|
+
// A cache hit vouches for the bytes it verified; a copy that differs is
|
|
19
|
+
// verified afresh.
|
|
20
|
+
if (cached && matchesVerifiedBundleBytes(cached, input.bundle)) {
|
|
21
|
+
assertVerifiedState(input, cached);
|
|
12
22
|
if (input.enforceLocalCheckpoint) {
|
|
13
23
|
await verifyCachedManifestCheckpoint({
|
|
14
24
|
current: cached,
|
|
@@ -16,9 +26,24 @@ export async function verifyDocumentManifestBundle(input) {
|
|
|
16
26
|
localCheckpoints: input.checkpointContext.localCheckpoints,
|
|
17
27
|
verifiedManifests: input.verifiedByHash,
|
|
18
28
|
});
|
|
29
|
+
// The caller reads this bundle on return, after the checkpoint read.
|
|
30
|
+
assertUnchangedSinceVerification(cached, input.bundle, input.label);
|
|
31
|
+
assertVerifiedState(input, cached);
|
|
19
32
|
}
|
|
20
33
|
return cached;
|
|
21
34
|
}
|
|
35
|
+
// Verify and record one copy taken before the first await, so the bytes
|
|
36
|
+
// recorded as verified are exactly the bytes verified.
|
|
37
|
+
const snapshot = structuredClone(input.bundle);
|
|
38
|
+
const verified = await verifyFreshDocumentManifestBundle({
|
|
39
|
+
...input,
|
|
40
|
+
bundle: snapshot,
|
|
41
|
+
});
|
|
42
|
+
assertUnchangedSinceVerification(verified, input.bundle, input.label, snapshot);
|
|
43
|
+
assertVerifiedState(input, verified);
|
|
44
|
+
return verified;
|
|
45
|
+
}
|
|
46
|
+
async function verifyFreshDocumentManifestBundle(input) {
|
|
22
47
|
const event = await verifyAccessEventBundle(input);
|
|
23
48
|
const manifest = readAccessManifest(input.bundle.manifest, `${input.label} manifest`);
|
|
24
49
|
const previousManifest = requireVerifiedDocumentPredecessor({
|
|
@@ -72,11 +97,7 @@ export async function verifyDocumentManifestBundle(input) {
|
|
|
72
97
|
paths: [...dependencyContainerPaths, targetContainerPath],
|
|
73
98
|
used: input.usedContainerManifests,
|
|
74
99
|
});
|
|
75
|
-
|
|
76
|
-
actual: input.bundle.state,
|
|
77
|
-
expected: readKeyingCanonicalJson(verified.value.state, `${input.label} state`),
|
|
78
|
-
label: `${input.label} state`,
|
|
79
|
-
});
|
|
100
|
+
assertVerifiedState(input, verified.value);
|
|
80
101
|
input.verifiedByHash.set(input.bundle.manifestHash, verified.value);
|
|
81
102
|
recordVerifiedBundleBytes(verified.value, input.bundle);
|
|
82
103
|
return verified.value;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"documentManifestVerification.js","sourceRoot":"","sources":["documentManifestVerification.ts"],"names":[],"mappings":"AACA,OAAO,EACL,uBAAuB,EACvB,uBAAuB,EAIvB,6BAA6B,GAC9B,MAAM,mBAAmB,CAAC;AAK3B,OAAO,EACL,eAAe,EACf,oBAAoB,EACpB,uBAAuB,GACxB,MAAM,mCAAmC,CAAC;AAE3C,OAAO,EACL,kCAAkC,EAClC,8BAA8B,GAC/B,MAAM,+CAA+C,CAAC;AACvD,OAAO,EAAE,yBAAyB,EAAE,MAAM,oCAAoC,CAAC;AAC/E,OAAO,EAAE,kBAAkB,EAAE,2BAA2B,EAAE,MAAM,YAAY,CAAC;AAM7E,OAAO,EAAE,0BAA0B,EAAE,MAAM,2BAA2B,CAAC;AACvE,OAAO,EACL,wCAAwC,EACxC,oCAAoC,GAErC,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,kCAAkC,EAAE,MAAM,+BAA+B,CAAC;AAInF,MAAM,CAAC,KAAK,UAAU,4BAA4B,CAAC,KAuBlD;IACC,MAAM,MAAM,GAAG,KAAK,CAAC,cAAc,CAAC,GAAG,CAAC,KAAK,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC;IACnE,IAAI,MAAM,EAAE,CAAC;QACX,IAAI,KAAK,CAAC,sBAAsB,EAAE,CAAC;YACjC,MAAM,8BAA8B,CAAC;gBACnC,OAAO,EAAE,MAAM;gBACf,OAAO,EAAE,KAAK,CAAC,iBAAiB,CAAC,OAAO;gBACxC,gBAAgB,EAAE,KAAK,CAAC,iBAAiB,CAAC,gBAAgB;gBAC1D,iBAAiB,EAAE,KAAK,CAAC,cAAc;aACxC,CAAC,CAAC;QACL,CAAC;QACD,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CAAC,MAAM,EAAE,CAAC;YACZ,MAAM,IAAI,uBAAuB,CAC/B,oBAAoB,EACpB,+CAA+C,KAAK,cAAc,CACnE,CAAC;QACJ,CAAC;QACD,MAAM,4BAA4B,CAAC;YACjC,qBAAqB,EAAE,KAAK,CAAC,qBAAqB;YAClD,4BAA4B,EAAE,IAAI;YAClC,MAAM;YACN,aAAa;YACb,iBAAiB,EAAE,KAAK,CAAC,iBAAiB;YAC1C,2BAA2B,EAAE,KAAK,CAAC,2BAA2B;YAC9D,sBAAsB,EAAE,KAAK;YAC7B,KAAK,EAAE,+CAA+C,KAAK,GAAG;YAC9D,oBAAoB,EAAE,KAAK,CAAC,oBAAoB;YAChD,cAAc,EAAE,KAAK,CAAC,cAAc;YACpC,cAAc;YACd,+BAA+B,EAAE,KAAK,CAAC,+BAA+B;SACvE,CAAC,CAAC;IACL,CAAC;IAED,MAAM,YAAY,GAAG,MAAM,4BAA4B,CAAC;QACtD,qBAAqB,EAAE,KAAK,CAAC,qBAAqB;QAClD,4BAA4B,EAAE,IAAI;QAClC,MAAM,EAAE,KAAK,CAAC,UAAU,CAAC,gBAAgB;QACzC,aAAa;QACb,iBAAiB,EAAE,KAAK,CAAC,iBAAiB;QAC1C,2BAA2B,EAAE,KAAK,CAAC,2BAA2B;QAC9D,sBAAsB,EAAE,IAAI;QAC5B,KAAK,EAAE,4BAA4B;QACnC,oBAAoB,EAAE,KAAK,CAAC,oBAAoB;QAChD,cAAc,EAAE,KAAK,CAAC,cAAc;QACpC,cAAc;QACd,+BAA+B,EAAE,KAAK,CAAC,+BAA+B;KACvE,CAAC,CAAC;IACH,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,CAAC;AAC1C,CAAC","sourcesContent":["import type { PrincipalPolicyAuthorization } from \"@tearleads/crypto\";\nimport {\n KeyingVerificationError,\n readKeyingCanonicalJson,\n type VerifiedContainerAccessManifest,\n type VerifiedDocumentLinkSetManifest,\n type VerifiedDocumentLinkSetSnapshot,\n verifyDocumentLinkSetManifest,\n} from \"@tearleads/crypto\";\nimport type {\n AccessManifestBundleWireResponse,\n DocumentWriterProjectionResponse,\n} from \"@tearleads/validators/response\";\nimport {\n addBundleByHash,\n assertCanonicalEqual,\n verifyAccessEventBundle,\n} from \"../checkpoints/bundleVerification\";\nimport type { ProjectionCheckpointContext } from \"../checkpoints/checkpointContext\";\nimport {\n loadManifestCheckpointVerification,\n verifyCachedManifestCheckpoint,\n} from \"../checkpoints/manifestCheckpointVerification\";\nimport { recordVerifiedBundleBytes } from \"../checkpoints/verifiedBundleBytes\";\nimport { readAccessManifest, readDocumentAccessEventBody } from \"../readers\";\nimport type {\n PrincipalPolicyCache,\n ProjectionUserKeyResolver,\n ReferencedPrincipalPolicyWarmer,\n} from \"../types\";\nimport { resolveEventContainerPaths } from \"./documentDependencyPaths\";\nimport {\n collectDocumentManifestPrincipalPolicies,\n recordUsedDocumentContainerManifests,\n type UsedDocumentContainerManifests,\n} from \"./documentManifestPolicies\";\nimport { requireVerifiedDocumentPredecessor } from \"./documentManifestPredecessor\";\n\ntype PolicyWarmer = ReferencedPrincipalPolicyWarmer | undefined;\n\nexport async function verifyDocumentManifestBundle(input: {\n readonly authorizationMembership?: \"current\" | \"referenced\" | undefined;\n readonly authorizationEvidence?:\n | readonly PrincipalPolicyAuthorization[]\n | undefined;\n readonly bundle: AccessManifestBundleWireResponse;\n readonly bundlesByHash: ReadonlyMap<string, AccessManifestBundleWireResponse>;\n readonly containerPathByManifestHash: ReadonlyMap<\n string,\n readonly VerifiedContainerAccessManifest[]\n >;\n readonly checkpointContext: ProjectionCheckpointContext;\n readonly enforceLocalCheckpoint: boolean;\n readonly label: string;\n readonly principalPolicyCache: PrincipalPolicyCache;\n readonly resolveUserKey: ProjectionUserKeyResolver;\n readonly requireAuthorizationEvidence?: boolean | undefined;\n readonly trustedPredecessorByHash?:\n | ReadonlyMap<string, VerifiedDocumentLinkSetSnapshot>\n | undefined;\n readonly usedContainerManifests?: UsedDocumentContainerManifests | undefined;\n readonly verifiedByHash: Map<string, VerifiedDocumentLinkSetManifest>;\n readonly warmReferencedPrincipalPolicies?: PolicyWarmer;\n}): Promise<VerifiedDocumentLinkSetManifest> {\n const cached = input.verifiedByHash.get(input.bundle.manifestHash);\n if (cached) {\n if (input.enforceLocalCheckpoint) {\n await verifyCachedManifestCheckpoint({\n current: cached,\n execSql: input.checkpointContext.execSql,\n localCheckpoints: input.checkpointContext.localCheckpoints,\n verifiedManifests: input.verifiedByHash,\n });\n }\n return cached;\n }\n\n const event = await verifyAccessEventBundle(input);\n const manifest = readAccessManifest(\n input.bundle.manifest,\n `${input.label} manifest`,\n );\n const previousManifest = requireVerifiedDocumentPredecessor({\n label: input.label,\n previousManifestHash: event.event.previousManifestHash,\n trustedPredecessorByHash: input.trustedPredecessorByHash,\n verifiedByHash: input.verifiedByHash,\n });\n const body = readDocumentAccessEventBody(\n event.body,\n `${input.label} event body`,\n );\n const { dependencyContainerPaths, targetContainerPath } =\n resolveEventContainerPaths({\n containerPathByManifestHash: input.containerPathByManifestHash,\n dependencyManifestHashes: event.event.dependencyManifestHashes,\n targetManifestHash: body.containerManifestHash,\n });\n const principalPolicies = await collectDocumentManifestPrincipalPolicies({\n authorizationEvidence: input.authorizationEvidence,\n checkpointContext: input.checkpointContext,\n organizationId: event.event.organizationId,\n paths: [...dependencyContainerPaths, targetContainerPath],\n principalPolicyCache: input.principalPolicyCache,\n resolveUserKey: input.resolveUserKey,\n requireAuthorizationEvidence: input.requireAuthorizationEvidence,\n warmReferencedPrincipalPolicies: input.warmReferencedPrincipalPolicies,\n });\n const checkpointVerification = input.enforceLocalCheckpoint\n ? await loadManifestCheckpointVerification({\n current: manifest,\n execSql: input.checkpointContext.execSql,\n localCheckpoints: input.checkpointContext.localCheckpoints,\n verifiedManifests: input.verifiedByHash,\n })\n : null;\n const verified = await verifyDocumentLinkSetManifest({\n // Served heads and history were committed under the group membership their\n // cited container heads referenced; a signer removed since must still\n // verify on a cold device. The API verifier gates new writes at current.\n authorizationMembership: input.authorizationMembership ?? \"referenced\",\n authorizingContainerPaths: dependencyContainerPaths,\n event,\n expectedManifestHash: input.bundle.manifestHash,\n manifest,\n previousManifest,\n principalPolicies,\n ...(checkpointVerification ?? {}),\n ...(targetContainerPath ? { targetContainerPath } : {}),\n });\n if (!verified.ok) {\n throw new KeyingVerificationError(\n verified.error.code,\n `${input.label} manifest verification failed: ${verified.error.message}`,\n );\n }\n\n recordUsedDocumentContainerManifests({\n paths: [...dependencyContainerPaths, targetContainerPath],\n used: input.usedContainerManifests,\n });\n assertCanonicalEqual({\n actual: input.bundle.state,\n expected: readKeyingCanonicalJson(\n verified.value.state,\n `${input.label} state`,\n ),\n label: `${input.label} state`,\n });\n input.verifiedByHash.set(input.bundle.manifestHash, verified.value);\n recordVerifiedBundleBytes(verified.value, input.bundle);\n\n return verified.value;\n}\n\nexport async function verifyDocumentProjectionManifests(\n input: Omit<\n Parameters<typeof verifyDocumentManifestBundle>[0],\n | \"bundle\"\n | \"bundlesByHash\"\n | \"enforceLocalCheckpoint\"\n | \"label\"\n | \"verifiedByHash\"\n > & { readonly projection: DocumentWriterProjectionResponse },\n) {\n const bundlesByHash = new Map<string, AccessManifestBundleWireResponse>();\n addBundleByHash(\n bundlesByHash,\n input.projection.documentManifest,\n \"Document writer projection manifest\",\n );\n const history = input.projection.documentManifestHistory;\n for (const [index, bundle] of history.entries()) {\n addBundleByHash(\n bundlesByHash,\n bundle,\n `Document writer projection manifest history[${index}]`,\n );\n }\n // An honest API never lists the head in its own history (it seeds the walk\n // with the head), so a repeat is a malformed or tampered projection and is\n // refused as such, rather than letting the head reach its verification\n // through the history cache and relying on the cached branch to re-run\n // every check the fresh path would.\n if (\n history.some(\n (bundle) =>\n bundle.manifestHash === input.projection.documentManifest.manifestHash,\n )\n ) {\n throw new KeyingVerificationError(\n \"duplicate_entry\",\n \"Document writer projection history repeats the current head\",\n );\n }\n\n const verifiedByHash = new Map<string, VerifiedDocumentLinkSetManifest>();\n for (let index = history.length - 1; index >= 0; index -= 1) {\n const bundle = history[index];\n if (!bundle) {\n throw new KeyingVerificationError(\n \"missing_dependency\",\n `Document writer projection manifest history[${index}] is missing`,\n );\n }\n await verifyDocumentManifestBundle({\n authorizationEvidence: input.authorizationEvidence,\n requireAuthorizationEvidence: true,\n bundle,\n bundlesByHash,\n checkpointContext: input.checkpointContext,\n containerPathByManifestHash: input.containerPathByManifestHash,\n enforceLocalCheckpoint: false,\n label: `Document writer projection manifest history[${index}]`,\n principalPolicyCache: input.principalPolicyCache,\n resolveUserKey: input.resolveUserKey,\n verifiedByHash,\n warmReferencedPrincipalPolicies: input.warmReferencedPrincipalPolicies,\n });\n }\n\n const headManifest = await verifyDocumentManifestBundle({\n authorizationEvidence: input.authorizationEvidence,\n requireAuthorizationEvidence: true,\n bundle: input.projection.documentManifest,\n bundlesByHash,\n checkpointContext: input.checkpointContext,\n containerPathByManifestHash: input.containerPathByManifestHash,\n enforceLocalCheckpoint: true,\n label: \"Document writer projection\",\n principalPolicyCache: input.principalPolicyCache,\n resolveUserKey: input.resolveUserKey,\n verifiedByHash,\n warmReferencedPrincipalPolicies: input.warmReferencedPrincipalPolicies,\n });\n return { headManifest, verifiedByHash };\n}\n"]}
|
|
1
|
+
{"version":3,"file":"documentManifestVerification.js","sourceRoot":"","sources":["documentManifestVerification.ts"],"names":[],"mappings":"AACA,OAAO,EACL,uBAAuB,EACvB,uBAAuB,EAIvB,6BAA6B,GAC9B,MAAM,mBAAmB,CAAC;AAK3B,OAAO,EACL,eAAe,EACf,oBAAoB,EACpB,uBAAuB,GACxB,MAAM,mCAAmC,CAAC;AAE3C,OAAO,EACL,kCAAkC,EAClC,8BAA8B,GAC/B,MAAM,+CAA+C,CAAC;AACvD,OAAO,EACL,gCAAgC,EAChC,0BAA0B,EAC1B,yBAAyB,GAC1B,MAAM,oCAAoC,CAAC;AAC5C,OAAO,EAAE,kBAAkB,EAAE,2BAA2B,EAAE,MAAM,YAAY,CAAC;AAM7E,OAAO,EAAE,0BA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AE,cAAc,EAAE,CAAC;AAC1C,CAAC","sourcesContent":["import type { PrincipalPolicyAuthorization } from \"@tearleads/crypto\";\nimport {\n KeyingVerificationError,\n readKeyingCanonicalJson,\n type VerifiedContainerAccessManifest,\n type VerifiedDocumentLinkSetManifest,\n type VerifiedDocumentLinkSetSnapshot,\n verifyDocumentLinkSetManifest,\n} from \"@tearleads/crypto\";\nimport type {\n AccessManifestBundleWireResponse,\n DocumentWriterProjectionResponse,\n} from \"@tearleads/validators/response\";\nimport {\n addBundleByHash,\n assertCanonicalEqual,\n verifyAccessEventBundle,\n} from \"../checkpoints/bundleVerification\";\nimport type { ProjectionCheckpointContext } from \"../checkpoints/checkpointContext\";\nimport {\n loadManifestCheckpointVerification,\n verifyCachedManifestCheckpoint,\n} from \"../checkpoints/manifestCheckpointVerification\";\nimport {\n assertUnchangedSinceVerification,\n matchesVerifiedBundleBytes,\n recordVerifiedBundleBytes,\n} from \"../checkpoints/verifiedBundleBytes\";\nimport { readAccessManifest, readDocumentAccessEventBody } from \"../readers\";\nimport type {\n PrincipalPolicyCache,\n ProjectionUserKeyResolver,\n ReferencedPrincipalPolicyWarmer,\n} from \"../types\";\nimport { resolveEventContainerPaths } from \"./documentDependencyPaths\";\nimport {\n collectDocumentManifestPrincipalPolicies,\n recordUsedDocumentContainerManifests,\n type UsedDocumentContainerManifests,\n} from \"./documentManifestPolicies\";\nimport { requireVerifiedDocumentPredecessor } from \"./documentManifestPredecessor\";\n\ntype PolicyWarmer = ReferencedPrincipalPolicyWarmer | undefined;\n\nfunction assertVerifiedState(\n input: {\n readonly bundle: AccessManifestBundleWireResponse;\n readonly label: string;\n },\n verified: VerifiedDocumentLinkSetManifest,\n): void {\n assertCanonicalEqual({\n actual: input.bundle.state,\n expected: readKeyingCanonicalJson(verified.state, `${input.label} state`),\n label: `${input.label} state`,\n });\n}\n\nexport async function verifyDocumentManifestBundle(input: {\n readonly authorizationMembership?: \"current\" | \"referenced\" | undefined;\n readonly authorizationEvidence?:\n | readonly PrincipalPolicyAuthorization[]\n | undefined;\n readonly bundle: AccessManifestBundleWireResponse;\n readonly bundlesByHash: ReadonlyMap<string, AccessManifestBundleWireResponse>;\n readonly containerPathByManifestHash: ReadonlyMap<\n string,\n readonly VerifiedContainerAccessManifest[]\n >;\n readonly checkpointContext: ProjectionCheckpointContext;\n readonly enforceLocalCheckpoint: boolean;\n readonly label: string;\n readonly principalPolicyCache: PrincipalPolicyCache;\n readonly resolveUserKey: ProjectionUserKeyResolver;\n readonly requireAuthorizationEvidence?: boolean | undefined;\n readonly trustedPredecessorByHash?:\n | ReadonlyMap<string, VerifiedDocumentLinkSetSnapshot>\n | undefined;\n readonly usedContainerManifests?: UsedDocumentContainerManifests | undefined;\n readonly verifiedByHash: Map<string, VerifiedDocumentLinkSetManifest>;\n readonly warmReferencedPrincipalPolicies?: PolicyWarmer;\n}): Promise<VerifiedDocumentLinkSetManifest> {\n const cached = input.verifiedByHash.get(input.bundle.manifestHash);\n // A cache hit vouches for the bytes it verified; a copy that differs is\n // verified afresh.\n if (cached && matchesVerifiedBundleBytes(cached, input.bundle)) {\n assertVerifiedState(input, cached);\n if (input.enforceLocalCheckpoint) {\n await verifyCachedManifestCheckpoint({\n current: cached,\n execSql: input.checkpointContext.execSql,\n localCheckpoints: input.checkpointContext.localCheckpoints,\n verifiedManifests: input.verifiedByHash,\n });\n // The caller reads this bundle on return, after the checkpoint read.\n assertUnchangedSinceVerification(cached, input.bundle, input.label);\n assertVerifiedState(input, cached);\n }\n return cached;\n }\n // Verify and record one copy taken before the first await, so the bytes\n // recorded as verified are exactly the bytes verified.\n const snapshot = structuredClone(input.bundle);\n const verified = await verifyFreshDocumentManifestBundle({\n ...input,\n bundle: snapshot,\n });\n assertUnchangedSinceVerification(\n verified,\n input.bundle,\n input.label,\n snapshot,\n );\n assertVerifiedState(input, verified);\n return verified;\n}\n\nasync function verifyFreshDocumentManifestBundle(\n input: Parameters<typeof verifyDocumentManifestBundle>[0],\n): Promise<VerifiedDocumentLinkSetManifest> {\n const event = await verifyAccessEventBundle(input);\n const manifest = readAccessManifest(\n input.bundle.manifest,\n `${input.label} manifest`,\n );\n const previousManifest = requireVerifiedDocumentPredecessor({\n label: input.label,\n previousManifestHash: event.event.previousManifestHash,\n trustedPredecessorByHash: input.trustedPredecessorByHash,\n verifiedByHash: input.verifiedByHash,\n });\n const body = readDocumentAccessEventBody(\n event.body,\n `${input.label} event body`,\n );\n const { dependencyContainerPaths, targetContainerPath } =\n resolveEventContainerPaths({\n containerPathByManifestHash: input.containerPathByManifestHash,\n dependencyManifestHashes: event.event.dependencyManifestHashes,\n targetManifestHash: body.containerManifestHash,\n });\n const principalPolicies = await collectDocumentManifestPrincipalPolicies({\n authorizationEvidence: input.authorizationEvidence,\n checkpointContext: input.checkpointContext,\n organizationId: event.event.organizationId,\n paths: [...dependencyContainerPaths, targetContainerPath],\n principalPolicyCache: input.principalPolicyCache,\n resolveUserKey: input.resolveUserKey,\n requireAuthorizationEvidence: input.requireAuthorizationEvidence,\n warmReferencedPrincipalPolicies: input.warmReferencedPrincipalPolicies,\n });\n const checkpointVerification = input.enforceLocalCheckpoint\n ? await loadManifestCheckpointVerification({\n current: manifest,\n execSql: input.checkpointContext.execSql,\n localCheckpoints: input.checkpointContext.localCheckpoints,\n verifiedManifests: input.verifiedByHash,\n })\n : null;\n const verified = await verifyDocumentLinkSetManifest({\n // Served heads and history were committed under the group membership their\n // cited container heads referenced; a signer removed since must still\n // verify on a cold device. The API verifier gates new writes at current.\n authorizationMembership: input.authorizationMembership ?? \"referenced\",\n authorizingContainerPaths: dependencyContainerPaths,\n event,\n expectedManifestHash: input.bundle.manifestHash,\n manifest,\n previousManifest,\n principalPolicies,\n ...(checkpointVerification ?? {}),\n ...(targetContainerPath ? { targetContainerPath } : {}),\n });\n if (!verified.ok) {\n throw new KeyingVerificationError(\n verified.error.code,\n `${input.label} manifest verification failed: ${verified.error.message}`,\n );\n }\n\n recordUsedDocumentContainerManifests({\n paths: [...dependencyContainerPaths, targetContainerPath],\n used: input.usedContainerManifests,\n });\n assertVerifiedState(input, verified.value);\n input.verifiedByHash.set(input.bundle.manifestHash, verified.value);\n recordVerifiedBundleBytes(verified.value, input.bundle);\n\n return verified.value;\n}\n\nexport async function verifyDocumentProjectionManifests(\n input: Omit<\n Parameters<typeof verifyDocumentManifestBundle>[0],\n | \"bundle\"\n | \"bundlesByHash\"\n | \"enforceLocalCheckpoint\"\n | \"label\"\n | \"verifiedByHash\"\n > & { readonly projection: DocumentWriterProjectionResponse },\n) {\n const bundlesByHash = new Map<string, AccessManifestBundleWireResponse>();\n addBundleByHash(\n bundlesByHash,\n input.projection.documentManifest,\n \"Document writer projection manifest\",\n );\n const history = input.projection.documentManifestHistory;\n for (const [index, bundle] of history.entries()) {\n addBundleByHash(\n bundlesByHash,\n bundle,\n `Document writer projection manifest history[${index}]`,\n );\n }\n // An honest API never lists the head in its own history (it seeds the walk\n // with the head), so a repeat is a malformed or tampered projection and is\n // refused as such, rather than letting the head reach its verification\n // through the history cache and relying on the cached branch to re-run\n // every check the fresh path would.\n if (\n history.some(\n (bundle) =>\n bundle.manifestHash === input.projection.documentManifest.manifestHash,\n )\n ) {\n throw new KeyingVerificationError(\n \"duplicate_entry\",\n \"Document writer projection history repeats the current head\",\n );\n }\n\n const verifiedByHash = new Map<string, VerifiedDocumentLinkSetManifest>();\n for (let index = history.length - 1; index >= 0; index -= 1) {\n const bundle = history[index];\n if (!bundle) {\n throw new KeyingVerificationError(\n \"missing_dependency\",\n `Document writer projection manifest history[${index}] is missing`,\n );\n }\n await verifyDocumentManifestBundle({\n authorizationEvidence: input.authorizationEvidence,\n requireAuthorizationEvidence: true,\n bundle,\n bundlesByHash,\n checkpointContext: input.checkpointContext,\n containerPathByManifestHash: input.containerPathByManifestHash,\n enforceLocalCheckpoint: false,\n label: `Document writer projection manifest history[${index}]`,\n principalPolicyCache: input.principalPolicyCache,\n resolveUserKey: input.resolveUserKey,\n verifiedByHash,\n warmReferencedPrincipalPolicies: input.warmReferencedPrincipalPolicies,\n });\n }\n\n const headManifest = await verifyDocumentManifestBundle({\n authorizationEvidence: input.authorizationEvidence,\n requireAuthorizationEvidence: true,\n bundle: input.projection.documentManifest,\n bundlesByHash,\n checkpointContext: input.checkpointContext,\n containerPathByManifestHash: input.containerPathByManifestHash,\n enforceLocalCheckpoint: true,\n label: \"Document writer projection\",\n principalPolicyCache: input.principalPolicyCache,\n resolveUserKey: input.resolveUserKey,\n verifiedByHash,\n warmReferencedPrincipalPolicies: input.warmReferencedPrincipalPolicies,\n });\n return { headManifest, verifiedByHash };\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bundleCitationRows.js","sourceRoot":"","sources":["bundleCitationRows.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AACzD,OAAO,EAAE,6BAA6B,IAAI,OAAO,EAAE,MAAM,kDAAkD,CAAC;AAU5G,MAAM,CAAC,MAAM,aAAa,GAAG;IAC3B,YAAY,EAAE,OAAO,CAAC,YAAY;IAClC,UAAU,EAAE,OAAO,CAAC,UAAU;IAC9B,YAAY,EAAE,OAAO,CAAC,YAAY;CACnC,CAAC;AAEF,MAAM,UAAU,UAAU,CACxB,EAAwB,EACxB,cAAsB,EACtB,MAAmB,EACnB,KAAa;IAEb,OAAO,EAAE;SACN,MAAM,CAAC,aAAa,CAAC;SACrB,IAAI,CAAC,OAAO,CAAC;SACb,KAAK,CACJ,GAAG,CACD,EAAE,CAAC,OAAO,CAAC,cAAc,EAAE,cAAc,CAAC,EAC1C,EAAE,CAAC,OAAO,CAAC,UAAU,EAAE,MAAM,CAAC,IAAI,CAAC,EACnC,EAAE,CAAC,OAAO,CAAC,QAAQ,EAAE,MAAM,CAAC,EAAE,CAAC,CAChC,CACF;SACA,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;SAC5B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AE,OAAO,IAAI,CAAC;QAC5C,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IACpB,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC","sourcesContent":["import type { ServedAccessManifestBundleWireResponse } from \"@tearleads/validators/util\";\nimport { and, desc, eq, inArray, or } from \"drizzle-orm\";\nimport { verifiedAccessManifestBundles as bundles } from \"../../sqlite/schema/verifiedManifestBundleSchema\";\nimport type { ClientSQLiteDatabase } from \"../../sqlite/sqlitePersistenceRuntime\";\nimport type { CitedObject } from \"./citationScope\";\n\nexport interface StoredBundleRow {\n readonly bundleDigest: string;\n readonly bundleJson: string;\n readonly manifestHash: string;\n}\n\nexport const storedColumns = {\n bundleDigest: bundles.bundleDigest,\n bundleJson: bundles.bundleJson,\n manifestHash: bundles.manifestHash,\n};\n\nexport function objectRows(\n db: ClientSQLiteDatabase,\n organizationId: string,\n object: CitedObject,\n limit: number,\n) {\n return db\n .select(storedColumns)\n .from(bundles)\n .where(\n and(\n eq(bundles.organizationId, organizationId),\n eq(bundles.objectKind, object.kind),\n eq(bundles.objectId, object.id),\n ),\n )\n .orderBy(desc(bundles.epoch))\n .limit(limit);\n}\n\n/** Select narrow metadata first, then only the JSON the old object loop chose. */\nexport async function batchedObjectRows(\n db: ClientSQLiteDatabase,\n organizationId: string,\n objects: readonly CitedObject[],\n limit: number,\n read: (\n row: StoredBundleRow,\n ) => Promise<ServedAccessManifestBundleWireResponse | null>,\n): Promise<StoredBundleRow[] | null> {\n const byObject = new Map<string, string[]>();\n // Document placements can span many independent lineages. Bound the IN\n // lists even though each individual lineage is already depth-bounded.\n for (let offset = 0; offset < objects.length; offset += 200) {\n const batch = objects.slice(offset, offset + 200);\n const rows = await db\n .select({\n manifestHash: bundles.manifestHash,\n objectKind: bundles.objectKind,\n objectId: bundles.objectId,\n })\n .from(bundles)\n .where(\n and(\n eq(bundles.organizationId, organizationId),\n or(\n and(\n eq(bundles.objectKind, \"container\"),\n inArray(\n bundles.objectId,\n batch.filter((o) => o.kind === \"container\").map((o) => o.id),\n ),\n ),\n and(\n eq(bundles.objectKind, \"document\"),\n inArray(\n bundles.objectId,\n batch.filter((o) => o.kind === \"document\").map((o) => o.id),\n ),\n ),\n ),\n ),\n )\n .orderBy(desc(bundles.epoch));\n for (const row of rows) {\n const key = `${row.objectKind}:${row.objectId}`;\n const hashes = byObject.get(key) ?? [];\n hashes.push(row.manifestHash);\n byObject.set(key, hashes);\n }\n }\n const selected = objects\n .flatMap((object) => byObject.get(`${object.kind}:${object.id}`) ?? [])\n .slice(0, limit);\n if (selected.length === 0) return [];\n const rows = await db\n .select(storedColumns)\n .from(bundles)\n .where(\n and(\n eq(bundles.organizationId, organizationId),\n inArray(bundles.manifestHash, selected),\n ),\n );\n const byHash = new Map(rows.map((row) => [row.manifestHash, row]));\n const ordered: StoredBundleRow[] = [];\n for (const hash of selected) {\n const row = byHash.get(hash);\n // Damage (or eviction between the two reads) changes the remaining quota\n // of subsequent objects. Replay the old loop instead of guessing it.\n if (!row || !(await read(row))) return null;\n ordered.push(row);\n }\n return ordered;\n}\n"]}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { toFingerprint } from "../../../internal/crypto/index.js";
|
|
2
|
-
import {
|
|
2
|
+
import { isServedAccessManifestBundleWireResponse, } from "../../../internal/validators/util/index.js";
|
|
3
3
|
import { and, asc, desc, eq, ne, notInArray } from "drizzle-orm";
|
|
4
4
|
import { verifiedAccessManifestBundles as bundles } from "../../sqlite/schema/verifiedManifestBundleSchema.js";
|
|
5
5
|
import { batchedObjectRows, objectRows, storedColumns, } from "./bundleCitationRows.js";
|
|
@@ -11,7 +11,7 @@ async function readStoredBundle(row) {
|
|
|
11
11
|
return null;
|
|
12
12
|
try {
|
|
13
13
|
const bundle = JSON.parse(row.bundleJson);
|
|
14
|
-
return
|
|
14
|
+
return isServedAccessManifestBundleWireResponse(bundle) &&
|
|
15
15
|
bundle.manifestHash === row.manifestHash
|
|
16
16
|
? bundle
|
|
17
17
|
: null;
|