federation/serverauth: store verified origin in request context

This commit is contained in:
Tulir Asokan 2025-05-04 01:04:12 +03:00
commit 63f35754c6
2 changed files with 17 additions and 1 deletions

View file

@ -16,6 +16,7 @@ type contextKey int
const (
contextKeyIPPort contextKey = iota
contextKeyDestinationServer
contextKeyOriginServer
)
func DestinationServerNameFromRequest(r *http.Request) string {
@ -28,3 +29,14 @@ func DestinationServerName(ctx context.Context) string {
}
return ""
}
func OriginServerNameFromRequest(r *http.Request) string {
return OriginServerName(r.Context())
}
func OriginServerName(ctx context.Context) string {
if origin, ok := ctx.Value(contextKeyOriginServer).(string); ok {
return origin
}
return ""
}

View file

@ -234,7 +234,11 @@ func (sa *ServerAuth) Authenticate(r *http.Request) (*http.Request, *mautrix.Res
return nil, &errInvalidRequestSignature
}
ctx := context.WithValue(r.Context(), contextKeyDestinationServer, destination)
ctx = log.With().Str("destination_server_name", destination).Logger().WithContext(ctx)
ctx = context.WithValue(ctx, contextKeyOriginServer, parsed.Origin)
ctx = log.With().
Str("origin_server_name", parsed.Origin).
Str("destination_server_name", destination).
Logger().WithContext(ctx)
modifiedReq := r.WithContext(ctx)
modifiedReq.Body = io.NopCloser(bytes.NewReader(reqBody))
return modifiedReq, nil