webdav: optional HTTP Basic Auth from [webdav.basic_auth]

WebDAV reaches the filer over gRPC, so neither the filer JWT nor the
filer's Basic Auth covers it, and it had no authentication of its own.
With a password set in [webdav.basic_auth], every request needs it.
Without one, startup warns when the gateway listens beyond loopback.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Chris LuandClaude Opus 5.5 committed 2026-11-24 11:41:04 -08:00
1 parent 93e7a81b01
commit 495c40c6aa
4 files changed
+95 -10

No files matched your search

+8
View File
@@ -74,6 +74,14 @@ enabled = true
user = ""
password = ""
# HTTP Basic Auth on the WebDAV gateway (weed webdav, weed server -webdav,
# weed mini). Without a password, anyone who can reach the WebDAV port reads,
# writes and deletes everything under its -filer.path.
# Env: WEED_WEBDAV_BASIC_AUTH_USER, WEED_WEBDAV_BASIC_AUTH_PASSWORD
[webdav.basic_auth]
user = ""
password = ""
# this jwt signing key is used for read operations on volume servers:
# - the Master server generates the JWT, which can be used to read a certain file on a volume server
# - every process that reads chunks straight from volume servers generates its
+13
View File
@@ -93,6 +93,11 @@ func (wo *WebDavOption) startWebDav() bool {
util.SetOutboundLocalIP(*wo.ipBind)
basicAuth := webDavBasicAuth()
if basicAuth == nil && !isLoopbackIp(*wo.ipBind) {
glog.Warningf("WebDAV has no authentication: anyone who can reach %s:%d can read, write and delete under %s. Set [webdav.basic_auth] in security.toml.", *wo.ipBind, *wo.port, *wo.filerRootPath)
}
// detect current user
uid, gid := uint32(0), uint32(0)
if u, err := user.Current(); err == nil {
@@ -142,6 +147,7 @@ func (wo *WebDavOption) startWebDav() bool {
CacheDir: *wo.cacheDir,
CacheSizeMB: *wo.cacheSizeMB,
MaxMB: *wo.maxMB,
BasicAuth: basicAuth,
})
if webdavServer_err != nil {
glog.Fatalf("WebDav Server startup error: %v", webdavServer_err)
@@ -185,3 +191,10 @@ func (wo *WebDavOption) startWebDav() bool {
return true
}
// webDavBasicAuth reads [webdav.basic_auth] from security.toml and the WEED_
// environment; nil means WebDAV is open.
func webDavBasicAuth() *security.BasicAuth {
v := util.GetViper()
return security.NewBasicAuth(v.GetString("webdav.basic_auth.user"), v.GetString("webdav.basic_auth.password"))
}
+30 -10
View File
@@ -44,6 +44,7 @@ type WebDavOption struct {
CacheDir string
CacheSizeMB int64
MaxMB int
BasicAuth *security.BasicAuth
}
type WebDavServer struct {
@@ -72,23 +73,42 @@ func NewWebDavServer(option *WebDavOption) (ws *WebDavServer, err error) {
fs = NewWrappedFs(fs, path.Clean(option.FilerRootPath))
}
ws = &WebDavServer{
option: option,
grpcDialOption: security.LoadClientTLS(util.GetViper(), "grpc.filer"),
Handler: listedEntriesHandler{
next: copyMoveGuardHandler{
fs: fs,
next: &webdav.Handler{
FileSystem: fs,
LockSystem: webdav.NewMemLS(),
},
var handler http.Handler = listedEntriesHandler{
next: copyMoveGuardHandler{
fs: fs,
next: &webdav.Handler{
FileSystem: fs,
LockSystem: webdav.NewMemLS(),
},
},
}
if option.BasicAuth != nil {
handler = basicAuthHandler{auth: option.BasicAuth, next: handler}
}
ws = &WebDavServer{
option: option,
grpcDialOption: security.LoadClientTLS(util.GetViper(), "grpc.filer"),
Handler: handler,
}
return ws, nil
}
type basicAuthHandler struct {
auth *security.BasicAuth
next http.Handler
}
func (h basicAuthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if !h.auth.Allows(r) {
w.Header().Set("WWW-Authenticate", `Basic realm="SeaweedFS WebDAV", charset="UTF-8"`)
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
h.next.ServeHTTP(w, r)
}
// putOutcomeKey carries the flag WebDavFileSystem.OpenFile sets when a PUT
// replaced an existing entry, so the PUT can answer 204 instead of 201
type putOutcomeKey struct{}
+44
View File
@@ -18,6 +18,7 @@ import (
"github.com/seaweedfs/seaweedfs/weed/pb"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/security"
"github.com/seaweedfs/seaweedfs/weed/util"
"github.com/seaweedfs/seaweedfs/weed/wdclient"
"github.com/stretchr/testify/assert"
@@ -994,3 +995,46 @@ func TestWebDavFileSystem_Rename_MissingDestParent(t *testing.T) {
assert.NoError(t, fs.Rename(context.Background(), "/dir/file.txt", "/dir/moved.txt"))
assert.NotNil(t, filer.req)
}
func TestWebDavBasicAuth(t *testing.T) {
ws, err := NewWebDavServer(&WebDavOption{Filer: "127.0.0.1:1", CacheDir: t.TempDir(), BasicAuth: security.NewBasicAuth("alice", "secret")})
if err != nil {
t.Fatal(err)
}
auth, ok := ws.Handler.(basicAuthHandler)
if !ok {
t.Fatalf("handler %T does not check Basic Auth", ws.Handler)
}
reached := 0
auth.next = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { reached++ })
for _, creds := range [][2]string{{}, {"alice", "wrong"}, {"bob", "secret"}} {
req := httptest.NewRequest("PROPFIND", "/buckets/", nil)
if creds[0] != "" {
req.SetBasicAuth(creds[0], creds[1])
}
rec := httptest.NewRecorder()
auth.ServeHTTP(rec, req)
if rec.Code != http.StatusUnauthorized || rec.Header().Get("WWW-Authenticate") == "" {
t.Fatalf("credentials %q: got %d %q, want 401 with a Basic challenge", creds, rec.Code, rec.Header().Get("WWW-Authenticate"))
}
}
if reached != 0 {
t.Fatalf("an unauthenticated request reached WebDAV")
}
req := httptest.NewRequest("PROPFIND", "/buckets/", nil)
req.SetBasicAuth("alice", "secret")
auth.ServeHTTP(httptest.NewRecorder(), req)
if reached != 1 {
t.Fatalf("valid credentials did not reach WebDAV")
}
open, err := NewWebDavServer(&WebDavOption{Filer: "127.0.0.1:1", CacheDir: t.TempDir()})
if err != nil {
t.Fatal(err)
}
if _, ok := open.Handler.(basicAuthHandler); ok {
t.Fatalf("Basic Auth is on without a password")
}
}