Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
44 changes: 25 additions & 19 deletions src/lib/upload-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import path from "path";
export interface UploadValidationOptions {
allowedExtensions: readonly string[];
maxSizeBytes: number;
allowedBaseDir?: string;
allowedBaseDir: string | undefined;
}

/**
Expand All @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions src/tools/appautomate-utils/native-execution/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/tools/appautomate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
),
},
{
Expand Down
2 changes: 1 addition & 1 deletion src/tools/applive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
),
},
{
Expand Down
23 changes: 23 additions & 0 deletions tests/tools/upload-validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(" ", {
Expand Down Expand Up @@ -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));
});
Expand Down
Loading