Skip to content

armgit / mso

Public Imported from GitHub
0

Browser-based graphical shell and control plane for a Linux server you own — real terminal, file manager, live metrics and BYOK AI in one mobile-first workspace. Next.js 16 · React 19 · node-pty · Tailwind v4 · shadcn/ui. Public alpha, self-hosted.

fix(export): create the markdown export atomically, closing a TOCTOU write

Abdurrahman Fakhruldb8506edd7a0
diff --git a/scripts/mso-agent-export-markdown.mjs b/scripts/mso-agent-export-markdown.mjs
index 0b33d4a7..740538b4 100644
--- a/scripts/mso-agent-export-markdown.mjs
+++ b/scripts/mso-agent-export-markdown.mjs
@@ -179,14 +179,29 @@ export function parseExportArgs(args = []) {
}
// Safely writes markdown export to disk, refusing to clobber existing files without force.
+//
+// The refusal used to be a check-then-write: existsSync said the file was absent, and a file
+// created between that check and the write was silently overwritten. CodeQL reports that as
+// js/file-system-race (high). The kernel can express "create, but only if absent" atomically, so
+// the flag does the deciding instead of a race-prone read.
export function writeExportFile(filePath, content, { force = false } = {}) {
const resolved = path.resolve(filePath);
- if (fs.existsSync(resolved) && !force) {
- throw new Error(`file already exists: ${filePath} (use --force to overwrite)`);
- }
const dir = path.dirname(resolved);
- if (!fs.existsSync(dir)) {
+ try {
fs.mkdirSync(dir, { recursive: true });
+ } catch (error) {
+ // EEXIST on an existing directory is the normal case.
+ if (error?.code !== "EEXIST") throw error;
+ }
+ if (!force) {
+ // wx fails with EEXIST if anything already occupies the path. No separate existence check.
+ try {
+ fs.writeFileSync(resolved, content, { encoding: "utf8", flag: "wx" });
+ } catch (error) {
+ if (error?.code === "EEXIST") throw new Error(`file already exists: ${filePath} (use --force to overwrite)`);
+ throw error;
+ }
+ return resolved;
}
fs.writeFileSync(resolved, content, "utf8");
return resolved;
diff --git a/scripts/mso-agent-export-markdown.test.ts b/scripts/mso-agent-export-markdown.test.ts
index 94b54e2f..e4fad11b 100644
--- a/scripts/mso-agent-export-markdown.test.ts
+++ b/scripts/mso-agent-export-markdown.test.ts
@@ -181,4 +181,38 @@ describe("MSO Agent session export markdown formatter", () => {
fs.rmSync(tmpDir, { recursive: true, force: true });
}
});
+
+ it("4b. refuses via the kernel flag, not a check-then-write race", () => {
+ // CodeQL js/file-system-race flags a write whose destination was checked first: a file created
+ // between existsSync and writeFileSync is silently overwritten. Asserting behaviour cannot show
+ // that, because both shapes refuse in the same cases. What distinguishes them is the mechanism,
+ // so this asserts the mechanism: the non-force write asks the kernel to create-or-fail.
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "mso-export-race-"));
+ const spy = vi.spyOn(fs, "writeFileSync");
+ try {
+ const outPath = path.join(tmpDir, "race.md");
+ writeExportFile(outPath, "first", { force: false });
+
+ const flags = spy.mock.calls
+ .filter(([target]) => target === outPath)
+ .map(([, , options]) => (options && typeof options === "object" ? options.flag : undefined));
+ expect(flags, "the create must be exclusive, or a concurrent file is clobbered").toContain("wx");
+
+ // And it still refuses, with the message users see.
+ expect(() => writeExportFile(outPath, "second", { force: false })).toThrow(/already exists/);
+ expect(fs.readFileSync(outPath, "utf8")).toBe("first");
+
+ // force:true is allowed to overwrite, and must not use the exclusive flag.
+ writeExportFile(outPath, "third", { force: true });
+ expect(fs.readFileSync(outPath, "utf8")).toBe("third");
+
+ // A nested destination builds its directory without racing on an existence check.
+ const nested = path.join(tmpDir, "a", "b", "c.md");
+ writeExportFile(nested, "deep", { force: false });
+ expect(fs.readFileSync(nested, "utf8")).toBe("deep");
+ } finally {
+ spy.mockRestore();
+ fs.rmSync(tmpDir, { recursive: true, force: true });
+ }
+ });
});