Skip to content

security(storage): reject path-traversal keys in filesystem storage#1351

Merged
ije merged 2 commits into
mainfrom
fix-legacy-route
May 4, 2026
Merged

security(storage): reject path-traversal keys in filesystem storage#1351
ije merged 2 commits into
mainfrom
fix-legacy-route

Conversation

@ije
Copy link
Copy Markdown
Member

@ije ije commented May 2, 2026

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.

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>
Copilot AI review requested due to automatic review settings May 2, 2026 12:49
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 joinRootSafe and route filesystem storage operations through it before touching the local filesystem.
  • Introduce ErrInvalidStorageKey for 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
@ije ije merged commit b4af172 into main May 4, 2026
5 checks passed
@ije ije deleted the fix-legacy-route branch May 4, 2026 13:24
@ije ije mentioned this pull request May 8, 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.

2 participants