jazzband / django-silk

Silky smooth profiling for Django
MIT License
4.35k stars 333 forks source link

execute_sql: fix DjangoUnicodeDecodeError being raised #658

Closed bpascard closed 1 year ago

bpascard commented 1 year ago

The aim of this pull request is to prevent an exception from being raised when profiling an SQL query that contains parameters which are arbitrary bytes.

Queries that contain parameters that are bytes can still raise DjangoUnicodeDecodeError if the codec cannot decode the bytes. This is because force_str() uses 'strict' mode by default. This fix permanently solves issue #157.

Before

>>> force_str(b'some query with \xb4')
Traceback (most recent call last):
  File "/lib/python3.9/site-packages/django/utils/encoding.py", line 62, in force_str
    s = str(s, encoding, errors)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb4 in position 16: invalid start byte

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/lib/python3.9/site-packages/django/utils/encoding.py", line 66, in force_str
    raise DjangoUnicodeDecodeError(s, *e.args)
django.utils.encoding.DjangoUnicodeDecodeError: 'utf-8' codec can't decode byte 0xb4 in position 16: invalid start byte. You passed in b'some query with \xb4' (<class 'bytes'>)

After

>>> force_str(b'some query with \xb4', errors="backslashreplace")
'some query with \\xb4'
codecov[bot] commented 1 year ago

Codecov Report

Merging #658 (3f1cace) into master (274898f) will not change coverage. The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master     #658   +/-   ##
=======================================
  Coverage   86.51%   86.51%           
=======================================
  Files          52       52           
  Lines        2091     2091           
=======================================
  Hits         1809     1809           
  Misses        282      282           
Impacted Files Coverage Δ
silk/sql.py 78.78% <100.00%> (ø)

:mega: We’re building smart automated test selection to slash your CI/CD build times. Learn more