Skip to content

fix: validate file paths in saveFile to prevent path traversal#971

Closed
AI-Reviewer-QS wants to merge 1 commit intoChromeDevTools:mainfrom
AI-Reviewer-QS:fix/savefile-path-validation
Closed

fix: validate file paths in saveFile to prevent path traversal#971
AI-Reviewer-QS wants to merge 1 commit intoChromeDevTools:mainfrom
AI-Reviewer-QS:fix/savefile-path-validation

Conversation

@AI-Reviewer-QS
Copy link
Copy Markdown

Summary

  • saveFile() accepts user-provided filenames and resolves them with path.resolve(), but does not validate the resulting path
  • This allows writing files to arbitrary locations outside the working directory via absolute paths (e.g., /etc/passwd) or ../ sequences
  • Added validation to ensure the resolved path stays within process.cwd() before writing

Reproduction

// These would previously succeed:
await context.saveFile(data, '/tmp/arbitrary-file.txt');
await context.saveFile(data, '../../outside-cwd/file.txt');

// After fix, both throw:
// "File path must be within the current working directory: /path/to/cwd"

Test plan

  • Verify relative paths within CWD still work (e.g., output/file.txt)
  • Verify absolute paths outside CWD are rejected
  • Verify ../ path traversal sequences are rejected
  • Verify screenshot saving (uses saveScreenshot, not affected) still works

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.
@google-cla
Copy link
Copy Markdown

google-cla Bot commented Feb 16, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@OrKoN
Copy link
Copy Markdown
Collaborator

OrKoN commented Feb 16, 2026

Thanks for the PR but the filePath is explicitly for absolute paths and not for paths within cwd() (as the MCP server cwd could be different from the project cwd). Please file a feature request to describe which scenario you are after. cc @natorion @nroscino

@OrKoN OrKoN closed this Feb 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants