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

onedrive: return an error if a ranged download doesn't return the requested range

Nick Craig-Wood2b4cb5f85b05
diff --git a/backend/onedrive/onedrive.go b/backend/onedrive/onedrive.go
index 5524dc0d4..8f866502c 100644
--- a/backend/onedrive/onedrive.go
+++ b/backend/onedrive/onedrive.go
@@ -2808,7 +2808,19 @@ func (o *Object) openWithRedirect(ctx context.Context, opts *rest.Opts) (in io.R
// Overwrite size with actual size since size readings from Onedrive is unreliable.
o.size = resp.ContentLength
}
- return resp.Body, err
+ return o.checkRange(resp, opts.Options)
+}
+
+// checkRange returns the body of a download, checking it is the range
+// requested in options.
+func (o *Object) checkRange(resp *http.Response, options []fs.OpenOption) (in io.ReadCloser, err error) {
+ // The size isn't passed as it is unreliable on OneDrive
+ err = rest.CheckContentRange(resp, options, -1)
+ if err != nil {
+ _ = resp.Body.Close()
+ return nil, err
+ }
+ return resp.Body, nil
}
// malwareDownloadError formats an error when the server blocks a malware-flagged file.
diff --git a/backend/onedrive/upload_internal_test.go b/backend/onedrive/upload_internal_test.go
index 0150e62e7..6f13a3051 100644
--- a/backend/onedrive/upload_internal_test.go
+++ b/backend/onedrive/upload_internal_test.go
@@ -419,3 +419,32 @@ func TestOpenRedirectAuth(t *testing.T) {
assert.ErrorIs(t, err, errPreauthRetired)
assert.Empty(t, *auths)
}
+
+// TestOpenRangeCheck checks that a download which doesn't return the
+// requested range is an error rather than wrong data.
+func TestOpenRangeCheck(t *testing.T) {
+ content := []byte("0123456789abcdefghij")
+ for _, test := range []struct {
+ name string
+ handler http.HandlerFunc
+ wantErr error
+ }{
+ {"ignored", func(w http.ResponseWriter, r *http.Request) {
+ _, _ = w.Write(content)
+ }, fs.ErrorRangeIgnored},
+ {"wrong range", func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Range", "bytes 0-4/20")
+ w.WriteHeader(http.StatusPartialContent)
+ _, _ = w.Write(content[:5])
+ }, nil},
+ } {
+ t.Run(test.name, func(t *testing.T) {
+ o, _ := newDownloadTest(t, content, test.handler)
+ _, err := o.Open(context.Background(), &fs.RangeOption{Start: 5, End: 9})
+ require.Error(t, err)
+ if test.wantErr != nil {
+ assert.ErrorIs(t, err, test.wantErr)
+ }
+ })
+ }
+}