-
Notifications
You must be signed in to change notification settings - Fork 68
security: use os.Root API for file path logic #1279
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -129,31 +129,43 @@ func (svc *Service) UploadBatchSpecWorkspaceFiles(ctx context.Context, workingDi | |
| } | ||
|
|
||
| func getFilePaths(workingDir, filePath string) ([]string, error) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any idea why this implementation didn't just use filepath.Walk instead of implementing its own recursion?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to deepsearch, it's to preserve the relative path structure from a particular working directory |
||
| root, err := os.OpenRoot(workingDir) | ||
| if err != nil { | ||
| return nil, errors.Wrap(err, "opening working directory") | ||
| } | ||
| defer root.Close() | ||
| return getFilePathsInRoot(root, filePath) | ||
| } | ||
|
|
||
| func getFilePathsInRoot(root *os.Root, filePath string) ([]string, error) { | ||
| // Clean the path to resolve any ".." segments before accessing the root. | ||
| filePath = filepath.Clean(filePath) | ||
|
|
||
| var filePaths []string | ||
| actualFilePath := filepath.Join(workingDir, filePath) | ||
| info, err := os.Stat(actualFilePath) | ||
| info, err := root.Stat(filePath) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if info.IsDir() { | ||
| dir, err := os.ReadDir(actualFilePath) | ||
| dir, err := root.Open(filePath) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| entries, err := dir.ReadDir(-1) | ||
| dir.Close() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| for _, dirEntry := range dir { | ||
| paths, err := getFilePaths(workingDir, filepath.Join(filePath, dirEntry.Name())) | ||
| for _, dirEntry := range entries { | ||
| paths, err := getFilePathsInRoot(root, filepath.Join(filePath, dirEntry.Name())) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| filePaths = append(filePaths, paths...) | ||
| } | ||
| } else { | ||
| relPath, err := filepath.Rel(workingDir, actualFilePath) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| filePaths = append(filePaths, relPath) | ||
| filePaths = append(filePaths, filePath) | ||
| } | ||
| return filePaths, nil | ||
| } | ||
|
|
@@ -201,7 +213,13 @@ func (svc *Service) uploadFile(ctx context.Context, workingDir, filePath, batchS | |
| const maxFileSize = 10 << 20 // 10MB | ||
|
|
||
| func createFormFile(w *multipart.Writer, workingDir string, mountPath string) error { | ||
| f, err := os.Open(filepath.Join(workingDir, mountPath)) | ||
| root, err := os.OpenRoot(workingDir) | ||
| if err != nil { | ||
| return errors.Wrap(err, "opening working directory") | ||
| } | ||
| defer root.Close() | ||
|
|
||
| f, err := root.Open(mountPath) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would appreciate a comment here for future devs to tell them why this should be used.