diff --git a/doc/TODO b/doc/TODO index be3a93c70..f829ac6cd 100644 --- a/doc/TODO +++ b/doc/TODO @@ -7,7 +7,6 @@ Before 0.1 - Website - FAQ - Single-instance and multi-window -- Tests for URL detection and fix corner cases - Maybe test under other platforms - Autosave feature (against segfaults/...) - Save tabs feature diff --git a/qutebrowser/test/utils/test_url.py b/qutebrowser/test/utils/test_url.py index c9c05e761..2dd858f78 100644 --- a/qutebrowser/test/utils/test_url.py +++ b/qutebrowser/test/utils/test_url.py @@ -126,6 +126,10 @@ class IsUrlTests(unittest.TestCase): 'http://foobar', 'localhost:8080', 'qutebrowser.org', + '127.0.0.1', + '::1', + '2001:41d0:2:6c11::1', + '94.23.233.17', ) NOT_URLS = ( @@ -133,6 +137,10 @@ class IsUrlTests(unittest.TestCase): 'localhost test', 'another . test', 'foo', + 'this is: not an URL', + '23.42', + '1337', + 'deadbeef', ) def test_urls(self, configmock): diff --git a/qutebrowser/utils/urlutils.py b/qutebrowser/utils/urlutils.py index fe431e224..21ffb6a30 100644 --- a/qutebrowser/utils/urlutils.py +++ b/qutebrowser/utils/urlutils.py @@ -21,6 +21,7 @@ import re import os.path +import ipaddress import urllib.parse from PyQt5.QtCore import QUrl @@ -78,9 +79,18 @@ def _is_url_naive(urlstr): True if the URL really is a URL, False otherwise. """ url = QUrl.fromUserInput(urlstr) - # We don't use url here because fromUserInput appends http:// - # automatically. - if not url.isValid(): + try: + ipaddress.ip_address(urlstr) + except ValueError: + pass + else: + # Valid IPv4/IPv6 address + return True + if re.search(r'^[0-9.]+$', urlstr): + # Qt treats things like "23.42" or "1337" as valid URLs which we don't + # want to. Note we already filtered *real* valid IPs above. + return False + elif not url.isValid(): return False elif '.' in url.host(): return True