Files
magpie/internal/davsync/digest_test.go
T
yetone 422f90e80b davsync: WebDAV answers a Digest challenge, MD5 and SHA-256 (caelum1990 on X)
Sync signed in with Basic only, so a server that takes Digest only (Apache
mod_auth_digest, some NAS and nginx setups) answered 401 and magpie said
the password was wrong. digestTransport (RFC 7616, qop=auth, -sess too)
wraps the dav client: a 401 with a Digest challenge is sent again with a
Digest header made from the same user name and password, SHA-256 chosen
over MD5 when both are offered, and the challenge is kept, so the
requests after it carry Digest with a rising nc and Basic is not sent to
that server again. A PUT's body is made again from GetBody. A Basic
server is untouched.

Run: go test ./internal/davsync under mote-run, ok; vet darwin, linux
nogui and windows on the package. New: TestDAVDigest (MD5, SHA-256 against
a fake Digest-only server that checks response, nc replay, opaque, uri and
the PUT body), TestDAVDigestWrongPassword (still reads as a wrong
password; a Basic server is left alone).
Broken by hand: newDAV without the transport, TestDAVDigest red with
"refused the user name or password (HTTP 401)".
Not tried: a real Digest server (only the fake, built from the RFC's
formula); auth-int and SHA-512-256 are not answered. The first request of
a connection still carries Basic, as before, so a Digest-only server over
plain http sees the password once; nothing else was changed in that path.
2026-11-13 10:23:47 +08:00

124 lines
3.8 KiB
Go

package davsync
import (
"context"
"crypto/md5"
"crypto/sha256"
"encoding/hex"
"hash"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
)
// fakeDigest is a WebDAV server that takes Digest only (RFC 7616, qop=auth)
// and answers a Basic header with a Digest challenge, as Apache's
// mod_auth_digest and nginx's auth modules do.
type fakeDigest struct {
mu sync.Mutex
alg string
newHash func() hash.Hash
basic int // requests that came with Basic
digested int // requests that passed
seenNC map[string]bool
}
func (f *fakeDigest) h(s string) string {
x := f.newHash()
x.Write([]byte(s))
return hex.EncodeToString(x.Sum(nil))
}
func (f *fakeDigest) ServeHTTP(w http.ResponseWriter, r *http.Request) {
f.mu.Lock()
defer f.mu.Unlock()
auth := r.Header.Get("Authorization")
if strings.HasPrefix(auth, "Basic ") {
f.basic++
}
if strings.HasPrefix(auth, "Digest ") {
p := authParams(auth[7:])
ha1 := f.h("me:fixture realm:pw")
ha2 := f.h(r.Method + ":" + r.URL.RequestURI())
want := f.h(ha1 + ":fixturenonce:" + p["nc"] + ":" + p["cnonce"] + ":auth:" + ha2)
if p["response"] == want && p["username"] == "me" && p["uri"] == r.URL.RequestURI() &&
p["qop"] == "auth" && p["algorithm"] == f.alg && p["opaque"] == "op" && !f.seenNC[p["nc"]] {
f.seenNC[p["nc"]] = true
f.digested++
body := make([]byte, 16)
n, _ := r.Body.Read(body)
if r.Method == http.MethodPut && string(body[:n]) != "payload" {
w.WriteHeader(http.StatusBadRequest) // the body must survive the second send
return
}
w.WriteHeader(http.StatusNotFound)
return
}
}
w.Header().Add("WWW-Authenticate", `Digest realm="fixture realm", nonce="fixturenonce", opaque="op", algorithm=`+f.alg+`, qop="auth"`)
w.WriteHeader(http.StatusUnauthorized)
}
// A server that takes Digest only is signed in to with the same user name
// and password, in MD5 and in SHA-256, and a PUT's body is sent again.
func TestDAVDigest(t *testing.T) {
for _, c := range []struct {
alg string
h func() hash.Hash
}{{"MD5", md5.New}, {"SHA-256", sha256.New}} {
t.Run(c.alg, func(t *testing.T) {
f := &fakeDigest{alg: c.alg, newHash: c.h, seenNC: map[string]bool{}}
srv := httptest.NewServer(f)
defer srv.Close()
d, err := newDAV(Config{URL: srv.URL, User: "me", Password: "pw"})
if err != nil {
t.Fatal(err)
}
ctx := context.Background()
if _, _, err := d.get(ctx, version{}); err != nil {
t.Fatalf("get: %v", err)
}
res, err := d.send(ctx, http.MethodPut, d.url("a"), []byte("payload"), nil)
if err != nil {
t.Fatalf("put: %v", err)
}
res.Body.Close()
if res.StatusCode == http.StatusBadRequest {
t.Fatalf("the PUT's body was lost on the second send")
}
if f.basic != 1 || f.digested < 2 {
t.Errorf("basic requests %d (want 1, the first), digest passes %d (want >= 2)", f.basic, f.digested)
}
})
}
}
// A wrong password still reads as one, and a server asking for Basic is
// left alone.
func TestDAVDigestWrongPassword(t *testing.T) {
f := &fakeDigest{alg: "MD5", newHash: md5.New, seenNC: map[string]bool{}}
srv := httptest.NewServer(f)
defer srv.Close()
d, _ := newDAV(Config{URL: srv.URL, User: "me", Password: "bad"})
if _, _, err := d.get(context.Background(), version{}); err != errLogin {
t.Errorf("err = %v, want errLogin", err)
}
var basic int
b := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if u, p, ok := r.BasicAuth(); ok && u == "me" && p == "pw" {
basic++
w.WriteHeader(http.StatusNotFound)
return
}
w.Header().Set("WWW-Authenticate", `Basic realm="x"`)
w.WriteHeader(http.StatusUnauthorized)
}))
defer b.Close()
d, _ = newDAV(Config{URL: b.URL, User: "me", Password: "pw"})
if _, _, err := d.get(context.Background(), version{}); err != nil || basic == 0 {
t.Errorf("basic server: err %v, passes %d", err, basic)
}
}