"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
pcloud: only accept pCloud hosts from the hostname in the OAuth callback
Nick Craig-Wood3760671b36c8
diff --git a/backend/pcloud/pcloud.go b/backend/pcloud/pcloud.goindex 87b6cfec0..ecf863342 100644--- a/backend/pcloud/pcloud.go+++ b/backend/pcloud/pcloud.go@@ -14,6 +14,7 @@ import ("net/http""net/url""path"+ "regexp""strconv""strings""time"@@ -56,6 +57,10 @@ var (ClientSecret: obscure.MustReveal(rcloneEncryptedClientSecret),RedirectURL: oauthutil.RedirectLocalhostURL,}++ // hostnameRe matches a lower case API hostname within pCloud's+ // domain, eg api.pcloud.com or eapi.pcloud.com+ hostnameRe = regexp.MustCompile(`^([a-z0-9]([a-z0-9-]*[a-z0-9])?\.)+pcloud\.com$`))// Update the TokenURL with the actual hostname@@ -63,6 +68,19 @@ func updateTokenURL(oauthConfig *oauthutil.Config, hostname string) {oauthConfig.TokenURL = "https://" + hostname + "/oauth2_token"}+// callbackHostname checks and returns the API hostname pCloud sent back in the+// OAuth callback, lower cased, or defaultHostname if it sent none.+func callbackHostname(hostname string) (string, error) {+ if hostname == "" {+ return defaultHostname, nil+ }+ hostname = strings.ToLower(hostname)+ if !hostnameRe.MatchString(hostname) {+ return "", fmt.Errorf("invalid hostname %q in oauth response", hostname)+ }+ return hostname, nil+}+// Register with Fsfunc init() {updateTokenURL(oauthConfig, defaultHostname)@@ -81,9 +99,9 @@ func init() {if auth == nil || auth.Form == nil {return errors.New("form not found in response")}- hostname := auth.Form.Get("hostname")- if hostname == "" {- hostname = defaultHostname+ hostname, err := callbackHostname(auth.Form.Get("hostname"))+ if err != nil {+ return err}// Save the hostname in the configm.Set("hostname", hostname)diff --git a/backend/pcloud/pcloud_internal_test.go b/backend/pcloud/pcloud_internal_test.gonew file mode 100644index 000000000..8c04ce573--- /dev/null+++ b/backend/pcloud/pcloud_internal_test.go@@ -0,0 +1,46 @@+package pcloud++import (+ "testing"++ "github.com/stretchr/testify/assert"+)++// TestCallbackHostname checks that only hosts within pCloud's domain+// are accepted.+func TestCallbackHostname(t *testing.T) {+ for _, test := range []struct {+ in string+ want string // "" means an error is expected+ }{+ {"", defaultHostname},+ {"api.pcloud.com", "api.pcloud.com"},+ {"eapi.pcloud.com", "eapi.pcloud.com"},+ {"EAPI.pCloud.COM", "eapi.pcloud.com"},+ {"new-region.api.pcloud.com", "new-region.api.pcloud.com"},+ {"pcloud.com", ""},+ {".pcloud.com", ""},+ {"-.pcloud.com", ""},+ {"a-.pcloud.com", ""},+ {"a..pcloud.com", ""},+ {"api.pcloud.com.", ""},+ {"api.pclоud.com", ""}, // Cyrillic о+ {"127.0.0.1:18443", ""},+ {"evil.example.com", ""},+ {"api.pcloud.com.evil.example.com", ""},+ {"api.pcloud.com:443", ""},+ {"api.pcloud.com/path", ""},+ {"[email protected]#.pcloud.com", ""},+ {"evil.example.com?.pcloud.com", ""},+ } {+ t.Run("["+test.in+"]", func(t *testing.T) {+ got, err := callbackHostname(test.in)+ if test.want == "" {+ assert.Error(t, err)+ } else {+ assert.NoError(t, err)+ }+ assert.Equal(t, test.want, got)+ })+ }+}