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

webdav: fix Nextcloud public share access by sending X-Requested-With - fixes #10039

drakeo338811a1de98501
diff --git a/backend/webdav/webdav.go b/backend/webdav/webdav.go
index 41cd7bb20..92fc51955 100644
--- a/backend/webdav/webdav.go
+++ b/backend/webdav/webdav.go
@@ -601,6 +601,10 @@ func NewFs(ctx context.Context, name, root string, m configmap.Mapper) (fs.Fs, e
if !f.findHeader(opt.Headers, "Referer") {
f.srv.SetHeader("Referer", u.String())
}
+ // Nextcloud public shares reject requests without this header
+ if opt.Vendor == "nextcloud" && strings.Contains(u.Path, "public.php/dav/files") && !f.findHeader(opt.Headers, "X-Requested-With") {
+ f.srv.SetHeader("X-Requested-With", "XMLHttpRequest")
+ }
if root != "" && !rootIsDir {
// Check to see if the root actually an existing file
diff --git a/backend/webdav/webdav_internal_test.go b/backend/webdav/webdav_internal_test.go
index 84af85266..2af6c5d69 100644
--- a/backend/webdav/webdav_internal_test.go
+++ b/backend/webdav/webdav_internal_test.go
@@ -579,3 +579,40 @@ func TestNotFoundInMultistatus(t *testing.T) {
_, err = f.List(ctx, "missing")
assert.Equal(t, fs.ErrorDirNotFound, err)
}
+
+// TestNextcloudPublicShareHeader checks X-Requested-With is sent for
+// Nextcloud public shares unless the user overrides it.
+func TestNextcloudPublicShareHeader(t *testing.T) {
+ for _, test := range []struct {
+ name string
+ path string
+ headers string
+ want string
+ }{
+ {"public share", "/public.php/dav/files/token", "", "XMLHttpRequest"},
+ {"user override", "/public.php/dav/files/token", "X-Requested-With,custom", "custom"},
+ {"regular nextcloud", "/remote.php/dav/files/user", "", ""},
+ } {
+ t.Run(test.name, func(t *testing.T) {
+ var got atomic.Value
+ got.Store("")
+ ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ got.Store(r.Header.Get("X-Requested-With"))
+ w.WriteHeader(http.StatusMultiStatus)
+ _, _ = io.WriteString(w, `<d:multistatus xmlns:d="DAV:"></d:multistatus>`)
+ }))
+ defer ts.Close()
+ configfile.Install()
+ m := configmap.Simple{
+ "type": "webdav",
+ "url": ts.URL + test.path,
+ "vendor": "nextcloud",
+ "headers": test.headers,
+ }
+ f, err := webdav.NewFs(context.Background(), remoteName, "", m)
+ require.NoError(t, err)
+ _, _ = f.List(context.Background(), "")
+ assert.Equal(t, test.want, got.Load().(string))
+ })
+ }
+}