diff --git a/internal/davsync/dav.go b/internal/davsync/dav.go index 87e393d00..5ab0c4601 100644 --- a/internal/davsync/dav.go +++ b/internal/davsync/dav.go @@ -201,7 +201,8 @@ var errLogin = errors.New("the WebDAV server refused the user name or password ( // forbidden says why a 403 was: servers answer it for a folder that isn't // there as much as for one the account may not use — a Synology does for a -// shared folder not made — so the folder in the address is looked at first. +// shared folder not made — so the folder in the address is looked at first, +// and then magpie's own folder inside it, where the write went. func (d *dav) forbidden(ctx context.Context) error { dir := d.base.Path if dir == "" { @@ -221,7 +222,16 @@ func (d *dav) forbidden(ctx context.Context) error { case res.StatusCode == http.StatusNotFound || res.StatusCode == http.StatusConflict || res.StatusCode == http.StatusGone: return fmt.Errorf("there is no folder %s on the WebDAV server (HTTP 403): make it there first, or leave it out of the address", dir) case res.StatusCode >= 200 && res.StatusCode < 300: - return fmt.Errorf("the WebDAV server doesn't let this account write in %s (HTTP 403): check the account may change files in that folder", dir) + // the address's folder lists fine, so the refusal is about + // magpie's own folder inside it, where the write went: the + // server may have kept the account from making it, or, with + // it there, from changing files in it — 坚果云 (Nutstore)'s + // free tier refuses writes with 403 once the account's + // upload quota is used up (#4927) + if !d.there(ctx, d.url(folder)+"/") { + return fmt.Errorf("the WebDAV server doesn't let magpie make its folder %s (HTTP 403): make the folder %s there first, or put an existing folder in the address", pathpkg.Join(dir, folder), folder) + } + return fmt.Errorf("the WebDAV server doesn't let this account change files in %s (HTTP 403): check the account may change files in that folder, and the account's storage quota is not used up", pathpkg.Join(dir, folder)) } } } diff --git a/internal/davsync/dav_test.go b/internal/davsync/dav_test.go index 9f40759bf..d804a30b3 100644 --- a/internal/davsync/dav_test.go +++ b/internal/davsync/dav_test.go @@ -21,7 +21,7 @@ func TestDAVForbidden(t *testing.T) { } path := strings.TrimSuffix(r.URL.Path, "/") switch { - case r.Method == "PROPFIND" && (path == "" || path == "/ro"): + case r.Method == "PROPFIND" && (path == "" || path == "/ro" || path == "/ro/magpie"): w.WriteHeader(http.StatusMultiStatus) case r.Method == "PROPFIND" && path == "/missing": w.WriteHeader(http.StatusNotFound) @@ -35,8 +35,10 @@ func TestDAVForbidden(t *testing.T) { ctx := context.Background() for _, c := range []struct{ dir, pass, want string }{ {"/missing", "pw", "there is no folder /missing"}, - {"/ro", "pw", "doesn't let this account write in /ro"}, + {"/ro", "pw", "doesn't let this account change files in /ro/magpie"}, {"/secret", "pw", "doesn't let this account use /secret"}, + // the server's root in the address: its folder is /magpie, not //magpie + {"", "pw", "doesn't let magpie make its folder /magpie ("}, {"/missing", "bad", "user name or password"}, } { d, err := newDAV(Config{URL: srv.URL + c.dir, User: "me", Password: c.pass}) @@ -53,6 +55,73 @@ func TestDAVForbidden(t *testing.T) { } } +// A 403 on the write itself, with everything listing fine, is told apart by +// magpie's own folder: there, the server refuses to change files in it — +// 坚果云 (Nutstore)'s free tier refuses writes with 403 once the account's +// upload quota is used up (#4927) — and the error names the folder the write +// went to, not just the address's, and says the quota. Missing, the server +// refused to have magpie make it, and the error says to make it by hand. +func TestDAVWriteRefused(t *testing.T) { + ctx := context.Background() + refuse := func(statusOf func(r *http.Request) int) *dav { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(statusOf(r)) + })) + t.Cleanup(srv.Close) + d, err := newDAV(Config{URL: srv.URL + "/dav/"}) + if err != nil { + t.Fatal(err) + } + return d + } + + // the folder is there (an earlier sync made it), the write is refused + d := refuse(func(r *http.Request) int { + switch { + case r.Method == "PROPFIND" && (r.URL.Path == "/dav/" || r.URL.Path == "/dav/magpie/"): + return http.StatusMultiStatus + default: + return http.StatusForbidden + } + }) + if _, err := d.put(ctx, []byte("x"), ""); err == nil || !strings.Contains(err.Error(), "change files in /dav/magpie") || !strings.Contains(err.Error(), "quota") { + t.Fatalf("a refused write into the folder there: %v", err) + } + + // the folder isn't there, and the server refuses to have magpie make it + d = refuse(func(r *http.Request) int { + switch { + case r.Method == "PROPFIND" && r.URL.Path == "/dav/": + return http.StatusMultiStatus + case r.Method == "MKCOL": + return http.StatusForbidden + default: + return http.StatusConflict + } + }) + if _, err := d.put(ctx, []byte("x"), ""); err == nil || !strings.Contains(err.Error(), "doesn't let magpie make its folder /dav/magpie") || !strings.Contains(err.Error(), "make the folder magpie there first") { + t.Fatalf("a refused folder: %v", err) + } + + // an address with no folder of its own: magpie's is at the root, + // named once, not "//magpie" + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method == "PROPFIND" && (r.URL.Path == "/" || r.URL.Path == "/magpie/") { + w.WriteHeader(http.StatusMultiStatus) + return + } + w.WriteHeader(http.StatusForbidden) + })) + t.Cleanup(srv.Close) + d, err := newDAV(Config{URL: srv.URL}) + if err != nil { + t.Fatal(err) + } + if _, err := d.put(ctx, []byte("x"), ""); err == nil || !strings.Contains(err.Error(), "change files in /magpie ") || strings.Contains(err.Error(), "//") { + t.Fatalf("a refused write under an address with no folder: %v", err) + } +} + // OpenList's and Alist's /dav/ lists their storages: a folder can't be made // there (MKCOL 405, as if it were) and a file can't be put (404). The error // says to put a storage in the address, naming them.