fix(auth): fail closed on unreadable stale locks

This commit is contained in:
Peter Steinberger
2026-05-14 07:26:26 +01:00
parent 3048ad4731
commit f84d031d38
3 changed files with 36 additions and 3 deletions
+1 -1
View File
@@ -62,7 +62,7 @@ export function shouldRemoveDeadOwnerOrExpiredLock(params: {
const createdAt = Date.parse(payload.createdAt);
return !Number.isFinite(createdAt) || (params.nowMs ?? Date.now()) - createdAt > params.staleMs;
}
return true;
return false;
}
export async function removeLockFileIfSnapshotMatches(params: {
+34 -1
View File
@@ -69,9 +69,10 @@ describe("acquireFileLock", () => {
stale: 10,
} as const;
const deadPid = Number.MAX_SAFE_INTEGER;
await fs.writeFile(
lockPath,
JSON.stringify({ pid: 999_999, createdAt: new Date(Date.now() - 60_000).toISOString() }),
JSON.stringify({ pid: deadPid, createdAt: new Date(Date.now() - 60_000).toISOString() }),
"utf8",
);
@@ -84,6 +85,38 @@ describe("acquireFileLock", () => {
}
});
it("keeps a reported stale lock when its payload is not readable", async () => {
const filePath = path.join(tempDir, "payload-pending");
const lockPath = `${filePath}.lock`;
const options = {
retries: {
retries: 0,
factor: 1,
minTimeout: 1,
maxTimeout: 1,
},
stale: 10,
} as const;
await fs.writeFile(lockPath, "{", "utf8");
let caught: { lockPath?: string } | undefined;
await expect(
(async () => {
try {
await acquireFileLock(filePath, options);
} catch (err) {
caught = err as { lockPath?: string };
throw err;
}
})(),
).rejects.toMatchObject({
code: FILE_LOCK_TIMEOUT_ERROR_CODE,
});
await expect(fs.realpath(caught?.lockPath ?? "")).resolves.toBe(await fs.realpath(lockPath));
await expect(fs.readFile(lockPath, "utf8")).resolves.toBe("{");
});
it("keeps a reported stale lock when its owner pid is alive", async () => {
const filePath = path.join(tempDir, "live-owner");
const lockPath = `${filePath}.lock`;
+1 -1
View File
@@ -56,7 +56,7 @@ async function shouldReclaimPluginLock(params: {
const createdAt = Date.parse(payload.createdAt);
return !Number.isFinite(createdAt) || params.nowMs - createdAt > params.staleMs;
}
return true;
return false;
}
function isFileLockError(error: unknown, code: string): boolean {