1
0
mirror of https://github.com/webrecorder/pywb.git synced 2025-03-24 06:59:52 +01:00

proxy: ip resolver: show 500 error if incorrect coll preconfigured for ip-based settings (todo: make it configurable?)

This commit is contained in:
Ilya Kreymer 2015-12-29 14:53:50 -08:00
parent ba19ff1cd5
commit a25096968a
2 changed files with 23 additions and 9 deletions

View File

@ -1,5 +1,6 @@
from wbrequestresponse import WbResponse from wbrequestresponse import WbResponse
from pywb.utils.loaders import extract_client_cookie from pywb.utils.loaders import extract_client_cookie
from pywb.utils.wbexception import WbException
from pywb.utils.statusandheaders import StatusAndHeaders from pywb.utils.statusandheaders import StatusAndHeaders
from pywb.rewrite.wburl import WbUrl from pywb.rewrite.wburl import WbUrl
@ -32,16 +33,16 @@ class BaseCollResolver(object):
# invalid parsing # invalid parsing
if proxy_coll == '': 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): if proxy_coll is None and isinstance(self.use_default_coll, str):
proxy_coll = self.use_default_coll proxy_coll = self.use_default_coll
if proxy_coll: if proxy_coll:
proxy_coll = '/' + proxy_coll + '/' path = '/' + proxy_coll + '/'
for r in self.routes: for r in self.routes:
matcher, c = r.is_handling(proxy_coll) matcher, c = r.is_handling(path)
if matcher: if matcher:
route = r route = r
coll = c coll = c
@ -49,7 +50,7 @@ class BaseCollResolver(object):
# if no match, return coll selection response # if no match, return coll selection response
if not route: 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 # if 'use_default_coll', find first WbUrl-handling collection
elif self.use_default_coll: elif self.use_default_coll:
@ -59,7 +60,7 @@ class BaseCollResolver(object):
# otherwise, return the appropriate coll selection response # otherwise, return the appropriate coll selection response
else: 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 return route, coll, matcher, ts, None
@ -89,7 +90,7 @@ class ProxyAuthResolver(BaseCollResolver):
proxy_coll = self.read_basic_auth_coll(proxy_auth) proxy_coll = self.read_basic_auth_coll(proxy_auth)
return proxy_coll, None 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) proxy_msg = 'Basic realm="{0}"'.format(self.auth_msg)
headers = [('Content-Type', 'text/plain'), headers = [('Content-Type', 'text/plain'),
@ -136,6 +137,9 @@ class IPCacheResolver(BaseCollResolver):
return ip 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): def get_proxy_coll_ts(self, env):
ip = env['REMOTE_ADDR'] ip = env['REMOTE_ADDR']
qs = env.get('pywb.proxy_query') qs = env.get('pywb.proxy_query')
@ -202,7 +206,7 @@ class CookieResolver(BaseCollResolver):
coll, ts, sesh_id = self.get_coll(env) coll, ts, sesh_id = self.get_coll(env)
return coll, ts return coll, ts
def select_coll_response(self, env): def select_coll_response(self, env, default_coll=None):
return self.make_magic_response('auto', return self.make_magic_response('auto',
env['REL_REQUEST_URI'], env['REL_REQUEST_URI'],
env) env)

View File

@ -25,11 +25,11 @@ class TestProxyIPResolver(BaseIntegration):
assert resp.content_type == 'text/plain' assert resp.content_type == 'text/plain'
assert resp.content_length > 0 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) parts = urlsplit(uri)
env = dict(REQUEST_URI=uri, QUERY_STRING=parts.query, SCRIPT_NAME='', REMOTE_ADDR=addr) 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 # '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): def test_proxy_ip_default_ts(self):
resp = self.get_url('http://www.iana.org/') resp = self.get_url('http://www.iana.org/')
@ -93,3 +93,13 @@ class TestProxyIPResolver(BaseIntegration):
resp = self.get_url('http://info.pywb.proxy/') resp = self.get_url('http://info.pywb.proxy/')
assert resp.json == {'ip': '127.0.0.1', 'coll': None, 'ts': None} 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