refactor(files): use caller paths for local evidence files
This commit is contained in:
@@ -77,7 +77,7 @@ describe("policy revocation approval", () => {
|
||||
const store = join(project, ".rea", "permissions.json");
|
||||
const grant = {
|
||||
grant_id: "project:revoke-me",
|
||||
capability: "evidence_read" as const,
|
||||
capability: "managed_runtime" as const,
|
||||
roots: [project],
|
||||
executables: [],
|
||||
environment_names: [],
|
||||
|
||||
@@ -45,18 +45,11 @@ describe("analysis snapshots: persistence", () => {
|
||||
entries: [entry],
|
||||
evidence_bundle: createEvidenceBundle([]),
|
||||
};
|
||||
const policy = {
|
||||
roots: [directory],
|
||||
maxBytes: 1024 * 1024,
|
||||
maxDepth: 64,
|
||||
maxStringLength: 1024,
|
||||
maxNodes: 10_000,
|
||||
};
|
||||
const written = await writeAnalysisSnapshot(snapshot, path, false, policy);
|
||||
const written = await writeAnalysisSnapshot(snapshot, path, false);
|
||||
expect(written.ok).toBe(true);
|
||||
expect((await readFile(path, "utf8")).endsWith("\n")).toBe(true);
|
||||
expect((await stat(path)).mode & 0o777).toBe(0o600);
|
||||
expect((await readAnalysisSnapshot(path, policy)).ok).toBe(true);
|
||||
expect((await readAnalysisSnapshot(path)).ok).toBe(true);
|
||||
|
||||
const altered: unknown = JSON.parse(await readFile(path, "utf8"));
|
||||
if (
|
||||
@@ -77,7 +70,7 @@ describe("analysis snapshots: persistence", () => {
|
||||
throw new TypeError("fixture entry is malformed");
|
||||
Reflect.set(first.parameters, "address", "0x2000");
|
||||
await writeFile(path, JSON.stringify(altered));
|
||||
const loaded = await readAnalysisSnapshot(path, policy);
|
||||
const loaded = await readAnalysisSnapshot(path);
|
||||
expect(loaded.ok).toBe(false);
|
||||
if (!loaded.ok) expect(loaded.error._tag).toBe("EvidenceIntegrityError");
|
||||
expect(() => parseAnalysisSnapshot(altered)).toThrow(/identifier/u);
|
||||
|
||||
@@ -15,7 +15,6 @@ import {
|
||||
import { parseBinaryTarget } from "../../../src/application/BinaryTargetResolver.js";
|
||||
import { createEvidence } from "../../../src/domain/evidence.js";
|
||||
import { createEvidenceBundle } from "../../../src/domain/evidenceBundle.js";
|
||||
import { permissionAuthorityForRoot } from "../../fixtures/permissionAuthority.js";
|
||||
import {
|
||||
REA_WORKFLOW_PROVIDER,
|
||||
workflowAnalysisProfile,
|
||||
@@ -27,7 +26,7 @@ afterEach(async () => {
|
||||
vi.unstubAllEnvs();
|
||||
});
|
||||
|
||||
describe("direct analysis snapshot filesystem permissions", () => {
|
||||
describe("direct analysis snapshot files", () => {
|
||||
it("forwards the CLI provider selector ahead of the environment preference", async () => {
|
||||
const directory = await createTestTempDirectory("rea-direct-provider-");
|
||||
const targetPath = join(directory, "fixture.hop");
|
||||
@@ -55,7 +54,7 @@ describe("direct analysis snapshot filesystem permissions", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("replays a cache hit with snapshot read authority only", async () => {
|
||||
it("replays a cache hit from the supplied path without root configuration", async () => {
|
||||
const directory = await createTestTempDirectory("rea-direct-snapshot-");
|
||||
const snapshotPath = join(directory, "analysis.json");
|
||||
const launcherPath = join(directory, "hopper-launcher");
|
||||
@@ -84,22 +83,9 @@ describe("direct analysis snapshot filesystem permissions", () => {
|
||||
entries: [],
|
||||
evidence_bundle: createEvidenceBundle([evidence]),
|
||||
};
|
||||
const policy = {
|
||||
roots: [directory],
|
||||
maxBytes: 1024 * 1024,
|
||||
maxDepth: 64,
|
||||
maxStringLength: 1024,
|
||||
maxNodes: 10_000,
|
||||
};
|
||||
expect(
|
||||
(await writeAnalysisSnapshot(snapshot, snapshotPath, false, policy)).ok,
|
||||
(await writeAnalysisSnapshot(snapshot, snapshotPath, false)).ok,
|
||||
).toBe(true);
|
||||
const authority = await permissionAuthorityForRoot(
|
||||
directory,
|
||||
["snapshot_read", "snapshot_write"],
|
||||
["snapshot_read"],
|
||||
);
|
||||
vi.stubEnv("REA_ANALYSIS_SNAPSHOT_ROOTS_JSON", JSON.stringify([directory]));
|
||||
vi.stubEnv("HOPPER_LAUNCHER_PATH", launcherPath);
|
||||
|
||||
await expect(
|
||||
@@ -109,7 +95,6 @@ describe("direct analysis snapshot filesystem permissions", () => {
|
||||
{},
|
||||
{
|
||||
snapshotPath,
|
||||
permissionAuthority: authority,
|
||||
},
|
||||
),
|
||||
).resolves.toEqual(evidence);
|
||||
|
||||
@@ -14,7 +14,6 @@ import { createEvidence } from "../../../src/domain/evidence.js";
|
||||
import {
|
||||
createEvidenceBundle,
|
||||
serializeEvidenceBundle,
|
||||
type EvidenceFilePolicy,
|
||||
} from "../../../src/domain/evidenceBundle.js";
|
||||
|
||||
const bundle = (result = true) =>
|
||||
@@ -26,46 +25,36 @@ const bundle = (result = true) =>
|
||||
),
|
||||
]);
|
||||
|
||||
const policy = (root: string): EvidenceFilePolicy => ({
|
||||
roots: [root],
|
||||
maxBytes: 1024 * 1024,
|
||||
maxDepth: 64,
|
||||
maxStringLength: 1024,
|
||||
maxNodes: 10_000,
|
||||
});
|
||||
|
||||
describe("evidence bundle publication", () => {
|
||||
it("allows only one simultaneous export without overwrite approval", async () => {
|
||||
const root = await createTestTempDirectory("rea-evidence-exclusive-");
|
||||
const path = join(root, "bundle.json");
|
||||
const candidates = [bundle(), bundle(false)];
|
||||
const results = await Promise.all(
|
||||
candidates.map((value) =>
|
||||
writeEvidenceBundle(value, path, false, policy(root)),
|
||||
),
|
||||
candidates.map((value) => writeEvidenceBundle(value, path, false)),
|
||||
);
|
||||
expect(results.filter(({ ok }) => ok)).toHaveLength(1);
|
||||
expect(results.filter(({ ok }) => !ok)).toMatchObject([
|
||||
{ ok: false, error: { _tag: "EvidenceFileError", reason: "exists" } },
|
||||
]);
|
||||
const winner = candidates[results.findIndex(({ ok }) => ok)];
|
||||
expect(await readEvidenceBundle(path, policy(root))).toEqual({
|
||||
expect(await readEvidenceBundle(path)).toEqual({
|
||||
ok: true,
|
||||
value: winner,
|
||||
});
|
||||
expect(await readdir(root)).toEqual(["bundle.json"]);
|
||||
});
|
||||
|
||||
it("round trips dot-prefixed child names beneath an approved root", async () => {
|
||||
it("round trips caller-selected paths without configured roots", async () => {
|
||||
const root = await createTestTempDirectory("rea-evidence-dot-child-");
|
||||
const directory = join(root, "..cache");
|
||||
await mkdir(directory);
|
||||
const path = join(directory, "bundle.json");
|
||||
const value = bundle();
|
||||
expect(
|
||||
await writeEvidenceBundle(value, path, false, policy(root)),
|
||||
).toMatchObject({ ok: true });
|
||||
expect(await readEvidenceBundle(path, policy(root))).toEqual({
|
||||
expect(await writeEvidenceBundle(value, path, false)).toMatchObject({
|
||||
ok: true,
|
||||
});
|
||||
expect(await readEvidenceBundle(path)).toEqual({
|
||||
ok: true,
|
||||
value,
|
||||
});
|
||||
@@ -77,17 +66,12 @@ describe("evidence bundle filesystem adapter", () => {
|
||||
const directory = await createTestTempDirectory("rea-evidence-");
|
||||
const path = join(directory, "bundle.json");
|
||||
const evidenceBundle = bundle();
|
||||
const first = await writeEvidenceBundle(
|
||||
evidenceBundle,
|
||||
path,
|
||||
false,
|
||||
policy(directory),
|
||||
);
|
||||
const first = await writeEvidenceBundle(evidenceBundle, path, false);
|
||||
expect(first).toMatchObject({ ok: true, value: { path } });
|
||||
expect(await readFile(path, "utf8")).toBe(
|
||||
serializeEvidenceBundle(evidenceBundle),
|
||||
);
|
||||
expect(await readEvidenceBundle(path, policy(directory))).toEqual({
|
||||
expect(await readEvidenceBundle(path)).toEqual({
|
||||
ok: true,
|
||||
value: evidenceBundle,
|
||||
});
|
||||
@@ -95,7 +79,6 @@ describe("evidence bundle filesystem adapter", () => {
|
||||
await compareEvidenceBundlesCommand({
|
||||
leftPath: path,
|
||||
rightPath: path,
|
||||
policy: policy(directory),
|
||||
}),
|
||||
).toMatchObject({
|
||||
ok: true,
|
||||
@@ -105,60 +88,39 @@ describe("evidence bundle filesystem adapter", () => {
|
||||
},
|
||||
});
|
||||
expect(
|
||||
await writeEvidenceBundle(evidenceBundle, path, false, policy(directory)),
|
||||
await writeEvidenceBundle(evidenceBundle, path, false),
|
||||
).toMatchObject({
|
||||
ok: false,
|
||||
error: { _tag: "EvidenceFileError", reason: "exists" },
|
||||
});
|
||||
expect(
|
||||
await writeEvidenceBundle(evidenceBundle, path, true, policy(directory)),
|
||||
).toMatchObject({ ok: true });
|
||||
expect(await writeEvidenceBundle(evidenceBundle, path, true)).toMatchObject(
|
||||
{ ok: true },
|
||||
);
|
||||
});
|
||||
|
||||
it("uses valid evidence roots when another configured root is missing", async () => {
|
||||
const directory = await createTestTempDirectory("rea-evidence-roots-");
|
||||
const path = join(directory, "bundle.json");
|
||||
const configured = {
|
||||
...policy(directory),
|
||||
roots: [join(directory, "missing"), directory],
|
||||
};
|
||||
expect(
|
||||
await writeEvidenceBundle(bundle(), path, false, configured),
|
||||
).toMatchObject({ ok: true });
|
||||
expect(await readEvidenceBundle(path, configured)).toMatchObject({
|
||||
ok: true,
|
||||
});
|
||||
});
|
||||
|
||||
it("rejects traversal and symlink escape from approved roots", async () => {
|
||||
it("reads the caller-selected symlink target and refuses to replace a symlink", async () => {
|
||||
const directory = await createTestTempDirectory("rea-evidence-");
|
||||
const root = join(directory, "approved");
|
||||
const outside = join(directory, "outside");
|
||||
await mkdir(root);
|
||||
await mkdir(outside);
|
||||
const outsidePath = join(outside, "bundle.json");
|
||||
await writeFile(outsidePath, serializeEvidenceBundle(bundle()));
|
||||
const link = join(root, "escaped.json");
|
||||
const link = join(directory, "escaped.json");
|
||||
await symlink(outsidePath, link);
|
||||
expect(await readEvidenceBundle(link, policy(root))).toMatchObject({
|
||||
ok: false,
|
||||
error: { _tag: "EvidenceFileError", reason: "outside-root" },
|
||||
expect(await readEvidenceBundle(link)).toMatchObject({
|
||||
ok: true,
|
||||
value: bundle(),
|
||||
});
|
||||
expect(
|
||||
await writeEvidenceBundle(bundle(), outsidePath, true, policy(root)),
|
||||
).toMatchObject({
|
||||
expect(await writeEvidenceBundle(bundle(), link, true)).toMatchObject({
|
||||
ok: false,
|
||||
error: { _tag: "EvidenceFileError", reason: "outside-root" },
|
||||
error: { _tag: "EvidenceFileError", reason: "not-file" },
|
||||
});
|
||||
});
|
||||
|
||||
it("rejects malformed, tampered, oversized, and deeply nested input", async () => {
|
||||
it("rejects malformed and tampered input", async () => {
|
||||
const directory = await createTestTempDirectory("rea-evidence-");
|
||||
const malformed = join(directory, "malformed.json");
|
||||
await writeFile(malformed, "{");
|
||||
expect(
|
||||
await readEvidenceBundle(malformed, policy(directory)),
|
||||
).toMatchObject({
|
||||
expect(await readEvidenceBundle(malformed)).toMatchObject({
|
||||
ok: false,
|
||||
error: { _tag: "EvidenceFileError", reason: "invalid-json" },
|
||||
});
|
||||
@@ -172,35 +134,9 @@ describe("evidence bundle filesystem adapter", () => {
|
||||
records: [{ ...tampered.records[0], normalized_result: "changed" }],
|
||||
}),
|
||||
);
|
||||
expect(
|
||||
await readEvidenceBundle(tamperedPath, policy(directory)),
|
||||
).toMatchObject({
|
||||
expect(await readEvidenceBundle(tamperedPath)).toMatchObject({
|
||||
ok: false,
|
||||
error: { _tag: "EvidenceIntegrityError" },
|
||||
});
|
||||
|
||||
const oversized = join(directory, "oversized.json");
|
||||
await writeFile(oversized, "x".repeat(100));
|
||||
expect(
|
||||
await readEvidenceBundle(oversized, {
|
||||
...policy(directory),
|
||||
maxBytes: 10,
|
||||
}),
|
||||
).toMatchObject({
|
||||
ok: false,
|
||||
error: { _tag: "EvidenceFileError", reason: "too-large" },
|
||||
});
|
||||
|
||||
const deep = join(directory, "deep.json");
|
||||
await writeFile(deep, JSON.stringify({ one: { two: { three: null } } }));
|
||||
expect(
|
||||
await readEvidenceBundle(deep, {
|
||||
...policy(directory),
|
||||
maxDepth: 1,
|
||||
}),
|
||||
).toMatchObject({
|
||||
ok: false,
|
||||
error: { _tag: "EvidenceFileError", reason: "too-large" },
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -69,7 +69,7 @@ describe("permission authority filesystem boundaries", () => {
|
||||
await symlink(join(outside, "secret"), join(root, "escape"));
|
||||
const ceiling = await canonicalizePermissionCeilings([
|
||||
{
|
||||
capability: "evidence_read",
|
||||
capability: "process_capture",
|
||||
roots: [join(sandbox, "root-alias")],
|
||||
executables: [],
|
||||
environment_names: [],
|
||||
@@ -90,7 +90,7 @@ describe("permission authority filesystem boundaries", () => {
|
||||
]);
|
||||
const inside = await canonicalizePermissionRequest(
|
||||
{
|
||||
capability: "evidence_read",
|
||||
capability: "process_capture",
|
||||
roots: [join(root, "inside")],
|
||||
executables: [],
|
||||
environment_names: [],
|
||||
@@ -102,7 +102,7 @@ describe("permission authority filesystem boundaries", () => {
|
||||
);
|
||||
const escaped = await canonicalizePermissionRequest(
|
||||
{
|
||||
capability: "evidence_read",
|
||||
capability: "process_capture",
|
||||
roots: [join(root, "escape")],
|
||||
executables: [],
|
||||
environment_names: [],
|
||||
@@ -128,7 +128,7 @@ describe("permission authority filesystem boundaries", () => {
|
||||
const sandbox = await createTestTempDirectory("rea-permission-write-");
|
||||
const result = await canonicalizePermissionRequest(
|
||||
{
|
||||
capability: "evidence_write",
|
||||
capability: "process_capture",
|
||||
roots: [join(sandbox, "new.json")],
|
||||
executables: [],
|
||||
environment_names: [],
|
||||
@@ -319,7 +319,7 @@ describe("permission authority policy reload", () => {
|
||||
});
|
||||
|
||||
const evidenceScope = (root: string): PermissionScope => ({
|
||||
capability: "evidence_read",
|
||||
capability: "process_capture",
|
||||
roots: [root],
|
||||
executables: [],
|
||||
environment_names: [],
|
||||
@@ -329,7 +329,7 @@ const evidenceScope = (root: string): PermissionScope => ({
|
||||
|
||||
const administratorGrant = (scope: PermissionScope): PermissionGrant => ({
|
||||
...scope,
|
||||
grant_id: "administrator:evidence_read",
|
||||
grant_id: "administrator:process_capture",
|
||||
lifetime: "administrator",
|
||||
operation_identity: null,
|
||||
expires_at: null,
|
||||
|
||||
@@ -17,7 +17,7 @@ describe("project permission store", () => {
|
||||
const path = join(project, ".rea", "permissions.json");
|
||||
const grant = {
|
||||
grant_id: "project:evidence-read",
|
||||
capability: "evidence_read" as const,
|
||||
capability: "process_capture" as const,
|
||||
roots: [project],
|
||||
executables: [],
|
||||
environment_names: [],
|
||||
@@ -122,7 +122,7 @@ describe("project permission store", () => {
|
||||
const path = join(project, ".rea", "permissions.json");
|
||||
const grant = (grantId: string) => ({
|
||||
grant_id: grantId,
|
||||
capability: "evidence_read" as const,
|
||||
capability: "process_capture" as const,
|
||||
roots: [project],
|
||||
executables: [],
|
||||
environment_names: [],
|
||||
|
||||
@@ -20,7 +20,7 @@ describe("permission store write ownership", () => {
|
||||
const result = await writeProjectPermissionStore(path, project, [
|
||||
{
|
||||
grant_id: "project:new",
|
||||
capability: "evidence_read",
|
||||
capability: "process_capture",
|
||||
roots: [project],
|
||||
executables: [],
|
||||
environment_names: [],
|
||||
|
||||
@@ -18,13 +18,6 @@ import {
|
||||
} from "../../../src/application/ReferenceSourceImportTypes.js";
|
||||
import { createHistoricalSourceManifest } from "../../../src/domain/referenceSourceGraph.js";
|
||||
|
||||
const limits = {
|
||||
maxBytes: 1024 * 1024,
|
||||
maxEntries: 1_000,
|
||||
maxDepth: 16,
|
||||
maxPathBytes: 4_096,
|
||||
};
|
||||
|
||||
const fixture = async (parent: string, name: string): Promise<string> => {
|
||||
const root = join(parent, name);
|
||||
await mkdir(join(root, "src"), { recursive: true });
|
||||
@@ -38,20 +31,13 @@ const fixture = async (parent: string, name: string): Promise<string> => {
|
||||
return root;
|
||||
};
|
||||
|
||||
const importTree = (
|
||||
root: string,
|
||||
approvedRoot: string | readonly string[],
|
||||
signal?: AbortSignal,
|
||||
) =>
|
||||
const importTree = (root: string, signal?: AbortSignal) =>
|
||||
importReferenceSource({
|
||||
root,
|
||||
caller: "reference-import-test",
|
||||
policy: {
|
||||
roots: typeof approvedRoot === "string" ? [approvedRoot] : approvedRoot,
|
||||
secretPatterns: [".env", ".env.*"],
|
||||
...limits,
|
||||
},
|
||||
limits,
|
||||
...(signal === undefined ? {} : { signal }),
|
||||
});
|
||||
|
||||
@@ -83,7 +69,6 @@ describe("reference source import error projection", () => {
|
||||
["symlink", "io"],
|
||||
["file", "io"],
|
||||
["file", "cancelled"],
|
||||
["file", "limit"],
|
||||
["unknown", "unsupported"],
|
||||
] as const) {
|
||||
const message = projectReferenceSourceEntryFailure({
|
||||
@@ -102,11 +87,9 @@ describe("reference source import error projection", () => {
|
||||
it("projects every import failure without raw parser or policy text", () => {
|
||||
const expectedCategories = {
|
||||
cancelled: "cancelled",
|
||||
"invalid-limits": "invalid_input",
|
||||
"invalid-root": "invalid_input",
|
||||
io: "execution_failure",
|
||||
parse: "execution_failure",
|
||||
policy: "permission_required",
|
||||
} as const;
|
||||
for (const [code, category] of Object.entries(expectedCategories)) {
|
||||
const projected = projectReferenceSourceImportError({
|
||||
@@ -117,14 +100,54 @@ describe("reference source import error projection", () => {
|
||||
expect(projected.category).toBe(category);
|
||||
expect(projected.message).not.toContain("SECRET");
|
||||
expect(projected.message).not.toContain("/private/path");
|
||||
expect(projected.message).toMatch(
|
||||
/try again|when ready|positive integer|Check that|REA_REFERENCE_ROOTS_JSON/u,
|
||||
);
|
||||
expect(projected.message).toMatch(/try again|when ready|Check that/u);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("reference source import behavior", () => {
|
||||
it("imports a source file larger than the former 16 MiB ceiling", async () => {
|
||||
const root = await createTestTempDirectory("rea-reference-large-");
|
||||
const size = 16 * 1024 * 1024 + 1;
|
||||
await writeFile(join(root, "large.bin"), Buffer.alloc(size, 0x61));
|
||||
|
||||
const result = await importReferenceSource({
|
||||
root,
|
||||
caller: "reference-import-test",
|
||||
policy: { secretPatterns: [] },
|
||||
});
|
||||
|
||||
expect(result.ok).toBe(true);
|
||||
if (!result.ok) return;
|
||||
expect(result.value.entries).toContainEqual(
|
||||
expect.objectContaining({
|
||||
path: "large.bin",
|
||||
kind: "file",
|
||||
size,
|
||||
content_state: "hashed",
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("imports more than ten thousand source entries", async () => {
|
||||
const root = await createTestTempDirectory("rea-reference-many-");
|
||||
await Promise.all(
|
||||
Array.from({ length: 10_001 }, (_, index) =>
|
||||
writeFile(join(root, `entry-${String(index).padStart(5, "0")}`), ""),
|
||||
),
|
||||
);
|
||||
|
||||
const result = await importReferenceSource({
|
||||
root,
|
||||
caller: "reference-import-test",
|
||||
policy: { secretPatterns: [] },
|
||||
});
|
||||
|
||||
expect(result.ok).toBe(true);
|
||||
if (!result.ok) return;
|
||||
expect(result.value.entries).toHaveLength(10_001);
|
||||
});
|
||||
|
||||
it("imports BMP and supplementary filenames in Unicode code point order", async () => {
|
||||
const root = await createTestTempDirectory("reference-unicode-");
|
||||
try {
|
||||
@@ -134,11 +157,11 @@ describe("reference source import behavior", () => {
|
||||
writeFile(join(root, path), "export const value = 1;\n"),
|
||||
),
|
||||
);
|
||||
const result = await importTree(root, root);
|
||||
const result = await importTree(root);
|
||||
expect(result.ok).toBe(true);
|
||||
if (!result.ok) return;
|
||||
expect(result.value.entries.map(({ path }) => path)).toEqual(paths);
|
||||
const repeated = await importTree(root, root);
|
||||
const repeated = await importTree(root);
|
||||
expect(repeated).toEqual(result);
|
||||
} finally {
|
||||
await rm(root, { recursive: true, force: true });
|
||||
@@ -151,8 +174,8 @@ describe("reference source import behavior", () => {
|
||||
const leftRoot = await fixture(parent, "left");
|
||||
const rightRoot = await fixture(parent, "right");
|
||||
const [left, right] = await Promise.all([
|
||||
importTree(leftRoot, parent),
|
||||
importTree(rightRoot, parent),
|
||||
importTree(leftRoot),
|
||||
importTree(rightRoot),
|
||||
]);
|
||||
expect(left.ok).toBe(true);
|
||||
expect(right.ok).toBe(true);
|
||||
@@ -179,42 +202,20 @@ describe("reference source import behavior", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("fails closed outside policy and honors cancellation", async () => {
|
||||
const parent = await createTestTempDirectory("rea-reference-policy-");
|
||||
it("imports the caller-selected directory and honors cancellation", async () => {
|
||||
const outside = await createTestTempDirectory("rea-reference-outside-");
|
||||
try {
|
||||
const root = await fixture(outside, "tree");
|
||||
const denied = await importTree(root, parent);
|
||||
expect(denied).toMatchObject({
|
||||
ok: false,
|
||||
error: { code: "policy" },
|
||||
});
|
||||
expect(await importTree(root)).toMatchObject({ ok: true });
|
||||
const controller = new AbortController();
|
||||
controller.abort();
|
||||
const cancelled = await importTree(root, outside, controller.signal);
|
||||
const cancelled = await importTree(root, controller.signal);
|
||||
expect(cancelled).toMatchObject({
|
||||
ok: false,
|
||||
error: { code: "cancelled" },
|
||||
});
|
||||
} finally {
|
||||
await Promise.all([
|
||||
rm(parent, { recursive: true, force: true }),
|
||||
rm(outside, { recursive: true, force: true }),
|
||||
]);
|
||||
}
|
||||
});
|
||||
|
||||
it("uses a valid reference root when another configured root is missing", async () => {
|
||||
const parent = await createTestTempDirectory("rea-reference-roots-");
|
||||
try {
|
||||
const root = await fixture(parent, "tree");
|
||||
const imported = await importTree(root, [
|
||||
join(parent, "missing"),
|
||||
parent,
|
||||
]);
|
||||
expect(imported).toMatchObject({ ok: true });
|
||||
} finally {
|
||||
await rm(parent, { recursive: true, force: true });
|
||||
await rm(outside, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
@@ -231,7 +232,7 @@ describe("reference source import behavior", () => {
|
||||
author: { name: "REA Test", email: "rea@example.invalid" },
|
||||
message: "fixture",
|
||||
});
|
||||
const result = await importTree(root, parent);
|
||||
const result = await importTree(root);
|
||||
expect(result.ok).toBe(true);
|
||||
if (!result.ok) throw result.error;
|
||||
expect(result.value.vcs).toEqual({ kind: "git", head: oid, dirty: null });
|
||||
|
||||
@@ -7,13 +7,6 @@ import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
|
||||
import { readReferenceSource } from "../../../src/reference/ReferenceSourceReader.js";
|
||||
|
||||
const limits = {
|
||||
maxBytes: 1_024,
|
||||
maxEntries: 10,
|
||||
maxDepth: 4,
|
||||
maxPathBytes: 100,
|
||||
} as const;
|
||||
|
||||
describe("readReferenceSource entries", () => {
|
||||
it("returns explicit entries in canonical code-point path order", async () => {
|
||||
const root = await createTestTempDirectory("rea-reference-");
|
||||
@@ -24,7 +17,7 @@ describe("readReferenceSource entries", () => {
|
||||
await writeFile(join(root, "\u{e000}"), "p");
|
||||
await writeFile(join(root, "\u{10000}"), "a");
|
||||
|
||||
const result = await readReferenceSource(root, limits);
|
||||
const result = await readReferenceSource(root);
|
||||
|
||||
expect(result.ok).toBe(true);
|
||||
if (!result.ok) return;
|
||||
@@ -37,7 +30,6 @@ describe("readReferenceSource entries", () => {
|
||||
"𐀀",
|
||||
]);
|
||||
expect(result.value.bytesRead).toBe(5);
|
||||
expect(result.value.truncated).toBe(false);
|
||||
expect(
|
||||
result.value.entries.map((entry) =>
|
||||
entry.status === "read" && entry.kind === "file"
|
||||
@@ -59,7 +51,7 @@ describe("readReferenceSource entries", () => {
|
||||
await symlink(join(outside, "secret"), join(root, "external"));
|
||||
await symlink("absent", join(root, "missing"));
|
||||
|
||||
const result = await readReferenceSource(root, limits);
|
||||
const result = await readReferenceSource(root);
|
||||
|
||||
expect(result.ok).toBe(true);
|
||||
if (!result.ok) return;
|
||||
@@ -97,56 +89,42 @@ describe("readReferenceSource entries", () => {
|
||||
expect(JSON.stringify(result.value.entries)).not.toContain(outside);
|
||||
});
|
||||
|
||||
it("makes byte, file, depth, and path limits explicit per entry", async () => {
|
||||
const root = await createTestTempDirectory("rea-reference-");
|
||||
await writeFile(join(root, "a"), "too large");
|
||||
await writeFile(join(root, "b"), "b");
|
||||
await mkdir(join(root, "deep"));
|
||||
await writeFile(join(root, "deep", "c"), "c");
|
||||
it("reads a source file larger than the former 16 MiB ceiling", async () => {
|
||||
const root = await createTestTempDirectory("rea-reference-large-");
|
||||
const size = 16 * 1024 * 1024 + 1;
|
||||
await writeFile(join(root, "large.bin"), Buffer.alloc(size, 0x61));
|
||||
|
||||
const result = await readReferenceSource(root, {
|
||||
maxBytes: 1,
|
||||
maxEntries: 1,
|
||||
maxDepth: 1,
|
||||
maxPathBytes: 4,
|
||||
});
|
||||
const result = await readReferenceSource(root);
|
||||
|
||||
expect(result.ok).toBe(true);
|
||||
if (!result.ok) return;
|
||||
expect(result.value.truncated).toBe(true);
|
||||
expect(result.value.entries).toEqual([
|
||||
expect(result.value.bytesRead).toBe(size);
|
||||
expect(result.value.entries).toContainEqual(
|
||||
expect.objectContaining({
|
||||
path: "a",
|
||||
status: "read",
|
||||
kind: "file",
|
||||
status: "failed",
|
||||
code: "limit",
|
||||
size: 9,
|
||||
path: "large.bin",
|
||||
size,
|
||||
}),
|
||||
]);
|
||||
expect(result.value.limitations).toContain(
|
||||
"Traversal stopped because the entry limit was reached.",
|
||||
);
|
||||
expect(result.value.entries).not.toContainEqual(
|
||||
expect.objectContaining({ path: "<tree>" }),
|
||||
);
|
||||
});
|
||||
|
||||
it("applies maxEntries as a global limit across every entry kind", async () => {
|
||||
const root = await createTestTempDirectory("rea-reference-");
|
||||
await writeFile(join(root, "a"), "a");
|
||||
await writeFile(join(root, "b"), "b");
|
||||
await writeFile(join(root, "c"), "c");
|
||||
it("traverses source directories deeper than the former depth ceiling", async () => {
|
||||
const root = await createTestTempDirectory("rea-reference-deep-");
|
||||
const segments = Array.from({ length: 40 }, (_, index) => `d${index}`);
|
||||
const directory = join(root, ...segments);
|
||||
await mkdir(directory, { recursive: true });
|
||||
await writeFile(join(directory, "deep.txt"), "deep");
|
||||
|
||||
const result = await readReferenceSource(root, {
|
||||
...limits,
|
||||
maxEntries: 1,
|
||||
});
|
||||
const result = await readReferenceSource(root);
|
||||
|
||||
expect(result.ok).toBe(true);
|
||||
if (!result.ok) return;
|
||||
expect(result.value.entries.map(({ path }) => path)).toEqual(["a"]);
|
||||
expect(result.value.bytesRead).toBe(1);
|
||||
expect(result.value.truncated).toBe(true);
|
||||
const path = [...segments, "deep.txt"].join("/");
|
||||
expect(result.value.entries).toContainEqual(
|
||||
expect.objectContaining({ status: "read", kind: "file", path }),
|
||||
);
|
||||
expect(result.value.entries).toHaveLength(41);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -158,7 +136,7 @@ describe("readReferenceSource failures and exclusions", () => {
|
||||
await writeFile(join(root, "kept"), "kept");
|
||||
const checked: string[] = [];
|
||||
|
||||
const result = await readReferenceSource(root, limits, {
|
||||
const result = await readReferenceSource(root, {
|
||||
shouldExclude: (path) => {
|
||||
checked.push(path);
|
||||
return path === "ignored";
|
||||
@@ -174,7 +152,7 @@ describe("readReferenceSource failures and exclusions", () => {
|
||||
it("sanitizes exclusion callback failures", async () => {
|
||||
const root = await createTestTempDirectory("rea-reference-");
|
||||
await writeFile(join(root, "file"), "value");
|
||||
const result = await readReferenceSource(root, limits, {
|
||||
const result = await readReferenceSource(root, {
|
||||
shouldExclude: () => {
|
||||
throw new Error("private callback detail");
|
||||
},
|
||||
@@ -192,7 +170,7 @@ describe("readReferenceSource failures and exclusions", () => {
|
||||
|
||||
it("sanitizes filesystem errors", async () => {
|
||||
const missing = join(tmpdir(), "rea-secret-root-that-does-not-exist");
|
||||
const result = await readReferenceSource(missing, limits);
|
||||
const result = await readReferenceSource(missing);
|
||||
|
||||
expect(result.ok).toBe(false);
|
||||
if (result.ok) return;
|
||||
@@ -205,7 +183,7 @@ describe("readReferenceSource failures and exclusions", () => {
|
||||
it("returns typed failures for cancellation and invalid roots", async () => {
|
||||
const controller = new AbortController();
|
||||
controller.abort();
|
||||
const cancelled = await readReferenceSource("/unused", limits, {
|
||||
const cancelled = await readReferenceSource("/unused", {
|
||||
signal: controller.signal,
|
||||
});
|
||||
expect(cancelled).toEqual({
|
||||
@@ -217,10 +195,7 @@ describe("readReferenceSource failures and exclusions", () => {
|
||||
},
|
||||
});
|
||||
|
||||
const invalid = await readReferenceSource(
|
||||
"/path/that/does/not/exist",
|
||||
limits,
|
||||
);
|
||||
const invalid = await readReferenceSource("/path/that/does/not/exist");
|
||||
expect(invalid.ok).toBe(false);
|
||||
if (invalid.ok) return;
|
||||
expect(invalid.error.code).toBe("invalid-root");
|
||||
|
||||
@@ -6,7 +6,6 @@ import { afterEach, describe, expect, it } from "vitest";
|
||||
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
|
||||
import type { PermissionAuthority } from "../../../src/application/PermissionAuthority.js";
|
||||
import type { ProjectPermissionStore } from "../../../src/application/ProjectPermissionStore.js";
|
||||
import type { PermissionGrant } from "../../../src/domain/permissionPolicy.js";
|
||||
import { ok } from "../../../src/domain/result.js";
|
||||
@@ -54,30 +53,20 @@ describe("runtime permission reload", () => {
|
||||
await validReadStarted.promise;
|
||||
|
||||
expect(runtime.options.processPolicy?.().status).toBe("disabled");
|
||||
expect(runtime.options.evidenceFilePolicy?.roots).toEqual([
|
||||
fixture.oldRoot,
|
||||
]);
|
||||
expect(runtime.options.artifactIntegrityContinueEnabled?.()).toBe(false);
|
||||
expectOptionalPolicies(runtime.options, { status: "disabled" });
|
||||
expect(await canRead(runtime.authority, fixture.oldRoot)).toBe(true);
|
||||
expect(await canRead(runtime.authority, fixture.newRoot)).toBe(false);
|
||||
|
||||
releaseValidRead.resolve();
|
||||
await expect
|
||||
.poll(() => canRead(runtime.authority, fixture.newRoot))
|
||||
.toBe(true);
|
||||
.poll(() => runtime.options.processPolicy?.().status)
|
||||
.toBe("enabled");
|
||||
|
||||
expect(runtime.options.processPolicy?.().status).toBe("enabled");
|
||||
expect(runtime.options.evidenceFilePolicy?.roots).toEqual([
|
||||
fixture.newRoot,
|
||||
]);
|
||||
expect(runtime.options.artifactIntegrityContinueEnabled?.()).toBe(true);
|
||||
expectOptionalPolicies(runtime.options, {
|
||||
status: "enabled",
|
||||
root: fixture.newRoot,
|
||||
});
|
||||
expect(await canRead(runtime.authority, fixture.oldRoot)).toBe(false);
|
||||
expect(await canRead(runtime.authority, fixture.newRoot)).toBe(true);
|
||||
});
|
||||
|
||||
it("serializes overlapping reloads so an older read cannot commit last", async () => {
|
||||
@@ -109,17 +98,12 @@ describe("runtime permission reload", () => {
|
||||
releaseFirstRead.resolve();
|
||||
await secondReadStarted.promise;
|
||||
await expect
|
||||
.poll(() => canRead(runtime.authority, fixture.latestRoot))
|
||||
.toBe(true);
|
||||
.poll(() => runtime.options.processPolicy?.().status)
|
||||
.toBe("disabled");
|
||||
|
||||
expect(runtime.options.processPolicy?.().status).toBe("disabled");
|
||||
expect(runtime.options.evidenceFilePolicy?.roots).toEqual([
|
||||
fixture.latestRoot,
|
||||
]);
|
||||
expect(runtime.options.artifactIntegrityContinueEnabled?.()).toBe(false);
|
||||
expectOptionalPolicies(runtime.options, { status: "disabled" });
|
||||
expect(await canRead(runtime.authority, fixture.newRoot)).toBe(false);
|
||||
expect(await canRead(runtime.authority, fixture.latestRoot)).toBe(true);
|
||||
});
|
||||
|
||||
it("continues with later reloads after an unexpected reload rejection", async () => {
|
||||
@@ -135,22 +119,14 @@ describe("runtime permission reload", () => {
|
||||
runtime.reload();
|
||||
runtime.reload();
|
||||
await expect.poll(() => reads).toBe(2);
|
||||
await expect
|
||||
.poll(() => canRead(runtime.authority, fixture.newRoot))
|
||||
.toBe(true);
|
||||
|
||||
expect(reads).toBe(2);
|
||||
expect(runtime.options.processPolicy?.().status).toBe("enabled");
|
||||
expect(runtime.options.evidenceFilePolicy?.roots).toEqual([
|
||||
fixture.newRoot,
|
||||
]);
|
||||
expect(runtime.options.artifactIntegrityContinueEnabled?.()).toBe(true);
|
||||
expectOptionalPolicies(runtime.options, {
|
||||
status: "enabled",
|
||||
root: fixture.newRoot,
|
||||
});
|
||||
expect(await canRead(runtime.authority, fixture.oldRoot)).toBe(false);
|
||||
expect(await canRead(runtime.authority, fixture.newRoot)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -199,7 +175,6 @@ const reloadFixture = async () => {
|
||||
),
|
||||
);
|
||||
const env: NodeJS.ProcessEnv = {
|
||||
REA_EVIDENCE_ROOTS_JSON: JSON.stringify([oldRoot]),
|
||||
REA_PERMISSION_PROJECT_ROOT: projectRoot,
|
||||
REA_PERMISSION_PROJECT_STORE: join(directory, "permissions.json"),
|
||||
};
|
||||
@@ -211,7 +186,6 @@ const configure = (
|
||||
root: string,
|
||||
enabled: boolean,
|
||||
): void => {
|
||||
env.REA_EVIDENCE_ROOTS_JSON = JSON.stringify([root]);
|
||||
env.REA_PROCESS_CAPTURE_ENABLED = String(enabled);
|
||||
env.REA_PROCESS_EXECUTABLE_ROOTS_JSON = JSON.stringify([root]);
|
||||
env.REA_PROCESS_WORKING_ROOTS_JSON = JSON.stringify([root]);
|
||||
@@ -271,14 +245,12 @@ const startRuntime = async (
|
||||
},
|
||||
});
|
||||
expect(result).toBe(0);
|
||||
const authority = options?.permissionAuthority;
|
||||
if (reload === undefined || options === undefined || authority === undefined)
|
||||
if (reload === undefined || options === undefined)
|
||||
throw new Error("Runtime reload seam was not initialized");
|
||||
const capturedOptions = options;
|
||||
return {
|
||||
reload,
|
||||
options: capturedOptions,
|
||||
authority,
|
||||
};
|
||||
};
|
||||
|
||||
@@ -294,7 +266,7 @@ const projectStore = (
|
||||
const projectGrant = (id: string, root: string) =>
|
||||
({
|
||||
grant_id: `project:${id}`,
|
||||
capability: "evidence_read",
|
||||
capability: "process_capture",
|
||||
roots: [root],
|
||||
executables: [],
|
||||
environment_names: [],
|
||||
@@ -305,25 +277,6 @@ const projectGrant = (id: string, root: string) =>
|
||||
expires_at: null,
|
||||
}) satisfies PermissionGrant;
|
||||
|
||||
const canRead = async (
|
||||
authority: PermissionAuthority,
|
||||
root: string,
|
||||
): Promise<boolean> =>
|
||||
(
|
||||
await authority.explain(
|
||||
{
|
||||
capability: "evidence_read",
|
||||
roots: [root],
|
||||
executables: [],
|
||||
environment_names: [],
|
||||
network: "none",
|
||||
mount: false,
|
||||
operation_identity: `read:${root}`,
|
||||
},
|
||||
"read",
|
||||
)
|
||||
).ok;
|
||||
|
||||
const deferred = <T>() => {
|
||||
let resolvePromise!: (value: T | PromiseLike<T>) => void;
|
||||
const promise = new Promise<T>((resolve) => {
|
||||
|
||||
Reference in New Issue
Block a user