summary refs log tree commit diff
diff options
context:
space:
mode:
authorAndrew Morgan <andrew@amorgan.xyz>2019-05-03 14:23:31 -0700
committerAndrew Morgan <andrew@amorgan.xyz>2019-05-03 14:26:00 -0700
commit13f430cee47973bc0352a01ab6b5334ce56405ea (patch)
treebe7ebf4ecc38f5a0b89bed059a6a2fa3b5671d06
parentyield deferred (diff)
downloadsynapse-13f430cee47973bc0352a01ab6b5334ce56405ea.tar.xz
Same behavior for no result and result blacklisted
-rw-r--r--synapse/http/client.py7
-rw-r--r--synapse/rest/media/v1/preview_url_resource.py9
-rw-r--r--tests/rest/media/v1/test_url_preview.py28
3 files changed, 26 insertions, 18 deletions
diff --git a/synapse/http/client.py b/synapse/http/client.py
index 3af5de52d7..30e8b287b6 100644
--- a/synapse/http/client.py
+++ b/synapse/http/client.py
@@ -82,7 +82,6 @@ class IPBlacklistingResolver(object):
             reactor (twisted.internet.reactor)
             ip_whitelist (netaddr.IPSet)
             ip_blacklist (netaddr.IPSet)
-            federation (bool): this resolver is for federation traffic
         """
         self._reactor = reactor
         self._ip_whitelist = ip_whitelist
@@ -104,7 +103,7 @@ class IPBlacklistingResolver(object):
                     ip_address, self._ip_whitelist, self._ip_blacklist
                 ):
                     logger.info(
-                        "Dropped %s from DNS resolution to %s" % (ip_address, hostname)
+                        "Dropped %s from DNS resolution to %s due to blacklist" % (ip_address, hostname)
                     )
                     has_bad_ip = True
 
@@ -165,9 +164,9 @@ class BlacklistingAgentWrapper(Agent):
                 ip_address, self._ip_whitelist, self._ip_blacklist
             ):
                 logger.info(
-                    "Blocking access to %s because of blacklist" % (ip_address,)
+                    "Blocking access to %s because of blacklist. Returning 0 results" % (ip_address,)
                 )
-                e = SynapseError(403, "IP address blocked by IP blacklist entry")
+                e = SynapseError(404, "No results found")
                 return defer.fail(Failure(e))
         except Exception:
             # Not an IP
diff --git a/synapse/rest/media/v1/preview_url_resource.py b/synapse/rest/media/v1/preview_url_resource.py
index ba3ab1d37d..07882bbd80 100644
--- a/synapse/rest/media/v1/preview_url_resource.py
+++ b/synapse/rest/media/v1/preview_url_resource.py
@@ -31,6 +31,7 @@ from six.moves import urllib_parse as urlparse
 from canonicaljson import json
 
 from twisted.internet import defer
+from twisted.internet.error import DNSLookupError
 from twisted.web.resource import Resource
 from twisted.web.server import NOT_DONE_YET
 
@@ -331,6 +332,14 @@ class PreviewUrlResource(Resource):
             except Exception as e:
                 # FIXME: pass through 404s and other error messages nicely
                 logger.warn("Error downloading %s: %r", url, e)
+
+                if isinstance(e, DNSLookupError):
+                    # DNS lookup returned no results
+                    # Note: This will also be the case if the found IP address is blacklisted
+                    raise SynapseError(
+                        404, "No results found", Codes.UNKNOWN
+                    )
+
                 raise SynapseError(
                     500, "Failed to download content: %s" % (
                         traceback.format_exception_only(sys.exc_info()[0], e),
diff --git a/tests/rest/media/v1/test_url_preview.py b/tests/rest/media/v1/test_url_preview.py
index 650ce95a6f..f5e4cc72e4 100644
--- a/tests/rest/media/v1/test_url_preview.py
+++ b/tests/rest/media/v1/test_url_preview.py
@@ -297,12 +297,12 @@ class URLPreviewTests(unittest.HomeserverTestCase):
 
         # No requests made.
         self.assertEqual(len(self.reactor.tcpClients), 0)
-        self.assertEqual(channel.code, 403)
+        self.assertEqual(channel.code, 404)
         self.assertEqual(
             channel.json_body,
             {
                 'errcode': 'M_UNKNOWN',
-                'error': 'IP address blocked by IP blacklist entry',
+                'error': 'No results found',
             },
         )
 
@@ -318,12 +318,12 @@ class URLPreviewTests(unittest.HomeserverTestCase):
         request.render(self.preview_url)
         self.pump()
 
-        self.assertEqual(channel.code, 403)
+        self.assertEqual(channel.code, 404)
         self.assertEqual(
             channel.json_body,
             {
                 'errcode': 'M_UNKNOWN',
-                'error': 'IP address blocked by IP blacklist entry',
+                'error': 'No results found',
             },
         )
 
@@ -339,14 +339,14 @@ class URLPreviewTests(unittest.HomeserverTestCase):
 
         # No requests made.
         self.assertEqual(len(self.reactor.tcpClients), 0)
-        self.assertEqual(channel.code, 403)
         self.assertEqual(
             channel.json_body,
             {
                 'errcode': 'M_UNKNOWN',
-                'error': 'IP address blocked by IP blacklist entry',
+                'error': 'No results found',
             },
         )
+        self.assertEqual(channel.code, 404)
 
     def test_blacklisted_ip_range_direct(self):
         """
@@ -358,12 +358,12 @@ class URLPreviewTests(unittest.HomeserverTestCase):
         request.render(self.preview_url)
         self.pump()
 
-        self.assertEqual(channel.code, 403)
+        self.assertEqual(channel.code, 404)
         self.assertEqual(
             channel.json_body,
             {
                 'errcode': 'M_UNKNOWN',
-                'error': 'IP address blocked by IP blacklist entry',
+                'error': 'No results found',
             },
         )
 
@@ -414,12 +414,12 @@ class URLPreviewTests(unittest.HomeserverTestCase):
         )
         request.render(self.preview_url)
         self.pump()
-        self.assertEqual(channel.code, 403)
+        self.assertEqual(channel.code, 404)
         self.assertEqual(
             channel.json_body,
             {
                 'errcode': 'M_UNKNOWN',
-                'error': 'IP address blocked by IP blacklist entry',
+                'error': 'No results found',
             },
         )
 
@@ -439,12 +439,12 @@ class URLPreviewTests(unittest.HomeserverTestCase):
 
         # No requests made.
         self.assertEqual(len(self.reactor.tcpClients), 0)
-        self.assertEqual(channel.code, 403)
+        self.assertEqual(channel.code, 404)
         self.assertEqual(
             channel.json_body,
             {
                 'errcode': 'M_UNKNOWN',
-                'error': 'IP address blocked by IP blacklist entry',
+                'error': 'No results found',
             },
         )
 
@@ -460,11 +460,11 @@ class URLPreviewTests(unittest.HomeserverTestCase):
         request.render(self.preview_url)
         self.pump()
 
-        self.assertEqual(channel.code, 403)
+        self.assertEqual(channel.code, 404)
         self.assertEqual(
             channel.json_body,
             {
                 'errcode': 'M_UNKNOWN',
-                'error': 'IP address blocked by IP blacklist entry',
+                'error': 'No results found',
             },
         )