Fix filestore Retrieve() failing when local cache directories are missing - #245
Fix filestore Retrieve() failing when local cache directories are missing#245concaf wants to merge 1 commit into
Conversation
…sing Retrieve() downloads files from S3 when the local cache is empty, but os.WriteFile fails if the parent directory doesn't exist (e.g. after a PVC delete and pod restart). Add os.MkdirAll before the write, matching what Store() already does.
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a runtime error in the filestore component where file retrieval fails if the local cache directory structure is missing, such as after a pod restart. By ensuring the directory path exists before attempting to write downloaded data, the system now correctly recovers from missing local state. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request updates the file retrieval process in pkg/filestore/filestore.go to automatically create parent directories if they do not exist before writing the file locally. The reviewer identified a critical path traversal vulnerability where the target path could escape the root directory, and provided a code suggestion to validate the path and simplify a redundant directory check.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| if dir := filepath.Dir(localPath); dir != "" { | ||
| if err := os.MkdirAll(dir, 0755); err != nil { | ||
| return nil, err | ||
| } | ||
| } |
There was a problem hiding this comment.
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.
| 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 | |
| } |
Summary
Retrieve()downloads files from S3 when the local cache is empty, butos.WriteFilefails if the parent directory doesn't exist (e.g. after a PVC delete and pod restart)os.MkdirAllbefore the write, matching whatStore()already doesTest plan
go build ./pkg/filestore/passesgo vet ./pkg/filestore/passes