Skip to content

Commit fb6ab74

Browse files
Merge pull request #8 from michael-lazar/hurl_escape
Properly escape url in the HTML generated in respnose to hURL requests
2 parents 02c65d6 + 3db9b79 commit fb6ab74

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
### Fixed
11+
12+
- Fixed not escaping the URL string when generating HTML responses to hURL: requests.
13+
1014
## v3.0.0 (2022-11-25)
1115

1216
### Added

pygopherd/handlers/url.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import html
12
import re
23

34
from pygopherd import gopherentry, handlers
@@ -42,6 +43,9 @@ def write(self, wfile):
4243
url = self.selector[4:] # Strip off URL:
4344
if self.selector[0] == "/":
4445
url = self.selector[5:]
46+
47+
url = html.escape(url)
48+
4549
outdoc = "<HTML><HEAD>\n"
4650
outdoc += '<META HTTP-EQUIV="refresh" content="5;URL=%s">' % url
4751
outdoc += "</HEAD><BODY>\n"
@@ -77,7 +81,6 @@ def canhandlerequest(self):
7781
)
7882

7983
def gethandler(self):
80-
8184
handlers.HandlerMultiplexer.init_default_handlers(self.config)
8285
handlerlist = [
8386
x for x in handlers.HandlerMultiplexer.handlers if x != URLTypeRewriter

tests/handlers/test_url.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,30 @@ def test_url_rewriter_handler(self):
3838
b'<A HREF="http://gopher.quux.org/">http://gopher.quux.org/</A>', out
3939
)
4040

41+
def test_handler_escape_urls(self):
42+
"""
43+
URLs should be escaped in the generated HTML.
44+
"""
45+
handler = HTMLURLHandler(
46+
'URL:http://gopher.quux.org/"<script>',
47+
"",
48+
self.protocol,
49+
self.config,
50+
self.stat_result,
51+
self.vfs,
52+
)
53+
54+
entry = handler.getentry()
55+
self.assertEqual(entry.mimetype, "text/html")
56+
self.assertEqual(entry.type, "h")
57+
58+
wfile = io.BytesIO()
59+
handler.write(wfile)
60+
61+
out = wfile.getvalue()
62+
self.assertNotIn(b'http://gopher.quux.org/"<script>', out)
63+
self.assertIn(b"http://gopher.quux.org/&quot;&lt;script&gt;", out)
64+
4165

4266
class TestURLTypeRewriterHandler(unittest.TestCase):
4367
def setUp(self):

0 commit comments

Comments
 (0)