From 3f175b6c68357e5ddfbb166f1137f96721ab0f36 Mon Sep 17 00:00:00 2001 From: Juan Manuel Castillo Pinto <33883620+jmmana@users.noreply.github.com> Date: Thu, 3 Dec 2026 19:26:42 -0500 Subject: [PATCH] Bound aggregate cohort membership reads at the database --- docs/DESIGN.md | 4 +++- src/sqlguardllm/mcp_server.py | 6 +++++- src/sqlguardllm/session_limits.py | 3 ++- tests/test_mcp.py | 29 +++++++++++++++++++++++++++++ 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/docs/DESIGN.md b/docs/DESIGN.md index e5450c9..af0e2a8 100644 --- a/docs/DESIGN.md +++ b/docs/DESIGN.md @@ -388,7 +388,9 @@ after one person joined would reveal that person's salary; the second request is refused. Asking twice for exactly the same rows is refused too. If the ids cannot be listed, the session is unknown, the cohort is larger than 512 ids, or the backend is SQL Server (no snapshot guarantee), the answer is -`cohort_unavailable`. History is process-local and expires after +`cohort_unavailable`. With the default limit, the identifier query requests at +most 513 rows, including one extra to detect an oversized set. History is +process-local and expires after `window_seconds`. Verified-catalog queries on a cohort source are refused. ### Aggregate cohort history for embedded applications diff --git a/src/sqlguardllm/mcp_server.py b/src/sqlguardllm/mcp_server.py index 0191c32..fc232c9 100644 --- a/src/sqlguardllm/mcp_server.py +++ b/src/sqlguardllm/mcp_server.py @@ -1149,7 +1149,11 @@ def _admit_cohort( ) refusal = "cohort_unavailable" if listing is not None and conn.dialect.name != "mssql": # no snapshot guarantee there - ids = conn.exec_driver_sql(listing).fetchmany(budgets.cohort_limit(source) + 1) + result = conn.exec_driver_sql(listing) + try: + ids = result.fetchmany(budgets.cohort_limit(source) + 1) + finally: + result.close() refusal = budgets.admit_cohort(session, source, [row[0] for row in ids]) or "" if refusal: if self.language == "es": diff --git a/src/sqlguardllm/session_limits.py b/src/sqlguardllm/session_limits.py index c333b78..7eb828b 100644 --- a/src/sqlguardllm/session_limits.py +++ b/src/sqlguardllm/session_limits.py @@ -336,7 +336,8 @@ def membership_sql(self, sql: str, dialect: str, source: str) -> str | None: where = statement.args.get("where") if where is not None: query = query.where(where.this.copy()) - return query.sql(dialect=dialect) + # Read one extra id to distinguish an exact fit from an incomplete cohort. + return query.limit(self.cohort_limit(source) + 1).sql(dialect=dialect) def admit_cohort( self, session: str | None, source: str, values: Iterable[object] diff --git a/tests/test_mcp.py b/tests/test_mcp.py index 87746f5..3810776 100644 --- a/tests/test_mcp.py +++ b/tests/test_mcp.py @@ -996,6 +996,35 @@ def test_cohort_repeating_the_same_group_is_refused(url: str) -> None: assert call_error(server, "query", total).startswith("differencing:") +def test_oversized_cohort_is_bounded_at_database_and_refuses_the_aggregate(url: str) -> None: + engine = create_engine(url) + server = create_server( + make_policy(), + engine, + row_budgets=RowBudgets( + {"salaries": 100}, + 60, + fixed_aggregate_sources=["salaries"], + cohorts={"salaries": ("id", 2)}, + cohort_max_members=2, + ), + single_session=True, + ) + reads: list[str] = [] + + @event.listens_for(engine, "before_cursor_execute") + def record_read(_conn: Any, _cursor: Any, statement: str, *_args: Any) -> None: + if statement.startswith("SELECT"): + reads.append(statement) + + try: + total = {"sql": "SELECT SUM(amount) AS total FROM salaries"} + assert call_error(server, "query", total).startswith("cohort_unavailable:") + assert reads == ["SELECT id FROM salaries LIMIT 3"] + finally: + engine.dispose() + + def test_cohort_config_must_name_a_fixed_source() -> None: with pytest.raises(ValueError, match="fixed_aggregate_sources"): RowBudgets({"salaries": 5}, 60, cohorts={"salaries": ("id", 2)})