Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pkg/filestore/filestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,12 @@ func (fs *FileStore) Retrieve(ctx context.Context, path string) ([]byte, error)
return nil, err
}

// write to local filesystem
// write to local filesystem — create parent directories if they don't exist
if dir := filepath.Dir(localPath); dir != "" {
if err := os.MkdirAll(dir, 0755); err != nil {
return nil, err
}
}
Comment on lines +219 to +223

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

security-high high

The path parameter is joined with fs.root using filepath.Join, which does not prevent directory traversal if path contains relative directory segments (e.g., ../../). This could allow writing files or creating directories outside the intended fs.root directory (Path Traversal).

To mitigate this, we should validate that the resolved localPath is indeed within fs.root before creating directories or writing the file. Additionally, filepath.Dir never returns an empty string (it returns . if empty), so the dir != "" check is redundant and can be simplified.

Suggested change
if dir := filepath.Dir(localPath); dir != "" {
if err := os.MkdirAll(dir, 0755); err != nil {
return nil, err
}
}
// Ensure the local path is within the root directory to prevent path traversal
rel, err := filepath.Rel(fs.root, localPath)
if err != nil || (len(rel) >= 2 && rel[0] == '.' && rel[1] == '.') {
return nil, fmt.Errorf("path escapes root directory: %s", path)
}
// write to local filesystem — create parent directories if they don't exist
dir := filepath.Dir(localPath)
if err := os.MkdirAll(dir, 0755); err != nil {
return nil, err
}

err = os.WriteFile(localPath, data, 0644)
if err != nil {
return nil, err
Expand Down
Loading