Fix path traversal in GET /api/chat-modes/:modeId#44
Merged
Conversation
The modeId route parameter was joined into a filesystem path without validation. Express decodes URL-encoded characters in route params, so a request like GET /api/chat-modes/..%2F..%2Fsome-file would escape the prompts directory and read any .json file the backend process can access. Validate modeId against a strict allowlist regex before constructing the path; reject anything else with 400. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
GET /api/chat-modes/:modeIdin backend/src/routes/chatModes.js joined themodeIdroute parameter directly into a filesystem path with no validation:Express decodes URL-encoded characters in route parameters, so a request like
GET /api/chat-modes/..%2F..%2Fsome-fileresolvesmodeIdto../../some-fileandpath.joinescapesPROMPTS_DIR. Any.jsonfile readable by the backend process could be disclosed through the response.Fix
Validate
modeIdagainst a strict allowlist regex (^[a-z0-9][a-z0-9-]{0,63}$) before constructing the path, and return400for anything else. The eight shipped mode ids (agent,planner,help-chat,brainstorm,discuss,edit,explore,generate) all match.Test plan
curl http://localhost:<port>/api/chat-modes/agent→ 200 with mode configcurl http://localhost:<port>/api/chat-modes/..%2F..%2Fpackage→ 400 (previously would attempt to readbackend/package.json)curl http://localhost:<port>/api/chat-modes/nonexistent→ 404curl http://localhost:<port>/api/chat-modes/UPPER→ 400