Skip to content

fix(filesystem): validate the request path as it will be used - #4945

Open
jayhemnani9910 wants to merge 1 commit into
modelcontextprotocol:mainfrom
jayhemnani9910:fix/filesystem-validate-exact-path
Open

jayhemnani9910 wants to merge 1 commit into
modelcontextprotocol:mainfrom
jayhemnani9910:fix/filesystem-validate-exact-path

Conversation

@jayhemnani9910

Copy link
Copy Markdown

Description

validatePath checks a request against the allowed directories after running it through normalizePath, which trims surrounding whitespace and strips a leading/trailing quote. It then returns the untrimmed path, and that's what the tool operates on. So the path that is checked and the path that is used can differ: with /x/allowed configured, a request for /x/allowed' (or with a trailing ", space or tab) is checked as /x/allowed but read, listed or written as the sibling /x/allowed'.

The trimming in normalizePath makes sense for directory arguments from the command line, where quotes can come from shell quoting. This keeps it there, and adds normalizePathFormat (the same normalization without the trimming) for paths that are validated and then used as-is in lib.ts.

Server Details

  • Server: filesystem
  • Changes to: path validation (lib.ts, path-utils.ts)

Motivation and Context

A request path should be validated in exactly the form it will be used. A file inside an allowed directory whose name ends with a quote (e.g. it's') still works.

How Has This Been Tested?

  • New __tests__/sibling-paths.test.ts: siblings named allowed', allowed", allowed and allowed<TAB> are rejected, a new file next to the allowed directory is rejected, and a quote-ending file inside it is allowed. The rejection cases fail on main and pass here.
  • All 174 filesystem tests pass; tsc is clean.
  • Ran the built server over stdio with an MCP client: read_text_file, list_directory and write_file on those sibling paths now return "Access denied".

Breaking Changes

None. Configured directories are still trimmed as before.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist

  • I have read the MCP Protocol Documentation
  • My changes follows MCP security best practices
  • My code follows the repository's style guidelines
  • New and existing tests pass locally

validatePath compared the request against the allowed directories after
normalizePath, which trims surrounding whitespace and quotes, but then
returned the untrimmed path for the file operation. Keep normalizePath's
trimming for configured directories and check request paths with a
format-only normalization, so the path that is checked is the path that
is used.
Copilot AI balanced review requested due to automatic review settings October 1, 2026 19:41
@changeset-bot

changeset-bot Bot commented Oct 1, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 05fcee5

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

This branch has not been deployed

No deployments
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.

2 participants