|
| 1 | +"""Table-scope guard: a query may only reference tables the semantic model declares. |
| 2 | +
|
| 3 | +Runs in the SAME shared safety pass as the fan/chasm pre-flight and the sensitive |
| 4 | +gate (execute_sql.py:_model_safety), so every engine entry point refuses a query |
| 5 | +that touches a table outside the model — not just whichever path obeyed a prose |
| 6 | +rule. Only physical table refs count; CTE names and derived-subquery aliases are |
| 7 | +not tables. Excluded (review_state='rejected') tables are dropped by the loader, |
| 8 | +so they never reach `_model_table_index` and land in the same "not declared → |
| 9 | +refuse" path exercised here via undeclared names. |
| 10 | +""" |
| 11 | + |
| 12 | +from __future__ import annotations |
| 13 | + |
| 14 | +import sys |
| 15 | +from pathlib import Path |
| 16 | + |
| 17 | +import pytest |
| 18 | + |
| 19 | +pytest.importorskip("pydantic") |
| 20 | +pytest.importorskip("sqlglot") |
| 21 | + |
| 22 | +REPO_ROOT = Path(__file__).resolve().parent.parent |
| 23 | +sys.path.insert(0, str(REPO_ROOT / "plugins" / "agami" / "scripts")) |
| 24 | + |
| 25 | +from semantic_model import models as m # noqa: E402 |
| 26 | +from semantic_model import runtime as rt # noqa: E402 |
| 27 | + |
| 28 | + |
| 29 | +def _scope_org(): |
| 30 | + """Org declaring exactly two tables: orders, customers.""" |
| 31 | + def _t(name): |
| 32 | + return m.Table(name=name, schema="public", storage_connection="c", grain=["id"], |
| 33 | + description=name, columns=[m.Column(name="id", type="integer")]) |
| 34 | + return m.Organization(organization="Shop", |
| 35 | + subject_areas=[m.SubjectArea(name="sales", |
| 36 | + tables_defined=[_t("orders"), _t("customers")])]) |
| 37 | + |
| 38 | + |
| 39 | +def test_declared_table_allowed(): |
| 40 | + assert rt.check_table_scope("SELECT * FROM orders", _scope_org()).action == "allow" |
| 41 | + |
| 42 | + |
| 43 | +def test_undeclared_table_refused(): |
| 44 | + res = rt.check_table_scope("SELECT * FROM sqlite_master", _scope_org()) |
| 45 | + assert res.action == "refuse" |
| 46 | + assert res.offending_tables == ["sqlite_master"] |
| 47 | + assert "sqlite_master" in res.reason |
| 48 | + |
| 49 | + |
| 50 | +def test_join_all_declared_allowed(): |
| 51 | + res = rt.check_table_scope( |
| 52 | + "SELECT * FROM orders o JOIN customers c ON o.customer_id = c.id", _scope_org()) |
| 53 | + assert res.action == "allow" |
| 54 | + |
| 55 | + |
| 56 | +def test_join_with_undeclared_refused_lists_only_bad_one(): |
| 57 | + res = rt.check_table_scope( |
| 58 | + "SELECT * FROM orders o JOIN payments p ON p.order_id = o.id", _scope_org()) |
| 59 | + assert res.action == "refuse" |
| 60 | + assert res.offending_tables == ["payments"] |
| 61 | + |
| 62 | + |
| 63 | +def test_cte_reference_allowed(): |
| 64 | + # `t` is a CTE name, not a physical table — must not be flagged. |
| 65 | + res = rt.check_table_scope( |
| 66 | + "WITH t AS (SELECT * FROM orders) SELECT * FROM t", _scope_org()) |
| 67 | + assert res.action == "allow" |
| 68 | + |
| 69 | + |
| 70 | +def test_cte_body_referencing_undeclared_refused(): |
| 71 | + res = rt.check_table_scope( |
| 72 | + "WITH t AS (SELECT * FROM secret_table) SELECT * FROM t", _scope_org()) |
| 73 | + assert res.action == "refuse" |
| 74 | + assert res.offending_tables == ["secret_table"] |
| 75 | + |
| 76 | + |
| 77 | +def test_subquery_alias_allowed(): |
| 78 | + # derived-table alias `x` is not a table; the inner `orders` is declared. |
| 79 | + res = rt.check_table_scope("SELECT * FROM (SELECT id FROM orders) x", _scope_org()) |
| 80 | + assert res.action == "allow" |
| 81 | + |
| 82 | + |
| 83 | +def test_schema_qualified_declared_allowed(): |
| 84 | + assert rt.check_table_scope("SELECT * FROM public.orders", _scope_org()).action == "allow" |
| 85 | + |
| 86 | + |
| 87 | +def test_case_insensitive_match(): |
| 88 | + assert rt.check_table_scope("SELECT * FROM ORDERS", _scope_org()).action == "allow" |
| 89 | + |
| 90 | + |
| 91 | +def test_empty_model_allows(): |
| 92 | + org = m.Organization(organization="Empty", subject_areas=[m.SubjectArea(name="s")]) |
| 93 | + assert rt.check_table_scope("SELECT * FROM anything", org).action == "allow" |
| 94 | + |
| 95 | + |
| 96 | +def test_non_select_degrades_to_allow(): |
| 97 | + # Non-SELECT is the upstream read-only guard's job; this gate defers (allow). |
| 98 | + assert rt.check_table_scope("DELETE FROM orders", _scope_org()).action == "allow" |
| 99 | + |
| 100 | + |
| 101 | +def test_unparseable_degrades_to_allow(): |
| 102 | + assert rt.check_table_scope("SELECT FROM WHERE ((", _scope_org()).action == "allow" |
0 commit comments