Skip to content

sqlite3: _completer raises OperationalError when an attached database name contains a double quote #158215

Description

@Tony123-tech

In Lib/sqlite3/_completer.py, the schema name from PRAGMA database_list
is interpolated directly into an SQL string:

SELECT name || ' ' FROM \"{schema}\".sqlite_master

If a user attaches a database whose name contains a double quote, the
resulting SQL is invalid and _complete() raises OperationalError instead
of returning completion matches.

Reproduction:

import sqlite3
from sqlite3 import _completer
con = sqlite3.connect(':memory:')
con.execute('ATTACH DATABASE \':memory:\' AS \'weird"name\'')
_completer._complete(con, 'a', 0)

Output:

OperationalError unrecognized token: "".sqlite_master
                WHERE name LIKE REPLACE(:text, '_', '^_') || '%' ESCAPE '^'"

The same problem exists in the column-completion query:

JOIN pragma_table_xinfo(sm.name,'{schema}') AS pti

Suggested fix: quote the schema name as an SQL identifier, replacing
embedded double quotes with two double quotes.

Discovered while reviewing the module with an AI assistant.

Linked PRs

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytopic-sqlite3type-bugAn unexpected behavior, bug, or error

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions