Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
29 changes: 28 additions & 1 deletion scripts/security/run-codex-scan-worker.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* @vitest-environment node */
import { createHash } from "node:crypto";
import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
import { chmod, mkdir, readFile, rm, stat, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { afterEach, describe, expect, it, vi } from "vitest";
import { createTestTempDirectories } from "../lib/testTempDirectories";
Expand All @@ -11,6 +11,7 @@ import {
normalizeSkillSpectorAnalysis,
publishWorkerHealthSummary,
processJob,
removeScanWorkspace,
runContinuouslyRefilledWorkerPool,
writeArtifactWorkspace,
writeJobDiagnostic,
Expand All @@ -32,6 +33,32 @@ function unsafeFixtureLabels() {
};
}

// Docker sandboxes leave root-owned files behind; EACCES used to escape
// processJob after the outcome was recorded and fail the whole shard.
it.skipIf(process.getuid?.() === 0)(
"logs and survives a workspace that cannot be removed",
async () => {
const workspace = await tempDirs.create();
const locked = join(workspace, "sandbox-output");
await mkdir(locked);
await writeFile(join(locked, "log.txt"), "root-owned in production");
await chmod(locked, 0o500);
const stdoutWrite = vi.spyOn(process.stdout, "write").mockImplementation(() => true);
try {
await expect(removeScanWorkspace(workspace, "securityScanJobs:locked")).resolves.toBe(false);
const logged = stdoutWrite.mock.calls.map((call) => String(call[0])).join("\n");
expect(logged).toContain("security_scan_workspace_cleanup_failed");
expect(logged).toContain("EACCES");
} finally {
stdoutWrite.mockRestore();
await chmod(locked, 0o700);
}

await expect(removeScanWorkspace(workspace, "securityScanJobs:unlocked")).resolves.toBe(true);
await expect(stat(workspace)).rejects.toMatchObject({ code: "ENOENT" });
},
);

describe("run-codex-scan-worker diagnostics", () => {
it("publishes the worker report to the Actions summary and diagnostics artifact", async () => {
const diagnosticsRoot = await tempDirs.create();
Expand Down
23 changes: 22 additions & 1 deletion scripts/security/run-codex-scan-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2818,11 +2818,32 @@ export async function processJob(
"preserving scan workspace because container removal was not confirmed",
);
} else {
await rm(workspace, { recursive: true, force: true });
await removeScanWorkspace(workspace, job.job._id);
}
}
}

// Docker sandboxes leave root-owned files in the bind-mounted workspace, so
// removal can fail with EACCES after the outcome is already recorded. A throw
// here would replace the job result and fail the whole shard; the hosted
// runner is discarded after the run, so log the leftover and continue.
export async function removeScanWorkspace(workspace: string, jobId: string) {
try {
await rm(workspace, { recursive: true, force: true });
return true;
} catch (error) {
logger.warn(
{
event: "security_scan_workspace_cleanup_failed",
jobId,
code: (error as NodeJS.ErrnoException | undefined)?.code ?? "unknown",
},
"security scan workspace cleanup failed; leaving it for runner teardown",
);
return false;
}
}

function isTransientClaimError(error: unknown) {
if (!(error instanceof Error)) return false;
// Convex hides internal failures behind this message. Authentication and
Expand Down
9 changes: 9 additions & 0 deletions specs/plugin-qualified-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,15 @@ cover multiple/late containers, ownership mismatches, daemon/removal failures,
unknown finality and contained overview cancellation. There is no real local
Docker-daemon claim; native image qualification remains the upstream evidence.

Once container removal is confirmed, the Codex scan worker deletes the
workspace on a best-effort basis. Docker sandboxes leave root-owned files in
the bind mount, so that delete can fail with EACCES after the job outcome is
already recorded. The worker logs `security_scan_workspace_cleanup_failed` and
keeps the recorded result. It does not fail the job or the shard, because the
hosted runner is discarded after the run. This applies only after removal is
confirmed. An unconfirmed removal still preserves the workspace and stops the
worker.

Repair validation: four cleanup/worker seam suites passed 213 tests; full unit
coverage with serialized file execution passed 8993 tests, 5 skipped. Full
TypeScript/build, lint and installed formatter passed. A prior parallel run
Expand Down
Loading