Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly escape url in the HTML generated in respnose to hURL requests #8

Merged
merged 1 commit into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Fixed

- Fixed not escaping the URL string when generating HTML responses to hURL: requests.

## v3.0.0 (2022-11-25)

### Added
Expand Down
5 changes: 4 additions & 1 deletion pygopherd/handlers/url.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import html
import re

from pygopherd import gopherentry, handlers
Expand Down Expand Up @@ -42,6 +43,9 @@ def write(self, wfile):
url = self.selector[4:] # Strip off URL:
if self.selector[0] == "/":
url = self.selector[5:]

url = html.escape(url)

outdoc = "<HTML><HEAD>\n"
outdoc += '<META HTTP-EQUIV="refresh" content="5;URL=%s">' % url
outdoc += "</HEAD><BODY>\n"
Expand Down Expand Up @@ -77,7 +81,6 @@ def canhandlerequest(self):
)

def gethandler(self):

handlers.HandlerMultiplexer.init_default_handlers(self.config)
handlerlist = [
x for x in handlers.HandlerMultiplexer.handlers if x != URLTypeRewriter
Expand Down
24 changes: 24 additions & 0 deletions tests/handlers/test_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,30 @@ def test_url_rewriter_handler(self):
b'<A HREF="http://gopher.quux.org/">http://gopher.quux.org/</A>', out
)

def test_handler_escape_urls(self):
"""
URLs should be escaped in the generated HTML.
"""
handler = HTMLURLHandler(
'URL:http://gopher.quux.org/"<script>',
"",
self.protocol,
self.config,
self.stat_result,
self.vfs,
)

entry = handler.getentry()
self.assertEqual(entry.mimetype, "text/html")
self.assertEqual(entry.type, "h")

wfile = io.BytesIO()
handler.write(wfile)

out = wfile.getvalue()
self.assertNotIn(b'http://gopher.quux.org/"<script>', out)
self.assertIn(b"http://gopher.quux.org/&quot;&lt;script&gt;", out)


class TestURLTypeRewriterHandler(unittest.TestCase):
def setUp(self):
Expand Down
Loading