From dfb623ce90b67042cffb6fe4afc152bdcfae0a4c Mon Sep 17 00:00:00 2001 From: selberhad Date: Mon, 3 Oct 2022 20:49:44 -0400 Subject: [PATCH] prevent urls with no protocol that are too short from being highlighted --- evennia/utils/tests/test_tagparsing.py | 6 ++++++ evennia/utils/text2html.py | 6 +++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/evennia/utils/tests/test_tagparsing.py b/evennia/utils/tests/test_tagparsing.py index 44d1cf9681..933d38d3f2 100644 --- a/evennia/utils/tests/test_tagparsing.py +++ b/evennia/utils/tests/test_tagparsing.py @@ -356,3 +356,9 @@ class TestTextToHTMLparser(TestCase): self.parser.convert_urls('Awwww.this should not be highlighted'), 'Awwww.this should not be highlighted' ) + + def test_invalid_www_url(self): + self.assertEqual( + self.parser.convert_urls('www.t'), + 'www.t' + ) diff --git a/evennia/utils/text2html.py b/evennia/utils/text2html.py index e2fedc835f..d10c1c5d39 100644 --- a/evennia/utils/text2html.py +++ b/evennia/utils/text2html.py @@ -91,6 +91,7 @@ class TextToHTMLparser(object): r'(?\[\]\s])+)(\.(?:\s|$)|&\w+;|)' ) re_protocol = re.compile(r'^(?:ftp|https?)://') + re_valid_no_protocol = re.compile(r'^(?:www|ftp)\.[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b[-a-zA-Z0-9@:%_\+.~#?&//=]*') re_mxplink = re.compile(r"\|lc(.*?)\|lt(.*?)\|le", re.DOTALL) re_mxpurl = re.compile(r"\|lu(.*?)\|lt(.*?)\|le", re.DOTALL) @@ -152,8 +153,11 @@ class TextToHTMLparser(object): if m: href = m.group(1) label = href - # if there is no protocol (i.e. starts with www) prefix with // so the link isn't treated as relative + # if there is no protocol (i.e. starts with www or ftp) + # prefix with // so the link isn't treated as relative if not self.re_protocol.match(href): + if not self.re_valid_no_protocol.match(href): + return text href = "//" + href rest = m.group(2) # -> added target to output prevent the web browser from attempting to