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.
TypeScript agentandroidandroid-automationappium-alternativeclaude-codecodexcursoremulatoriosios-automationmcpmcp-servermobilemobile-automationmobile-testingreal-devicesimulatorskills
fix: resolve every symlink in the output path before checking allowed roots (#506)
Gil Megidishb5b6e327ebb8
diff --git a/src/utils.ts b/src/utils.tsindex 63493a6..17b2459 100644--- a/src/utils.ts+++ b/src/utils.ts@@ -79,16 +79,52 @@ export function validateFileExtension(filePath: string, allowedExtensions: strin}}+// Walk `filePath` to the real location a write would land on, following a+// symlink in EVERY component - not just the parent directory. Resolving only the+// parent let a symlink at, or before, the final component redirect the write+// while the check still saw an in-root path: a single final-component link was+// covered, but a chain whose intermediate link sits inside an allowed root and+// whose final target is dangling (the file a write would create) was not, so the+// write escaped. Follow each hop to its target until a component that either+// exists as a real file or does not exist at all, and hand that to the root check.function resolveWithSymlinks(filePath: string): string {- const resolved = path.resolve(filePath);- const dir = path.dirname(resolved);- const filename = path.basename(resolved);+ let current = path.resolve(filePath);++ // bound the walk so a symlink cycle cannot spin forever; a cycle makes the+ // eventual write fail with ELOOP, so it is not an escape+ for (let hops = 0; hops < 40; hops++) {+ const dir = path.dirname(current);+ const filename = path.basename(current);++ // resolve the containing directory first (it may be a symlink itself,+ // e.g. /tmp and /var on macOS) so it lines up with the allowed roots+ let realDir: string;+ try {+ realDir = fs.realpathSync(dir);+ } catch {+ realDir = dir;+ }- try {- return path.join(fs.realpathSync(dir), filename);- } catch {- return resolved;+ const candidate = path.join(realDir, filename);++ let stat: fs.Stats;+ try {+ stat = fs.lstatSync(candidate);+ } catch {+ // nothing there yet, it will be created at `candidate`+ return candidate;+ }++ if (!stat.isSymbolicLink()) {+ return candidate;+ }++ // follow the link to its target - which may itself be a link, or may not+ // exist yet (exactly where a write would create the file) - and resolve again+ current = path.resolve(realDir, fs.readlinkSync(candidate));}++ return current;}export function validateOutputPath(filePath: string): void {diff --git a/test/utils.ts b/test/utils.tsindex 3d2a279..085993f 100644--- a/test/utils.ts+++ b/test/utils.ts@@ -1,4 +1,5 @@import { test, expect } from "@playwright/test";+import fs from "node:fs";import path from "node:path";import os from "node:os";import {@@ -123,4 +124,67 @@ test.describe("utils", () => {expect(() => validateOutputPath(target)).toThrow();});});++ // a symlink as the final path component would redirect a write, so it must be+ // resolved too, not just the parent directory (see issue #412)+ test.describe("validateOutputPath symlinks", () => {+ let nameCounter = 0;+ const createSymlink = (target: string): string => {+ const link = path.join(process.cwd(), `mobile-mcp-symlink-${process.pid}-${Date.now()}-${nameCounter++}.png`);+ fs.rmSync(link, { force: true });+ fs.symlinkSync(target, link);+ return link;+ };++ // windows needs privileges to create a symlink, so this only runs where the+ // behaviour it guards can be exercised+ test.skip(process.platform === "win32", "creating a symlink needs elevated rights on windows");++ test("should reject a final-component symlink whose target is outside the allowed directories", () => {+ const link = createSymlink(path.join(path.sep, "mobile-mcp-outside", "escaped.png"));+ try {+ expect(() => validateOutputPath(link)).toThrow();+ } finally {+ fs.rmSync(link, { force: true });+ }+ });++ test("should still accept a final-component symlink whose target stays inside an allowed directory", () => {+ const link = createSymlink(path.join(os.tmpdir(), "mobile-mcp-symlink-inside.png"));+ try {+ expect(() => validateOutputPath(link)).not.toThrow();+ } finally {+ fs.rmSync(link, { force: true });+ }+ });++ // a single link is not enough: the target of the first link can itself be a+ // link inside an allowed root that bottoms out somewhere outside it, so the+ // whole chain has to be followed - stopping at the first hop accepts an+ // in-root name while the write lands outside it+ test("should reject a symlink chain whose dangling target is outside the allowed directories", () => {+ const outside = path.join(path.sep, "mobile-mcp-outside", "escaped.png");+ const first = createSymlink(outside);+ const second = createSymlink(path.basename(first));+ try {+ expect(() => validateOutputPath(first)).toThrow();+ expect(() => validateOutputPath(second)).toThrow();+ } finally {+ fs.rmSync(second, { force: true });+ fs.rmSync(first, { force: true });+ }+ });++ test("should still accept a symlink chain whose target stays inside an allowed directory", () => {+ const first = createSymlink(path.join(os.tmpdir(), "mobile-mcp-chain-inside.png"));+ const second = createSymlink(path.basename(first));+ try {+ expect(() => validateOutputPath(first)).not.toThrow();+ expect(() => validateOutputPath(second)).not.toThrow();+ } finally {+ fs.rmSync(second, { force: true });+ fs.rmSync(first, { force: true });+ }+ });+ });});