Skip to content

Commit 7365ee3

Browse files
committed
Prevent TypeError when bytes passed to cursor.execute in debug middleware
If DjangoDebugMiddleware is installed, calling `cursor.execute(b)` where b is a `bytes` object causes the recording (and thus the entire database call) to throw a TypeError due to https://github.com/graphql-python/graphene-django/blob/775644b5369bdc5fbb45d3535ae391a069ebf9d4/graphene_django/debug/sql/tracking.py#L126 : ``` "is_select": sql.lower().strip().startswith("select"), ``` Calling execute with a bytes parameter, to my knowledge, is not currently done within the high-level abstractions in the Django ORM, but is very much supported by psycopg2, as evidenced by the use in psycopg2's own `execute_values` in https://github.com/psycopg/psycopg2/blob/2_9_3/lib/extras.py#L1270 : ``` cur.execute(b''.join(parts)) ``` This fix ensures that the sql parameter is safely decoded before scanning whether it begins with SELECT; since this is the only usage, the change is trivial. The only workaround if code calls execute_values is to disable the DjangoDebugMiddleware altogether, which is far from ideal.
1 parent e8f36b0 commit 7365ee3

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

graphene_django/debug/sql/tracking.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ def _record(self, method, sql, params):
109109
alias = getattr(self.db, "alias", "default")
110110
conn = self.db.connection
111111
vendor = getattr(conn, "vendor", "unknown")
112+
sql_str = sql.decode(errors="ignore") if isinstance(sql, bytes) else sql
112113

113114
params = {
114115
"vendor": vendor,
@@ -122,7 +123,7 @@ def _record(self, method, sql, params):
122123
"start_time": start_time,
123124
"stop_time": stop_time,
124125
"is_slow": duration > 10,
125-
"is_select": sql.lower().strip().startswith("select"),
126+
"is_select": sql_str.lower().strip().startswith("select"),
126127
}
127128

128129
if vendor == "postgresql":

0 commit comments

Comments
 (0)