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

ulozto: fix auth tokens leaking on redirects from download links GHSA-3xpx-hjg2-vrjh CVE-PENDING

Nick Craig-Wood8d0b08d12509
diff --git a/backend/ulozto/ulozto.go b/backend/ulozto/ulozto.go
index 2b638e299..f6dce5608 100644
--- a/backend/ulozto/ulozto.go
+++ b/backend/ulozto/ulozto.go
@@ -113,6 +113,22 @@ type Fs struct {
pacer *fs.Pacer // pacer for API calls
}
+// authHeaders are the request headers carrying the app and session
+// tokens which must not be forwarded when a redirect leaves the
+// original host.
+var authHeaders = []string{"X-Auth-Token", "X-User-Token"}
+
+// newClient makes the http client used for all uloz.to requests.
+//
+// Downloads carry the auth headers to whatever URL the API hands out,
+// so the client strips them on cross host redirects and refuses an
+// HTTPS to HTTP downgrade.
+func newClient(ctx context.Context) *http.Client {
+ client := fshttp.NewClient(ctx)
+ client.CheckRedirect = rest.StripHeadersOnCrossHostRedirectFn(authHeaders...)
+ return client
+}
+
// NewFs constructs a Fs from the path, container:path
func NewFs(ctx context.Context, name, root string, m configmap.Mapper) (fs.Fs, error) {
// Parse config into Options struct
@@ -125,7 +141,7 @@ func NewFs(ctx context.Context, name, root string, m configmap.Mapper) (fs.Fs, e
// Strip leading and trailing slashes, see https://github.com/rclone/rclone/issues/7796 for details.
root = strings.Trim(root, "/")
- client := fshttp.NewClient(ctx)
+ client := newClient(ctx)
f := &Fs{
name: name,
diff --git a/backend/ulozto/ulozto_internal_test.go b/backend/ulozto/ulozto_internal_test.go
new file mode 100644
index 000000000..99e1aca6e
--- /dev/null
+++ b/backend/ulozto/ulozto_internal_test.go
@@ -0,0 +1,95 @@
+package ulozto
+
+import (
+ "context"
+ "encoding/json"
+ "io"
+ "net/http"
+ "net/http/httptest"
+ "sync/atomic"
+ "testing"
+
+ "github.com/rclone/rclone/backend/ulozto/api"
+ "github.com/rclone/rclone/fs"
+ "github.com/rclone/rclone/lib/pacer"
+ "github.com/rclone/rclone/lib/rest"
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+// authTestHeaders is a deliberately literal copy of authHeaders so
+// that a header dropped from the production list fails
+// TestAuthHeaders rather than silently losing test coverage.
+var authTestHeaders = map[string]string{
+ "X-Auth-Token": "secret-app-token",
+ "X-User-Token": "secret-user-token",
+}
+
+func TestAuthHeaders(t *testing.T) {
+ assert.ElementsMatch(t, []string{"X-Auth-Token", "X-User-Token"}, authHeaders)
+}
+
+// newTestFs makes an Fs talking to apiURL with the auth headers set,
+// without going through NewFs which would authenticate.
+func newTestFs(ctx context.Context, apiURL string) *Fs {
+ client := newClient(ctx)
+ f := &Fs{
+ name: "ulozto-test",
+ cdn: rest.NewClient(client),
+ rest: rest.NewClient(client).SetRoot(apiURL),
+ pacer: fs.NewPacer(ctx, pacer.NewDefault(pacer.MinSleep(minSleep), pacer.MaxSleep(maxSleep), pacer.DecayConstant(decayConstant))),
+ }
+ f.rest.SetErrorHandler(errorHandler)
+ for header, value := range authTestHeaders {
+ f.rest.SetHeader(header, value)
+ }
+ return f
+}
+
+// TestOpenStripsAuthHeadersOnCrossHostRedirect checks that a download
+// sends the auth headers to the host the API named but not to a host
+// that download redirects to.
+func TestOpenStripsAuthHeadersOnCrossHostRedirect(t *testing.T) {
+ ctx := context.Background()
+
+ var otherHits atomic.Int32
+ other := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ otherHits.Add(1)
+ for header := range authTestHeaders {
+ assert.Empty(t, r.Header.Get(header), "%s should have been stripped", header)
+ }
+ _, _ = w.Write([]byte("payload"))
+ }))
+ defer other.Close()
+
+ var cdnHits atomic.Int32
+ cdn := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ cdnHits.Add(1)
+ assert.Equal(t, "/file?hash=h", r.URL.RequestURI())
+ for header, value := range authTestHeaders {
+ assert.Equal(t, value, r.Header.Get(header), "%s should be sent to the download host", header)
+ }
+ http.Redirect(w, r, other.URL+"/payload", http.StatusTemporaryRedirect)
+ }))
+ defer cdn.Close()
+
+ apiServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ assert.Equal(t, "/v5/file/download-link/vipdata", r.URL.Path)
+ w.Header().Set("Content-Type", "application/json")
+ require.NoError(t, json.NewEncoder(w).Encode(api.GetDownloadLinkResponse{
+ Link: cdn.URL + "/file",
+ Hash: "h",
+ }))
+ }))
+ defer apiServer.Close()
+
+ o := &Object{fs: newTestFs(ctx, apiServer.URL), slug: "slug"}
+ rc, err := o.Open(ctx)
+ require.NoError(t, err)
+ body, err := io.ReadAll(rc)
+ require.NoError(t, err)
+ require.NoError(t, rc.Close())
+ assert.Equal(t, "payload", string(body))
+ assert.Equal(t, int32(1), cdnHits.Load())
+ assert.Equal(t, int32(1), otherHits.Load())
+}