security(storage): reject path-traversal keys in filesystem storage#1351
Merged
Conversation
Validate keys with filepath.IsLocal before filepath.Join(fs.root, key). Prevents crafted legacy/cache keys with ../ segments from writing outside the storage root (arbitrary file write). Add ErrInvalidStorageKey and tests. Co-authored-by: Cursor <cursoragent@cursor.com>
There was a problem hiding this comment.
Pull request overview
This PR hardens the filesystem-backed storage implementation by validating local keys before joining them to the storage root, aiming to prevent ..-based path traversal and arbitrary writes outside the configured storage directory.
Changes:
- Add
joinRootSafeand route filesystem storage operations through it before touching the local filesystem. - Introduce
ErrInvalidStorageKeyfor rejected filesystem keys. - Add regression tests covering several traversal-style keys and a valid nested path.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
internal/storage/storage_fs.go |
Adds centralized key validation and applies it to filesystem Stat, Get, Put, Delete, List, and DeleteAll. |
internal/storage/storage_fs_test.go |
Adds regression coverage for traversal-style keys across core filesystem storage operations. |
internal/storage/storage.go |
Defines the new shared ErrInvalidStorageKey error. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| func (fs *fsStorage) List(prefix string) (keys []string, err error) { | ||
| dir := strings.TrimSuffix(utils.NormalizePathname(prefix)[1:], "/") | ||
| return findFiles(filepath.Join(fs.root, dir), dir) | ||
| dir = strings.Trim(strings.TrimSpace(dir), "/") |
|
|
||
| func (fs *fsStorage) DeleteAll(prefix string) (deletedKeys []string, err error) { | ||
| dir := strings.TrimSuffix(utils.NormalizePathname(prefix)[1:], "/") | ||
| dir = strings.Trim(strings.TrimSpace(dir), "/") |
Comment on lines
+121
to
+123
| scanRoot, ferr = fs.joinRootSafe(dir) | ||
| if ferr != nil { | ||
| return nil, ferr |
Comment on lines
+140
to
+142
| absDir, err := fs.joinRootSafe(dir) | ||
| if err != nil { | ||
| return nil, err |
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.
Validate keys with filepath.IsLocal before filepath.Join(fs.root, key). Prevents crafted legacy/cache keys with ../ segments from writing outside the storage root (arbitrary file write). Add ErrInvalidStorageKey and tests.