Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Kill off HomeServer.get_ip_from_request() #9080

Merged
merged 4 commits into from
Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/9080.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Remove redundant `Homeserver.get_ip_from_request` method.
4 changes: 2 additions & 2 deletions synapse/api/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ async def get_user_by_req(
AuthError if access is denied for the user in the access token
"""
try:
ip_addr = self.hs.get_ip_from_request(request)
ip_addr = request.getClientIP()
user_agent = request.get_user_agent("")

access_token = self.get_access_token_from_request(request)
Expand Down Expand Up @@ -275,7 +275,7 @@ async def _get_appservice_user_id(self, request):
return None, None

if app_service.ip_range_whitelist:
ip_address = IPAddress(self.hs.get_ip_from_request(request))
ip_address = IPAddress(request.getClientIP())
if ip_address not in app_service.ip_range_whitelist:
return None, None

Expand Down
9 changes: 2 additions & 7 deletions synapse/handlers/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,6 @@ async def validate_user_via_ui_auth(
requester: Requester,
request: SynapseRequest,
request_body: Dict[str, Any],
clientip: str,
description: str,
) -> Tuple[dict, Optional[str]]:
"""
Expand All @@ -301,8 +300,6 @@ async def validate_user_via_ui_auth(

request_body: The body of the request sent by the client

clientip: The IP address of the client.

description: A human readable string to be displayed to the user that
describes the operation happening on their account.

Expand Down Expand Up @@ -351,7 +348,7 @@ async def validate_user_via_ui_auth(

try:
result, params, session_id = await self.check_ui_auth(
flows, request, request_body, clientip, description
flows, request, request_body, description
)
except LoginError:
# Update the ratelimiter to say we failed (`can_do_action` doesn't raise).
Expand Down Expand Up @@ -426,7 +423,6 @@ async def check_ui_auth(
flows: List[List[str]],
request: SynapseRequest,
clientdict: Dict[str, Any],
clientip: str,
description: str,
) -> Tuple[dict, dict, str]:
"""
Expand All @@ -448,8 +444,6 @@ async def check_ui_auth(
clientdict: The dictionary from the client root level, not the
'auth' key: this method prompts for auth if none is sent.

clientip: The IP address of the client.

description: A human readable string to be displayed to the user that
describes the operation happening on their account.

Expand Down Expand Up @@ -540,6 +534,7 @@ async def check_ui_auth(
await self.store.set_ui_auth_clientdict(sid, clientdict)

user_agent = request.get_user_agent("")
clientip = request.getClientIP()

await self.store.add_user_agent_ip_to_ui_auth_session(
session.session_id, user_agent, clientip
Expand Down
19 changes: 3 additions & 16 deletions synapse/rest/client/v2_alpha/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,7 @@ async def on_POST(self, request):
requester = await self.auth.get_user_by_req(request)
try:
params, session_id = await self.auth_handler.validate_user_via_ui_auth(
requester,
request,
body,
self.hs.get_ip_from_request(request),
"modify your account password",
requester, request, body, "modify your account password",
)
except InteractiveAuthIncompleteError as e:
# The user needs to provide more steps to complete auth, but
Expand All @@ -215,7 +211,6 @@ async def on_POST(self, request):
[[LoginType.EMAIL_IDENTITY]],
request,
body,
self.hs.get_ip_from_request(request),
"modify your account password",
)
except InteractiveAuthIncompleteError as e:
Expand Down Expand Up @@ -309,11 +304,7 @@ async def on_POST(self, request):
return 200, {}

await self.auth_handler.validate_user_via_ui_auth(
requester,
request,
body,
self.hs.get_ip_from_request(request),
"deactivate your account",
requester, request, body, "deactivate your account",
)
result = await self._deactivate_account_handler.deactivate_account(
requester.user.to_string(), erase, id_server=body.get("id_server")
Expand Down Expand Up @@ -695,11 +686,7 @@ async def on_POST(self, request):
assert_valid_client_secret(client_secret)

await self.auth_handler.validate_user_via_ui_auth(
requester,
request,
body,
self.hs.get_ip_from_request(request),
"add a third-party identifier to your account",
requester, request, body, "add a third-party identifier to your account",
)

validation_session = await self.identity_handler.validate_threepid_session(
Expand Down
4 changes: 2 additions & 2 deletions synapse/rest/client/v2_alpha/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ async def on_POST(self, request, stagetype):
authdict = {"response": response, "session": session}

success = await self.auth_handler.add_oob_auth(
LoginType.RECAPTCHA, authdict, self.hs.get_ip_from_request(request)
LoginType.RECAPTCHA, authdict, request.getClientIP()
)

if success:
Expand All @@ -144,7 +144,7 @@ async def on_POST(self, request, stagetype):
authdict = {"session": session}

success = await self.auth_handler.add_oob_auth(
LoginType.TERMS, authdict, self.hs.get_ip_from_request(request)
LoginType.TERMS, authdict, request.getClientIP()
)

if success:
Expand Down
12 changes: 2 additions & 10 deletions synapse/rest/client/v2_alpha/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,7 @@ async def on_POST(self, request):
assert_params_in_dict(body, ["devices"])

await self.auth_handler.validate_user_via_ui_auth(
requester,
request,
body,
self.hs.get_ip_from_request(request),
"remove device(s) from your account",
requester, request, body, "remove device(s) from your account",
)

await self.device_handler.delete_devices(
Expand Down Expand Up @@ -133,11 +129,7 @@ async def on_DELETE(self, request, device_id):
raise

await self.auth_handler.validate_user_via_ui_auth(
requester,
request,
body,
self.hs.get_ip_from_request(request),
"remove a device from your account",
requester, request, body, "remove a device from your account",
)

await self.device_handler.delete_device(requester.user.to_string(), device_id)
Expand Down
6 changes: 1 addition & 5 deletions synapse/rest/client/v2_alpha/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,7 @@ async def on_POST(self, request):
body = parse_json_object_from_request(request)

await self.auth_handler.validate_user_via_ui_auth(
requester,
request,
body,
self.hs.get_ip_from_request(request),
"add a device signing key to your account",
requester, request, body, "add a device signing key to your account",
)

result = await self.e2e_keys_handler.upload_signing_keys_for_user(user_id, body)
Expand Down
8 changes: 2 additions & 6 deletions synapse/rest/client/v2_alpha/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ async def on_GET(self, request):
403, "Registration has been disabled", errcode=Codes.FORBIDDEN
)

ip = self.hs.get_ip_from_request(request)
ip = request.getClientIP()
with self.ratelimiter.ratelimit(ip) as wait_deferred:
await wait_deferred

Expand Down Expand Up @@ -513,11 +513,7 @@ async def on_POST(self, request):
# not this will raise a user-interactive auth error.
try:
auth_result, params, session_id = await self.auth_handler.check_ui_auth(
self._registration_flows,
request,
body,
self.hs.get_ip_from_request(request),
"register a new account",
self._registration_flows, request, body, "register a new account",
)
except InteractiveAuthIncompleteError as e:
# The user needs to provide more steps to complete auth.
Expand Down
4 changes: 0 additions & 4 deletions synapse/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,6 @@ def get_reactor(self) -> twisted.internet.base.ReactorBase:
"""
return self._reactor

def get_ip_from_request(self, request) -> str:
# X-Forwarded-For is handled by our custom request type.
return request.getClientIP()

def is_mine(self, domain_specific_string: DomainSpecificString) -> bool:
return domain_specific_string.domain == self.hostname

Expand Down