From a25096968a20e57e0ecd1f69f78acb3802a3ac44 Mon Sep 17 00:00:00 2001 From: Ilya Kreymer Date: Tue, 29 Dec 2015 14:53:50 -0800 Subject: [PATCH] proxy: ip resolver: show 500 error if incorrect coll preconfigured for ip-based settings (todo: make it configurable?) --- pywb/framework/proxy_resolvers.py | 18 +++++++++++------- tests/test_proxy_http_ip.py | 14 ++++++++++++-- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/pywb/framework/proxy_resolvers.py b/pywb/framework/proxy_resolvers.py index 89cb0c53..221e9d86 100644 --- a/pywb/framework/proxy_resolvers.py +++ b/pywb/framework/proxy_resolvers.py @@ -1,5 +1,6 @@ from wbrequestresponse import WbResponse from pywb.utils.loaders import extract_client_cookie +from pywb.utils.wbexception import WbException from pywb.utils.statusandheaders import StatusAndHeaders from pywb.rewrite.wburl import WbUrl @@ -32,16 +33,16 @@ class BaseCollResolver(object): # invalid parsing if proxy_coll == '': - return None, None, None, None, self.select_coll_response(env) + return None, None, None, None, self.select_coll_response(env, proxy_coll) if proxy_coll is None and isinstance(self.use_default_coll, str): proxy_coll = self.use_default_coll if proxy_coll: - proxy_coll = '/' + proxy_coll + '/' + path = '/' + proxy_coll + '/' for r in self.routes: - matcher, c = r.is_handling(proxy_coll) + matcher, c = r.is_handling(path) if matcher: route = r coll = c @@ -49,7 +50,7 @@ class BaseCollResolver(object): # if no match, return coll selection response if not route: - return None, None, None, None, self.select_coll_response(env) + return None, None, None, None, self.select_coll_response(env, proxy_coll) # if 'use_default_coll', find first WbUrl-handling collection elif self.use_default_coll: @@ -59,7 +60,7 @@ class BaseCollResolver(object): # otherwise, return the appropriate coll selection response else: - return None, None, None, None, self.select_coll_response(env) + return None, None, None, None, self.select_coll_response(env, proxy_coll) return route, coll, matcher, ts, None @@ -89,7 +90,7 @@ class ProxyAuthResolver(BaseCollResolver): proxy_coll = self.read_basic_auth_coll(proxy_auth) return proxy_coll, None - def select_coll_response(self, env): + def select_coll_response(self, env, default_coll=None): proxy_msg = 'Basic realm="{0}"'.format(self.auth_msg) headers = [('Content-Type', 'text/plain'), @@ -136,6 +137,9 @@ class IPCacheResolver(BaseCollResolver): return ip + def select_coll_response(self, env, default_coll=None): + raise WbException('Invalid Proxy Collection Specified: ' + str(default_coll)) + def get_proxy_coll_ts(self, env): ip = env['REMOTE_ADDR'] qs = env.get('pywb.proxy_query') @@ -202,7 +206,7 @@ class CookieResolver(BaseCollResolver): coll, ts, sesh_id = self.get_coll(env) return coll, ts - def select_coll_response(self, env): + def select_coll_response(self, env, default_coll=None): return self.make_magic_response('auto', env['REL_REQUEST_URI'], env) diff --git a/tests/test_proxy_http_ip.py b/tests/test_proxy_http_ip.py index 696ca877..5251fd36 100644 --- a/tests/test_proxy_http_ip.py +++ b/tests/test_proxy_http_ip.py @@ -25,11 +25,11 @@ class TestProxyIPResolver(BaseIntegration): assert resp.content_type == 'text/plain' assert resp.content_length > 0 - def get_url(self, uri, addr='127.0.0.1'): + def get_url(self, uri, addr='127.0.0.1', status='*'): parts = urlsplit(uri) env = dict(REQUEST_URI=uri, QUERY_STRING=parts.query, SCRIPT_NAME='', REMOTE_ADDR=addr) # 'Simulating' proxy by settings REQUEST_URI explicitly to full url with empty SCRIPT_NAME - return self.testapp.get('/x-ignore-this-x', extra_environ=env) + return self.testapp.get('/x-ignore-this-x', extra_environ=env, status=status) def test_proxy_ip_default_ts(self): resp = self.get_url('http://www.iana.org/') @@ -93,3 +93,13 @@ class TestProxyIPResolver(BaseIntegration): resp = self.get_url('http://info.pywb.proxy/') assert resp.json == {'ip': '127.0.0.1', 'coll': None, 'ts': None} + def test_proxy_set_coll_invalid(self): + resp = self.get_url('http://info.pywb.proxy/set?coll=invalid') + assert resp.content_type == 'application/json' + assert resp.json == {'ip': '127.0.0.1', 'coll': 'invalid', 'ts': None} + + def test_proxy_ip_invalid_coll(self): + resp = self.get_url('http://www.iana.org/', status=500) + assert 'Invalid Proxy Collection Specified: invalid' in resp.body + +