You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It's not immediately obvious how to escape the repl (replacement for matches) argument to re.sub() and re.subn() if repl is chosen by a potentially hostile actor. Obviously, re.escape() isn't the answer, as that escapes far too much.
The right answer seems to be escaped_repl = raw_repl.replace(bslash, bslash*2) where bslash = '\\'. It might be worth adding this to the documentation.
Here's the code I used to empirically validate the "right answer" given above (checked on Python 3.8 & 3.12):
Documentation
It's not immediately obvious how to escape the
repl
(replacement for matches) argument tore.sub()
andre.subn()
ifrepl
is chosen by a potentially hostile actor. Obviously,re.escape()
isn't the answer, as that escapes far too much.The right answer seems to be
escaped_repl = raw_repl.replace(bslash, bslash*2)
wherebslash = '\\'
. It might be worth adding this to the documentation.Here's the code I used to empirically validate the "right answer" given above (checked on Python 3.8 & 3.12):
The text was updated successfully, but these errors were encountered: