# M04 Module Review ## Verdict FAIL ## Must-fix 1. **Published versions are not actually immutable at the persistence boundary** (confidence: 100) - **Location:** `services/watchman/migrations/versions/20261118_0006_query_definitions.py:42`; `services/watchman/src/watchman/services/query_versions.py:23`; `services/watchman/src/watchman/services/queries.py:385` - The migration grants `watchman_app` `UPDATE` on `query_versions` and installs no trigger or database rule to reject updates. The only guard is the standalone `forbid_version_content_mutation()` helper, but no production write path calls it. Any present or future application code using a SQLAlchemy session can therefore alter a published SQL snapshot and commit it. - This violates D24's immutable-published-version requirement despite the absence of an HTTP update route. - **Fix:** enforce append-only versions at the database boundary (for example, revoke UPDATE/DELETE for the application role, or add an update/delete-rejecting trigger with a narrowly controlled maintenance path). Add an integration test that attempts a direct ORM/Core update and proves it is rejected. 2. **SQL Server previews can execute writes when the saved datasource credentials have write access** (confidence: 98) - **Location:** `services/watchman/src/watchman/queries/drivers.py:260` - PostgreSQL and MySQL establish a read-only transaction before executing the submitted template. The SQL Server runner sends the template directly via `cur.execute(sql, params)` and has no equivalent server-side read-only transaction or application gate. `DataSource.read_only_intent` is only stored/displayed and is never checked or verified before execution (`services/watchman/src/watchman/services/datasources.py:303`, `services/watchman/src/watchman/queries/execute.py:151`). - A single-statement `UPDATE`, `DELETE`, DDL, or an executing stored procedure is accepted by the template parser and will mutate a SQL Server datasource whenever its configured login has write privileges. Parameter binding prevents value injection; it does not make arbitrary SQL read-only. This violates the read-only-preview boundary. - **Fix:** do not allow SQL Server preview until a server-enforced read-only execution method is available, or require and verify a least-privileged read-only login as an executable precondition. Add a live SQL Server regression test proving a write statement cannot change data. ## Should-fix None. ## Notes - Review covered the supplied M04 backend, API/auth, datasource-reference, frontend, tests, migrations, and task/documentation scope. - The reviewed API routes correctly restrict query operations to `watchman-admin`; parameter values are bound rather than text-spliced; preview outcomes keep empty, illegal-type, and failure distinct; and preview audit payloads omit SQL, parameters, and rows. - Existing test results were provided by the orchestrator. This review did not re-run them. ## Remediation Both must-fix findings were addressed (no commit made). 1. **Immutable `query_versions` at the DB boundary** — new migration `20261118_0007_query_versions_immutable`: - `REVOKE UPDATE, DELETE` on `query_versions` from `watchman_app`; `SELECT, INSERT` re-asserted. - Statement-level `BEFORE UPDATE OR DELETE OR TRUNCATE` trigger (`trg_query_versions_append_only`) that always raises `restrict_violation` (23001), so it also stops the table owner and any future grant, even when forced RLS hides all rows. - Downgrade drops trigger and function and restores the 0006 grants (round-trip upgrade/downgrade/upgrade verified locally). - Tests (`test_query_versions.py`): app-role privilege matrix; Core and ORM UPDATE/DELETE fail with SQLSTATE 42501 and the stored SQL is unchanged; INSERT of a new version still works; the owner (migrator) is rejected by the trigger for UPDATE/DELETE/TRUNCATE. - Because the app role can no longer delete versions, test cleanup now uses a superuser purge (`tests/_privileged_cleanup.py`, `session_replication_role = replica`); there is no application maintenance path. 2. **SQL Server read-only boundary** — chosen fix: refuse, do not gate on statement text. - `run_query` returns `failure_kind = engine_readonly_unsupported` (fixed safe message, no details from the driver) for `engine == "sqlserver"` right after `assert_usable`, before validation, secret decryption or any connection. The circuit is untouched. - `_run_sqlserver` was removed from `queries/drivers.py`, so `fetch_rows` also raises `UnsupportedDataSourceError` for SQL Server: no write path exists even if the guard were bypassed. The M03 probe (`datasources/probe.py`) is unchanged. - Tests: `run_query` on a sqlserver datasource is refused for SELECT, UPDATE, DELETE and EXEC with no driver/secret access; `fetch_rows("sqlserver")` opens nothing; an API preview test confirms the refusal outcome and a single audit event. Validation: `uv run pytest` in `services/watchman` — 456 passed. Not covered: a live SQL Server test (none needed, since execution is refused); the frontend has not been checked for display of the new `failure_kind`.