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: prevent direct remote markdown file export

Abdurrahman Fakhrul602b2e2164f4
diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md
index 03a1ae4c..d749a354 100644
--- a/docs/CHANGELOG.md
+++ b/docs/CHANGELOG.md
@@ -8,6 +8,7 @@ this is the *what*, and it is what Settings → Account → About shows as “Wh
**Fixed**
+- prevent direct remote markdown file export
- `settings` retain About subpage across shell changes
- `ui` preserve external editor height on short screens
- `n8n` keep frame controls stable during refresh
diff --git a/docs/PROGRESS.md b/docs/PROGRESS.md
index a372de75..7186f1a1 100644
--- a/docs/PROGRESS.md
+++ b/docs/PROGRESS.md
@@ -1,3 +1,9 @@
+## 2026-11-04: restrict Markdown transcript file export
+
+Markdown transcripts may contain server-provided content. The TUI now prints them for
+review and copying instead of writing that content directly to a local file. File flags
+are refused before fetching a session; the separate non-Markdown export is unchanged.
+
## 2026-11-03: frontend contract for every feature and agent
Frontend rules were spread across product direction, shell design notes and the dynamic
diff --git a/docs/TUI-PARITY-PLAN.md b/docs/TUI-PARITY-PLAN.md
index a45c140f..93baebac 100644
--- a/docs/TUI-PARITY-PLAN.md
+++ b/docs/TUI-PARITY-PLAN.md
@@ -28,6 +28,8 @@ unless a phase explicitly pays for it.
| P0-5 | `/queue [list|clear]` follow-up queue for this session | local queue in session state, reuses `agentRound` | shipped |
| P0-6 | `/theme [dark|light|mono|status]` | `C` palette in `mso-agent-ui.mjs`, honours `NO_COLOR` | shipped |
+For `/export --markdown`, omit `--out` and `--force`: Markdown prints in the TUI and can be copied to a file. Direct Markdown file export is unavailable because session content can originate from a remote server. The non-Markdown export retains its existing `--out` option.
+
### Phase 0 shipped evidence
- P0-1 (`/export`): case "/export" in scripts/mso-agent-commands.mjs, tested in scripts/mso-agent-export.test.ts
- P0-2 (`/share`): case "/share" in scripts/mso-agent-commands.mjs, tested in scripts/mso-agent-export.test.ts
diff --git a/scripts/mso-agent-cmd-parity.mjs b/scripts/mso-agent-cmd-parity.mjs
index df461aa8..cb06e0e9 100644
--- a/scripts/mso-agent-cmd-parity.mjs
+++ b/scripts/mso-agent-cmd-parity.mjs
@@ -10,7 +10,6 @@ import { createPrinter } from "./mso-agent-print.mjs";
import {
formatExportMarkdown,
parseExportArgs,
- writeExportFile,
} from "./mso-agent-export-markdown.mjs";
export const SHARE_UNAVAILABLE_MESSAGE =
@@ -47,6 +46,9 @@ export async function handleParityCommand(ctx, cmd, args) {
try {
const parsed = parseExportArgs(args);
if (parsed.markdown) {
+ if (args.some((arg) => arg === "--out" || arg.startsWith("--out=")) || parsed.force) {
+ throw new Error("Markdown export to a file is unavailable. Omit --out and --force to print the transcript, then copy it to a file.");
+ }
let target = session;
if (parsed.ref && target?.agentSession?.id !== parsed.ref && target?.id !== parsed.ref) {
if (typeof ctx.api === "function") {
@@ -55,12 +57,7 @@ export async function handleParityCommand(ctx, cmd, args) {
}
}
const markdown = formatExportMarkdown(target);
- if (parsed.outFile) {
- writeExportFile(parsed.outFile, markdown, { force: parsed.force });
- print(`session exported to ${parsed.outFile}.`);
- } else {
- print(markdown);
- }
+ print(markdown);
return "handled";
}
const outIdx = args.indexOf("--out");
diff --git a/scripts/mso-agent-commands.mjs b/scripts/mso-agent-commands.mjs
index 15f42a7d..1dac6ebe 100644
--- a/scripts/mso-agent-commands.mjs
+++ b/scripts/mso-agent-commands.mjs
@@ -8,7 +8,7 @@ export const COMMAND_MANIFEST = [
{ name: "/tabs", usage: "[n|name|next|prev]", description: "list tabs, switch by index/@name, or jump active" },
{ name: "/rename", usage: "<name>", description: "rename the short @agent handle" },
{ name: "/title", usage: "<text>", description: "rename the session title/description" },
- { name: "/export", usage: "[ref] [--out f]", description: "export session transcript to stdout or a file" },
+ { name: "/export", usage: "[ref] [--out f|--markdown]", description: "export a session; Markdown prints to the screen" },
{ name: "/share", description: "temporary share link (server download-only today)" },
{ name: "/status", description: "model/auth/context/token/session details" },
{ name: "/permission", usage: "[mode]", description: "choose ask | auto-write | yolo" },
diff --git a/scripts/mso-agent-export-markdown.mjs b/scripts/mso-agent-export-markdown.mjs
index 740538b4..3ded7ced 100644
--- a/scripts/mso-agent-export-markdown.mjs
+++ b/scripts/mso-agent-export-markdown.mjs
@@ -1,6 +1,3 @@
-import fs from "node:fs";
-import path from "node:path";
-
// Strips ANSI escape sequences so exported transcripts are clean readable Markdown.
export function stripAnsi(text) {
if (!text) return "";
@@ -177,32 +174,3 @@ export function parseExportArgs(args = []) {
}
return flags;
}
-
-// 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);
- const dir = path.dirname(resolved);
- 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 e4fad11b..1ba3a136 100644
--- a/scripts/mso-agent-export-markdown.test.ts
+++ b/scripts/mso-agent-export-markdown.test.ts
@@ -1,10 +1,6 @@
-import fs from "node:fs";
-import os from "node:os";
-import path from "node:path";
import { describe, expect, it, vi } from "vitest";
import {
formatExportMarkdown,
- writeExportFile,
} from "./mso-agent-export-markdown.mjs";
import { handleParityCommand } from "./mso-agent-cmd-parity.mjs";
@@ -108,29 +104,6 @@ describe("MSO Agent session export markdown formatter", () => {
expect(markdown).toContain("Red error output");
});
- it("4. refuses to overwrite an existing file without --force and overwrites with --force", () => {
- // File export must not destroy existing files accidentally without an explicit force override.
- const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "mso-export-test-"));
- try {
- const outPath = path.join(tmpDir, "session.md");
- fs.writeFileSync(outPath, "initial content", "utf8");
-
- // Attempting to overwrite without force must throw an error
- expect(() => {
- writeExportFile(outPath, "new content", { force: false });
- }).toThrow(/already exists.*--force/);
-
- // Verify file content was not clobbered
- expect(fs.readFileSync(outPath, "utf8")).toBe("initial content");
-
- // Overwriting with force must succeed
- writeExportFile(outPath, "overwritten content", { force: true });
- expect(fs.readFileSync(outPath, "utf8")).toBe("overwritten content");
- } finally {
- fs.rmSync(tmpDir, { recursive: true, force: true });
- }
- });
-
it("5. carries session name, model, and turn count in the header", () => {
// Provenance header must document identity, active model, and conversation length.
const session = {
@@ -153,66 +126,26 @@ describe("MSO Agent session export markdown formatter", () => {
expect(markdown).toContain("Deployment Audit");
});
- it("integrates with handleParityCommand for /export --markdown", async () => {
- // Tests that slash command routing invokes markdown export and writes safely.
- const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "mso-parity-export-"));
- try {
- const outPath = path.join(tmpDir, "export.md");
- const session = {
- agentSession: { id: "ses-1", name: "milo", title: "TUI Export", model: "m-test" },
- history: [
- { role: "user", text: "Hello" },
- { role: "assistant", text: "Hi there" },
- ],
- };
- const printed: string[] = [];
- const ctx = {
- session,
- runCli: vi.fn(),
- printer: { print: (s: string) => printed.push(s) },
- C: {},
- };
-
- const outcome = await handleParityCommand(ctx, "/export", ["--markdown", "--out", outPath]);
- expect(outcome).toBe("handled");
- expect(fs.existsSync(outPath)).toBe(true);
- expect(fs.readFileSync(outPath, "utf8")).toContain("TUI Export");
- } finally {
- 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");
+ it("refuses file flags before fetching and still prints remote transcripts", async () => {
+ const printed: string[] = [];
+ const api = vi.fn().mockResolvedValue({ session: { title: "Remote", history: [] } });
+ const ctx = {
+ session: { id: "local", history: [] }, api, runCli: vi.fn(),
+ write: (s: string) => printed.push(s), C: {},
+ };
+ const errors = vi.spyOn(console, "error").mockImplementation(() => {});
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");
+ for (const flags of [["--out", "export.md"], ["--out="], ["--force"], ["-f"]]) {
+ expect(await handleParityCommand(ctx, "/export", ["remote", "--markdown", ...flags])).toBe("handled");
+ }
+ expect(api).not.toHaveBeenCalled();
+ expect(errors).toHaveBeenCalledTimes(4);
+ expect(errors).toHaveBeenCalledWith(expect.stringContaining("Omit --out"));
+ expect(await handleParityCommand(ctx, "/export", ["remote", "--markdown"])).toBe("handled");
+ expect(api).toHaveBeenCalledOnce();
+ expect(printed.join("\n")).toContain("Remote");
} finally {
- spy.mockRestore();
- fs.rmSync(tmpDir, { recursive: true, force: true });
+ errors.mockRestore();
}
});
});
diff --git a/scripts/mso-agent-slash.mjs b/scripts/mso-agent-slash.mjs
index 7dfbf72d..ec1fe5bf 100644
--- a/scripts/mso-agent-slash.mjs
+++ b/scripts/mso-agent-slash.mjs
@@ -32,7 +32,7 @@ const BUILTIN_SLASH_ITEMS = [
{ text: "/title", meta: "Rename the session title/description", kind: "command" },
{
text: "/export",
- meta: "Export session transcript to stdout or a file",
+ meta: "Export a session; Markdown prints to the screen",
kind: "command",
},
{