diff --git a/changelog.d/8580.bugfix b/changelog.d/8580.bugfix
new file mode 100644
index 0000000000000000000000000000000000000000..31734fd97d71e996d44e2ffad0d5d963948ecdea
--- /dev/null
+++ b/changelog.d/8580.bugfix
@@ -0,0 +1 @@
+Fix a bug where Synapse would blindly forward bad responses from federation to clients when retrieving profile information.
diff --git a/synapse/handlers/profile.py b/synapse/handlers/profile.py
index da5692e03e4c03c63537356bd1f65cea86c6829d..3875e53c08da45a145e4bfad25823b3335365d71 100644
--- a/synapse/handlers/profile.py
+++ b/synapse/handlers/profile.py
@@ -98,6 +98,13 @@ class ProfileHandler(BaseHandler):
             except RequestSendFailed as e:
                 raise SynapseError(502, "Failed to fetch profile") from e
             except HttpResponseException as e:
+                if e.code < 500 and e.code != 404:
+                    # Other codes are not allowed in c2s API
+                    logger.info(
+                        "Server replied with wrong response: %s %s", e.code, e.msg
+                    )
+
+                    raise SynapseError(502, "Failed to fetch profile")
                 raise e.to_synapse_error()
 
     async def get_profile_from_cache(self, user_id: str) -> JsonDict: