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

serve webdav: fix path traversal above the served directory GHSA-44pm-2q5p-fm63 CVE-PENDING

Nick Craig-Woodc266a63ced8a
diff --git a/cmd/serve/webdav/webdav.go b/cmd/serve/webdav/webdav.go
index 32db09bb5..d866373ba 100644
--- a/cmd/serve/webdav/webdav.go
+++ b/cmd/serve/webdav/webdav.go
@@ -8,6 +8,7 @@ import (
"mime"
"net"
"net/http"
+ "net/url"
"os"
"path"
"strconv"
@@ -398,6 +399,24 @@ func (w *WebDAV) postprocess(r *http.Request, remote string) {
func (w *WebDAV) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
urlPath := r.URL.Path
+ err := checkPath(urlPath)
+ // COPY and MOVE also take a path from the Destination header.
+ //
+ // The webdav handler strips the BaseURL from it as a string
+ // prefix, turning "/base../x" into "../x", so check what is
+ // left after doing the same.
+ if err == nil && (r.Method == "COPY" || r.Method == "MOVE") {
+ var u *url.URL
+ u, err = url.Parse(r.Header.Get("Destination"))
+ if err == nil {
+ err = checkPath(strings.TrimPrefix(u.Path, w.opt.HTTP.BaseURL))
+ }
+ }
+ if err != nil {
+ fs.Infof(urlPath, "%s from %s rejected: %v", r.Method, r.RemoteAddr, err)
+ http.Error(rw, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
+ return
+ }
isDir := strings.HasSuffix(urlPath, "/")
remote := strings.Trim(urlPath, "/")
if !w.opt.DisableDirList && (r.Method == "GET" || r.Method == "HEAD") && isDir {
@@ -515,6 +534,23 @@ func (w *WebDAV) logRequest(r *http.Request, err error) {
fs.Infof(r.URL.Path, "%s from %s", r.Method, r.RemoteAddr)
}
+// checkPath returns an error if the client supplied path name has a
+// "." or ".." element. The VFS joins the path onto the Fs root, so
+// such an element would otherwise address objects outside the
+// directory served.
+//
+// The elements are checked individually rather than the path compared
+// with path.Clean because path.Clean can't resolve a leading ".." in
+// a relative path and leaves it in place.
+func checkPath(name string) error {
+ for elem := range strings.SplitSeq(name, "/") {
+ if elem == "." || elem == ".." {
+ return fmt.Errorf("path %q has a %q element", name, elem)
+ }
+ }
+ return nil
+}
+
// Mkdir creates a directory
func (w *WebDAV) Mkdir(ctx context.Context, name string, perm os.FileMode) (err error) {
// defer log.Trace(name, "perm=%v", perm)("err = %v", &err)
diff --git a/cmd/serve/webdav/webdav_test.go b/cmd/serve/webdav/webdav_test.go
index b0a3310f1..8b10c9890 100644
--- a/cmd/serve/webdav/webdav_test.go
+++ b/cmd/serve/webdav/webdav_test.go
@@ -13,19 +13,23 @@ import (
"flag"
"io"
"net/http"
+ "net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
_ "github.com/rclone/rclone/backend/local"
+ _ "github.com/rclone/rclone/backend/memory"
"github.com/rclone/rclone/cmd/serve/proxy"
"github.com/rclone/rclone/cmd/serve/servetest"
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/config/configmap"
"github.com/rclone/rclone/fs/config/obscure"
"github.com/rclone/rclone/fs/filter"
+ "github.com/rclone/rclone/fs/operations"
"github.com/rclone/rclone/fs/rc"
+ "github.com/rclone/rclone/fstest"
"github.com/rclone/rclone/lib/random"
"github.com/rclone/rclone/vfs/vfscommon"
"github.com/stretchr/testify/assert"
@@ -560,3 +564,162 @@ func TestAuthProxyDownloadOutlivesCache(t *testing.T) {
require.NoError(t, err)
assert.True(t, contents == string(start)+string(rest), "download corrupted")
}
+
+// TestWebDavPathTraversal checks that request paths with "." or ".."
+// components are rejected with 400 before they reach the VFS, so they
+// can't be joined with the Fs root to reach objects outside the
+// directory served.
+//
+// It is run with and without a BaseURL as the webdav handler strips
+// the BaseURL from the Destination header itself.
+func TestWebDavPathTraversal(t *testing.T) {
+ for _, baseURL := range []string{"", "/base"} {
+ t.Run("BaseURL="+baseURL, func(t *testing.T) {
+ testPathTraversal(t, baseURL)
+ })
+ }
+}
+
+// testPathTraversal runs the path traversal checks against a server
+// with the BaseURL given.
+//
+// The memory backend is used because it resolves ".." by joining paths,
+// unlike the local backend which encodes the components as file names.
+func testPathTraversal(t *testing.T, baseURL string) {
+ ctx := context.Background()
+ bucket := ":memory:webdav-traversal-test" + strings.ReplaceAll(baseURL, "/", "-")
+ modTime := fstest.Time("2001-02-03T04:05:06.499999999Z")
+
+ // outside is the parent of the directory served and holds an object
+ // which must stay unreachable through the server.
+ outside, err := fs.NewFs(ctx, bucket)
+ require.NoError(t, err)
+ _, err = operations.Rcat(ctx, outside, "outside-secret.txt", io.NopCloser(strings.NewReader("SECRET")), modTime, nil)
+ require.NoError(t, err)
+
+ f, err := fs.NewFs(ctx, bucket+"/served-root")
+ require.NoError(t, err)
+ _, err = operations.Rcat(ctx, f, "inside.txt", io.NopCloser(strings.NewReader("INSIDE")), modTime, nil)
+ require.NoError(t, err)
+ _, err = operations.Rcat(ctx, f, "dir/inside-dir.txt", io.NopCloser(strings.NewReader("INSIDE DIR")), modTime, nil)
+ require.NoError(t, err)
+
+ opt := Opt
+ opt.HTTP.ListenAddr = []string{"localhost:0"}
+ opt.HTTP.BaseURL = baseURL
+ w, err := newWebDAV(ctx, f, &opt, &vfscommon.Opt, &proxy.Opt)
+ require.NoError(t, err)
+ // The requests are made directly to the router, but the server
+ // must be serving for Shutdown to close its listener.
+ w.server.Serve()
+ defer func() {
+ assert.NoError(t, w.Shutdown())
+ }()
+ router := w.server.Router()
+
+ // do makes a request for urlPath, and for COPY and MOVE with the
+ // Destination dest, both of which have the baseURL added.
+ do := func(method, urlPath, dest string) *httptest.ResponseRecorder {
+ body := ""
+ switch method {
+ case "PUT":
+ body = "EVIL"
+ case "LOCK":
+ body = `<?xml version="1.0" encoding="utf-8"?><D:lockinfo xmlns:D="DAV:"><D:lockscope><D:exclusive/></D:lockscope><D:locktype><D:write/></D:locktype></D:lockinfo>`
+ }
+ req := httptest.NewRequest(method, baseURL+urlPath, strings.NewReader(body))
+ switch method {
+ case "COPY", "MOVE":
+ req.Header.Set("Destination", baseURL+dest)
+ case "LOCK":
+ req.Header.Set("Depth", "0")
+ case "PROPFIND":
+ req.Header.Set("Depth", "1")
+ }
+ rec := httptest.NewRecorder()
+ router.ServeHTTP(rec, req)
+ return rec
+ }
+
+ // A path inside the served root is unaffected
+ rec := do("GET", "/inside.txt", "")
+ assert.Equal(t, http.StatusOK, rec.Code)
+ assert.Equal(t, "INSIDE", rec.Body.String())
+
+ // The root itself can be made
+ rec = do("MKCOL", "/", "")
+ assert.Equal(t, http.StatusCreated, rec.Code)
+
+ type request struct {
+ method string
+ urlPath string
+ dest string
+ }
+ // Every request naming a path with a "." or ".." element must fail.
+ //
+ // The MKCOL requests come first so that, should one succeed, the
+ // later requests are routed through the directory node it made.
+ requests := []request{
+ {"MKCOL", "/..", ""},
+ {"MKCOL", "/../", ""},
+ {"MKCOL", "/../new-dir", ""},
+ {"MKCOL", "/a/../../new-dir", ""},
+ {"MKCOL", "/.", ""},
+ {"MKCOL", "/./new-dir", ""},
+ {"PUT", "/../outside-write.txt", ""},
+ {"PUT", "/../outside-secret.txt", ""},
+ {"PUT", "/%2e%2e/outside-write.txt", ""},
+ {"PUT", "/a/../../outside-write.txt", ""},
+ {"LOCK", "/../outside-lock.txt", ""},
+ {"COPY", "/inside.txt", "/../outside-copy.txt"},
+ {"COPY", "/inside.txt", "/../outside-secret.txt"},
+ {"COPY", "/inside.txt", "/%2e%2e/outside-copy.txt"},
+ {"MOVE", "/inside.txt", "/../outside-move.txt"},
+ {"MOVE", "/inside.txt", "/a/../../outside-move.txt"},
+ {"MKCOL", "/../outside-dir", ""},
+ {"MOVE", "/../outside-dir", "/stolen-dir"},
+ {"COPY", "/../outside-dir", "/copied-dir"},
+ {"MOVE", "/../outside-secret.txt", "/stolen.txt"},
+ {"COPY", "/../outside-secret.txt", "/copied.txt"},
+ {"PROPPATCH", "/../outside-secret.txt", ""},
+ {"PROPFIND", "/..", ""},
+ {"GET", "/../outside-secret.txt", ""},
+ {"GET", "/../", ""},
+ {"GET", "/../?download=zip", ""},
+ {"HEAD", "/../", ""},
+ {"DELETE", "/../outside-secret.txt", ""},
+ {"DELETE", "/..", ""},
+ }
+ if baseURL != "" {
+ // Stripping the BaseURL as a string prefix from these
+ // destinations leaves a path starting with a ".." element.
+ //
+ // The directory COPY comes first so that, should it succeed,
+ // the later requests are routed through the node it made.
+ requests = append([]request{
+ {"COPY", "/dir", ".."},
+ {"COPY", "/inside.txt", "../outside-copy.txt"},
+ {"COPY", "/inside.txt", "../outside-secret.txt"},
+ {"COPY", "/inside.txt", "%2e%2e/outside-copy.txt"},
+ {"MOVE", "/inside.txt", "../outside-move.txt"},
+ }, requests...)
+ }
+ for _, test := range requests {
+ name := test.method + " " + test.urlPath
+ if test.dest != "" {
+ name += " to " + test.dest
+ }
+ t.Run(name, func(t *testing.T) {
+ rec := do(test.method, test.urlPath, test.dest)
+ assert.Equal(t, http.StatusBadRequest, rec.Code)
+ })
+ }
+
+ // Nothing was created, modified, moved or removed, either outside
+ // the served root or inside it.
+ fstest.CheckListingWithPrecision(t, outside, []fstest.Item{
+ fstest.NewItem("outside-secret.txt", "SECRET", modTime),
+ fstest.NewItem("served-root/inside.txt", "INSIDE", modTime),
+ fstest.NewItem("served-root/dir/inside-dir.txt", "INSIDE DIR", modTime),
+ }, []string{"served-root", "served-root/dir"}, fs.GetModifyWindow(ctx, outside))
+}