"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files
Go azure-blobazure-blob-storageazure-filesbackblaze-b2cloud-storagedropboxencryptionftpfuse-filesystemgogolanggoogle-cloud-storagegoogle-driveonedriveopenstack-swiftrclones3sftpsyncwebdav
seafile: fix HTTPS to HTTP redirects leaking credentials GHSA-m97j-7x65-wh93 CVE-PENDING
Nick Craig-Wood0c6e76b6b35e
diff --git a/backend/seafile/seafile.go b/backend/seafile/seafile.goindex 16e4ab4dd..26bcd25a4 100644--- a/backend/seafile/seafile.go+++ b/backend/seafile/seafile.go@@ -206,7 +206,7 @@ func NewFs(ctx context.Context, name, root string, m configmap.Mapper) (fs.Fs, eopt: *opt,endpoint: u,endpointURL: u.String(),- srv: rest.NewClient(fshttp.NewClient(ctx)).SetRoot(u.String()),+ srv: rest.NewClient(newClient(ctx)).SetRoot(u.String()),pacer: getPacer(ctx, opt.URL),}f.features = (&fs.Features{@@ -373,7 +373,7 @@ func Config(ctx context.Context, name string, m configmap.Mapper, config fs.Confif !strings.HasPrefix(url, "/") {url += "/"}- srv := rest.NewClient(fshttp.NewClient(ctx)).SetRoot(url)+ srv := rest.NewClient(newClient(ctx)).SetRoot(url)// We loop asking for a 2FA codectx, cancel := context.WithTimeout(context.Background(), 10*time.Second)@@ -401,6 +401,17 @@ func Config(ctx context.Context, name string, m configmap.Mapper, config fs.Confreturn nil, fmt.Errorf("unknown state %q", config.State)}+// newClient returns the HTTP client used to talk to the Seafile API.+//+// It refuses redirects which downgrade HTTPS to plaintext HTTP as+// following one would send the authorization token, or the password+// during configuration, in cleartext.+func newClient(ctx context.Context) *http.Client {+ client := fshttp.NewClient(ctx)+ client.CheckRedirect = rest.RefuseHTTPSDowngradeRedirectFn+ return client+}+// Shutdown the Fsfunc (f *Fs) Shutdown(ctx context.Context) error {if f.renew == nil {diff --git a/backend/seafile/seafile_internal_test.go b/backend/seafile/seafile_internal_test.goindex 1077d4ede..51c0bd7de 100644--- a/backend/seafile/seafile_internal_test.go+++ b/backend/seafile/seafile_internal_test.go@@ -6,6 +6,8 @@ import ("net/http""net/http/httptest""path"+ "strings"+ "sync/atomic""testing""time"@@ -289,3 +291,85 @@ func TestUploadErrorReleasesSource(t *testing.T) {t.Fatal("source still in use after upload returned")}}++// testPassword is the password the downgrade tests look for on the+// plaintext hop. It is distinctive so that it can't match the JSON+// field names of the authentication request.+const testPassword = "s3cret-pass-4242"++// newDowngradeServers returns an HTTPS server which answers the server+// info request itself and redirects every other request to a plaintext+// HTTP server on the same machine. The returned flags record whether+// the plaintext server ever received an Authorization header or+// testPassword in a request body.+func newDowngradeServers(t *testing.T) (tlsSrv *httptest.Server, sawAuth, sawPassword *atomic.Bool) {+ t.Helper()+ sawAuth, sawPassword = new(atomic.Bool), new(atomic.Bool)++ plain := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ if r.Header.Get("Authorization") != "" {+ sawAuth.Store(true)+ }+ body, _ := io.ReadAll(r.Body)+ if strings.Contains(string(body), testPassword) {+ sawPassword.Store(true)+ }+ w.Header().Set("Content-Type", "application/json")+ _, _ = w.Write([]byte("[]"))+ }))+ t.Cleanup(plain.Close)++ tlsSrv = httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ if r.URL.Path == "/api2/server-info/" {+ w.Header().Set("Content-Type", "application/json")+ _, _ = w.Write([]byte(`{"version":"9.0.0"}`))+ return+ }+ http.Redirect(w, r, plain.URL+r.URL.Path, http.StatusTemporaryRedirect)+ }))+ t.Cleanup(tlsSrv.Close)++ return tlsSrv, sawAuth, sawPassword+}++// TestRefuseHTTPSDowngradeRedirect checks that neither the+// authorization token nor the password is sent over plaintext HTTP+// when the server redirects an HTTPS request to an http:// URL.+func TestRefuseHTTPSDowngradeRedirect(t *testing.T) {+ ctx, ci := fs.AddConfig(context.Background())+ ci.InsecureSkipVerify = true // trust the httptest certificate++ // Every API call made through the Fs carries the token.+ t.Run("Token", func(t *testing.T) {+ tlsSrv, sawAuth, _ := newDowngradeServers(t)+ m := configmap.Simple{"url": tlsSrv.URL, "user": "username", "auth_token": "secret"}+ fsys, err := NewFs(ctx, "test", "", m)+ require.NoError(t, err)+ f := fsys.(*Fs)+ _, err = f.getLibraries(ctx)+ assert.False(t, sawAuth.Load(), "authorization token was sent over plaintext HTTP")+ assert.ErrorIs(t, err, rest.ErrHTTPSDowngrade)+ })++ // Without a token NewFs posts the user's password to get one.+ t.Run("Login", func(t *testing.T) {+ tlsSrv, _, sawPassword := newDowngradeServers(t)+ m := configmap.Simple{"url": tlsSrv.URL, "user": "username", "pass": obscure.MustObscure(testPassword)}+ _, err := NewFs(ctx, "test", "", m)+ assert.False(t, sawPassword.Load(), "password was sent over plaintext HTTP")+ assert.ErrorIs(t, err, rest.ErrHTTPSDowngrade)+ })++ // The 2FA config flow posts the user's password to get a token.+ t.Run("Password", func(t *testing.T) {+ tlsSrv, _, sawPassword := newDowngradeServers(t)+ m := configmap.Simple{"url": tlsSrv.URL, "2fa": "true", "user": "username", "pass": obscure.MustObscure(testPassword)}+ out, err := Config(ctx, "test", m, fs.ConfigIn{State: "2fa_do", Result: "123456"})+ require.NoError(t, err)+ require.NotNil(t, out)+ assert.False(t, sawPassword.Load(), "password was sent over plaintext HTTP")+ assert.Equal(t, "2fa_error", out.State)+ require.NotNil(t, out.Option)+ assert.Contains(t, out.Option.Help, rest.ErrHTTPSDowngrade.Error())+ })+}