Skip to content

Commit a27ec9b

Browse files
committed
fix: validate file paths in saveFile to prevent path traversal
The saveFile() method accepts user-provided filenames and resolves them with path.resolve(), but does not validate the resulting path. This allows writing files to arbitrary locations via absolute paths or ../ sequences. Add validation to ensure the resolved file path stays within the current working directory before writing.
1 parent 8d765c0 commit a27ec9b

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

src/McpContext.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,11 +701,17 @@ export class McpContext implements Context {
701701
): Promise<{filename: string}> {
702702
try {
703703
const filePath = path.resolve(filename);
704+
const cwd = process.cwd();
705+
if (!filePath.startsWith(cwd + path.sep) && filePath !== cwd) {
706+
throw new Error(
707+
`File path must be within the current working directory: ${cwd}`,
708+
);
709+
}
704710
await fs.writeFile(filePath, data);
705711
return {filename};
706712
} catch (err) {
707713
this.logger(err);
708-
throw new Error('Could not save a screenshot to a file', {cause: err});
714+
throw new Error('Could not save a file', {cause: err});
709715
}
710716
}
711717

0 commit comments

Comments
 (0)