Repository navigation
fix(security): drop bogus T-SQL OPTION (MAX_EXECUTION_TIME) on MSSQL sampling — silent zero-finding regression - #238
Merged
Conversation
…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]>
This was referenced Apr 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
1. Bug & impact
connectors/sql_sampling.py::_plan_mssql_column_sample(introduced infeat(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:MAX_EXECUTION_TIMEis a MySQL-only optimizer hint (MySQL 8.4 reference). T-SQL does not acceptOPTION (MAX_EXECUTION_TIME = N)(Microsoft Learn — Query Hints).Severity: CRITICAL — silent data-loss-grade false negative.
5000 mstimeout when the operator does not overridesample_statement_timeout_ms, so this clause was applied on every MSSQL sampling statement, by default, in 1.7.4-beta.SQLConnector.sample()catches that exception inside a broadexcept Exceptionand returns""— the scanner then sees no values and reports no sensitivity findings for any column on that target.sample_skippedwarning in the audit log to indicate that every column on every MSSQL target was effectively skipped.This violates two doctrinal contracts simultaneously:
2. Trigger scenario (reproducible)
targets[].driver: mssqltarget without overridingsample_statement_timeout_ms.data-boar scan. The connector resolves a default 5000 ms budget.SamplingManager.build_column_sampleappendsOPTION (MAX_EXECUTION_TIME = 5000)to every generatedTOP (n) … WITH (NOLOCK)statement.SQLConnector.sample()swallows the error → empty sample →scanner.scan_columnreturnsLOW→ no finding persisted.POC_SUMMARY_*.md) andscan_manifest_*.yamlclaim 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:
connect_timeout, already wired in_connect_args_from_target), andLOCK_TIMEOUTsession 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_sampleno longer inlines anyOPTIONclause.statement_timeout_msis accepted for API parity and still recorded in the audit log viacore/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— replacestest_build_mssql_option_max_execution_when_timeoutwithtest_build_mssql_never_emits_option_max_execution_time, a regression guard that fails ifOPTIONorMAX_EXECUTION_TIMEever reappear in the MSSQL plan, even whenstatement_timeout_msis 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 getsSET LOCAL statement_timeout, MSSQL has no equivalent and operators are pointed toconnect_timeout+ Resource Governor.Defensive architecture posture
WITH (NOLOCK)and theIS NOT NULLfilter are preserved; the change strictly removes invalid SQL — no new isolation level, no new transaction, no new round-trip.-- Data Boar Compliance Scan) is preserved as the leading line perDEFENSIVE_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-betashould 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
docs/ops/inspirations/DEFENSIVE_SCANNING_MANIFESTO.md,docs/ops/inspirations/THE_ART_OF_THE_FALLBACK.md.