feat: safe Evidence tree enumeration and bounded blob streaming (P6)

This commit is contained in:
2026-08-13 12:28:49 +02:00
parent e80a8b35ec
commit 0c1033889a
2 changed files with 193 additions and 5 deletions
+64 -5
View File
@@ -16,6 +16,12 @@ export interface GitStatus {
lastError?: WorkspaceErrorCode;
}
export interface EvidenceTreeObject {
mode: "100644" | "100755";
oid: string;
posixPath: string;
}
export class WorkspaceRegistryError extends Error {
constructor(readonly code: WorkspaceErrorCode, message: string) {
super(message);
@@ -266,16 +272,69 @@ export class GitWorkspaceRepository {
throw new WorkspaceRegistryError("workspace_invalid", "Workspace annotations object is invalid");
}
const blobId = match[3];
const contents = await this.gitBlobBuffer(blobId, 16 * 1024 * 1024);
const contents = await this.gitBlobBytes(blobId, 16 * 1024 * 1024, "annotations");
if (!isValidUtf8(contents)) {
throw new WorkspaceRegistryError("workspace_invalid", "Workspace annotations object is not valid UTF-8");
}
return { blobId, contents };
}
private async gitBlobBuffer(objectId: string, maxBytes: number): Promise<Buffer> {
/**
* Recursively enumerate a canonical `<id>/evidence` tree at an exact commit as regular Git blobs.
* Symlinks (120000), gitlinks (160000), non-regular modes, non-blob types, traversal/absolute/
* duplicate/cross-namespace paths, and NUL/newline-bearing names are refused.
*/
async evidenceTreeObjects(revision: string, id: string): Promise<EvidenceTreeObject[]> {
if (!/^[0-9a-f]{40}$/.test(revision)) {
throw new WorkspaceRegistryError("workspace_invalid", "Workspace Evidence revision is invalid");
}
if (!/^[a-z][a-z0-9-]{2,62}$/.test(id)) {
throw new WorkspaceRegistryError("workspace_invalid", "Workspace Evidence path is invalid");
}
const prefix = `${id}/evidence`;
const listing = await this.git(["ls-tree", "-r", "-z", "--full-tree", revision, "--", prefix]);
const entries = listing.split("\0").filter((entry) => entry.length > 0);
const seen = new Set<string>();
const objects: EvidenceTreeObject[] = [];
for (const entry of entries) {
const tab = entry.lastIndexOf("\t");
if (tab < 0) throw new WorkspaceRegistryError("workspace_invalid", "Workspace Evidence object is invalid");
const name = entry.slice(tab + 1);
const meta = entry.slice(0, tab);
const match = /^([0-9]{6}) (blob|commit|tree) ([0-9a-f]{40})$/.exec(meta);
if (match === null || match[2] !== "blob" || (match[1] !== "100644" && match[1] !== "100755")) {
throw new WorkspaceRegistryError("workspace_invalid", "Workspace Evidence object is invalid");
}
if (name === prefix) {
// The Evidence root resolves to a single regular blob (or symlink/gitlink already refused above).
throw new WorkspaceRegistryError("workspace_invalid", "Workspace Evidence root is invalid");
}
if (!name.startsWith(`${prefix}/`)) {
throw new WorkspaceRegistryError("workspace_invalid", "Workspace Evidence object escapes its namespace");
}
const rel = name.slice(prefix.length + 1);
if (rel.length === 0 || rel.includes("\0") || rel.includes("\n") || rel.includes("\r")) {
throw new WorkspaceRegistryError("workspace_invalid", "Workspace Evidence path is invalid");
}
const segments = rel.split("/");
if (segments.some((segment) => segment === "" || segment === "." || segment === "..")) {
throw new WorkspaceRegistryError("workspace_invalid", "Workspace Evidence path is invalid");
}
if (seen.has(rel)) throw new WorkspaceRegistryError("workspace_invalid", "Workspace Evidence path is duplicated");
seen.add(rel);
objects.push({ mode: match[1] as "100644" | "100755", oid: match[3], posixPath: rel });
}
return objects;
}
/** Read one Evidence blob with a per-object byte bound. */
evidenceBlobBytes(objectId: string, maxBytes: number): Promise<Buffer> {
return this.gitBlobBytes(objectId, maxBytes, "Evidence");
}
private async gitBlobBytes(objectId: string, maxBytes: number, label: string): Promise<Buffer> {
if (!/^[0-9a-f]{40}$/.test(objectId)) {
throw new WorkspaceRegistryError("workspace_invalid", "Workspace annotations object is invalid");
throw new WorkspaceRegistryError("workspace_invalid", `Workspace ${label} object is invalid`);
}
try {
const { stdout } = await execFileAsync(
@@ -289,14 +348,14 @@ export class GitWorkspaceRepository {
},
);
if (stdout.length > maxBytes) {
throw new WorkspaceRegistryError("workspace_invalid", "Workspace annotations object is too large");
throw new WorkspaceRegistryError("workspace_invalid", `Workspace ${label} object is too large`);
}
return stdout;
} catch (error) {
if (error instanceof WorkspaceRegistryError) throw error;
const detail = error instanceof Error ? error.message : "";
if (/maxBuffer|stdout maxBuffer/i.test(detail)) {
throw new WorkspaceRegistryError("workspace_invalid", "Workspace annotations object is too large");
throw new WorkspaceRegistryError("workspace_invalid", `Workspace ${label} object is too large`);
}
throw this.sanitizeGitError(error);
}