Opened 60 minutes ago

Last modified 27 minutes ago

#37469 new Bug

Lexemes with only escaped characters cause a crash

Reported by: François Freitag Owned by:
Component: contrib.postgres Version: 6.0
Severity: Normal Keywords: full-text search, psycopg3
Cc: François Freitag, Calvin Vu Triage Stage: Accepted
Has patch: no 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 59 minutes ago.

Download all attachments as: .zip

Change History (3)

by François Freitag, 59 minutes ago

Attachment: repro.patch​ added

comment:1 by François Freitag, 53 minutes ago

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

comment:2 by Jacob Walls, 27 minutes 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?

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