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)
Change History (10)
by , 3 weeks ago
| Attachment: | repro.patch added |
|---|
comment:1 by , 3 weeks ago
| Summary: | Lexeme with escaped character causes a crash → Lexemes with only escaped characters cause a crash |
|---|
comment:2 by , 3 weeks ago
| Cc: | added |
|---|---|
| Component: | Uncategorized → contrib.postgres |
| Keywords: | psycopg3 added |
| Triage Stage: | Unreviewed → Accepted |
| Version: | dev → 6.0 |
comment:3 by , 3 weeks ago
| Has patch: | set |
|---|---|
| Owner: | set to |
| Status: | new → assigned |
I'm working on a patch for this issue.
comment:5 by , 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 , 3 weeks ago
Thanks for the explaining the process, I wouldn’t have submitted a PR otherwise.
comment:7 by , 3 weeks ago
| Owner: | changed from to |
|---|
comment:8 by , 3 weeks ago
| Triage Stage: | Accepted → Ready for checkin |
|---|
Thanks for the report! Appears to only affect the psycopg3 path. Would you like to submit a PR?