Handle FF sending bad search suggestion param

Occasionally, Firefox will send the search suggestion
string to the server without a mimetype, resulting in the suggestion
only appearing in Flask's `request.data` field. This field is typically
not used for parsing arguments, as the documentation states:

Contains the incoming request data as string in case it came with a
mimetype Flask does not handle.

This fix captures the bytes object sent to the server and parses it into
a normal query to be used in forming suggestions.
main
Ben Busby 2020-10-28 23:02:41 -04:00
parent 7a61220aa5
commit 933ce7e068
2 changed files with 10 additions and 2 deletions

View File

@ -136,6 +136,10 @@ def opensearch():
@app.route('/autocomplete', methods=['GET', 'POST']) @app.route('/autocomplete', methods=['GET', 'POST'])
def autocomplete(): def autocomplete():
q = g.request_params.get('q') q = g.request_params.get('q')
if not q:
# FF will occasionally (incorrectly) send the q field without a
# mimetype in the format "b'q=<query>'" through the request.data field
q = str(request.data).replace('q=', '')
# Search bangs if the query begins with "!", but not "! " (feeling lucky) # Search bangs if the query begins with "!", but not "! " (feeling lucky)
if q.startswith('!') and len(q) > 1 and not q.startswith('! '): if q.startswith('!') and len(q) > 1 and not q.startswith('! '):

File diff suppressed because one or more lines are too long