48 lines
2.8 KiB
TypeScript
48 lines
2.8 KiB
TypeScript
import { readFileSync } from "node:fs";
|
|
import { join } from "node:path";
|
|
import { S3Client, ListObjectsV2Command } from "@aws-sdk/client-s3";
|
|
import { parseWorkspaceYaml } from "../workspaces/schema.js";
|
|
import { evidencePolicy } from "../workspaces/evidence/preprocessing.js";
|
|
import type { parseDatabaseBootstrap } from "./bootstrap-documents.js";
|
|
import { probePublicEvidenceUrl, publicEvidenceAgent } from "./evidence-probe-http.js";
|
|
|
|
type Entry = ReturnType<typeof parseDatabaseBootstrap>["databases"][number];
|
|
|
|
/** Read-only availability probes; domain correctness and materialization remain runtime gates. */
|
|
export async function probeEvidence(entry: Entry, root: string): Promise<void> {
|
|
const evidence = parseWorkspaceYaml(readFileSync(join(root, entry.workspaceId, "workspace.yaml"), "utf8")).evidence;
|
|
if (!evidence || evidence.source.type === "filesystem") return;
|
|
if (evidencePolicy(evidence)) throw new Error("Evidence egress policy refused");
|
|
const secret = (name: keyof NonNullable<Entry["evidenceSecretFiles"]>) => {
|
|
const path = entry.evidenceSecretFiles?.[name];
|
|
if (!path) throw new Error("Evidence credential missing");
|
|
return readFileSync(path, "utf8").trim();
|
|
};
|
|
const source = evidence.source;
|
|
if (source.type === "http") {
|
|
const urls: unknown = source.authentication === "signed_urls_file"
|
|
? JSON.parse(secret("evidence.signed_urls")) : source.uris;
|
|
if (!Array.isArray(urls) || urls.length !== source.uris.length || urls.length > 1000) throw new Error("Invalid signed URLs");
|
|
for (const [index, value] of urls.entries()) {
|
|
if (typeof value !== "string") throw new Error("Invalid signed URL");
|
|
const url = new URL(value);
|
|
const provenance = new URL(source.uris[index]);
|
|
// Signed queries may authorize the same identity, never a different host/path.
|
|
if (url.origin !== provenance.origin || url.pathname !== provenance.pathname || url.username || url.password || url.hash) throw new Error("Invalid signed URL identity");
|
|
await probePublicEvidenceUrl(url);
|
|
}
|
|
return;
|
|
}
|
|
// The shared runtime policy currently permits trusted AWS endpoints with explicit file credentials.
|
|
const location = new URL(source.uri);
|
|
const client = new S3Client({
|
|
region: source.region ?? "us-east-1", maxAttempts: 1,
|
|
requestHandler: { httpsAgent: publicEvidenceAgent(), connectionTimeout: 5_000, requestTimeout: 5_000 },
|
|
credentials: { accessKeyId: secret("evidence.access_key"), secretAccessKey: secret("evidence.secret_key"),
|
|
...(entry.evidenceSecretFiles?.["evidence.session_token"] ? { sessionToken: secret("evidence.session_token") } : {}) },
|
|
});
|
|
try {
|
|
await client.send(new ListObjectsV2Command({ Bucket: location.hostname, Prefix: decodeURIComponent(location.pathname.slice(1)), MaxKeys: 1 }), { abortSignal: AbortSignal.timeout(5_000) });
|
|
} finally { client.destroy(); }
|
|
}
|