fix(server): 不存在的账号返回 401 而非 404,避免泄漏账号存在性

未知账号与未认证/停用账号统一返回 401 + WWW-Authenticate。
新增 account_test.go 验证带/不带凭据均为 401。
This commit is contained in:
Yunling Zhang committed 2026-09-28 17:38:16 +08:00
1 parent 068ccf7560
commit 5417c747aa
2 files changed
+28

No files matched your search

+25
View File
@@ -0,0 +1,25 @@
package server
import (
"net/http"
"testing"
)
func TestDAVUnknownAccountChallenge(t *testing.T) {
s, _, _ := newTestServer(t)
// 未带任何凭据的未知账号,与“账号存在但未认证”一致:401 + WWW-Authenticate。
rec := request(s, "PROPFIND", "/ghost/", "", "", nil, map[string]string{"Depth": "0"})
if rec.Code != http.StatusUnauthorized {
t.Fatalf("未知账号应为 401,实际 %d", rec.Code)
}
if got := rec.Header().Get("WWW-Authenticate"); got != `Basic realm="davbox"` {
t.Fatalf("未知账号应带 WWW-Authenticate,实际 %q", got)
}
// 带任意凭据结果一样,不泄漏账号是否存在。
rec = request(s, "PROPFIND", "/ghost/", "ghost", "whatever", nil, map[string]string{"Depth": "0"})
if rec.Code != http.StatusUnauthorized {
t.Fatalf("未知账号(带凭据)应为 401,实际 %d", rec.Code)
}
}
+3
View File
@@ -75,6 +75,9 @@ func (s *Server) route(w http.ResponseWriter, r *http.Request) {
s.handleDAV(w, r, seg)
return
}
// 账号不存在时返回与未认证一致的 401,避免泄漏账号是否存在。
writeAuthChallenge(w)
return
}
writeError(w, http.StatusNotFound, "未找到")
}