Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/hub/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"@corbits-builder/coder": "workspace:*",
"@corbits-builder/coordinator": "workspace:*",
"@corbits-builder/designer": "workspace:*",
"@corbits-builder/presenter": "workspace:*",
"@corbits-builder/reviewer": "workspace:*",
"@corbits-builder/schemas": "workspace:*",
"@corbits-builder/writer": "workspace:*",
Expand Down
75 changes: 75 additions & 0 deletions apps/hub/src/artifacts/approval-package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ export type StoredPackageSource = {
kind: string;
version: number;
sha256: string;
approved?: boolean;
};

export type SealedPackageAssignment = {
audience: string;
decisionNeeded?: string;
sources: readonly {
artifactId: string;
kind: string;
version: number;
sha256: string;
}[];
};

export type StoredSourceReader = (
Expand Down Expand Up @@ -118,11 +130,61 @@ async function verifySourcePin(
message: `${source.artifactId}@${String(source.version)}`,
},
]);
if (stored.approved === false)
throw new ProjectError(422, "unapproved_package_source", [
{
path: "manifest.sources",
message: `${source.kind}@${String(source.version)}`,
},
]);
}

function verifySealedAssignment(
bundle: ApprovalPackageBundle,
sealed: SealedPackageAssignment,
): void {
if (bundle.manifest.audience.trim() !== sealed.audience.trim()) {
throw new ProjectError(422, "package_assignment_mismatch", [
{
path: "manifest.audience",
message: `expected ${sealed.audience}`,
},
]);
}
if (
sealed.decisionNeeded !== undefined &&
bundle.manifest.decisionNeeded.trim() !== sealed.decisionNeeded.trim()
) {
throw new ProjectError(422, "package_assignment_mismatch", [
{
path: "manifest.decisionNeeded",
message: `expected ${sealed.decisionNeeded}`,
},
]);
}
const actualKeys = bundle.manifest.sources.map(
(s) => `${s.artifactId}:${s.kind}:${String(s.version)}:${s.sha256}`,
);
const expectedKeys = sealed.sources.map(
(s) => `${s.artifactId}:${s.kind}:${String(s.version)}:${s.sha256}`,
);
if (
actualKeys.length !== expectedKeys.length ||
actualKeys.some((key, idx) => key !== expectedKeys[idx])
) {
throw new ProjectError(422, "package_assignment_mismatch", [
{
path: "manifest.sources",
message: "sources do not match the approved hand-off selection",
},
]);
}
}

export async function validateApprovalPackage(
input: string | ApprovalPackageBundle,
readSource: StoredSourceReader,
sealedAssignment?: SealedPackageAssignment,
): Promise<ApprovalPackageBundle> {
const bundle =
typeof input === "string" ? parseApprovalPackageBundle(input) : input;
Expand All @@ -133,6 +195,9 @@ export async function validateApprovalPackage(
]);
}
scanApprovalPackageForSecrets(bundle);
if (sealedAssignment !== undefined) {
verifySealedAssignment(bundle, sealedAssignment);
}
for (const source of bundle.manifest.sources)
await verifySourcePin(source, readSource);
return bundle;
Expand All @@ -141,17 +206,27 @@ export async function validateApprovalPackage(
export function readProjectPackageSource(
db: ArtifactDb,
tenantId: string,
isApproved?: (
artifactId: string,
kind: string,
version: number,
) => Promise<boolean>,
): StoredSourceReader {
return async (artifactId, version) => {
if (!UUID_RE.test(artifactId)) return undefined;
const row = await getArtifact(db, artifactId);
if (!row || row.tenantId !== tenantId) return undefined;
const stored = await getArtifactVersion(db, artifactId, version);
if (!stored?.contentSha256) return undefined;
const approved =
isApproved === undefined
? true
: await isApproved(artifactId, row.kind, stored.version);
return {
kind: row.kind,
version: stored.version,
sha256: stored.contentSha256,
approved,
};
};
}
Expand Down
Loading