fix: redact non-SQL content echoed in safety threat messages (main sanity failure)

Merged
AN
2mo ago
+237-3

Issue for this PR

Closes #1110

Type of change

  • Bug fix

What does this PR do?

Fixes the Sanity (Verdaccio) failure on main (security phase [6/15] Path traversal in file args). That job only runs on pushes to main, so #1090's PR CI could not see it.

Root cause: #1090 correctly started surfacing real engine ThreatFinding messages in check --checks safety (they previously collapsed into one generic warning). The engine's multi_statement rule quotes the raw "statement type" token verbatim, and its matched_pattern is the raw input line — for non-SQL input (altimate check ../../../../etc/passwd) that reflects the file's contents into CLI output (Disallowed statement type: ROOT:X:0:0:…), which the sanity test rightly flags as an information-disclosure hazard.

Fix at the dispatcher boundary so every consumer (CLI, tools, review) is covered:

  • EngineCoerce.redactThreatText keeps SQL-keyword-like statement types (DROP, ALTER TABLE) and replaces arbitrary content with <non-SQL content redacted> in threat message/detail.
  • multi_statement threats additionally redact matched_pattern (for that rule the pattern IS the raw input line); injection rules keep their SQL-shaped patterns. Diff-scoping identity keys stay consistent because base and head redact identically.
  • Wired into the altimate_core.safety handler and the composite check.

Engine-side fix filed upstream: AltimateAI/altimate-core-internal#765 (the token should be sanitized at the source so all consumers are safe by default).

How did you verify your code works?

  • Reproduced the exact sanity scenario locally before/after: altimate check <passwd-like file> echoed ROOT:X:0:0:… before, now renders <non-SQL content redacted>; real SQL still reports Disallowed statement type: DROP.
  • New threat-redaction.test.ts: unit tests for the redaction (keyword kept / content redacted / unrelated messages untouched) + real-engine tests through both handlers asserting passwd-like input no longer echoes and DROP survives in messages.
  • Full test/altimate + test/cli: 4793 pass / 0 fail; typecheck and marker check clean.
  • Not verified: the Verdaccio sanity harness itself (needs the docker-compose stack); the failing grep condition (root:x:0 in output) is reproduced and covered directly.

Screenshots / recordings

Not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

🤖 Generated with Claude Code


[!NOTE]
Medium Risk
Security-sensitive output sanitization on all safety consumers; ordering relative to diff-scoping is critical to avoid regressing composite check behavior.

Overview
Fixes an information-disclosure path where safety scans could echo arbitrary input (e.g. /etc/passwd lines) in CLI and tool output after real multi_statement threat messages were surfaced.

Adds EngineCoerce.redactThreatText and EngineCoerce.redactScan: threat message/detail keep only single-word SQL statement keywords the engine actually emits; everything else becomes <non-SQL content redacted>. multi_statement threats always set matched_pattern to <redacted>; other rules keep SQL-shaped patterns.

Wires redaction into altimate_core.safety, sql.analyze, altimate_core.grade (nested safety), and altimate_core.check — with redaction after base/head diff-scoping so subtraction still compares raw matched_pattern keys (fixes pre-existing threats incorrectly resurfacing).

New threat-redaction.test.ts covers allowlist/quote bypass cases, integration across handlers, and identical base/head yielding zero threats.

Reviewed by Cursor Bugbot for commit 389ff1aff37e645f9416cf7aefd5c59bfc6bdccc. Bugbot is set up for automated code reviews on this repo. Configure here.


Summary by cubic

Redacts non-SQL content in safety threat output and fixes the main-only Verdaccio sanity failure. Previously multi_statement echoed raw input in the “statement type” and matched_pattern; now we preserve single-word SQL statement types and redact everything else after diff-scoping.

  • Adds EngineCoerce.redactThreatText and EngineCoerce.redactScan: exact single-keyword allowlist matching engine output, greedy-quote handling, and "" for non-SQL tokens.
  • Sets matched_pattern to "" for multi_statement; other rules keep SQL-shaped patterns.
  • Applies redaction in altimate_core.safety, sql.analyze, altimate_core.check (after subtraction), and altimate_core.grade (defense-in-depth).
  • No caller or config changes. Tests cover keyword preservation, non-SQL redaction, quote bypasses, all consumers, and a regression ensuring identical base/head scans yield zero threats.

Summary by CodeRabbit

  • Bug Fixes

    • Improved threat-result sanitization by removing non-SQL content from displayed messages, details, and matched patterns.
    • Preserved relevant SQL statement types and patterns needed for accurate threat comparison.
    • Ensured safety, composite, analysis, and grading results remain correctly filtered and recalculated after redaction.
  • Tests

    • Added coverage for threat-message redaction, SQL preservation, embedded-quote bypass protection, unchanged unrelated messages, and diff-scoped results.
    • Added integration coverage across safety scans, composite checks, SQL analysis, and grading.