1
0
mirror of https://github.com/webrecorder/pywb.git synced 2025-03-15 00:03:28 +01:00

[#715] Forward custom headers for cdx queries (#813)

In particular the X-Pywb-ACL-User header must be forwarded in order
for it to be able to control CDX-queries
This commit is contained in:
Jonas Linde 2023-02-15 23:05:21 +01:00 committed by GitHub
parent 454486bf75
commit 5c427b9ff2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 4 deletions

View File

@ -434,7 +434,11 @@ class FrontEndApp(object):
cdx_url += 'limit=' + str(self.query_limit)
try:
res = requests.get(cdx_url, stream=True)
headers = {}
for key in environ.keys():
if key.startswith("HTTP_X_"):
headers[key[5:].replace("_", "-")] = environ[key]
res = requests.get(cdx_url, stream=True, headers=headers)
status_line = '{} {}'.format(res.status_code, res.reason)
content_type = res.headers.get('Content-Type')

View File

@ -5,6 +5,8 @@ org,iana)/_css/2013.1/fonts/opensans-semibold.ttf - {"access": "allow"}
org,iana)/_css - {"access": "exclude"}
org,iana)/### - {"access": "allow"}
org,iana)/ - {"access": "exclude"}
com,example)/?example=3 - {"access": "block", "user": "staff"}
com,example)/?example=3 - {"access": "exclude", "user": "staff2"}
org,example)/?example=1 - {"access": "block"}
com,example)/?example=2 - {"access": "allow_ignore_embargo"}
com,example)/?example=1 - {"access": "allow_ignore_embargo", "user": "staff2"}

View File

@ -41,12 +41,23 @@ class TestACLApp(BaseConfigTest):
assert 'Access Blocked' in resp.text
def test_allow_via_acl_header(self):
resp = self.query('http://www.iana.org/about/')
resp = self.testapp.get('/pywb/cdx?url=http://www.iana.org/about/', headers={"X-Pywb-Acl-User": "staff"})
assert len(resp.text.splitlines()) == 1
resp = self.testapp.get('/pywb/mp_/http://www.iana.org/about/', headers={"X-Pywb-Acl-User": "staff"}, status=200)
def test_block_via_acl_header(self):
resp = self.testapp.get('/pywb/cdx?url=http://example.com/?example=3', headers={"X-Pywb-Acl-User": "staff"})
assert len(resp.text.splitlines()) > 0
resp = self.testapp.get('/pywb/mp_/http://example.com/?example=3', headers={"X-Pywb-Acl-User": "staff"}, status=451)
def test_exclude_via_acl_header(self):
resp = self.testapp.get('/pywb/cdx?url=http://example.com/?example=3', headers={"X-Pywb-Acl-User": "staff2"})
assert len(resp.text.splitlines()) == 0
resp = self.testapp.get('/pywb/mp_/http://example.com/?example=3', headers={"X-Pywb-Acl-User": "staff2"}, status=404)
def test_allowed_more_specific(self):
resp = self.query('http://www.iana.org/_css/2013.1/fonts/opensans-semibold.ttf')

View File

@ -46,8 +46,12 @@ class TestEmbargoApp(BaseConfigTest):
def test_embargo_ignore_acl_with_header_only(self):
# ignore embargo with custom header only
headers = {"X-Pywb-ACL-User": "staff2"}
resp = self.testapp.get('/pywb-embargo-acl/20140126201054mp_/http://example.com/?example=1', status=200, headers=headers)
resp = self.testapp.get('/pywb-embargo-acl/cdx?url=http://example.com/?example=1', headers=headers)
assert len(resp.text.splitlines()) > 0
resp = self.testapp.get('/pywb-embargo-acl/20140126201054mp_/http://example.com/?example=1', status=200, headers=headers)
resp = self.testapp.get('/pywb-embargo-acl/cdx?url=http://example.com/?example=1')
assert len(resp.text.splitlines()) == 0
resp = self.testapp.get('/pywb-embargo-acl/20140126201054mp_/http://example.com/?example=1', status=404)