Skip to content

OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security

fix(multiscan): remove incomplete supervisor locks (#196)

mldangelo-oaia62f301b906c
diff --git a/sdk/typescript/src/multiscan.ts b/sdk/typescript/src/multiscan.ts
index 9704014c2..e90e78eac 100644
--- a/sdk/typescript/src/multiscan.ts
+++ b/sdk/typescript/src/multiscan.ts
@@ -8,6 +8,7 @@ import {
realpath,
rename,
rm,
+ rmdir,
truncate,
utimes,
writeFile,
@@ -341,16 +342,45 @@ async function acquireLock(output: string): Promise<() => Promise<void>> {
if (!existing.stale) {
throw new Error("A multiscan supervisor is already running.");
}
- await recoverLock(output, path, existing.owner);
- return await acquireLock(output);
+ const stale = await recoverLock(output, path, existing.owner);
+ try {
+ return await acquireLock(output);
+ } finally {
+ await rm(stale, { recursive: true, force: true });
+ }
}
+ const createdLock = await lstat(path);
const owner = `${JSON.stringify({
pid: process.pid,
ownerId: randomUUID(),
hostname: hostname(),
processStartedAt: performance.timeOrigin,
})}\n`;
- await writeFile(ownerPath, owner, { flag: "wx", mode: 0o600 });
+ try {
+ await writeFile(ownerPath, owner, { flag: "wx", mode: 0o600 });
+ } catch (error) {
+ const currentLock = await lstat(path).catch(
+ (cleanup: NodeJS.ErrnoException) => {
+ if (cleanup.code !== "ENOENT") throw cleanup;
+ return undefined;
+ },
+ );
+ if (
+ currentLock?.dev === createdLock.dev &&
+ currentLock.ino === createdLock.ino
+ ) {
+ await rmdir(path).catch((cleanup: NodeJS.ErrnoException) => {
+ if (
+ cleanup.code !== "ENOENT" &&
+ cleanup.code !== "ENOTEMPTY" &&
+ cleanup.code !== "EEXIST"
+ ) {
+ throw cleanup;
+ }
+ });
+ }
+ throw error;
+ }
let heartbeat = Promise.resolve();
const timer = setInterval(() => {
@@ -451,7 +481,7 @@ async function recoverLock(
output: string,
path: string,
expectedOwner: string | undefined,
-): Promise<void> {
+): Promise<string> {
const recoveryPath = join(path, ".recovering");
let claim;
try {
@@ -480,7 +510,7 @@ async function recoverLock(
const stale = join(output, `.lock.stale-${randomUUID()}`);
await rename(path, stale);
moved = true;
- await rm(stale, { recursive: true });
+ return stale;
} finally {
if (!moved) await rm(recoveryPath, { force: true });
}
diff --git a/sdk/typescript/tests-ts/multiscan.test.ts b/sdk/typescript/tests-ts/multiscan.test.ts
index 3ef41a0b2..e0594f13f 100644
--- a/sdk/typescript/tests-ts/multiscan.test.ts
+++ b/sdk/typescript/tests-ts/multiscan.test.ts
@@ -883,6 +883,11 @@ describe("multiscan", () => {
expect(summary).toMatchObject({ completed: 1, failed: 0 });
await expect(access(lock)).rejects.toThrow();
+ expect(
+ (await readdir(paths.output)).some((name) =>
+ name.startsWith(".lock.stale-"),
+ ),
+ ).toBe(false);
});
test("preserves an active legacy supervisor lock", async () => {
@@ -1170,21 +1175,15 @@ describe("multiscan", () => {
).toEqual(replacement);
});
- test("never removes a replacement owner's lock when owner creation fails", async () => {
+ test("removes an empty supervisor lock when owner creation fails", async () => {
const paths = await fixture();
- const source = await repository(paths.root, "owner-creation-race");
+ const source = await repository(paths.root, "owner-creation-failure");
await writeFile(
paths.input,
- `id,repository,revision\nrace,${source.path},${source.revision}\n`,
+ `id,repository,revision\nfailure,${source.path},${source.revision}\n`,
);
const lock = join(paths.output, ".lock");
const ownerPath = join(lock, "owner.json");
- const replacement = JSON.stringify({
- pid: process.pid,
- ownerId: "replacement-supervisor",
- hostname: hostname(),
- processStartedAt: performance.timeOrigin,
- });
const originalWriteFile = filesystem.writeFile;
const writeOwner = spyOn(filesystem, "writeFile").mockImplementation(
async (path, data, options) => {
@@ -1192,32 +1191,84 @@ describe("multiscan", () => {
return await originalWriteFile(path, data, options);
}
writeOwner.mockRestore();
- await rename(lock, join(paths.output, ".lock.stale-owner-creation"));
- await mkdir(lock, { mode: 0o700 });
- await originalWriteFile(ownerPath, replacement, { mode: 0o600 });
- throw Object.assign(new Error("replacement already owns the lock"), {
- code: "EEXIST",
+ throw Object.assign(new Error("could not publish lock owner"), {
+ code: "EACCES",
});
},
);
+ const security = client(async (_repository, scanOptions = {}) =>
+ completedScan(scanOptions.outputDir!),
+ );
try {
+ await expect(runMultiscan(options(paths, security))).rejects.toThrow(
+ "could not publish lock owner",
+ );
+ await expect(access(lock)).rejects.toThrow();
await expect(
- runMultiscan(
- options(
- paths,
- client(async (_repository, scanOptions = {}) =>
- completedScan(scanOptions.outputDir!),
- ),
- ),
- ),
- ).rejects.toThrow("replacement already owns the lock");
- expect(await readFile(ownerPath, "utf8")).toBe(replacement);
+ runMultiscan(options(paths, security)),
+ ).resolves.toMatchObject({ completed: 1 });
} finally {
writeOwner.mockRestore();
}
});
+ test.each([false, true])(
+ "never removes a replacement lock when owner creation fails (owner published: %s)",
+ async (ownerPublished) => {
+ const paths = await fixture();
+ const source = await repository(paths.root, "owner-creation-race");
+ await writeFile(
+ paths.input,
+ `id,repository,revision\nrace,${source.path},${source.revision}\n`,
+ );
+ const lock = join(paths.output, ".lock");
+ const ownerPath = join(lock, "owner.json");
+ const replacement = JSON.stringify({
+ pid: process.pid,
+ ownerId: "replacement-supervisor",
+ hostname: hostname(),
+ processStartedAt: performance.timeOrigin,
+ });
+ const originalWriteFile = filesystem.writeFile;
+ const writeOwner = spyOn(filesystem, "writeFile").mockImplementation(
+ async (path, data, options) => {
+ if (String(path) !== ownerPath) {
+ return await originalWriteFile(path, data, options);
+ }
+ writeOwner.mockRestore();
+ await rename(lock, join(paths.output, ".lock.stale-owner-creation"));
+ await mkdir(lock, { mode: 0o700 });
+ if (ownerPublished) {
+ await originalWriteFile(ownerPath, replacement, { mode: 0o600 });
+ }
+ throw Object.assign(new Error("replacement already owns the lock"), {
+ code: "EEXIST",
+ });
+ },
+ );
+
+ try {
+ await expect(
+ runMultiscan(
+ options(
+ paths,
+ client(async (_repository, scanOptions = {}) =>
+ completedScan(scanOptions.outputDir!),
+ ),
+ ),
+ ),
+ ).rejects.toThrow("replacement already owns the lock");
+ await access(lock);
+ if (ownerPublished) {
+ expect(await readFile(ownerPath, "utf8")).toBe(replacement);
+ }
+ } finally {
+ writeOwner.mockRestore();
+ }
+ },
+ );
+
test("retries a failed attempt and records both durable receipts", async () => {
const paths = await fixture();
const source = await repository(paths.root, "retry");