mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-07 06:53:09 -06:00
Review comments
This commit is contained in:
parent
c2aa33e983
commit
a3c76e9f2f
|
|
@ -679,8 +679,8 @@ func MakeFedAPI(
|
||||||
return httputil.MakeExternalAPI(metricsName, h)
|
return httputil.MakeExternalAPI(metricsName, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
// MakeFedAPI makes an http.Handler that checks matrix federation authentication.
|
// MakeFedHTMLAPI makes an http.Handler that checks matrix federation authentication.
|
||||||
func MakeFedAPIHTML(
|
func MakeFedHTMLAPI(
|
||||||
serverName spec.ServerName,
|
serverName spec.ServerName,
|
||||||
isLocalServerName func(spec.ServerName) bool,
|
isLocalServerName func(spec.ServerName) bool,
|
||||||
keyRing gomatrixserverlib.JSONVerifier,
|
keyRing gomatrixserverlib.JSONVerifier,
|
||||||
|
|
|
||||||
|
|
@ -64,7 +64,7 @@ type downloadRequest struct {
|
||||||
ThumbnailSize types.ThumbnailSize
|
ThumbnailSize types.ThumbnailSize
|
||||||
Logger *log.Entry
|
Logger *log.Entry
|
||||||
DownloadFilename string
|
DownloadFilename string
|
||||||
forFederation bool // whether we need to return a multipart/mixed response
|
multipartResponse bool // whether we need to return a multipart/mixed response (for requests coming in over federation)
|
||||||
fedClient fclient.FederationClient
|
fedClient fclient.FederationClient
|
||||||
origin spec.ServerName
|
origin spec.ServerName
|
||||||
}
|
}
|
||||||
|
|
@ -122,10 +122,10 @@ func Download(
|
||||||
activeThumbnailGeneration *types.ActiveThumbnailGeneration,
|
activeThumbnailGeneration *types.ActiveThumbnailGeneration,
|
||||||
isThumbnailRequest bool,
|
isThumbnailRequest bool,
|
||||||
customFilename string,
|
customFilename string,
|
||||||
forFederation bool,
|
federationRequest bool,
|
||||||
) {
|
) {
|
||||||
// This happens if we call Download for a federation request
|
// This happens if we call Download for a federation request
|
||||||
if forFederation && origin == "" {
|
if federationRequest && origin == "" {
|
||||||
origin = cfg.Matrix.ServerName
|
origin = cfg.Matrix.ServerName
|
||||||
}
|
}
|
||||||
dReq := &downloadRequest{
|
dReq := &downloadRequest{
|
||||||
|
|
@ -139,7 +139,7 @@ func Download(
|
||||||
"MediaID": mediaID,
|
"MediaID": mediaID,
|
||||||
}),
|
}),
|
||||||
DownloadFilename: customFilename,
|
DownloadFilename: customFilename,
|
||||||
forFederation: forFederation,
|
multipartResponse: federationRequest,
|
||||||
origin: cfg.Matrix.ServerName,
|
origin: cfg.Matrix.ServerName,
|
||||||
fedClient: fedClient,
|
fedClient: fedClient,
|
||||||
}
|
}
|
||||||
|
|
@ -383,7 +383,7 @@ func (r *downloadRequest) respondFromLocalFile(
|
||||||
" style-src 'unsafe-inline';" +
|
" style-src 'unsafe-inline';" +
|
||||||
" object-src 'self';"
|
" object-src 'self';"
|
||||||
|
|
||||||
if !r.forFederation {
|
if !r.multipartResponse {
|
||||||
w.Header().Set("Content-Security-Policy", contentSecurityPolicy)
|
w.Header().Set("Content-Security-Policy", contentSecurityPolicy)
|
||||||
if _, err = io.Copy(w, responseFile); err != nil {
|
if _, err = io.Copy(w, responseFile); err != nil {
|
||||||
return nil, fmt.Errorf("io.Copy: %w", err)
|
return nil, fmt.Errorf("io.Copy: %w", err)
|
||||||
|
|
@ -827,10 +827,10 @@ func (r *downloadRequest) fetchRemoteFile(
|
||||||
r.Logger.Debug("Fetching remote file")
|
r.Logger.Debug("Fetching remote file")
|
||||||
|
|
||||||
// Attempt to download via authenticated media endpoint
|
// Attempt to download via authenticated media endpoint
|
||||||
isMultiPart := true
|
isAuthed := true
|
||||||
resp, err := r.fedClient.DownloadMedia(ctx, r.origin, r.MediaMetadata.Origin, string(r.MediaMetadata.MediaID))
|
resp, err := r.fedClient.DownloadMedia(ctx, r.origin, r.MediaMetadata.Origin, string(r.MediaMetadata.MediaID))
|
||||||
if err != nil || (resp != nil && resp.StatusCode != http.StatusOK) {
|
if err != nil || (resp != nil && resp.StatusCode != http.StatusOK) {
|
||||||
isMultiPart = false
|
isAuthed = false
|
||||||
// try again on the unauthed endpoint
|
// try again on the unauthed endpoint
|
||||||
// create request for remote file
|
// create request for remote file
|
||||||
resp, err = client.CreateMediaDownloadRequest(ctx, r.MediaMetadata.Origin, string(r.MediaMetadata.MediaID))
|
resp, err = client.CreateMediaDownloadRequest(ctx, r.MediaMetadata.Origin, string(r.MediaMetadata.MediaID))
|
||||||
|
|
@ -838,7 +838,7 @@ func (r *downloadRequest) fetchRemoteFile(
|
||||||
if resp != nil && resp.StatusCode == http.StatusNotFound {
|
if resp != nil && resp.StatusCode == http.StatusNotFound {
|
||||||
return "", false, fmt.Errorf("File with media ID %q does not exist on %s", r.MediaMetadata.MediaID, r.MediaMetadata.Origin)
|
return "", false, fmt.Errorf("File with media ID %q does not exist on %s", r.MediaMetadata.MediaID, r.MediaMetadata.Origin)
|
||||||
}
|
}
|
||||||
return "", false, fmt.Errorf("file with media ID %q could not be downloaded from %s", r.MediaMetadata.MediaID, r.MediaMetadata.Origin)
|
return "", false, fmt.Errorf("file with media ID %q could not be downloaded from %s: %w", r.MediaMetadata.MediaID, r.MediaMetadata.Origin, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
defer resp.Body.Close() // nolint: errcheck
|
defer resp.Body.Close() // nolint: errcheck
|
||||||
|
|
@ -850,7 +850,7 @@ func (r *downloadRequest) fetchRemoteFile(
|
||||||
var contentLength int64
|
var contentLength int64
|
||||||
var reader io.Reader
|
var reader io.Reader
|
||||||
var parseErr error
|
var parseErr error
|
||||||
if isMultiPart {
|
if isAuthed {
|
||||||
r.Logger.Debug("Downloaded file using authenticated endpoint")
|
r.Logger.Debug("Downloaded file using authenticated endpoint")
|
||||||
var params map[string]string
|
var params map[string]string
|
||||||
_, params, err = mime.ParseMediaType(resp.Header.Get("Content-Type"))
|
_, params, err = mime.ParseMediaType(resp.Header.Get("Content-Type"))
|
||||||
|
|
@ -891,15 +891,15 @@ func (r *downloadRequest) fetchRemoteFile(
|
||||||
}
|
}
|
||||||
req, reqErr := http.NewRequest(http.MethodGet, redirect, nil)
|
req, reqErr := http.NewRequest(http.MethodGet, redirect, nil)
|
||||||
if reqErr != nil {
|
if reqErr != nil {
|
||||||
return "", false, fmt.Errorf("failed to create request to %s: %w", redirect, err)
|
return "", false, fmt.Errorf("failed to create request to %s: %w", redirect, reqErr)
|
||||||
}
|
}
|
||||||
redirectResp, reqErr := client.DoHTTPRequest(ctx, req)
|
redirectResp, reqErr := client.DoHTTPRequest(ctx, req)
|
||||||
if reqErr != nil {
|
if reqErr != nil {
|
||||||
return "", false, fmt.Errorf("error following redirect: %w", err)
|
return "", false, fmt.Errorf("error following redirect: %w", reqErr)
|
||||||
}
|
}
|
||||||
defer redirectResp.Body.Close() // nolint: errcheck
|
defer redirectResp.Body.Close() // nolint: errcheck
|
||||||
if redirectResp.StatusCode != http.StatusOK {
|
if redirectResp.StatusCode != http.StatusOK {
|
||||||
return "", false, fmt.Errorf("unexpected status code %d after following redirect", resp.StatusCode)
|
return "", false, fmt.Errorf("unexpected status code %d after following redirect", redirectResp.StatusCode)
|
||||||
}
|
}
|
||||||
contentLength, reader, parseErr = r.GetContentLengthAndReader(redirectResp.Header.Get("Content-Length"), redirectResp.Body, maxFileSizeBytes)
|
contentLength, reader, parseErr = r.GetContentLengthAndReader(redirectResp.Header.Get("Content-Length"), redirectResp.Body, maxFileSizeBytes)
|
||||||
r.MediaMetadata.ContentType = types.ContentType(redirectResp.Header.Get("Content-Type"))
|
r.MediaMetadata.ContentType = types.ContentType(redirectResp.Header.Get("Content-Type"))
|
||||||
|
|
|
||||||
|
|
@ -115,10 +115,10 @@ func Setup(
|
||||||
).Methods(http.MethodGet, http.MethodOptions)
|
).Methods(http.MethodGet, http.MethodOptions)
|
||||||
|
|
||||||
// same, but for federation
|
// same, but for federation
|
||||||
v1fedMux.Handle("/download/{mediaId}", routing.MakeFedAPIHTML(cfg.Global.ServerName, cfg.Global.IsLocalServerName, keyRing,
|
v1fedMux.Handle("/download/{mediaId}", routing.MakeFedHTMLAPI(cfg.Global.ServerName, cfg.Global.IsLocalServerName, keyRing,
|
||||||
makeDownloadAPI("download_authed_federation", &cfg.MediaAPI, rateLimits, db, client, federationClient, activeRemoteRequests, activeThumbnailGeneration, true),
|
makeDownloadAPI("download_authed_federation", &cfg.MediaAPI, rateLimits, db, client, federationClient, activeRemoteRequests, activeThumbnailGeneration, true),
|
||||||
)).Methods(http.MethodGet, http.MethodOptions)
|
)).Methods(http.MethodGet, http.MethodOptions)
|
||||||
v1fedMux.Handle("/thumbnail/{mediaId}", routing.MakeFedAPIHTML(cfg.Global.ServerName, cfg.Global.IsLocalServerName, keyRing,
|
v1fedMux.Handle("/thumbnail/{mediaId}", routing.MakeFedHTMLAPI(cfg.Global.ServerName, cfg.Global.IsLocalServerName, keyRing,
|
||||||
makeDownloadAPI("thumbnail_authed_federation", &cfg.MediaAPI, rateLimits, db, client, federationClient, activeRemoteRequests, activeThumbnailGeneration, true),
|
makeDownloadAPI("thumbnail_authed_federation", &cfg.MediaAPI, rateLimits, db, client, federationClient, activeRemoteRequests, activeThumbnailGeneration, true),
|
||||||
)).Methods(http.MethodGet, http.MethodOptions)
|
)).Methods(http.MethodGet, http.MethodOptions)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue