Skip to content

fix: CTE names no longer hide real tables in table check - #1545

Open
jannisd07 wants to merge 1 commit into
dataease:mainfrom
jannisd07:fix/table-check-cte-bypass
Open

jannisd07 wants to merge 1 commit into
dataease:mainfrom
jannisd07:fix/table-check-cte-bypass

Conversation

@jannisd07

Copy link
Copy Markdown
Contributor

extract_tables_from_sql() dropped every table whose name matched any CTE in the statement, no matter the scope or schema. So WITH secret AS (SELECT 1) SELECT * FROM public.secret ... or a CTE in a subquery could hide a real table from the allowlist check.

Now tables are resolved per scope with traverse_scope. Only unqualified tables that match a CTE visible in their scope are skipped. On any error during extraction the function returns an empty set, so the query is rejected.

Tests in test_table_check.py: the bypass cases from the issue plus a non-recursive self-reference are rejected, normal and recursive CTEs still pass, and an error doesn't leak partial results. pytest -q tests/test_table_check.py 26 passed, full suite no new failures

Fixes #1543

A CTE name no longer hides a real table with the same name: schema-qualified
tables, tables outside the CTE's scope and the non-recursive self-reference
inside a CTE body are now checked against the allowed tables. When extraction
fails, no partial result is returned, so the query is rejected.

Fixes dataease#1543

This branch has not been deployed

No deployments
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.

[BUG] extract_tables_from_sql() table-access allowlist bypassed via CTE name shadowing (schema-qualified or inner-scope CTEs)

1 participant