Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

archive: fix "directory not found" for archive paths containing "./" or "//" GHSA-66hp-wgxq-6f5q

Nick Craig-Wood32175374bae4
diff --git a/backend/archive/archive.go b/backend/archive/archive.go
index 20cca571a..ec0e6c36c 100644
--- a/backend/archive/archive.go
+++ b/backend/archive/archive.go
@@ -185,8 +185,12 @@ func NewFs(ctx context.Context, name, root string, m configmap.Mapper) (outFs fs
foundArchive := subArchive(remote)
if foundArchive != nil {
fs.Debugf(nil, "Found archiver for %q remote %q", foundArchive.archiver.Extension, foundArchive.remote)
- // Archive path
- foundArchive.root = strings.Trim(remote[len(foundArchive.remote):], "/")
+ // Archive path, in canonical form so that it compares equal
+ // to the cleaned entry names inside the archive
+ foundArchive.root = strings.Trim(path.Clean(remote[len(foundArchive.remote):]), "/")
+ if foundArchive.root == "." {
+ foundArchive.root = ""
+ }
// Path to the archive
archiveRemote := remote[:len(foundArchive.remote)]
// Remote is archive leaf name
diff --git a/backend/archive/archive_internal_test.go b/backend/archive/archive_internal_test.go
index 0866b7dec..f0ea6a253 100644
--- a/backend/archive/archive_internal_test.go
+++ b/backend/archive/archive_internal_test.go
@@ -3,6 +3,7 @@
package archive
import (
+ "archive/zip"
"bytes"
"context"
"fmt"
@@ -277,3 +278,32 @@ func TestArchiveSquashfsIssue9004(t *testing.T) {
assert.True(t, bytes.HasPrefix(data, []byte("<?xml")))
})
}
+
+// TestArchiveUncleanRoot checks that a path into an archive which isn't
+// in canonical form (with "./" or doubled slashes) still finds its
+// directory.
+func TestArchiveUncleanRoot(t *testing.T) {
+ fstest.Initialise()
+ ctx := context.Background()
+
+ var buf bytes.Buffer
+ zw := zip.NewWriter(&buf)
+ w, err := zw.Create("sub/dir/a.txt")
+ require.NoError(t, err)
+ _, err = w.Write([]byte("data"))
+ require.NoError(t, err)
+ require.NoError(t, zw.Close())
+ zipPath := filepath.Join(t.TempDir(), "test.zip")
+ require.NoError(t, os.WriteFile(zipPath, buf.Bytes(), 0600))
+
+ for _, root := range []string{"sub/dir", "sub/./dir", "sub//dir", "./sub/dir/", "sub/dir/."} {
+ t.Run(root, func(t *testing.T) {
+ f, err := cache.Get(ctx, ":archive:"+zipPath+"/"+root)
+ require.NoError(t, err)
+ entries, err := f.List(ctx, "")
+ require.NoError(t, err)
+ require.Len(t, entries, 1)
+ assert.Equal(t, "a.txt", entries[0].Remote())
+ })
+ }
+}