Skip to content

Commit 466968e

Browse files
committed
cookie_rewriter: ensure cookie paths are always relative
cookie_rewriter tests: add cookie rewriter tests for secure, httponly html_rewriter tests: add <base> rel and abs rewrite tests no cover for waitress as its not used by default
1 parent 569614d commit 466968e

File tree

4 files changed

+28
-15
lines changed

4 files changed

+28
-15
lines changed

pywb/apps/cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ def load(self): #pragma: no cover
4949
pass
5050

5151
def run(self):
52-
if self.r.server == 'waitress':
52+
if self.r.server == 'waitress': #pragma: no cover
5353
self.run_waitress()
5454
else:
5555
self.run_wsgiref()
5656

57-
def run_waitress(self):
57+
def run_waitress(self): #pragma: no cover
5858
from waitress import serve
5959
print(self.desc)
6060
serve(self.application, port=self.r.port, threads=self.r.threads)

pywb/rewrite/cookie_rewriter.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,14 @@ def rewrite(self, cookie_str, header='Set-Cookie'):
1818

1919
for name, morsel in cookie.iteritems():
2020
morsel = self.rewrite_cookie(name, morsel)
21+
2122
if morsel:
23+
path = morsel.get('path')
24+
if path:
25+
inx = path.find(self.url_rewriter.rel_prefix)
26+
if inx > 0:
27+
morsel['path'] = path[inx:]
28+
2229
results.append((header, morsel.OutputString()))
2330

2431
return results
@@ -78,23 +85,14 @@ class HostScopeCookieRewriter(WbUrlBaseCookieRewriter):
7885
"""
7986

8087
def rewrite_cookie(self, name, morsel):
81-
new_path = None
82-
8388
# if domain set, expand cookie to host prefix
8489
if morsel.get('domain'):
8590
del morsel['domain']
86-
new_path = self.url_rewriter.rewrite('/')
91+
morsel['path'] = self.url_rewriter.rewrite('/')
8792

8893
# set cookie to rewritten path
8994
elif morsel.get('path'):
90-
new_path = self.url_rewriter.rewrite(morsel['path'])
91-
92-
if new_path:
93-
inx = new_path.find(self.url_rewriter.rel_prefix)
94-
if inx > 0:
95-
new_path = new_path[inx:]
96-
97-
morsel['path'] = new_path
95+
morsel['path'] = self.url_rewriter.rewrite(morsel['path'])
9896

9997
self._remove_age_opts(morsel)
10098
return morsel

pywb/rewrite/test/test_cookie_rewriter.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,15 @@
3434
3535
3636
# HostCookieRewriter -- set path to host
37-
>>> rewrite_cookie('some=value; Path=/diff/path/;', urlrewriter, 'host')
37+
>>> rewrite_cookie('some=value; Path=/diff/path/', urlrewriter, 'host')
3838
[('Set-Cookie', 'some=value; Path=/pywb/20131226101010/http://example.com/diff/path/')]
3939
4040
>>> rewrite_cookie('some=value; Domain=.example.com; Path=/diff/path/; Max-Age=1500', urlrewriter, 'host')
4141
[('Set-Cookie', 'some=value; Path=/pywb/20131226101010/http://example.com/')]
4242
43+
>>> rewrite_cookie('some=value; Domain=.example.com; Secure; Path=/diff/path/; HttpOnly; Max-Age=1500', urlrewriter, 'host')
44+
[('Set-Cookie', 'some=value; httponly; Path=/pywb/20131226101010/http://example.com/')]
45+
4346
4447
# RootCookieRewriter -- always sets Path=/, removes Domain
4548
>>> rewrite_cookie('some=value; Path=/diff/path/;', urlrewriter, 'root')
@@ -62,7 +65,7 @@
6265
from pywb.rewrite.cookie_rewriter import MinimalScopeCookieRewriter, get_cookie_rewriter
6366
from pywb.rewrite.url_rewriter import UrlRewriter
6467

65-
urlrewriter = UrlRewriter('20131226101010/http://example.com/some/path/index.html', '/pywb/')
68+
urlrewriter = UrlRewriter('20131226101010/http://example.com/some/path/index.html', 'http://localhost:8080/pywb/', rel_prefix='/pywb/')
6669

6770
urlrewriter2 = UrlRewriter('em_/http://example.com/', '/preview/')
6871

pywb/rewrite/test/test_html_rewriter.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@
2424
>>> parse('<html><head><base href="http://example.com/diff/path/file.html"/>')
2525
<html><head><base href="/web/20131226101010/http://example.com/diff/path/file.html"/>
2626
27+
# Full Path
28+
>>> parse('<html><head><base href="http://example.com/diff/path/file.html"/>', urlrewriter=full_path_urlrewriter)
29+
<html><head><base href="http://localhost:80/web/20131226101010/http://example.com/diff/path/file.html"/>
30+
31+
# Rel Base
32+
>>> parse('<html><head><base href="/other/file.html"/>', urlrewriter=full_path_urlrewriter)
33+
<html><head><base href="/web/20131226101010/http://example.com/other/file.html"/>
34+
2735
>>> parse('<base href="static/"/><img src="image.gif"/>')
2836
<base href="/web/20131226101010/http://example.com/some/path/static/"/><img src="/web/20131226101010im_/http://example.com/some/path/static/image.gif"/>
2937
@@ -186,6 +194,10 @@
186194
'/web/',
187195
rewrite_opts=dict(punycode_links=False))
188196

197+
full_path_urlrewriter = UrlRewriter('20131226101010/http://example.com/some/path/index.html',
198+
'http://localhost:80/web/',
199+
rewrite_opts=dict(punycode_links=False))
200+
189201
urlrewriter_pencode = UrlRewriter('20131226101010/http://example.com/some/path/index.html',
190202
'/web/',
191203
rewrite_opts=dict(punycode_links=True))

0 commit comments

Comments
 (0)