-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support MSC3916 (without MSC3911) (#509)
* Add support for MSC3916 * Add changelog * Add tests for preview_url and config authenticated endpoints * Add placeholder tests for downloads and thumbnails * Test X-Matrix auth header stuff * Validate signing keys more correctly * Add early documentation for what this setup will look like * Fix imports * Update tests * Add resolvematrix.dev tests * Fix URL preview test * Support receiving `/versions` and enabling MSC3916 support * Remove placeholder docs * Make outbound federation requests using MSC3916 * Validate X-Matrix destination correctly * Factor out signing key generation * Allow overriding the auth header in tests * Print signing key path when printing domains * Configure test MMR instances with a signing key * Allow lazy ServeFile implementations * Add federation download test * Re-add merge conflicts in changelog * Support http-only federation for tests * Strip Go-added URI segments * Fix test shutdown * Remove unused test * Enable failing tests * Ensure signing keys exist inside container * Fix signing key alignment between dependencies * Ensure signing key information is carried into the config object * Generally treat homeserver config a bite more safely * Support and use new 3916v2 federation download URL * Fix signing key permissions? * Fix routing * Update redirect-supporting behaviour * Support redirects * Finish tests * Mark test function as deprecated to discourage use * Avoid testcontainers tests from overwriting the config concurrently. * host.docker.internal doesn't exist on linux * Temporarily disable upload tests * Support federation thumbnails again * Fix tests for auth header * Switch to stable endpoints * Maybe use the correct stable endpoint too * Revert "Temporarily disable upload tests" This reverts commit e21fa01. * Try fixing tests * Hardcode `host.docker.internal` again * Fix redirect behaviour on federation * Move endpoints to correct package * Maybe remove the dev code
- Loading branch information
Showing
51 changed files
with
2,057 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package _routers | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
|
||
"github.com/t2bot/matrix-media-repo/api/_apimeta" | ||
"github.com/t2bot/matrix-media-repo/api/_responses" | ||
"github.com/t2bot/matrix-media-repo/common" | ||
"github.com/t2bot/matrix-media-repo/common/rcontext" | ||
"github.com/t2bot/matrix-media-repo/matrix" | ||
) | ||
|
||
type GeneratorWithServerFn = func(r *http.Request, ctx rcontext.RequestContext, server _apimeta.ServerInfo) interface{} | ||
|
||
func RequireServerAuth(generator GeneratorWithServerFn) GeneratorFn { | ||
return func(r *http.Request, ctx rcontext.RequestContext) interface{} { | ||
serverName, err := matrix.ValidateXMatrixAuth(r, true) | ||
if err != nil { | ||
ctx.Log.Debug("Error with X-Matrix auth: ", err) | ||
if errors.Is(err, matrix.ErrNoXMatrixAuth) { | ||
return &_responses.ErrorResponse{ | ||
Code: common.ErrCodeUnauthorized, | ||
Message: "no auth provided (required)", | ||
InternalCode: common.ErrCodeMissingToken, | ||
} | ||
} | ||
if errors.Is(err, matrix.ErrWrongDestination) { | ||
return &_responses.ErrorResponse{ | ||
Code: common.ErrCodeUnauthorized, | ||
Message: "no auth provided for this destination (required)", | ||
InternalCode: common.ErrCodeBadRequest, | ||
} | ||
} | ||
return &_responses.ErrorResponse{ | ||
Code: common.ErrCodeForbidden, | ||
Message: "invalid auth provided (required)", | ||
InternalCode: common.ErrCodeBadRequest, | ||
} | ||
} | ||
return generator(r, ctx, _apimeta.ServerInfo{ | ||
ServerName: serverName, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package r0 | ||
|
||
import ( | ||
"net/http" | ||
"slices" | ||
|
||
"github.com/getsentry/sentry-go" | ||
"github.com/t2bot/matrix-media-repo/api/_apimeta" | ||
"github.com/t2bot/matrix-media-repo/api/_responses" | ||
"github.com/t2bot/matrix-media-repo/matrix" | ||
|
||
"github.com/t2bot/matrix-media-repo/common/rcontext" | ||
) | ||
|
||
func ClientVersions(r *http.Request, rctx rcontext.RequestContext, user _apimeta.UserInfo) interface{} { | ||
versions, err := matrix.ClientVersions(rctx, r.Host, user.UserId, user.AccessToken, r.RemoteAddr) | ||
if err != nil { | ||
rctx.Log.Error(err) | ||
sentry.CaptureException(err) | ||
return _responses.InternalServerError("unable to get versions") | ||
} | ||
|
||
// This is where we'd add our feature/version support as needed | ||
if versions.Versions == nil { | ||
versions.Versions = make([]string, 1) | ||
} | ||
|
||
// We add v1.11 by force, even though we can't reliably say the rest of the server implements it. This | ||
// is because server admins which point `/versions` at us are effectively opting in to whatever features | ||
// we need to advertise support for. In our case, it's at least Authenticated Media (MSC3916). | ||
if !slices.Contains(versions.Versions, "v1.11") { | ||
versions.Versions = append(versions.Versions, "v1.11") | ||
} | ||
|
||
return versions | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package v1 | ||
|
||
import ( | ||
"bytes" | ||
"net/http" | ||
|
||
"github.com/t2bot/matrix-media-repo/api/_apimeta" | ||
"github.com/t2bot/matrix-media-repo/api/_responses" | ||
"github.com/t2bot/matrix-media-repo/api/_routers" | ||
"github.com/t2bot/matrix-media-repo/api/r0" | ||
"github.com/t2bot/matrix-media-repo/common/rcontext" | ||
"github.com/t2bot/matrix-media-repo/util/readers" | ||
) | ||
|
||
func ClientDownloadMedia(r *http.Request, rctx rcontext.RequestContext, user _apimeta.UserInfo) interface{} { | ||
r.URL.Query().Set("allow_remote", "true") | ||
r.URL.Query().Set("allow_redirect", "true") | ||
return r0.DownloadMedia(r, rctx, user) | ||
} | ||
|
||
func FederationDownloadMedia(r *http.Request, rctx rcontext.RequestContext, server _apimeta.ServerInfo) interface{} { | ||
query := r.URL.Query() | ||
query.Set("allow_remote", "false") | ||
query.Set("allow_redirect", "true") // we override how redirects work in the response | ||
r.URL.RawQuery = query.Encode() | ||
r = _routers.ForceSetParam("server", r.Host, r) | ||
|
||
res := r0.DownloadMedia(r, rctx, _apimeta.UserInfo{}) | ||
if dl, ok := res.(*_responses.DownloadResponse); ok { | ||
return &_responses.DownloadResponse{ | ||
ContentType: "multipart/mixed", | ||
Filename: "", | ||
SizeBytes: 0, | ||
Data: readers.NewMultipartReader( | ||
&readers.MultipartPart{ContentType: "application/json", Reader: readers.MakeCloser(bytes.NewReader([]byte("{}")))}, | ||
&readers.MultipartPart{ContentType: dl.ContentType, FileName: dl.Filename, Reader: dl.Data}, | ||
), | ||
TargetDisposition: "attachment", | ||
} | ||
} else if rd, ok := res.(*_responses.RedirectResponse); ok { | ||
return &_responses.DownloadResponse{ | ||
ContentType: "multipart/mixed", | ||
Filename: "", | ||
SizeBytes: 0, | ||
Data: readers.NewMultipartReader( | ||
&readers.MultipartPart{ContentType: "application/json", Reader: readers.MakeCloser(bytes.NewReader([]byte("{}")))}, | ||
&readers.MultipartPart{Location: rd.ToUrl}, | ||
), | ||
TargetDisposition: "attachment", | ||
} | ||
} else { | ||
return res | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package v1 | ||
|
||
import ( | ||
"bytes" | ||
"net/http" | ||
|
||
"github.com/t2bot/matrix-media-repo/api/_apimeta" | ||
"github.com/t2bot/matrix-media-repo/api/_responses" | ||
"github.com/t2bot/matrix-media-repo/api/_routers" | ||
"github.com/t2bot/matrix-media-repo/api/r0" | ||
"github.com/t2bot/matrix-media-repo/common/rcontext" | ||
"github.com/t2bot/matrix-media-repo/util/readers" | ||
) | ||
|
||
func ClientThumbnailMedia(r *http.Request, rctx rcontext.RequestContext, user _apimeta.UserInfo) interface{} { | ||
r.URL.Query().Set("allow_remote", "true") | ||
r.URL.Query().Set("allow_redirect", "true") | ||
return r0.ThumbnailMedia(r, rctx, user) | ||
} | ||
|
||
func FederationThumbnailMedia(r *http.Request, rctx rcontext.RequestContext, server _apimeta.ServerInfo) interface{} { | ||
query := r.URL.Query() | ||
query.Set("allow_remote", "false") | ||
query.Set("allow_redirect", "true") // we override how redirects work in the response | ||
r.URL.RawQuery = query.Encode() | ||
r = _routers.ForceSetParam("server", r.Host, r) | ||
|
||
res := r0.ThumbnailMedia(r, rctx, _apimeta.UserInfo{}) | ||
if dl, ok := res.(*_responses.DownloadResponse); ok { | ||
return &_responses.DownloadResponse{ | ||
ContentType: "multipart/mixed", | ||
Filename: "", | ||
SizeBytes: 0, | ||
Data: readers.NewMultipartReader( | ||
&readers.MultipartPart{ContentType: "application/json", Reader: readers.MakeCloser(bytes.NewReader([]byte("{}")))}, | ||
&readers.MultipartPart{ContentType: dl.ContentType, FileName: dl.Filename, Reader: dl.Data}, | ||
), | ||
TargetDisposition: "attachment", | ||
} | ||
} else if rd, ok := res.(*_responses.RedirectResponse); ok { | ||
return &_responses.DownloadResponse{ | ||
ContentType: "multipart/mixed", | ||
Filename: "", | ||
SizeBytes: 0, | ||
Data: readers.NewMultipartReader( | ||
&readers.MultipartPart{ContentType: "application/json", Reader: readers.MakeCloser(bytes.NewReader([]byte("{}")))}, | ||
&readers.MultipartPart{Location: rd.ToUrl}, | ||
), | ||
TargetDisposition: "attachment", | ||
} | ||
} else { | ||
return res | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.