Reduce scope of exception handler. (#9106)

Removes a bare `except Exception` clause and replaces it with
catching a specific exception around the portion that might throw.
This commit is contained in:
Patrick Cloke 2021-01-13 13:27:49 -05:00 committed by GitHub
parent d1eb1b96e8
commit aee8e6a95d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 5 deletions

1
changelog.d/9106.misc Normal file
View File

@ -0,0 +1 @@
Reduce the scope of caught exceptions in `BlacklistingAgentWrapper`.

View File

@ -32,7 +32,7 @@ from typing import (
import treq import treq
from canonicaljson import encode_canonical_json from canonicaljson import encode_canonical_json
from netaddr import IPAddress, IPSet from netaddr import AddrFormatError, IPAddress, IPSet
from prometheus_client import Counter from prometheus_client import Counter
from zope.interface import implementer, provider from zope.interface import implementer, provider
@ -261,16 +261,16 @@ class BlacklistingAgentWrapper(Agent):
try: try:
ip_address = IPAddress(h.hostname) ip_address = IPAddress(h.hostname)
except AddrFormatError:
# Not an IP
pass
else:
if check_against_blacklist( if check_against_blacklist(
ip_address, self._ip_whitelist, self._ip_blacklist ip_address, self._ip_whitelist, self._ip_blacklist
): ):
logger.info("Blocking access to %s due to blacklist" % (ip_address,)) logger.info("Blocking access to %s due to blacklist" % (ip_address,))
e = SynapseError(403, "IP address blocked by IP blacklist entry") e = SynapseError(403, "IP address blocked by IP blacklist entry")
return defer.fail(Failure(e)) return defer.fail(Failure(e))
except Exception:
# Not an IP
pass
return self._agent.request( return self._agent.request(
method, uri, headers=headers, bodyProducer=bodyProducer method, uri, headers=headers, bodyProducer=bodyProducer