From b7c3e7b959d720282366a582e6a48cf637b36253 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Wed, 24 Jun 2015 07:46:15 +0200 Subject: [PATCH] Disallow {foo} in search engine URLs. This causes an KeyError otherwise when trying to use str.format to insert the search term. --- qutebrowser/config/configtypes.py | 7 +++++++ tests/config/test_configtypes.py | 5 +++++ 2 files changed, 12 insertions(+) diff --git a/qutebrowser/config/configtypes.py b/qutebrowser/config/configtypes.py index 23909b9c1..d3f0b0d8d 100644 --- a/qutebrowser/config/configtypes.py +++ b/qutebrowser/config/configtypes.py @@ -1110,8 +1110,15 @@ class SearchEngineUrl(BaseType): return else: raise configexc.ValidationError(value, "may not be empty!") + if '{}' not in value: raise configexc.ValidationError(value, "must contain \"{}\"") + try: + value.format("") + except KeyError: + raise configexc.ValidationError( + value, "may not contain {...} (use {{ and }} for literal {/})") + url = QUrl(value.replace('{}', 'foobar')) if not url.isValid(): raise configexc.ValidationError(value, "invalid url, {}".format( diff --git a/tests/config/test_configtypes.py b/tests/config/test_configtypes.py index 6b6a08594..edb0bb8cb 100644 --- a/tests/config/test_configtypes.py +++ b/tests/config/test_configtypes.py @@ -1881,6 +1881,11 @@ class TestSearchEngineUrl: with pytest.raises(configexc.ValidationError): self.t.validate(':{}') + def test_validate_format_string(self): + """Test validate with a {foo} format string.""" + with pytest.raises(configexc.ValidationError): + self.t.validate('foo{bar}baz{}') + def test_transform_empty(self): """Test transform with an empty value.""" assert self.t.transform('') is None