diff --git a/README.md b/README.md index d8c349e2..8322f079 100644 --- a/README.md +++ b/README.md @@ -292,6 +292,8 @@ Select the “Installed” tab. Click the “Configure MCP Servers” button at } ``` +> **File & app uploads:** tools that upload a local file/app (`uploadProductRequirementFile`, `takeAppScreenshot`, `runAppTestsOnBrowserStack`, `runAppLiveSession`) require the `MCP_UPLOAD_BASE_DIR` env var set to a directory containing those files; uploads are restricted to it. + ### 💡 List of BrowserStack MCP Tools As of now we support 45 tools. diff --git a/src/lib/upload-validator.ts b/src/lib/upload-validator.ts index d516d129..fe420b66 100644 --- a/src/lib/upload-validator.ts +++ b/src/lib/upload-validator.ts @@ -4,7 +4,7 @@ import path from "path"; export interface UploadValidationOptions { allowedExtensions: readonly string[]; maxSizeBytes: number; - allowedBaseDir?: string; + allowedBaseDir: string | undefined; } /** @@ -17,7 +17,8 @@ export interface UploadValidationOptions { * - File extension is in `allowedExtensions` (case-insensitive) * - No path segment is a hidden dir/file (starts with `.`); blocks ~/.ssh, * ~/.aws, .env, etc. even after symlink resolution - * - If `allowedBaseDir` is set, the canonical path must live inside it + * - `allowedBaseDir` is mandatory: uploads are refused unless it is configured + * (via MCP_UPLOAD_BASE_DIR), and the canonical path must live inside it */ export function validateUploadPath( filePath: string, @@ -71,23 +72,28 @@ export function validateUploadPath( ); } - if (options.allowedBaseDir) { - let baseCanonical: string; - try { - baseCanonical = fs.realpathSync(path.resolve(options.allowedBaseDir)); - } catch { - throw new Error( - `Upload rejected: configured MCP_UPLOAD_BASE_DIR does not exist (${options.allowedBaseDir}).`, - ); - } - const baseWithSep = baseCanonical.endsWith(path.sep) - ? baseCanonical - : baseCanonical + path.sep; - if (canonical !== baseCanonical && !canonical.startsWith(baseWithSep)) { - throw new Error( - `Upload rejected: file must be located inside ${baseCanonical}.`, - ); - } + if (!options.allowedBaseDir) { + throw new Error( + "Upload rejected: MCP_UPLOAD_BASE_DIR is not set. Set it to the directory " + + "containing the files to upload, then restart the MCP server.", + ); + } + + let baseCanonical: string; + try { + baseCanonical = fs.realpathSync(path.resolve(options.allowedBaseDir)); + } catch { + throw new Error( + `Upload rejected: configured MCP_UPLOAD_BASE_DIR does not exist (${options.allowedBaseDir}).`, + ); + } + const baseWithSep = baseCanonical.endsWith(path.sep) + ? baseCanonical + : baseCanonical + path.sep; + if (canonical !== baseCanonical && !canonical.startsWith(baseWithSep)) { + throw new Error( + `Upload rejected: file must be located inside ${baseCanonical}.`, + ); } return canonical; diff --git a/src/tools/appautomate-utils/native-execution/constants.ts b/src/tools/appautomate-utils/native-execution/constants.ts index 8b1a5360..58d5aa10 100644 --- a/src/tools/appautomate-utils/native-execution/constants.ts +++ b/src/tools/appautomate-utils/native-execution/constants.ts @@ -26,7 +26,8 @@ export const RUN_APP_AUTOMATE_SCHEMA = { " xcodebuild clean -scheme YOUR_SCHEME && \\\n" + " xcodebuild archive -scheme YOUR_SCHEME -configuration Release -archivePath build/app.xcarchive && \\\n" + " xcodebuild -exportArchive -archivePath build/app.xcarchive -exportPath build/ipa -exportOptionsPlist exportOptions.plist\n\n" + - "If in other directory, provide existing app path", + "If in other directory, provide existing app path.\n" + + "The resolved file must be located inside the directory set in MCP_UPLOAD_BASE_DIR.", ), testSuitePath: z .string() @@ -38,7 +39,8 @@ export const RUN_APP_AUTOMATE_SCHEMA = { " xcodebuild test-without-building -scheme YOUR_SCHEME -destination 'generic/platform=iOS' && \\\n" + " cd ~/Library/Developer/Xcode/DerivedData/*/Build/Products/Debug-iphonesimulator/ && \\\n" + " zip -r Tests.zip *.xctestrun *-Runner.app\n\n" + - "If in other directory, provide existing test file path", + "If in other directory, provide existing test file path.\n" + + "The resolved file must be located inside the directory set in MCP_UPLOAD_BASE_DIR.", ), devices: z .array(MobileDeviceSchema) diff --git a/src/tools/appautomate.ts b/src/tools/appautomate.ts index 36d05826..0fbd0e29 100644 --- a/src/tools/appautomate.ts +++ b/src/tools/appautomate.ts @@ -333,7 +333,7 @@ export default function addAppAutomationTools( appPath: z .string() .describe( - "The path to the .apk or .ipa file. Required for app installation.", + "The path to the .apk or .ipa file. Required for app installation. Must be located inside the directory set in MCP_UPLOAD_BASE_DIR.", ), }, { diff --git a/src/tools/applive.ts b/src/tools/applive.ts index e188e06e..a8e159a3 100644 --- a/src/tools/applive.ts +++ b/src/tools/applive.ts @@ -103,7 +103,7 @@ export default function addAppLiveTools( appPath: z .string() .describe( - "The path to the .ipa or .apk file to install on the device. Always ask the user for the app path, do not assume it.", + "The path to the .ipa or .apk file to install on the device. Always ask the user for the app path, do not assume it. Must be located inside the directory set in MCP_UPLOAD_BASE_DIR.", ), }, { diff --git a/tests/tools/upload-validator.test.ts b/tests/tools/upload-validator.test.ts index 51eed475..f423f194 100644 --- a/tests/tools/upload-validator.test.ts +++ b/tests/tools/upload-validator.test.ts @@ -32,10 +32,32 @@ describe("validateUploadPath", () => { const resolved = validateUploadPath(file, { allowedExtensions: APP_BINARY_EXTENSIONS, maxSizeBytes: MAX_APP_UPLOAD_BYTES, + allowedBaseDir: workDir, }); expect(resolved).toBe(fs.realpathSync(file)); }); + it("refuses when no base directory is configured (MCP_UPLOAD_BASE_DIR unset)", () => { + const file = write("app.apk"); + expect(() => + validateUploadPath(file, { + allowedExtensions: APP_BINARY_EXTENSIONS, + maxSizeBytes: MAX_APP_UPLOAD_BYTES, + }), + ).toThrow(/MCP_UPLOAD_BASE_DIR is not set/); + }); + + it("refuses when the configured base directory does not exist", () => { + const file = write("app.apk"); + expect(() => + validateUploadPath(file, { + allowedExtensions: APP_BINARY_EXTENSIONS, + maxSizeBytes: MAX_APP_UPLOAD_BYTES, + allowedBaseDir: path.join(os.tmpdir(), "no-such-base-dir-xyz"), + }), + ).toThrow(/does not exist/); + }); + it("rejects an empty path", () => { expect(() => validateUploadPath(" ", { @@ -185,6 +207,7 @@ describe("validateUploadPath", () => { const resolved = validateUploadPath(file, { allowedExtensions: APP_BINARY_EXTENSIONS, maxSizeBytes: MAX_APP_UPLOAD_BYTES, + allowedBaseDir: workDir, }); expect(resolved).toBe(fs.realpathSync(file)); });