|
1 | 1 | import { removeDesktop3pStandardPivot, writeDesktop3pConfig } from "../../src/claude/desktop-3p"; |
2 | 2 | import { afterEach, beforeEach, describe, expect, spyOn, test } from "bun:test"; |
3 | | -import { existsSync, mkdirSync, mkdtempSync, realpathSync, readFileSync, readdirSync, statSync, writeFileSync } from "node:fs"; |
| 3 | +import { spawnSync } from "node:child_process"; |
| 4 | +import { existsSync, lstatSync, mkdirSync, mkdtempSync, realpathSync, readFileSync, readdirSync, statSync, writeFileSync } from "node:fs"; |
4 | 5 | import { join } from "node:path"; |
5 | 6 | import { tmpdir } from "node:os"; |
6 | 7 | import * as configIO from "../../src/config"; |
| 8 | +import { repoPath } from "../helpers/repo-root"; |
7 | 9 | import { saveConfig, loadConfig, readConfigDiagnostics, atomicWriteFile } from "../../src/config"; |
8 | 10 | import { withClientLifecycleSync, type ClientLifecycleHeld } from "../../src/client/lifecycle-lock"; |
9 | 11 | import { serviceApiTokenFingerprint, writeServiceApiTokenFile, replaceServiceApiTokenFile, writeTokenBackup, serviceApiTokenBackupPath, removeServiceApiTokenFileIfOwned } from "../../src/lib/service-secrets"; |
@@ -359,4 +361,37 @@ describe("connection-owned Desktop projection store", () => { |
359 | 361 | expect(readDesktopDisconnectReceipt()).toMatchObject({ kind: "valid", value: { phase: "complete" } }); |
360 | 362 | expect(inspectRemoteDesktopCleanup().kind).toBe("absent"); |
361 | 363 | }); |
| 364 | + |
| 365 | + test.skipIf(process.platform === "win32")("a regular-to-FIFO swap during a store read is refused and classified unsafe", () => { |
| 366 | + const target = join(library, "original.json"); |
| 367 | + const fifo = join(dir, "store-read-replacement"); |
| 368 | + writeFileSync(target, "{}\n"); |
| 369 | + expect(spawnSync("mkfifo", [fifo], { timeout: 5000 }).status).toBe(0); |
| 370 | + // Bound the parent even if a regression reintroduces a blocking open: the child swaps the |
| 371 | + // regular profile for the FIFO at the shared reader's open, then performs the store read. |
| 372 | + // "unsafe" is the store's classification of the reader refusal; "conflict" would mean only |
| 373 | + // the post-read identity recheck fired, and a hang is killed by the child timeout. |
| 374 | + const code = `import { spyOn } from "bun:test"; |
| 375 | + import * as fs from "node:fs"; |
| 376 | + import { StoreIO } from ${JSON.stringify(repoPath("src/claude/desktop-remote-store-io.ts"))}; |
| 377 | + import { DesktopStoreError } from ${JSON.stringify(repoPath("src/claude/desktop-remote-store-state.ts"))}; |
| 378 | + const open = fs.openSync; |
| 379 | + spyOn(fs, "openSync").mockImplementation((path, flags, mode) => { |
| 380 | + if (path === ${JSON.stringify(target)}) fs.renameSync(${JSON.stringify(fifo)}, path); |
| 381 | + return open(path, flags, mode); |
| 382 | + }); |
| 383 | + try { |
| 384 | + new StoreIO().read(${JSON.stringify(target)}); |
| 385 | + console.log("read_not_refused"); |
| 386 | + } catch (error) { |
| 387 | + console.log(error instanceof DesktopStoreError ? error.reason : "unexpected_error"); |
| 388 | + }`; |
| 389 | + const child = spawnSync(process.execPath, ["--no-env-file", "-e", code], { |
| 390 | + env: { ...process.env }, encoding: "utf8", timeout: 10000, maxBuffer: 1024 * 1024, |
| 391 | + }); |
| 392 | + expect(child.error).toBeUndefined(); |
| 393 | + expect(child.status).toBe(0); |
| 394 | + expect(child.stdout.trim()).toBe("unsafe"); |
| 395 | + expect(lstatSync(target).isFIFO()).toBe(true); |
| 396 | + }, 20000); |
362 | 397 | }); |
0 commit comments