|
| 1 | +import { describe, expect, test, beforeAll, afterAll } from "bun:test" |
| 2 | +import { EngineCoerce } from "../../src/altimate/native/engine-coerce" |
| 3 | + |
| 4 | +let coreAvailable = false |
| 5 | +try { |
| 6 | + require.resolve("@altimateai/altimate-core") |
| 7 | + coreAvailable = true |
| 8 | +} catch {} |
| 9 | +const describeIf = coreAvailable ? describe : describe.skip |
| 10 | + |
| 11 | +describe("redactThreatText", () => { |
| 12 | + test("keeps single SQL-statement-keyword types (the only shape the engine emits)", () => { |
| 13 | + expect(EngineCoerce.redactThreatText("Disallowed statement type: DROP")).toBe("Disallowed statement type: DROP") |
| 14 | + expect(EngineCoerce.redactThreatText("Statement type 'ALTER' is not in the allowed list")).toBe( |
| 15 | + "Statement type 'ALTER' is not in the allowed list", |
| 16 | + ) |
| 17 | + }) |
| 18 | + |
| 19 | + test("redacts non-SQL content (e.g. /etc/passwd lines)", () => { |
| 20 | + const msg = "Disallowed statement type: ROOT:X:0:0:ROOT:/ROOT:/BIN/BASH" |
| 21 | + expect(EngineCoerce.redactThreatText(msg)).toBe("Disallowed statement type: <non-SQL content redacted>") |
| 22 | + const detail = "Statement type 'ROOT:X:0:0:ROOT:/ROOT:/BIN/BASH' is not in the allowed list: [\"SELECT\"]" |
| 23 | + expect(EngineCoerce.redactThreatText(detail)).toContain("'<non-SQL content redacted>'") |
| 24 | + expect(EngineCoerce.redactThreatText(detail)).not.toContain("ROOT:X:0") |
| 25 | + }) |
| 26 | + |
| 27 | + test("redacts keyword-shaped but non-SQL content (allowlist, not shape)", () => { |
| 28 | + // Shape alone would pass these — the first word must be a SQL statement keyword. |
| 29 | + expect(EngineCoerce.redactThreatText("Disallowed statement type: TOP SECRET PASSWORD")).toBe( |
| 30 | + "Disallowed statement type: <non-SQL content redacted>", |
| 31 | + ) |
| 32 | + expect(EngineCoerce.redactThreatText("Disallowed statement type: AKIAIOSFODNN7EXAMPLE")).toBe( |
| 33 | + "Disallowed statement type: <non-SQL content redacted>", |
| 34 | + ) |
| 35 | + expect(EngineCoerce.redactThreatText("Statement type 'PRIVATE NOTE' is not in the allowed list")).toBe( |
| 36 | + "Statement type '<non-SQL content redacted>' is not in the allowed list", |
| 37 | + ) |
| 38 | + // ANY multi-word phrase redacts — the engine only emits single-word |
| 39 | + // statement types (live-probed), so multi-word input is never engine |
| 40 | + // vocabulary: keyword-prefixed tails and keyword permutations both die. |
| 41 | + expect(EngineCoerce.redactThreatText("Disallowed statement type: SET SECRET PASSWORD")).toBe( |
| 42 | + "Disallowed statement type: <non-SQL content redacted>", |
| 43 | + ) |
| 44 | + expect(EngineCoerce.redactThreatText("Disallowed statement type: DROP DEAD FRED")).toBe( |
| 45 | + "Disallowed statement type: <non-SQL content redacted>", |
| 46 | + ) |
| 47 | + expect(EngineCoerce.redactThreatText("Disallowed statement type: SELECT DELETE UPDATE")).toBe( |
| 48 | + "Disallowed statement type: <non-SQL content redacted>", |
| 49 | + ) |
| 50 | + }) |
| 51 | + |
| 52 | + test("embedded apostrophes cannot close the quoted match early", () => { |
| 53 | + // Greedy capture spans to the LAST quote; the embedded quote fails the |
| 54 | + // shape check and everything inside is redacted. |
| 55 | + const attack = "Statement type 'DROP'ROOT:X:0:0' is not in the allowed list" |
| 56 | + const out = EngineCoerce.redactThreatText(attack) |
| 57 | + expect(out).not.toContain("ROOT:X:0") |
| 58 | + expect(out).toContain("<non-SQL content redacted>") |
| 59 | + }) |
| 60 | + |
| 61 | + test("leaves unrelated messages untouched", () => { |
| 62 | + expect(EngineCoerce.redactThreatText("Tautology attack detected (numeric constant comparison)")).toBe( |
| 63 | + "Tautology attack detected (numeric constant comparison)", |
| 64 | + ) |
| 65 | + }) |
| 66 | +}) |
| 67 | + |
| 68 | +// Sanity security test [6/15] regression: `altimate check /etc/passwd` must not |
| 69 | +// reflect file contents in threat messages (found by the main-only Verdaccio |
| 70 | +// sanity job after PR #1090 surfaced real threat messages). |
| 71 | +describeIf("safety consumers redact raw-input echoes (real engine)", () => { |
| 72 | + let D: typeof import("../../src/altimate/native/dispatcher") |
| 73 | + let priorTelemetry: string | undefined |
| 74 | + |
| 75 | + beforeAll(async () => { |
| 76 | + priorTelemetry = process.env.ALTIMATE_TELEMETRY_DISABLED |
| 77 | + process.env.ALTIMATE_TELEMETRY_DISABLED = "true" |
| 78 | + const { registerAll } = await import("../../src/altimate/native/altimate-core") |
| 79 | + const { registerAllSql } = await import("../../src/altimate/native/sql/register") |
| 80 | + registerAll() |
| 81 | + registerAllSql() |
| 82 | + D = await import("../../src/altimate/native/dispatcher") |
| 83 | + }) |
| 84 | + |
| 85 | + afterAll(async () => { |
| 86 | + if (priorTelemetry === undefined) delete process.env.ALTIMATE_TELEMETRY_DISABLED |
| 87 | + else process.env.ALTIMATE_TELEMETRY_DISABLED = priorTelemetry |
| 88 | + // registerAll mutates shared Dispatcher state — reset for test isolation. |
| 89 | + ;(await import("../../src/altimate/native/dispatcher")).reset() |
| 90 | + }) |
| 91 | + |
| 92 | + const PASSWD = "root:x:0:0:root:/root:/bin/bash\ndaemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin\n" |
| 93 | + |
| 94 | + test("altimate_core.safety does not echo non-SQL file content", async () => { |
| 95 | + const r = await D.call("altimate_core.safety", { sql: PASSWD }) |
| 96 | + expect(r.success).toBe(true) |
| 97 | + const threats = ((r.data as any).threats ?? []) as any[] |
| 98 | + expect(threats.length).toBeGreaterThan(0) |
| 99 | + const text = JSON.stringify(threats) |
| 100 | + expect(text.toLowerCase()).not.toContain("root:x:0") |
| 101 | + expect(text).toContain("redacted") |
| 102 | + }) |
| 103 | + |
| 104 | + test("composite check does not echo non-SQL file content in threats", async () => { |
| 105 | + const r = await D.call("altimate_core.check", { sql: PASSWD }) |
| 106 | + expect(r.success).toBe(true) |
| 107 | + // The sanitizer must actually be exercised — an empty threat list would |
| 108 | + // make the not-contains assertion vacuous. |
| 109 | + const threats = (((r.data as any).safety?.threats ?? []) as any[]) |
| 110 | + expect(threats.length).toBeGreaterThan(0) |
| 111 | + const text = JSON.stringify(threats) |
| 112 | + expect(text.toLowerCase()).not.toContain("root:x:0") |
| 113 | + expect(text).toContain("redacted") |
| 114 | + }) |
| 115 | + |
| 116 | + test("sql.analyze does not echo non-SQL file content in issues", async () => { |
| 117 | + const r = await D.call("sql.analyze", { sql: PASSWD }) |
| 118 | + const safetyIssues = (((r as any).issues ?? []) as any[]).filter((i) => i.type === "safety") |
| 119 | + // The sanitizer must actually be exercised — no safety issues would make |
| 120 | + // the not-contains assertion vacuous. |
| 121 | + expect(safetyIssues.length).toBeGreaterThan(0) |
| 122 | + const text = JSON.stringify(safetyIssues) |
| 123 | + expect(text.toLowerCase()).not.toContain("root:x:0") |
| 124 | + expect(text).toContain("redacted") |
| 125 | + }) |
| 126 | + |
| 127 | + test("altimate_core.grade result does not echo non-SQL file content", async () => { |
| 128 | + // EvalResult's safety section is { method, score } — no threat scan to |
| 129 | + // echo (the handler's redactScan is defense-in-depth if that changes). |
| 130 | + // Assert the whole result is echo-free, minus the `sql` field, which by |
| 131 | + // contract carries back the caller's own input. |
| 132 | + const r = await D.call("altimate_core.grade", { sql: PASSWD }) |
| 133 | + expect(r.success).toBe(true) |
| 134 | + // `sql` (EvalResult) and `lint.sql` (LintResult) carry back the caller's |
| 135 | + // own input by contract and are never rendered by the CLI grade check. |
| 136 | + const { sql: _echoedInput, ...rest } = r.data as any |
| 137 | + if (rest.lint) rest.lint = { ...rest.lint, sql: undefined } |
| 138 | + expect(JSON.stringify(rest).toLowerCase()).not.toContain("root:x:0") |
| 139 | + }) |
| 140 | + |
| 141 | + test("diff-scoping still filters pre-existing multi_statement threats despite redaction", async () => { |
| 142 | + // Regression (cursor review on #1111): redacting BEFORE base subtraction |
| 143 | + // rewrote head matched_patterns to "<redacted>" so base keys never matched |
| 144 | + // and pre-existing threats resurfaced as introduced. Redaction now runs |
| 145 | + // after subtraction: identical base/head must yield zero threats. |
| 146 | + const r = await D.call("altimate_core.check", { sql: PASSWD, base_sql: PASSWD }) |
| 147 | + expect((((r.data as any).safety?.threats ?? []) as any[]).length).toBe(0) |
| 148 | + expect((r.data as any).safety.safe).toBe(true) |
| 149 | + }) |
| 150 | + |
| 151 | + test("real SQL statement types are preserved in messages", async () => { |
| 152 | + const r = await D.call("altimate_core.safety", { sql: "SELECT 1; DROP TABLE users;" }) |
| 153 | + const threats = ((r.data as any).threats ?? []) as any[] |
| 154 | + const messages = threats.map((t) => `${t.message} ${t.detail}`).join(" ") |
| 155 | + // SQL-keyword statement types stay useful in messages/details |
| 156 | + // (matched_pattern is always redacted for multi_statement by design). |
| 157 | + expect(messages).toContain("DROP") |
| 158 | + expect(messages).not.toContain("redacted") |
| 159 | + }) |
| 160 | +}) |
0 commit comments