Skip to content

Commit 87cdcc9

Browse files
fix(storage): Fix TM download dir corner case (#14142)
This PR fixes the corner case where download file path is resolved exactly to the download directory. The fix now checks for the parent folder of the final resolved file path and checks the relative path against the local directory.
1 parent b903e29 commit 87cdcc9

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

storage/transfermanager/downloader.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -994,9 +994,11 @@ func isSubPath(localDirectory, filePath string) (bool, error) {
994994
return false, fmt.Errorf("cannot convert file path to absolute path: %w", err)
995995
}
996996

997-
// The relative path from the local directory to the file path.
998-
// ex: if localDirectory is /tmp/foo and filePath is /tmp/foo/bar, rel will be "bar".
999-
rel, err := filepath.Rel(absLocalDirectory, absFilePath)
997+
absFilePathFolder := filepath.Dir(absFilePath)
998+
// The relative path from the local directory to the final file path's directory.
999+
// ex 1: if localDirectory is /tmp/foo and filePath is /tmp/foo/bar, rel will be ".".
1000+
// ex 2: if localDirectory is /tmp/foo and filePath is /tmp/bar, rel will be "..".
1001+
rel, err := filepath.Rel(absLocalDirectory, absFilePathFolder)
10001002
if err != nil {
10011003
return false, err
10021004
}

storage/transfermanager/downloader_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,12 @@ func TestIsSubPath(t *testing.T) {
264264
name: "filePath is the same as localDirectory",
265265
localDirectory: "/tmp/foo",
266266
filePath: "/tmp/foo",
267+
wantIsSub: false,
268+
},
269+
{
270+
name: "local directory is /",
271+
localDirectory: "/",
272+
filePath: "/../../a",
267273
wantIsSub: true,
268274
},
269275
{

0 commit comments

Comments
 (0)