From eb276df8767b23b0073481be9f46dff056f36c3e Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Tue, 19 Jan 2016 07:03:43 +0100 Subject: [PATCH] Handle empty term in urlutils._get_search_url. Fixes #1239. --- qutebrowser/utils/urlutils.py | 10 ++++++++-- tests/unit/utils/test_urlutils.py | 8 +++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/qutebrowser/utils/urlutils.py b/qutebrowser/utils/urlutils.py index 53637a466..b20e833c8 100644 --- a/qutebrowser/utils/urlutils.py +++ b/qutebrowser/utils/urlutils.py @@ -74,6 +74,8 @@ def _parse_search_term(s): term = s else: term = split[1] + elif not split: + raise ValueError("Empty search term!") else: engine = None term = s @@ -253,8 +255,12 @@ def is_url(urlstr): if not autosearch: # no autosearch, so everything is a URL unless it has an explicit # search engine. - engine, _term = _parse_search_term(urlstr) - return engine is None + try: + engine, _term = _parse_search_term(urlstr) + except ValueError: + return False + else: + return engine is None if not qurl_userinput.isValid(): # This will also catch URLs containing spaces. diff --git a/tests/unit/utils/test_urlutils.py b/tests/unit/utils/test_urlutils.py index 99e61a832..4f85022b9 100644 --- a/tests/unit/utils/test_urlutils.py +++ b/tests/unit/utils/test_urlutils.py @@ -268,6 +268,12 @@ def test_get_search_url(urlutils_config_stub, url, host, query): assert url.query() == query +@pytest.mark.parametrize('url', ['\n', ' ', '\n ']) +def test_get_search_url_invalid(urlutils_config_stub, url): + with pytest.raises(ValueError): + urlutils._get_search_url(url) + + @pytest.mark.parametrize('is_url, is_url_no_autosearch, uses_dns, url', [ # Normal hosts (True, True, False, 'http://foobar'), @@ -288,7 +294,7 @@ def test_get_search_url(urlutils_config_stub, url, host, query): # _has_explicit_scheme False, special_url True (True, True, False, 'qute::foo'), # Invalid URLs - (False, True, False, ''), + (False, False, False, ''), (False, True, False, 'http:foo:0'), # Not URLs (False, True, False, 'foo bar'), # no DNS because of space