Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion src/sqlguardllm/mcp_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
3 changes: 2 additions & 1 deletion src/sqlguardllm/session_limits.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
29 changes: 29 additions & 0 deletions tests/test_mcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)})
Loading