-
Notifications
You must be signed in to change notification settings - Fork 87
Support MSC3916 (without MSC3911) #509
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
51 commits
Select commit
Hold shift + click to select a range
c39fc98
Add support for MSC3916
turt2live a004bbf
Add changelog
turt2live f9d2316
Add tests for preview_url and config authenticated endpoints
turt2live 2405827
Add placeholder tests for downloads and thumbnails
turt2live f3d20d9
Test X-Matrix auth header stuff
turt2live 5356506
Validate signing keys more correctly
turt2live 3fb16f5
Add early documentation for what this setup will look like
turt2live beac841
Fix imports
turt2live 5a51564
Update tests
turt2live 4adc55a
Add resolvematrix.dev tests
turt2live 05ac6a8
Fix URL preview test
turt2live 6aae7de
Support receiving `/versions` and enabling MSC3916 support
turt2live 1773891
Remove placeholder docs
turt2live 9f4b29f
Make outbound federation requests using MSC3916
turt2live 237e153
Validate X-Matrix destination correctly
turt2live 579987b
Factor out signing key generation
turt2live 4bfddf3
Allow overriding the auth header in tests
turt2live 0944a9a
Print signing key path when printing domains
turt2live b5472bd
Configure test MMR instances with a signing key
turt2live 476e92d
Allow lazy ServeFile implementations
turt2live 9879099
Add federation download test
turt2live 2c0fcde
Re-add merge conflicts in changelog
turt2live dcb3249
Support http-only federation for tests
turt2live f7e1504
Strip Go-added URI segments
turt2live 2cb930b
Fix test shutdown
turt2live 8f79ea0
Remove unused test
turt2live c793319
Enable failing tests
turt2live 03dd83e
Ensure signing keys exist inside container
turt2live 9405db7
Fix signing key alignment between dependencies
turt2live d2862d0
Ensure signing key information is carried into the config object
turt2live 5af3035
Generally treat homeserver config a bite more safely
turt2live 4572673
Support and use new 3916v2 federation download URL
turt2live 1cc666d
Fix signing key permissions?
turt2live b0ba084
Fix routing
turt2live c7776f0
Update redirect-supporting behaviour
turt2live bf17b97
Support redirects
turt2live 21e8281
Finish tests
turt2live 99ab04a
Mark test function as deprecated to discourage use
turt2live 8f01e45
Avoid testcontainers tests from overwriting the config concurrently.
turt2live fa40656
host.docker.internal doesn't exist on linux
turt2live 5f16648
Temporarily disable upload tests
turt2live 806464d
Support federation thumbnails again
turt2live 5133471
Fix tests for auth header
turt2live 9697e11
Switch to stable endpoints
turt2live 85e7cbe
Maybe use the correct stable endpoint too
turt2live 69ac7b9
Revert "Temporarily disable upload tests"
turt2live a55b0d8
Try fixing tests
turt2live 547cc1a
Hardcode `host.docker.internal` again
turt2live 3039539
Fix redirect behaviour on federation
turt2live 2e92cac
Move endpoints to correct package
turt2live 55742cc
Maybe remove the dev code
turt2live File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.