Skip to content

Commit 5c427b9

Browse files
authored
[#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
1 parent 454486b commit 5c427b9

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

pywb/apps/frontendapp.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,11 @@ def serve_cdx(self, environ, coll='$root'):
434434
cdx_url += 'limit=' + str(self.query_limit)
435435

436436
try:
437-
res = requests.get(cdx_url, stream=True)
437+
headers = {}
438+
for key in environ.keys():
439+
if key.startswith("HTTP_X_"):
440+
headers[key[5:].replace("_", "-")] = environ[key]
441+
res = requests.get(cdx_url, stream=True, headers=headers)
438442

439443
status_line = '{} {}'.format(res.status_code, res.reason)
440444
content_type = res.headers.get('Content-Type')

sample_archive/access/pywb.aclj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ org,iana)/_css/2013.1/fonts/opensans-semibold.ttf - {"access": "allow"}
55
org,iana)/_css - {"access": "exclude"}
66
org,iana)/### - {"access": "allow"}
77
org,iana)/ - {"access": "exclude"}
8+
com,example)/?example=3 - {"access": "block", "user": "staff"}
9+
com,example)/?example=3 - {"access": "exclude", "user": "staff2"}
810
org,example)/?example=1 - {"access": "block"}
911
com,example)/?example=2 - {"access": "allow_ignore_embargo"}
1012
com,example)/?example=1 - {"access": "allow_ignore_embargo", "user": "staff2"}

tests/test_acl.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,23 @@ def test_blocked_url(self):
4141
assert 'Access Blocked' in resp.text
4242

4343
def test_allow_via_acl_header(self):
44-
resp = self.query('http://www.iana.org/about/')
45-
44+
resp = self.testapp.get('/pywb/cdx?url=http://www.iana.org/about/', headers={"X-Pywb-Acl-User": "staff"})
4645
assert len(resp.text.splitlines()) == 1
4746

4847
resp = self.testapp.get('/pywb/mp_/http://www.iana.org/about/', headers={"X-Pywb-Acl-User": "staff"}, status=200)
4948

49+
def test_block_via_acl_header(self):
50+
resp = self.testapp.get('/pywb/cdx?url=http://example.com/?example=3', headers={"X-Pywb-Acl-User": "staff"})
51+
assert len(resp.text.splitlines()) > 0
52+
53+
resp = self.testapp.get('/pywb/mp_/http://example.com/?example=3', headers={"X-Pywb-Acl-User": "staff"}, status=451)
54+
55+
def test_exclude_via_acl_header(self):
56+
resp = self.testapp.get('/pywb/cdx?url=http://example.com/?example=3', headers={"X-Pywb-Acl-User": "staff2"})
57+
assert len(resp.text.splitlines()) == 0
58+
59+
resp = self.testapp.get('/pywb/mp_/http://example.com/?example=3', headers={"X-Pywb-Acl-User": "staff2"}, status=404)
60+
5061
def test_allowed_more_specific(self):
5162
resp = self.query('http://www.iana.org/_css/2013.1/fonts/opensans-semibold.ttf')
5263

tests/test_embargo.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,12 @@ def test_embargo_ignore_acl(self):
4646
def test_embargo_ignore_acl_with_header_only(self):
4747
# ignore embargo with custom header only
4848
headers = {"X-Pywb-ACL-User": "staff2"}
49-
resp = self.testapp.get('/pywb-embargo-acl/20140126201054mp_/http://example.com/?example=1', status=200, headers=headers)
5049

50+
resp = self.testapp.get('/pywb-embargo-acl/cdx?url=http://example.com/?example=1', headers=headers)
51+
assert len(resp.text.splitlines()) > 0
52+
resp = self.testapp.get('/pywb-embargo-acl/20140126201054mp_/http://example.com/?example=1', status=200, headers=headers)
53+
resp = self.testapp.get('/pywb-embargo-acl/cdx?url=http://example.com/?example=1')
54+
assert len(resp.text.splitlines()) == 0
5155
resp = self.testapp.get('/pywb-embargo-acl/20140126201054mp_/http://example.com/?example=1', status=404)
5256

5357

0 commit comments

Comments
 (0)