-
Notifications
You must be signed in to change notification settings - Fork 1.7k
confine GCS downloads to destination root #10060
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
base: main
Are you sure you want to change the base?
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -79,7 +79,10 @@ func (n *Native) DownloadRecursive(ctx context.Context, src, dst string) error { | |||||
| } | ||||||
|
|
||||||
| for uri, localPath := range files { | ||||||
| fullPath := filepath.Join(dst, localPath) | ||||||
| fullPath, err := resolveDestinationPath(dst, localPath) | ||||||
| if err != nil { | ||||||
| return err | ||||||
| } | ||||||
| dir := filepath.Dir(fullPath) | ||||||
| if _, err := os.Stat(dir); os.IsNotExist(err) { | ||||||
| if err := os.MkdirAll(dir, os.ModePerm); err != nil { | ||||||
|
|
@@ -95,6 +98,34 @@ func (n *Native) DownloadRecursive(ctx context.Context, src, dst string) error { | |||||
| return nil | ||||||
| } | ||||||
|
|
||||||
| func resolveDestinationPath(dst, localPath string) (string, error) { | ||||||
| normalizedLocalPath := filepath.Clean(filepath.FromSlash(localPath)) | ||||||
|
Contributor
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. The call to
Suggested change
|
||||||
| if filepath.IsAbs(normalizedLocalPath) { | ||||||
| return "", fmt.Errorf("gcs object path %q escapes destination root %q", localPath, dst) | ||||||
| } | ||||||
|
|
||||||
| joinedPath := filepath.Clean(filepath.Join(dst, localPath)) | ||||||
|
Contributor
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. |
||||||
| dstRoot, err := filepath.Abs(dst) | ||||||
| if err != nil { | ||||||
| return "", fmt.Errorf("failed to resolve destination root: %w", err) | ||||||
| } | ||||||
|
|
||||||
| fullPath, err := filepath.Abs(joinedPath) | ||||||
| if err != nil { | ||||||
| return "", fmt.Errorf("failed to resolve destination path: %w", err) | ||||||
| } | ||||||
|
|
||||||
| relPath, err := filepath.Rel(dstRoot, fullPath) | ||||||
| if err != nil { | ||||||
| return "", fmt.Errorf("failed to compare destination path: %w", err) | ||||||
| } | ||||||
| if relPath == ".." || strings.HasPrefix(relPath, ".."+string(filepath.Separator)) { | ||||||
| return "", fmt.Errorf("gcs object path %q escapes destination root %q", localPath, dst) | ||||||
| } | ||||||
|
|
||||||
| return joinedPath, nil | ||||||
| } | ||||||
|
|
||||||
| // Uploads a single file to the given dst. | ||||||
| func (n *Native) UploadFile(ctx context.Context, src, dst string) error { | ||||||
| f, err := os.Open(src) | ||||||
|
|
||||||
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.
The
resolveDestinationPathfunction performs expensive operations likefilepath.Abs(which involves a system call to get the current working directory). Since the destination rootdstis constant for all files in the loop, you should resolve the absolute path ofdstonce outside the loop and pass it to a validation helper. This will significantly improve performance when downloading many files.