Skip to content

Commit

Permalink
Support being redirected over federation
Browse files Browse the repository at this point in the history
Fixes #515
  • Loading branch information
turt2live committed Jan 14, 2024
1 parent 3915d58 commit 741b4b9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
* Dendrite homeservers can now have their media imported safely, and `adminApiKind` may be set to `dendrite`.
* Exporting MMR's data to Synapse is now possible with `import_to_synapse`. To use it, first run `gdpr_export` or similar.
* Errors encountered during a background task, such as an API-induced export, are exposed as `error_message` in the admin API.
* MMR will follow redirects on federated downloads up to 5 hops.

### Changed

Expand Down
15 changes: 11 additions & 4 deletions matrix/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"os"
"time"

"github.com/sirupsen/logrus"
"github.com/turt2live/matrix-media-repo/common/rcontext"
)

Expand Down Expand Up @@ -80,7 +79,7 @@ func doRequest(ctx rcontext.RequestContext, method string, urlStr string, body i
}

func FederatedGet(url string, realHost string, ctx rcontext.RequestContext) (*http.Response, error) {
logrus.Debug("Doing federated GET to " + url + " with host " + realHost)
ctx.Log.Debug("Doing federated GET to " + url + " with host " + realHost)

cb := getFederationBreaker(realHost)

Expand Down Expand Up @@ -117,7 +116,6 @@ func FederatedGet(url string, realHost string, ctx rcontext.RequestContext) (*ht
ServerName: realHost,
},
},
Timeout: time.Duration(ctx.Config.TimeoutSeconds.Federation) * time.Second,
}
} else {
ctx.Log.Warn("Ignoring any certificate errors while making request")
Expand All @@ -143,10 +141,19 @@ func FederatedGet(url string, realHost string, ctx rcontext.RequestContext) (*ht
}
client = &http.Client{
Transport: tr,
Timeout: time.Duration(ctx.Config.TimeoutSeconds.UrlPreviews) * time.Second,
}
}

client.Timeout = time.Duration(ctx.Config.TimeoutSeconds.UrlPreviews) * time.Second
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
if len(via) > 5 { // arbitrary
return errors.New("too many redirects")
}
ctx.Log.Debugf("Redirected to %s", req.URL.String())
client.Transport = nil // Clear our TLS handler as we're out of the Matrix certificate verification steps
return nil
}

resp, err = client.Do(req)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion pipelines/_steps/download/try_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TryDownload(ctx rcontext.RequestContext, origin string, mediaId string) (*d
return
}

downloadUrl := fmt.Sprintf("%s/_matrix/media/v3/download/%s/%s?allow_remote=false", baseUrl, url.PathEscape(origin), url.PathEscape(mediaId))
downloadUrl := fmt.Sprintf("%s/_matrix/media/v3/download/%s/%s?allow_remote=false&allow_redirect=true", baseUrl, url.PathEscape(origin), url.PathEscape(mediaId))
resp, err := matrix.FederatedGet(downloadUrl, realHost, ctx)
metrics.MediaDownloaded.With(prometheus.Labels{"origin": origin}).Inc()
if err != nil {
Expand Down

0 comments on commit 741b4b9

Please sign in to comment.