"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
serve: make auth proxy revocation take effect within 5 minutes GHSA-vmgx-g9c5-669p CVE-PENDING
Nick Craig-Wood9aba8e4e4190
diff --git a/cmd/serve/proxy/proxy.go b/cmd/serve/proxy/proxy.goindex f89decdfe..9ef401c32 100644--- a/cmd/serve/proxy/proxy.go+++ b/cmd/serve/proxy/proxy.go@@ -153,6 +153,11 @@ being reused. A backend created for a public key login is neverreused for a password login, so a public key cannot be sent as apassword to skip the program.+A cached answer is checked with the program again after 5 minutes even+if the user is in constant use, so revoking a user in the program+takes effect within 5 minutes. For |serve sftp| this applies to new+logins - an existing connection is not closed.+This can be used to build general purpose proxies to any kind ofbackend that rclone supports.@@ -205,14 +210,13 @@ type cacheEntry struct {// A variable so tests can adjust it.var accessKeyRefreshInterval = 10 * time.Second-// accessKeyRevalidateInterval is how long a cached access key ID is-// trusted before the proxy is asked about it again even though its-// signatures are still verifying, so a revoked access key ID stops-// working within this time rather than for as long as it stays in-// use.+// revalidateInterval is how long a cached answer from the proxy is+// trusted before the proxy is asked again even though the credential+// is still in use, so a revoked user or access key ID stops working+// within this time rather than for as long as it stays in use.//// A variable so tests can adjust it.-var accessKeyRevalidateInterval = 5 * time.Minute+var revalidateInterval = 5 * time.Minute// authKind is the kind of credential the client presentedtype authKind int@@ -450,6 +454,10 @@ func (p *Proxy) call(user, auth string, kind authKind, clientIP string) (value a// Call runs the auth proxy with the username and password/public key provided// returning a *vfs.VFS and the key used in the VFS cache.//+// A cached answer older than revalidateInterval is checked with the+// proxy again so a revoked user is refused even if it is in constant+// use.+//// remoteAddr is the address of the client as returned by net.Addr.String().// It may be empty if the client has no IP address, for example when// connecting over a unix socket.@@ -467,6 +475,14 @@ func (p *Proxy) Call(user, auth string, isPublicKey bool, remoteAddr string) (VF// Look in the cache first with the credential-aware keyvalue, ok := p.vfsCache.GetMaybe(cacheKey)+ // Ask the proxy again once the cached answer is old enough, so+ // a revoked user is refused even if the entry is in constant use+ if entry, isEntry := value.(cacheEntry); ok && isEntry {+ if time.Since(time.Unix(0, entry.refreshed.Load())) >= revalidateInterval {+ ok = false+ }+ }+// If not found then call the proxy for a fresh answerif !ok {// Logins with the same credentials which arrive together@@ -518,7 +534,7 @@ func (p *Proxy) Call(user, auth string, isPublicKey bool, remoteAddr string) (VF// safe to do when a signature fails to verify in case the secret has// been rotated.//-// A cached answer older than accessKeyRevalidateInterval is always+// A cached answer older than revalidateInterval is always// checked with the proxy so a revoked access key ID is refused even// if it is in constant use.//@@ -531,7 +547,7 @@ func (p *Proxy) CallAccessKey(accessKeyID, remoteAddr string, refresh bool) (VFSif entry, isEntry := value.(cacheEntry); isEntry {age := time.Since(time.Unix(0, entry.refreshed.Load()))switch {- case age >= accessKeyRevalidateInterval:+ case age >= revalidateInterval:refresh = truecase age < accessKeyRefreshInterval:refresh = falsediff --git a/cmd/serve/proxy/proxy_code.go b/cmd/serve/proxy/proxy_code.goindex 5fcc1c86a..ef65a00e4 100644--- a/cmd/serve/proxy/proxy_code.go+++ b/cmd/serve/proxy/proxy_code.go@@ -53,16 +53,18 @@ func main() {if out["_root"] == "" {out["_root"] = ""}+ // A user or access key ID can be revoked+ if revoked := os.Getenv("RCLONE_TEST_PROXY_REVOKED"); revoked != "" && in["user"] == revoked {+ log.Fatalf("user %q revoked", in["user"])+ }// S3 access key auth has neither pass nor public_key and needs// the secret returned, unless the user asks for it to be omitted// or empty. The secret's suffix can be changed to simulate a- // rotation and an access key ID can be revoked.+ // rotation._, havePass := in["pass"]_, havePublicKey := in["public_key"]switch {case havePass || havePublicKey || in["user"] == "nosecret":- case in["user"] == os.Getenv("RCLONE_TEST_PROXY_REVOKED"):- log.Fatalf("access key ID %q revoked", in["user"])case in["user"] == "emptysecret":out["_secret_access_key"] = ""default:diff --git a/cmd/serve/proxy/proxy_test.go b/cmd/serve/proxy/proxy_test.goindex 78e6e6bca..c8e29ec30 100644--- a/cmd/serve/proxy/proxy_test.go+++ b/cmd/serve/proxy/proxy_test.go@@ -10,6 +10,7 @@ import ("path/filepath""strings""sync"+ "sync/atomic""testing""time"@@ -217,7 +218,8 @@ func TestRun(t *testing.T) {// If a cached entry's pwHash somehow doesn't match the supplied// auth (eg a hash collision on the cache key), Call must reject// it. Simulate by corrupting the cached pwHash.- entry := cacheEntry{vfs: vfs, pwHash: sha256.Sum256([]byte("tampered"))}+ entry := cacheEntry{vfs: vfs, pwHash: sha256.Sum256([]byte("tampered")), refreshed: new(atomic.Int64)}+ entry.refreshed.Store(time.Now().UnixNano())p.vfsCache.Put(cacheKey, entry)vfs, vfsKey, err = p.Call(testUser, testPass, false, testAddr)require.Error(t, err)@@ -239,6 +241,34 @@ func TestRun(t *testing.T) {assert.Equal(t, 1, p.vfsCache.Entries())})+ t.Run("Call revalidates", func(t *testing.T) {+ assert.Equal(t, 0, p.vfsCache.Entries())+ defer p.vfsCache.Clear()+ oldRevalidate := revalidateInterval+ defer func() { revalidateInterval = oldRevalidate }()++ VFS, _, err := p.Call(testUser, testPass, false, testAddr)+ require.NoError(t, err)++ // A recently confirmed entry is used without asking the proxy+ t.Setenv("RCLONE_TEST_PROXY_REVOKED", testUser)+ VFS2, _, err := p.Call(testUser, testPass, false, testAddr)+ require.NoError(t, err, "cached entry should still be trusted")+ assert.Same(t, VFS, VFS2)++ // Once old enough the proxy is asked again and can revoke the user+ revalidateInterval = 0+ _, _, err = p.Call(testUser, testPass, false, testAddr)+ require.ErrorContains(t, err, "revoked")++ // A user the proxy still allows keeps the cached backend+ t.Setenv("RCLONE_TEST_PROXY_REVOKED", "")+ VFS3, _, err := p.Call(testUser, testPass, false, testAddr)+ require.NoError(t, err)+ assert.Same(t, VFS, VFS3)+ assert.Equal(t, 1, p.vfsCache.Entries())+ })+privateKey, privateKeyErr := rsa.GenerateKey(rand.Reader, 2048)if privateKeyErr != nil {fs.Fatal(nil, "error generating test private key "+privateKeyErr.Error())@@ -334,7 +364,8 @@ func TestRun(t *testing.T) {// If a cached entry's pwHash somehow doesn't match the supplied// auth (eg a hash collision on the cache key), Call must reject// it. Simulate by corrupting the cached pwHash.- entry := cacheEntry{vfs: vfs, kind: authPublicKey, pwHash: sha256.Sum256([]byte("tampered"))}+ entry := cacheEntry{vfs: vfs, kind: authPublicKey, pwHash: sha256.Sum256([]byte("tampered")), refreshed: new(atomic.Int64)}+ entry.refreshed.Store(time.Now().UnixNano())p.vfsCache.Put(cacheKey, entry)vfs, vfsKey, err = p.Call(testUser, publicKeyString, true, testAddr)require.Error(t, err)@@ -369,7 +400,8 @@ func TestRun(t *testing.T) {// A cached entry of the wrong kind must be rejected even if// the cache key somehow matches (eg a hash collision).- entry := cacheEntry{vfs: vfs2, kind: authPublicKey, pwHash: sha256.Sum256([]byte(publicKeyString))}+ entry := cacheEntry{vfs: vfs2, kind: authPublicKey, pwHash: sha256.Sum256([]byte(publicKeyString)), refreshed: new(atomic.Int64)}+ entry.refreshed.Store(time.Now().UnixNano())p.vfsCache.Put(vfsKey2, entry)vfs2, vfsKey2, err = p.Call(testUser, publicKeyString, false, testAddr)require.Error(t, err)@@ -513,13 +545,13 @@ func TestCallAccessKey(t *testing.T) {// Check a cached entry is revalidated with the proxy once it is// old enough even without a refresh being asked for- oldRevalidate := accessKeyRevalidateInterval- defer func() { accessKeyRevalidateInterval = oldRevalidate }()+ oldRevalidate := revalidateInterval+ defer func() { revalidateInterval = oldRevalidate }()t.Setenv("RCLONE_TEST_PROXY_REVOKED", "AKID")_, secret8, err := p.CallAccessKey("AKID", remoteAddr, false)require.NoError(t, err, "cached entry should still be trusted")assert.Equal(t, "AKID-rotated-again", secret8)- accessKeyRevalidateInterval = 0+ revalidateInterval = 0_, _, err = p.CallAccessKey("AKID", remoteAddr, false)require.ErrorContains(t, err, "revoked")}