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: never allow the filesystem root or home directory as an output root (#508)

Gil Megidish804d626ea0f5
diff --git a/src/utils.ts b/src/utils.ts
index 801f42e..e211f91 100644
--- a/src/utils.ts
+++ b/src/utils.ts
@@ -37,11 +37,21 @@ function resolveRoot(root: string): string {
}
}
+// the filesystem root, the home directory, or anything above it would make
+// almost every file the user owns writable, so it is never an implicit root
+function isTooBroadToAllow(dir: string): boolean {
+ const resolvedDir = resolveRoot(dir);
+ const home = resolveRoot(os.homedir());
+ return resolvedDir === home || isPathUnderRoot(home, resolvedDir);
+}
+
function getAllowedRoots(): string[] {
- const roots = [
- os.tmpdir(),
- process.cwd(),
- ];
+ const roots = [os.tmpdir()];
+
+ if (!isTooBroadToAllow(process.cwd())) {
+ roots.push(process.cwd());
+ }
+
// macOS /tmp is a symlink to /private/tmp, add both to be safe
if (process.platform === "darwin") {
@@ -146,7 +156,7 @@ export function validateOutputPath(filePath: string): void {
if (!isAllowed) {
const dir = path.dirname(resolved);
throw new ActionableError(
- `"${dir}" is not in the list of allowed directories. Allowed directories include the current directory and the temp directory on this host.`
+ `"${dir}" is not in the list of allowed directories. Allowed directories: ${allowedRoots.join(", ")}.`
);
}
}
diff --git a/test/utils.ts b/test/utils.ts
index 9a62d17..5cffdf8 100644
--- a/test/utils.ts
+++ b/test/utils.ts
@@ -127,6 +127,37 @@ test.describe("utils", () => {
const target = path.join(process.cwd(), escape, "mobile-mcp-test-output.png");
expect(() => validateOutputPath(target)).toThrow();
});
+
+ // some mcp hosts start the server from / or from the home directory, which
+ // must not turn the whole disk or the whole home into an allowed root
+ const whileRunningFrom = (dir: string, check: () => void) => {
+ const original = process.cwd();
+ process.chdir(dir);
+ try {
+ check();
+ } finally {
+ process.chdir(original);
+ }
+ };
+
+ test("should reject a path in the home directory when started from the home directory", () => {
+ whileRunningFrom(os.homedir(), () => {
+ expect(() => validateOutputPath(path.join(os.homedir(), "mobile-mcp-test-output.png"))).toThrow();
+ });
+ });
+
+ test("should reject a path anywhere on disk when started from the filesystem root", () => {
+ const root = path.parse(process.cwd()).root;
+ whileRunningFrom(root, () => {
+ expect(() => validateOutputPath(path.join(os.homedir(), "mobile-mcp-test-output.png"))).toThrow();
+ });
+ });
+
+ test("should still accept the temp directory when started from the home directory", () => {
+ whileRunningFrom(os.homedir(), () => {
+ expect(() => validateOutputPath(path.join(os.tmpdir(), "mobile-mcp-test-output.png"))).not.toThrow();
+ });
+ });
});
// a symlink as the final path component would redirect a write, so it must be