Opened 3 weeks ago

Closed 3 weeks ago

#37469 closed Bug (fixed)

Lexemes with only escaped characters cause a crash

Reported by: François Freitag Owned by: François Freitag
Component: contrib.postgres Version: 6.0
Severity: Normal Keywords: full-text search, psycopg3
Cc: François Freitag, Calvin Vu Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Searching for Lexeme('&') passes the provided value through psql_escape, which returns None. When UTF8Dumper calls bytes(None, "utf-8"):

ERROR: test_quote_lexeme (postgres_tests.test_search.TestLexemes.test_quote_lexeme) (lexeme='&')
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/pdi/dev/django/tests/postgres_tests/test_search.py", line 886, in test_quote_lexeme
    self.assertEqual(quote_lexeme(lexeme), quoted)
                     ~~~~~~~~~~~~^^^^^^^^
  File "/home/pdi/dev/django/django/contrib/postgres/search.py", line 26, in quote_lexeme
    return UTF8Dumper(str).quote(psql_escape(value)).decode()
           ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
  File "/home/pdi/dev/django/.venv/lib/python3.14/site-packages/psycopg/adapt.py", line 59, in quote
    if (value := self.dump(obj)) is None:
                 ~~~~~~~~~^^^^^
  File "/home/pdi/dev/django/django/contrib/postgres/search.py", line 23, in dump
    return bytes(obj, "utf-8")
TypeError: encoding without a string argument

See the attached patch to reproduce in the django test suite.

Lexeme is documented as:

Lexeme objects allow search operators to be safely used with strings from an untrusted source. The content of each lexeme is escaped so that any operators that may exist in the string itself will not be interpreted.

Attachments (1)

repro.patch​ (539 bytes ) - added by François Freitag 3 weeks ago.

Download all attachments as: .zip

Change History (10)

by François Freitag, 3 weeks ago

Attachment: repro.patch​ added

comment:1 by François Freitag, 3 weeks ago

Summary: Lexeme with escaped character causes a crash → Lexemes with only escaped characters cause a crash

comment:2 by Jacob Walls, 3 weeks ago

Cc: Calvin Vu added
Component: Uncategorized → contrib.postgres
Keywords: psycopg3 added
Triage Stage: Unreviewed → Accepted
Version: dev → 6.0

Thanks for the report! Appears to only affect the psycopg3 path. Would you like to submit a PR?

comment:3 by himanshu0870, 3 weeks ago

Has patch: set
Owner: set to himanshu0870
Status: new → assigned

I'm working on a patch for this issue.

comment:4 by François Freitag, 3 weeks ago

comment:5 by Jacob Walls, 3 weeks ago

Thanks for the link. No pressure, but if you weren't sure, you are certainly welcome to submit a PR despite the previous messages. (The original reporter has right of first refusal.)

comment:6 by François Freitag, 3 weeks ago

Thanks for the explaining the process, I wouldn’t have submitted a PR otherwise.

​https://github.com/django/django/pull/22241

comment:7 by François Freitag, 3 weeks ago

Owner: changed from himanshu0870 to François Freitag

comment:8 by Jacob Walls, 3 weeks ago

Triage Stage: Accepted → Ready for checkin

comment:9 by Jacob Walls <jacobtylerwalls@…>, 3 weeks ago

Resolution: → fixed
Status: assigned → closed

In 1860f0a3:

Fixed #37469 -- Made psycopg adapter UTF8Dumper handle None.

The adapter for psycopg2 serialized None to SQL NULL [1]. This behavior
should be maintained for psycopg3, as psql_escape returns None when no
value remain after escaping and stripping.

Thanks to Camille Daniel for the initial bug report.

[1] ​https://github.com/psycopg/psycopg2/blob/7d0375492ec3707b82b89f632c9654b400466d86/psycopg/adapter_asis.c#L42-L45

Note: See TracTickets for help on using tickets.
Back to Top