Skip to content

fix(security): drop bogus T-SQL OPTION (MAX_EXECUTION_TIME) on MSSQL sampling — silent zero-finding regression - #238

Merged
FabioLeitao merged 2 commits into
mainfrom
cursor/sre-automation-agent-protocol-80c8
Apr 27, 2026
Merged

FabioLeitao merged 2 commits into
mainfrom
cursor/sre-automation-agent-protocol-80c8

Conversation

@cursor

@cursor cursor Bot commented Apr 27, 2026

Copy link
Copy Markdown
Contributor

1. Bug & impact

connectors/sql_sampling.py::_plan_mssql_column_sample (introduced in feat(connectors): SQL column sampling policy, SRE timeouts, and metadata caps — 5cd402c) emitted an invalid T-SQL clause on every SQL Server sampling statement when a per-statement timeout budget was active:

-- Data Boar Compliance Scan
SELECT TOP (n) "col" FROM "schema"."tbl" WITH (NOLOCK)
WHERE "col" IS NOT NULL
OPTION (MAX_EXECUTION_TIME = 5000)

MAX_EXECUTION_TIME is a MySQL-only optimizer hint (MySQL 8.4 reference). T-SQL does not accept OPTION (MAX_EXECUTION_TIME = N) (Microsoft Learn — Query Hints).

Severity: CRITICAL — silent data-loss-grade false negative.

  • The connector default path resolves a 5000 ms timeout when the operator does not override sample_statement_timeout_ms, so this clause was applied on every MSSQL sampling statement, by default, in 1.7.4-beta.
  • SQL Server raises “Incorrect syntax near 'MAX_EXECUTION_TIME'”.
  • SQLConnector.sample() catches that exception inside a broad except Exception and returns "" — the scanner then sees no values and reports no sensitivity findings for any column on that target.
  • Operators get a clean compliance report, with only an attenuated sample_skipped warning in the audit log to indicate that every column on every MSSQL target was effectively skipped.

This violates two doctrinal contracts simultaneously:

  • DEFENSIVE_SCANNING_MANIFESTO §3 — “do not blur dialects”. We were emitting MySQL hint syntax inside SQL Server statements.
  • THE_ART_OF_THE_FALLBACK §3 — “Data Boar never falls through to a weaker level silently.” We were silently demoting every MSSQL column read from “sampled” to “empty”, with no executive-report-grade demotion entry.

2. Trigger scenario (reproducible)

  1. Configure any targets[].driver: mssql target without overriding sample_statement_timeout_ms.
  2. Run data-boar scan. The connector resolves a default 5000 ms budget.
  3. SamplingManager.build_column_sample appends OPTION (MAX_EXECUTION_TIME = 5000) to every generated TOP (n) … WITH (NOLOCK) statement.
  4. SQL Server rejects each statement with a syntax error.
  5. SQLConnector.sample() swallows the error → empty sample → scanner.scan_column returns LOW → no finding persisted.
  6. The executive report (POC_SUMMARY_*.md) and scan_manifest_*.yaml claim the SQL Server target is clean.

The misbehaviour was enshrined by tests/test_sql_sampling.py::test_build_mssql_option_max_execution_when_timeout, which asserted the bogus syntax and therefore prevented review from catching the dialect mismatch.

3. Root cause

A MySQL-shaped hint was copy-pasted into the MSSQL planner without checking T-SQL grammar. The unit test asserted the literal string match instead of validating the dialect, so CI happily green-lit the regression.

SQL Server has no query-level execution-time hint. Bounding MSSQL statement time belongs at:

  • the connection layer (connect_timeout, already wired in _connect_args_from_target), and
  • the DBA-side Resource Governor / LOCK_TIMEOUT session option.

4. Fix & validation

Surgical, behaviour-narrowing change. Three files are functionally affected; the rest is doc/test alignment.

  • connectors/sql_sampling.py — _plan_mssql_column_sample no longer inlines any OPTION clause. statement_timeout_ms is accepted for API parity and still recorded in the audit log via core/scan_audit_log.py, but explicitly not rendered into MSSQL SQL. Inline RCA comment + module-level note added so future contributors do not reintroduce the same hint by analogy.
  • tests/test_sql_sampling.py — replaces test_build_mssql_option_max_execution_when_timeout with test_build_mssql_never_emits_option_max_execution_time, a regression guard that fails if OPTION or MAX_EXECUTION_TIME ever reappear in the MSSQL plan, even when statement_timeout_ms is set. Other MSSQL assertions (TOP, WITH (NOLOCK), IS NOT NULL) are preserved.
  • docs/USAGE.md, docs/USAGE.pt_BR.md, docs/TESTING.md, docs/TESTING.pt_BR.md — corrected the operator narrative: MySQL gets /*+ MAX_EXECUTION_TIME(N) */, PostgreSQL gets SET LOCAL statement_timeout, MSSQL has no equivalent and operators are pointed to connect_timeout + Resource Governor.

Defensive architecture posture

  • Zero impact on database locks. WITH (NOLOCK) and the IS NOT NULL filter are preserved; the change strictly removes invalid SQL — no new isolation level, no new transaction, no new round-trip.
  • No regression risk to PostgreSQL, MySQL/MariaDB, Oracle, SQLite, Snowflake, DuckDB, Cockroach paths — the planner functions are dialect-isolated and untouched.
  • Compliance Scan attribution (-- Data Boar Compliance Scan) is preserved as the leading line per DEFENSIVE_SCANNING_MANIFESTO.md §4.

Tests

  • uv run python -m pytest tests/test_sql_sampling.py → 17 passed.
  • uv run python -m pytest tests/test_sql_connector.py tests/test_scan_audit_log.py tests/test_executive_report.py tests/test_safe_prefix.py → 29 passed.
  • uv run pre-commit run --all-files (Ruff, Ruff format, plans-stats, markdown, pt-BR locale, commercial guard, external plan link guard, PII guards, uv.lock parity) → all hooks green via the commit hook chain.

Operator action

Operators with MSSQL targets currently on 1.7.3 / 1.7.4-beta should plan to upgrade once this fix lands and then re-run any SQL Server scan; prior reports for those targets are likely incomplete (zero or near-zero findings) regardless of true PII content.

5. References

Open in Web View Automation 

…sampling

Critical correctness bug introduced in feat(connectors) 5cd402c.

  Bug & impact
  ------------
  _plan_mssql_column_sample emitted

      SELECT TOP (n) "col" FROM ... WITH (NOLOCK) WHERE "col" IS NOT NULL
      OPTION (MAX_EXECUTION_TIME = 5000)

  whenever a per-target sample timeout was active. The default connector
  path resolves the timeout to 5000 ms when the operator does not set one,
  so this OPTION clause was emitted on *every* SQL Server sampling
  statement, by default.

  MAX_EXECUTION_TIME is a MySQL-only optimizer hint. T-SQL does not accept
  OPTION (MAX_EXECUTION_TIME = N) and the engine raises a syntax error.
  SQLConnector.sample() catches that error inside a broad
  'except Exception' block and returns an empty string, so the scanner
  reports no sensitivity for any MSSQL column. The result is a silent,
  systemic, data-loss-grade false negative on every SQL Server target,
  with no operator-visible failure beyond a log warning.

  Trigger scenario
  ----------------
  1. Operator points Data Boar at any 'driver: mssql' target without
     overriding sample_statement_timeout_ms.
  2. SQLConnector resolves a 5000 ms default timeout.
  3. SamplingManager.build_column_sample appends OPTION (MAX_EXECUTION_TIME
     = 5000) to the generated TOP + NOLOCK SQL.
  4. SQL Server rejects the statement with 'Incorrect syntax near
     MAX_EXECUTION_TIME'.
  5. The connector swallows the exception, returns empty samples, and the
     compliance report claims the database is clean.

  Root cause
  ----------
  Cross-dialect copy of the MySQL hint was applied to MSSQL without a
  reference to T-SQL hint syntax. SQL Server has no equivalent
  query-level execution time hint; bounding statement time on MSSQL
  belongs at the connection layer (connect_timeout) and at the DBA-side
  resource governor.

  Fix & validation
  ----------------
  - connectors/sql_sampling.py: _plan_mssql_column_sample no longer
    inlines an OPTION clause. The statement_timeout_ms argument is kept
    for API parity (audit log still records the budget) and explicitly
    documented as not emitted into MSSQL SQL.
  - tests/test_sql_sampling.py: replaces the regression-inducing test
    with a guard that fails if 'OPTION' or 'MAX_EXECUTION_TIME' ever
    reappears in the MSSQL plan, even when statement_timeout_ms is set.
  - docs/USAGE.md, docs/USAGE.pt_BR.md, docs/TESTING.md,
    docs/TESTING.pt_BR.md: corrected the operator narrative so MSSQL no
    longer claims a fictitious OPTION (MAX_EXECUTION_TIME = ...) hint.
  - Defensive Scanning Manifesto and The Art Of The Fallback both
    require: no silent demotion to weaker coverage. This fix restores
    that contract.

  No database lock impact: WITH (NOLOCK) and the IS NOT NULL filter are
  preserved; this change strictly removes invalid SQL.

Made-with: Cursor

Co-authored-by: Fabio Tavares Leitão <[email protected]>
@FabioLeitao
FabioLeitao marked this pull request as ready for review April 27, 2026 19:10
@FabioLeitao
FabioLeitao merged commit 333e0aa into main Apr 27, 2026
1 check passed
@FabioLeitao
FabioLeitao deleted the cursor/sre-automation-agent-protocol-80c8 branch April 27, 2026 19:10
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.

2 participants