Skip to content

Commit 08b0ac8

Browse files
authored
scheme: add support for X-Forwarded-Proto header to specify the scheme to better address #314, #374 (#395)
1 parent b39274c commit 08b0ac8

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

pywb/apps/rewriterapp.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ def __init__(self, framed_replay=False, jinja_env=None, config=None, paths=None)
103103
else:
104104
self.csp_header = None
105105

106+
# deprecated: Use X-Forwarded-Proto header instead!
106107
self.force_scheme = config.get('force_scheme')
107108

108109
def add_csp_header(self, wb_url, status_headers):
@@ -223,8 +224,10 @@ def render_content(self, wb_url, kwargs, environ):
223224
wb_url = wb_url.replace('#', '%23')
224225
wb_url = WbUrl(wb_url)
225226

226-
if self.force_scheme:
227-
environ['wsgi.url_scheme'] = self.force_scheme
227+
proto = environ.get('HTTP_X_FORWARDED_PROTO', self.force_scheme)
228+
229+
if proto:
230+
environ['wsgi.url_scheme'] = proto
228231

229232
is_timegate = self._check_accept_dt(wb_url, environ)
230233

tests/test_force_https.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,21 @@
55
class TestForceHttps(BaseConfigTest):
66
@classmethod
77
def setup_class(cls):
8-
super(TestForceHttps, cls).setup_class('config_test.yaml',
9-
custom_config={'force_scheme': 'https'})
8+
super(TestForceHttps, cls).setup_class('config_test.yaml')
9+
10+
def test_force_https_replay_1(self, fmod):
11+
resp = self.get('/pywb/20140128051539{0}/http://example.com/', fmod,
12+
headers={'X-Forwarded-Proto': 'https'})
13+
14+
assert '"https://localhost:80/pywb/20140128051539{0}/http://www.iana.org/domains/example"'.format(fmod) in resp.text, resp.text
15+
16+
17+
# ============================================================================
18+
class TestForceHttpsConfig(BaseConfigTest):
19+
@classmethod
20+
def setup_class(cls):
21+
super(TestForceHttpsConfig, cls).setup_class('config_test.yaml',
22+
custom_config={'force_scheme': 'https'})
1023

1124
def test_force_https_replay_1(self, fmod):
1225
resp = self.get('/pywb/20140128051539{0}/http://example.com/', fmod)
@@ -18,11 +31,11 @@ def test_force_https_replay_1(self, fmod):
1831
class TestForceHttpsRedirect(BaseConfigTest):
1932
@classmethod
2033
def setup_class(cls):
21-
super(TestForceHttpsRedirect, cls).setup_class('config_test_redirect_classic.yaml',
22-
custom_config={'force_scheme': 'https'})
34+
super(TestForceHttpsRedirect, cls).setup_class('config_test_redirect_classic.yaml')
2335

2436
def test_force_https_redirect_replay_1(self, fmod):
25-
resp = self.get('/pywb/20140128051539{0}/http://example.com/', fmod)
37+
resp = self.get('/pywb/20140128051539{0}/http://example.com/', fmod,
38+
headers={'X-Forwarded-Proto': 'https'})
2639

2740
assert resp.headers['Location'] == 'https://localhost:80/pywb/20140127171251{0}/http://example.com'.format(fmod)
2841
resp = resp.follow()
@@ -37,11 +50,11 @@ def test_force_https_redirect_replay_1(self, fmod):
3750
class TestForceHttpsRoot(BaseConfigTest):
3851
@classmethod
3952
def setup_class(cls):
40-
super(TestForceHttpsRoot, cls).setup_class('config_test_root_coll.yaml',
41-
custom_config={'force_scheme': 'https'})
53+
super(TestForceHttpsRoot, cls).setup_class('config_test_root_coll.yaml')
4254

4355
def test_force_https_root_replay_1(self, fmod):
44-
resp = self.get('/20140128051539{0}/http://www.iana.org/domains/example', fmod)
56+
resp = self.get('/20140128051539{0}/http://www.iana.org/domains/example', fmod,
57+
headers={'X-Forwarded-Proto': 'https'})
4558

4659
assert resp.headers['Location'] == 'https://localhost:80/20140128051539{0}/https://www.iana.org/domains/reserved'.format(fmod)
4760

0 commit comments

Comments
 (0)