Skip to content
Closed
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
42 changes: 24 additions & 18 deletions src/lib/upload-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
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