Skip to content

Mobile MCP: let AI agents drive your iOS and Android apps. MCP server for simulators, emulators and real devices. Works with Claude Code, Codex, Cursor and any MCP client.

fix: replace the output file by rename instead of writing through a link (#533)

Gil Megidishb619543f9c16
diff --git a/src/utils.ts b/src/utils.ts
index e211f91..d1113d1 100644
--- a/src/utils.ts
+++ b/src/utils.ts
@@ -1,6 +1,7 @@
import path from "node:path";
import os from "node:os";
import fs from "node:fs";
+import { randomBytes } from "node:crypto";
import { ActionableError } from "./robot";
export function validatePackageName(packageName: string): void {
@@ -161,17 +162,30 @@ export function validateOutputPath(filePath: string): void {
}
}
-// O_NOFOLLOW makes the open itself refuse a symlink at the final component, so a
-// link planted after validateOutputPath cannot redirect the write.
-// ponytail: windows has no O_NOFOLLOW, there we rely on validateOutputPath alone
-const WRITE_NO_FOLLOW_FLAGS = fs.constants.O_WRONLY | fs.constants.O_CREAT | fs.constants.O_TRUNC | (fs.constants.O_NOFOLLOW ?? 0);
+// a sibling of `to` with an unguessable name, created with O_EXCL ("wx"), so a
+// file or link planted there makes the write fail instead of redirecting it
+function partialPathFor(to: string): string {
+ return path.join(path.dirname(to), `.${path.basename(to)}.${randomBytes(8).toString("hex")}.partial`);
+}
+// write next to `filePath` and rename over it: rename replaces whatever sits at
+// `filePath` (a symlink, a hard link, a regular file) instead of writing through
+// it, so a link can never point the write at another file
export function writeOutputFile(filePath: string, data: string | Buffer): void {
- const fd = fs.openSync(filePath, WRITE_NO_FOLLOW_FLAGS);
+ const partial = partialPathFor(filePath);
+ const existing = fs.lstatSync(filePath, { throwIfNoEntry: false });
try {
- fs.writeFileSync(fd, data);
- } finally {
- fs.closeSync(fd);
+ // 0600 while half-written; a link at `filePath` is replaced, so only a
+ // regular file there passes its mode on to the new file
+ fs.writeFileSync(partial, data, { flag: "wx", mode: 0o600 });
+ if (existing?.isFile()) {
+ fs.chmodSync(partial, existing.mode & 0o777);
+ }
+
+ fs.renameSync(partial, filePath);
+ } catch (error) {
+ fs.rmSync(partial, { force: true });
+ throw error;
}
}
@@ -196,7 +210,7 @@ export function moveOutputFile(from: string, to: string): void {
}
- const partial = path.join(path.dirname(to), `.${path.basename(to)}.${process.pid}-${Date.now()}.partial`);
+ const partial = partialPathFor(to);
try {
fs.copyFileSync(from, partial, fs.constants.COPYFILE_EXCL);
fs.renameSync(partial, to);
diff --git a/test/utils.ts b/test/utils.ts
index 5cffdf8..f804d9f 100644
--- a/test/utils.ts
+++ b/test/utils.ts
@@ -236,13 +236,19 @@ test.describe("utils", () => {
});
});
- // a symlink can be planted between validateOutputPath and the write, so the
- // write itself must refuse to follow a symlink at the final component
+ // a link can be planted at the output path, before or after validateOutputPath,
+ // so the write must replace whatever is there instead of writing through it
test.describe("writeOutputFile", () => {
test.skip(process.platform === "win32", "creating a symlink needs elevated rights on windows");
const uniqueTempPath = (name: string): string => path.join(os.tmpdir(), `mobile-mcp-${process.pid}-${Date.now()}-${name}`);
+ const expectRegularFileWith = (filePath: string, content: string) => {
+ expect(fs.lstatSync(filePath).isSymbolicLink()).toBe(false);
+ expect(fs.statSync(filePath).nlink).toBe(1);
+ expect(fs.readFileSync(filePath, "utf8")).toBe(content);
+ };
+
test("should not write through a symlink planted after the path was validated", () => {
const victim = uniqueTempPath("victim.txt");
const output = uniqueTempPath("output.log");
@@ -251,14 +257,81 @@ test.describe("utils", () => {
validateOutputPath(output);
fs.symlinkSync(victim, output);
- expect(() => writeOutputFile(output, "OVERWRITTEN")).toThrow();
+ writeOutputFile(output, "OVERWRITTEN");
+
+ expect(fs.readFileSync(victim, "utf8")).toBe("ORIGINAL");
+ expectRegularFileWith(output, "OVERWRITTEN");
+ } finally {
+ fs.rmSync(output, { force: true });
+ fs.rmSync(victim, { force: true });
+ }
+ });
+
+ test("should not write through a hard link at the output path", () => {
+ const victim = uniqueTempPath("victim.txt");
+ const output = uniqueTempPath("shot.png");
+ fs.writeFileSync(victim, "ORIGINAL");
+ fs.linkSync(victim, output);
+ try {
+ validateOutputPath(output);
+
+ writeOutputFile(output, "OVERWRITTEN");
+
expect(fs.readFileSync(victim, "utf8")).toBe("ORIGINAL");
+ expectRegularFileWith(output, "OVERWRITTEN");
} finally {
fs.rmSync(output, { force: true });
fs.rmSync(victim, { force: true });
}
});
+ test("should replace an existing output file", () => {
+ const output = uniqueTempPath("existing.log");
+ fs.writeFileSync(output, "OLD CONTENT THAT IS LONGER");
+ try {
+ writeOutputFile(output, "NEW");
+ expectRegularFileWith(output, "NEW");
+ } finally {
+ fs.rmSync(output, { force: true });
+ }
+ });
+
+ const permissionsOf = (filePath: string): number => fs.statSync(filePath).mode & 0o777;
+
+ test("should keep the permissions of the file it replaces", () => {
+ const output = uniqueTempPath("existing-mode.log");
+ fs.writeFileSync(output, "OLD");
+ fs.chmodSync(output, 0o640);
+ try {
+ writeOutputFile(output, "NEW");
+ expect(permissionsOf(output)).toBe(0o640);
+ } finally {
+ fs.rmSync(output, { force: true });
+ }
+ });
+
+ test("should create a new file readable only by its owner", () => {
+ const output = uniqueTempPath("new-mode.log");
+ try {
+ writeOutputFile(output, "NEW");
+ expect(permissionsOf(output)).toBe(0o600);
+ } finally {
+ fs.rmSync(output, { force: true });
+ }
+ });
+
+ test("should leave no partial file behind when the write fails", () => {
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), "mobile-mcp-"));
+ const output = path.join(dir, "output.log");
+ fs.mkdirSync(output);
+ try {
+ expect(() => writeOutputFile(output, "DATA")).toThrow();
+ expect(fs.readdirSync(dir)).toEqual(["output.log"]);
+ } finally {
+ fs.rmSync(dir, { recursive: true, force: true });
+ }
+ });
+
test("should write a regular file", () => {
const output = uniqueTempPath("regular.log");
try {