From 52ff26a608209bf72d1c02632ee2d72bc453c56b Mon Sep 17 00:00:00 2001
From: Neil Alexander <neilalexander@users.noreply.github.com>
Date: Mon, 14 Nov 2022 14:01:26 +0000
Subject: [PATCH] Fix profiles/invites maybe

---
 federationapi/routing/invite.go  | 14 ++++++++++++++
 federationapi/routing/profile.go | 10 +---------
 2 files changed, 15 insertions(+), 9 deletions(-)

diff --git a/federationapi/routing/invite.go b/federationapi/routing/invite.go
index 504204504..522ae8711 100644
--- a/federationapi/routing/invite.go
+++ b/federationapi/routing/invite.go
@@ -140,6 +140,20 @@ func processInvite(
 		}
 	}
 
+	if event.StateKey() == nil {
+		return util.JSONResponse{
+			Code: http.StatusBadRequest,
+			JSON: jsonerror.BadJSON("The invite event has no state key"),
+		}
+	}
+
+	if _, domain, err := cfg.Matrix.SplitLocalID('@', *event.StateKey()); err != nil {
+		return util.JSONResponse{
+			Code: http.StatusBadRequest,
+			JSON: jsonerror.InvalidArgumentValue(fmt.Sprintf("The user ID is invalid or domain %q does not belong to this server", domain)),
+		}
+	}
+
 	// Check that the event is signed by the server sending the request.
 	redacted, err := gomatrixserverlib.RedactEventJSON(event.JSON(), event.Version())
 	if err != nil {
diff --git a/federationapi/routing/profile.go b/federationapi/routing/profile.go
index f672811af..e4d2230ad 100644
--- a/federationapi/routing/profile.go
+++ b/federationapi/routing/profile.go
@@ -22,7 +22,6 @@ import (
 	"github.com/matrix-org/dendrite/internal/eventutil"
 	"github.com/matrix-org/dendrite/setup/config"
 	userapi "github.com/matrix-org/dendrite/userapi/api"
-	"github.com/matrix-org/gomatrixserverlib"
 	"github.com/matrix-org/util"
 )
 
@@ -42,16 +41,9 @@ func GetProfile(
 		}
 	}
 
-	_, domain, err := gomatrixserverlib.SplitID('@', userID)
+	_, domain, err := cfg.Matrix.SplitLocalID('@', userID)
 	if err != nil {
 		util.GetLogger(httpReq.Context()).WithError(err).Error("gomatrixserverlib.SplitID failed")
-		return util.JSONResponse{
-			Code: http.StatusBadRequest,
-			JSON: jsonerror.MissingArgument(fmt.Sprintf("Format of user ID %q is invalid", userID)),
-		}
-	}
-
-	if domain != cfg.Matrix.ServerName {
 		return util.JSONResponse{
 			Code: http.StatusBadRequest,
 			JSON: jsonerror.InvalidArgumentValue(fmt.Sprintf("Domain %q does not match this server", domain)),