diff --git a/apps/search/client.py b/apps/search/client.py index 8e8bbf2807d..52f0fcb01a2 100644 --- a/apps/search/client.py +++ b/apps/search/client.py @@ -29,6 +29,9 @@ def get_category_id(category, application): if len(category): return category[0].id +def sanitize_query(term): + term = term.strip('^$ ') + return term def extract_from_query(term, filter, regexp, options={}): """ @@ -227,6 +230,7 @@ def query(self, term, limit=10, offset=0, **kwargs): # * Logging try: + term = sanitize_query(term) result = sc.Query(term) except socket.timeout: log.error("Query has timed out.") diff --git a/apps/search/tests.py b/apps/search/tests.py index 4bb2f2d5f8f..f4c4304fc14 100644 --- a/apps/search/tests.py +++ b/apps/search/tests.py @@ -223,6 +223,17 @@ def test_status_filter(self): eq_(query("MozEx", status=[amo.STATUS_PUBLIC, amo.STATUS_SANDBOX])[0].id, 40) + def test_badchars(self): + """ Sphinx doesn't like queries that are entirely '$', '^' or '^ $' """ + bad_guys = ('^', '$', '$ ^', '^ s $', '$s^', ' $ s ^', ' ^ s $', + '^$', '^ $') + + for guy in bad_guys: + try: + query(guy) + except SearchError: + assert False, "Error querying for %s" % guy + class TestSearchForm(test_utils.TestCase): fixtures = ['base/addons']