Search code examples
pythondjangosearchparameters

Invalid hex encoding in query string


im currently working on a DJango search with multiple query_params, since im using Sentry i found out that sometimes there's an issue triggered when you search for example something with percentages:

"100% natural" "100% unique" "50% blah blah blah"

Sentry: Unhandled Invalid hex encoding in query string.

This is marked in the oauth lib

if INVALID_HEX_PATTERN.search(query):
        raise ValueError('Invalid hex encoding in query string.')

The current search code allows to pass any query_param like this:

re_path(r"^search/?$", search_system, name="search")

And inside the view i do have this:

query = request.query_params.get("query")
query = query.replace("%20", " ") if query else None

i tried to replace the %20 for an space, but sometimes it happens and sometimes it doesn't so it happens randomly, i don't know if im doing something wrong, or actually the question would be:

is there anything i could do to avoid triggering this alert without doing anything in sentry? for example like cleaning up the data like a form

Thanks in advance.

To avoid this issue i tried to set the query replace("%20", " "), my best guess is that the error is triggered when you have the "percentage + space" next to each other so the url looks like

search/100%25%20natural which in python should be like "100% natural" but as i said sometimes it triggers and sometimes it doesn't.


Solution

  • There is a function for url decoding your data. space is not the only possible encoded chars...

    from urllib.parse import unquote
    
    query = request.query_params.get("query")
    query = unquote(query) if query else None