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

webdav: fix headers option leaking to other hosts on redirect GHSA-3rqf-7h69-gg5v CVE-PENDING

Nick Craig-Wood224a515d6894
diff --git a/backend/webdav/webdav.go b/backend/webdav/webdav.go
index 3310887e2..41cd7bb20 100644
--- a/backend/webdav/webdav.go
+++ b/backend/webdav/webdav.go
@@ -135,6 +135,13 @@ The input format is comma separated list of key,value pairs. Standard
For example, to set a Cookie use 'Cookie,name=value', or '"Cookie","name=value"'.
You can set multiple headers, e.g. '"Cookie","name=value","Authorization","xxx"'.
+
+The headers are only sent to the host in the configured URL. If the
+server redirects to another host (including a subdomain or a different
+port) the headers are not sent to it, or to any further hop in that
+redirect chain. The exception is the directory listing and file
+download requests when auth_redirect is set, as that keeps all
+credentials across redirects.
`,
Default: fs.CommaSepList{},
Advanced: true,
@@ -235,6 +242,8 @@ type Fs struct {
canChunk bool // set if nextcloud and nextcloud_chunk_size is set
canRecalcHash bool // set if the server can recalculate checksums with PATCH (nextcloud)
authSingleflight *singleflight.Group
+ // redirect policy which keeps opt.Headers on the configured host
+ checkRedirect func(req *http.Request, via []*http.Request) error
}
// Object describes a webdav object
@@ -389,7 +398,7 @@ func (f *Fs) readMetaDataForPath(ctx context.Context, path string) (info *api.Pr
ExtraHeaders: map[string]string{
"Depth": "0",
},
- CheckRedirect: rest.PreserveMethodRedirectFn,
+ CheckRedirect: f.preserveMethodRedirect,
}
if f.hasOCMD5 || f.hasOCSHA1 {
opts.Body = bytes.NewBuffer(owncloudProps)
@@ -555,8 +564,17 @@ func NewFs(ctx context.Context, name, root string, m configmap.Mapper) (fs.Fs, e
rt: ntlmssp.Negotiator{RoundTripper: t},
}
}
- // Refuse redirects that downgrade HTTPS to plaintext HTTP.
- client.CheckRedirect = rest.RefuseHTTPSDowngradeRedirectFn
+ // Refuse redirects that downgrade HTTPS to plaintext HTTP and
+ // don't send the configured headers to another host.
+ //
+ // The auth_redirect option bypasses this for the requests it applies
+ // to as it is an explicit opt-in to keep credentials across redirects.
+ headerNames := make([]string, 0, len(opt.Headers)/2)
+ for i := 0; i < len(opt.Headers); i += 2 {
+ headerNames = append(headerNames, opt.Headers[i])
+ }
+ f.checkRedirect = rest.StripHeadersOnCrossHostRedirectFn(headerNames...)
+ client.CheckRedirect = f.checkRedirect
f.srv = rest.NewClient(client).SetRoot(u.String())
f.features = (&fs.Features{
@@ -635,6 +653,16 @@ func (f *Fs) fetchBearerToken(cmd fs.SpaceSepList) (string, error) {
return stdoutString, nil
}
+// preserveMethodRedirect is an http.Client.CheckRedirect function
+// which applies f.checkRedirect and then preserves the original HTTP
+// method like rest.PreserveMethodRedirectFn.
+func (f *Fs) preserveMethodRedirect(req *http.Request, via []*http.Request) error {
+ if err := f.checkRedirect(req, via); err != nil {
+ return err
+ }
+ return rest.PreserveMethodRedirectFn(req, via)
+}
+
// Adds the configured headers to the request if any
func (f *Fs) addHeaders(headers fs.CommaSepList) {
for i := 0; i < len(headers); i += 2 {
diff --git a/backend/webdav/webdav_internal_test.go b/backend/webdav/webdav_internal_test.go
index 87f223120..84af85266 100644
--- a/backend/webdav/webdav_internal_test.go
+++ b/backend/webdav/webdav_internal_test.go
@@ -8,6 +8,7 @@ import (
"net/http"
"net/http/httptest"
"strings"
+ "sync"
"sync/atomic"
"testing"
"time"
@@ -208,6 +209,100 @@ func TestListAllAuthRedirect(t *testing.T) {
assert.NotEmpty(t, targetAuth, "Authorization header should be preserved across redirect")
}
+// redirectTestHeaders are the headers option for the redirect tests.
+// The one starting with "*" is sent without canonicalising.
+var redirectTestHeaders = []string{"X-Potato", "sausage", "*x-marrow", "pea"}
+
+// redirectHeaders makes a webdav remote with redirectTestHeaders
+// pointing at a server which redirects every request to a second
+// server on hostname, or back to itself if hostname is empty, and
+// exercises each of the backend's redirect policies against it: the
+// PROPFIND used to look up an object, the GET used to read it and the
+// PROPFIND used to list a directory. It returns the headers the
+// redirect target saw on each of those requests.
+func redirectHeaders(t *testing.T, hostname string) []http.Header {
+ var target string
+ var mu sync.Mutex
+ var seen []http.Header
+ handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if !strings.HasPrefix(r.URL.Path, "/moved/") {
+ http.Redirect(w, r, target+"/moved"+r.URL.Path, http.StatusTemporaryRedirect)
+ return
+ }
+ mu.Lock()
+ seen = append(seen, r.Header.Clone())
+ mu.Unlock()
+ switch r.Method {
+ case "PROPFIND":
+ w.WriteHeader(http.StatusMultiStatus)
+ _, err := fmt.Fprint(w, fileInfoResponse)
+ assert.NoError(t, err)
+ case http.MethodGet:
+ _, err := fmt.Fprint(w, "hello")
+ assert.NoError(t, err)
+ default:
+ http.Error(w, "unexpected method", http.StatusMethodNotAllowed)
+ }
+ })
+ ts := httptest.NewServer(handler)
+ defer ts.Close()
+ if hostname == "" {
+ target = ts.URL
+ } else {
+ // Serve the final response from a second server on a different host
+ other := httptest.NewServer(handler)
+ defer other.Close()
+ target = strings.Replace(other.URL, "127.0.0.1", hostname, 1)
+ }
+
+ configfile.Install()
+ m := configmap.Simple{
+ "type": "webdav",
+ "url": ts.URL,
+ "headers": strings.Join(redirectTestHeaders, ","),
+ }
+ ctx := context.Background()
+ f, err := webdav.NewFs(ctx, remoteName, "", m)
+ require.NoError(t, err)
+
+ // PROPFIND with Depth 0 uses rest.PreserveMethodRedirectFn
+ o, err := f.NewObject(ctx, "file.txt")
+ require.NoError(t, err)
+ // GET uses the client's default redirect policy
+ fd, err := o.Open(ctx)
+ require.NoError(t, err)
+ data, err := io.ReadAll(fd)
+ require.NoError(t, err)
+ require.NoError(t, fd.Close())
+ assert.Equal(t, "hello", string(data))
+ // PROPFIND with Depth 1 uses the client's default redirect policy
+ _, err = f.List(ctx, "")
+ require.NoError(t, err)
+
+ mu.Lock()
+ defer mu.Unlock()
+ require.Len(t, seen, 3, "expected the redirect target to see all three requests")
+ return seen
+}
+
+func TestRedirectKeepsHeadersOnSameHost(t *testing.T) {
+ for _, got := range redirectHeaders(t, "") {
+ for i := 0; i < len(redirectTestHeaders); i += 2 {
+ assert.Equal(t, redirectTestHeaders[i+1], got.Get(strings.TrimPrefix(redirectTestHeaders[i], "*")))
+ }
+ }
+}
+
+func TestRedirectStripsHeadersOnHostChange(t *testing.T) {
+ // Redirect to a different host name and port. net/http strips
+ // Authorization on its own here so this checks the custom headers.
+ for _, got := range redirectHeaders(t, "localhost") {
+ for i := 0; i < len(redirectTestHeaders); i += 2 {
+ assert.Empty(t, got.Values(strings.TrimPrefix(redirectTestHeaders[i], "*")), "header %q leaked to redirect target", redirectTestHeaders[i])
+ }
+ }
+}
+
// TestReservedCharactersInPathAreEscaped verifies that reserved characters
// like semicolons and equals signs in file paths are percent-encoded in
// HTTP requests to the WebDAV server (RFC 3986 compliance).