0

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.

  • Your data seems to be URL-encoded (characters not valid for a URL are "escaped") Maybe [this other question](https://stackoverflow.com/q/16566069) will help? – Savir Oct 28 '22 at 15:37
  • Does that apply to this, i can see that the url sent to the backend is: ?`query=100%25+natural` but the browser shows: `search/100%25%20natural` i can see one shows a +natural and the other %20 – CriticaIError Oct 28 '22 at 17:23

1 Answers1

0

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

Lucas Grugru
  • 1,664
  • 1
  • 4
  • 18