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: refuse to write screenshots and logs through a symlink (#509)

Gil Megidish500514132ae2
diff --git a/src/server.ts b/src/server.ts
index d0e1c61..1dadad5 100644
--- a/src/server.ts
+++ b/src/server.ts
@@ -15,7 +15,7 @@ import { getJpegDimensions } from "./jpeg";
import { describeCoordinateMapping } from "./coordinate-mapping";
import { Mobilecli } from "./mobilecli";
import { MobileDevice } from "./mobile-device";
-import { validateOutputPath, validateFileExtension, withLowercaseScheme } from "./utils";
+import { validateOutputPath, validateFileExtension, withLowercaseScheme, writeOutputFile } from "./utils";
import { formatElements } from "./format-elements";
type ScreenshotContent = { type: "text", text: string } | { type: "image", data: string, mimeType: string };
@@ -902,7 +902,7 @@ export const createMcpServer = (): McpServer => {
throw new ActionableError("This device only supports png screenshots, save to a .png file instead.");
}
- fs.writeFileSync(saveTo, screenshot);
+ writeOutputFile(saveTo, screenshot);
return `Screenshot saved to: ${saveTo}`;
}
);
@@ -1117,7 +1117,7 @@ export const createMcpServer = (): McpServer => {
return logs || "No log entries received";
}
- fs.writeFileSync(saveTo, logs + "\n");
+ writeOutputFile(saveTo, logs + "\n");
const lineCount = logs ? logs.split("\n").length : 0;
return `Saved ${lineCount} log entries to: ${saveTo}`;
}
diff --git a/src/utils.ts b/src/utils.ts
index 7ae6141..b43abbe 100644
--- a/src/utils.ts
+++ b/src/utils.ts
@@ -150,3 +150,17 @@ 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);
+
+export function writeOutputFile(filePath: string, data: string | Buffer): void {
+ const fd = fs.openSync(filePath, WRITE_NO_FOLLOW_FLAGS);
+ try {
+ fs.writeFileSync(fd, data);
+ } finally {
+ fs.closeSync(fd);
+ }
+}
diff --git a/test/utils.ts b/test/utils.ts
index 2d95f2d..7b649ae 100644
--- a/test/utils.ts
+++ b/test/utils.ts
@@ -8,6 +8,7 @@ import {
validateFileExtension,
validateOutputPath,
withLowercaseScheme,
+ writeOutputFile,
} from "../src/utils";
test.describe("utils", () => {
@@ -200,4 +201,38 @@ 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
+ 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}`);
+
+ test("should not write through a symlink planted after the path was validated", () => {
+ const victim = uniqueTempPath("victim.txt");
+ const output = uniqueTempPath("output.log");
+ fs.writeFileSync(victim, "ORIGINAL");
+ try {
+ validateOutputPath(output);
+ fs.symlinkSync(victim, output);
+
+ expect(() => writeOutputFile(output, "OVERWRITTEN")).toThrow();
+ expect(fs.readFileSync(victim, "utf8")).toBe("ORIGINAL");
+ } finally {
+ fs.rmSync(output, { force: true });
+ fs.rmSync(victim, { force: true });
+ }
+ });
+
+ test("should write a regular file", () => {
+ const output = uniqueTempPath("regular.log");
+ try {
+ writeOutputFile(output, "HELLO");
+ expect(fs.readFileSync(output, "utf8")).toBe("HELLO");
+ } finally {
+ fs.rmSync(output, { force: true });
+ }
+ });
+ });
});