Skip to content

Escape identifiers in arraycontains/arraynotcontains filters (refs #2431)#2853

Open
winklemad wants to merge 1 commit into
simonw:mainfrom
winklemad:escape-identifiers-array-filters
Open

Escape identifiers in arraycontains/arraynotcontains filters (refs #2431)#2853
winklemad wants to merge 1 commit into
simonw:mainfrom
winklemad:escape-identifiers-array-filters

Conversation

@winklemad

Copy link
Copy Markdown

The arraycontains / arraynotcontains filters build their SQL with literal bracket quoting:

":{p} in (select value from json_each([{t}].[{c}]))"

Bracket quoting can't escape a ] inside an identifier, so filtering a JSON-array column whose table or column name contains ] produces malformed SQL and fails, even though the table page itself loads fine:

GET /data/normal.json?ta%5Dgs__arraycontains=x
-> 400  near "gs": syntax error
   (generated SQL: ... json_each([normal].[ta]gs]) ...)

Fix

Use escape_sqlite() (already imported) for the table and column identifiers — the same approach #2846 used for the array facet SQL, which fixed this bug class for facets but left the array filters untouched. After the fix the generated SQL is ... json_each("normal"."ta]gs") ... and the filter returns the correct rows.

Refs #2431.

Tests

Updated the two exact-SQL assertions in tests/test_filters.py to the escaped form and added ]-in-name regression cases. pytest tests/test_filters.py tests/test_facets.py tests/test_table_api.py passes; black/ruff clean.

Related follow-ups (not in this PR)

Other literal [{...}] identifier-quoting sites remain (per #2431): app.py ATTACH DATABASE, and a few in views/table.py (label/autocomplete, drop-table confirm, keyset-pagination fallback). Happy to follow up on those separately if you'd like.

The array-membership filters built SQL with literal bracket quoting
(json_each([table].[column])), which cannot escape a "]" in a table or
column name, so filtering an array column whose name contains "]" failed
with a SQLite syntax error even though the table page loaded fine.

Use escape_sqlite() for the table and column identifiers, matching the
approach taken in simonw#2846 for the array facet SQL.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant