mirror of
https://github.com/simonw/datasette.git
synced 2026-11-25 11:29:43 +01:00
Several internal helpers quoted table names using SQLite [bracket]
identifiers built with an f-string, e.g. PRAGMA foreign_key_list([{table}]).
Bracket quoting cannot escape a "]" character, so any table whose name
contains "]" (for example "[foo]" or "foo]") produced
"sqlite3.OperationalError: unrecognized token" - crashing schema
introspection at startup and 500-ing the table page.
Switch these call sites to the existing escape_sqlite() helper, which uses
"double quote" quoting with correct "" escaping (the same approach already
used elsewhere in the codebase and in the test suite):
- utils/internal_db.py: PRAGMA foreign_key_list / index_list
- utils/__init__.py: get_outbound_foreign_keys
- database.py: table_counts count query
- facets.py: default "select * from" SQL
Added a regression test covering table names with "]" characters.
Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
parent
9cfc252394
commit
591b909a4d
5 changed files with 41 additions and 7 deletions
|
|
@ -1,6 +1,6 @@
|
|||
from datasette.app import Datasette
|
||||
from datasette.plugins import DEFAULT_PLUGINS
|
||||
from datasette.utils import UNSTABLE_API_MESSAGE
|
||||
from datasette.utils import UNSTABLE_API_MESSAGE, escape_sqlite, tilde_encode
|
||||
from datasette.utils.sqlite import sqlite_version
|
||||
from datasette.version import __version__
|
||||
from .fixtures import make_app_client, EXPECTED_PLUGINS
|
||||
|
|
@ -930,6 +930,37 @@ async def test_tilde_encoded_database_names(db_name):
|
|||
assert response2.status_code == 200
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("table_name", ("[foo]", "foo]", "[foo]/bar"))
|
||||
async def test_table_with_reserved_characters_in_name(table_name):
|
||||
# Table names containing characters such as "]" that cannot be escaped
|
||||
# using SQLite [bracket] quoting used to break schema introspection and
|
||||
# the table page - https://github.com/simonw/datasette/issues/2431
|
||||
ds = Datasette()
|
||||
db = ds.add_memory_database("test_reserved_table_names")
|
||||
await db.execute_write(
|
||||
"create table {} (id integer primary key, name text)".format(
|
||||
escape_sqlite(table_name)
|
||||
)
|
||||
)
|
||||
await db.execute_write(
|
||||
"insert into {} (id, name) values (1, 'one')".format(escape_sqlite(table_name))
|
||||
)
|
||||
# Schema introspection (populate_schema_tables) must not crash:
|
||||
db_response = await ds.client.get("/test_reserved_table_names.json")
|
||||
assert db_response.status_code == 200
|
||||
tables = {t["name"]: t for t in db_response.json()["tables"]}
|
||||
assert tables[table_name]["count"] == 1
|
||||
# And the table page itself must load and return the row:
|
||||
table_response = await ds.client.get(
|
||||
"/test_reserved_table_names/{}.json?_shape=array".format(
|
||||
tilde_encode(table_name)
|
||||
)
|
||||
)
|
||||
assert table_response.status_code == 200
|
||||
assert table_response.json() == [{"id": 1, "name": "one"}]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize(
|
||||
"config,expected",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue