Skip to content

Commit 7b39204

Browse files
anandgupta42claude
andcommitted
fix: harden threat redaction — keyword allowlist, greedy quotes, all consumers
Round-2 review hardening on the redaction: - Keyword ALLOWLIST, not shape: the token is kept only when its first word is a known SQL statement keyword — shape alone passed content like "TOP SECRET PASSWORD" or "AKIAIOSFODNN7EXAMPLE". - Greedy quoted match: an embedded apostrophe in the raw token ("Statement type 'DROP'ROOT:X:0' is not…") previously closed the match early and leaked the remainder; greedy capture spans to the last quote and embedded quotes fail the check, redacting everything inside. - All scanSql consumers covered: `redactScan` moved to `EngineCoerce` (pure, no NAPI import) and applied in `sql.analyze` (copied raw message/detail into issues) and the `altimate_core.grade` handler (EvalResult embeds a full safety scan that the CLI grade check renders). - Tests: allowlist/apostrophe cases; composite + safety tests assert success AND non-empty threats AND the redaction marker (previously vacuous if threats were missing); sql.analyze + grade echo tests; Dispatcher.reset() and telemetry-env restore in afterAll; shared dispatcher import hoisted into beforeAll. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent c9a1597 commit 7b39204

4 files changed

Lines changed: 126 additions & 42 deletions

File tree

‎packages/opencode/src/altimate/native/altimate-core.ts‎

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,6 @@ function fail(error: unknown): AltimateCoreResult {
4242
return { success: false, data: {}, error: String(error) }
4343
}
4444

45-
/** Redact raw-input echoes from a scan result's threats (see EngineCoerce.redactThreatText). */
46-
function redactScan(scan: Record<string, unknown>): Record<string, unknown> {
47-
const threats = (scan.threats as any[] | undefined)?.map((t: any) => ({
48-
...t,
49-
message: typeof t.message === "string" ? EngineCoerce.redactThreatText(t.message) : t.message,
50-
detail: typeof t.detail === "string" ? EngineCoerce.redactThreatText(t.detail) : t.detail,
51-
// For multi_statement the matched pattern IS the raw input line — an
52-
// arbitrary-file content echo. Injection rules keep their SQL-shaped
53-
// patterns (useful, and inherently query-derived).
54-
matched_pattern: t.rule === "multi_statement" ? "<redacted>" : t.matched_pattern,
55-
}))
56-
return threats ? { ...scan, threats } : scan
57-
}
58-
5945
// ---------------------------------------------------------------------------
6046
// IFF / QUALIFY transpile transforms (ported from Python guard.py)
6147
// ---------------------------------------------------------------------------
@@ -128,7 +114,7 @@ export function registerAll(): void {
128114
register("altimate_core.safety", async (params) => {
129115
try {
130116
const raw = core.scanSql(params.sql)
131-
const data = redactScan(toData(raw))
117+
const data = EngineCoerce.redactScan(toData(raw))
132118
return ok(true, data)
133119
} catch (e) {
134120
return fail(e)
@@ -246,7 +232,7 @@ export function registerAll(): void {
246232
// Redact AFTER diff-scoping: subtraction must compare raw engine
247233
// matched_patterns on both sides (redacting first would rewrite head
248234
// multi_statement keys to "<redacted>" and never match the base).
249-
safety = redactScan(safety)
235+
safety = EngineCoerce.redactScan(safety)
250236
// PII exposure for the composite check — the tool has always rendered a
251237
// PII section; previously nothing populated it. Additive: a PII failure
252238
// must not fail the whole composite.
@@ -488,7 +474,13 @@ export function registerAll(): void {
488474
try {
489475
const schema = schemaOrEmpty(params.schema_path, params.schema_context)
490476
const raw = await core.evaluate(params.sql, schema)
491-
return ok(true, toData(raw))
477+
const data = toData(raw)
478+
// EvalResult embeds a full safety scan — redact its threat echoes too
479+
// (the CLI grade check renders nested threat messages).
480+
if (data.safety && typeof data.safety === "object") {
481+
data.safety = EngineCoerce.redactScan(data.safety as Record<string, unknown>)
482+
}
483+
return ok(true, data)
492484
} catch (e) {
493485
return fail(e)
494486
}

‎packages/opencode/src/altimate/native/engine-coerce.ts‎

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,55 @@ export function piiColumnsFromReport(piiData: unknown): Array<Record<string, any
6464
*
6565
* The engine's `multi_statement` rule quotes the offending "statement type"
6666
* verbatim — for non-SQL input (e.g. `altimate check /etc/passwd`) that
67-
* reflects arbitrary file content back into CLI/tool output. Keep the token
68-
* only when it looks like a SQL keyword phrase; otherwise redact.
67+
* reflects arbitrary file content back into CLI/tool output. A token is kept
68+
* only when it BOTH looks like a short keyword phrase AND starts with a known
69+
* SQL statement keyword — shape alone would pass content like
70+
* "TOP SECRET PASSWORD" or "AKIAIOSFODNN7EXAMPLE".
6971
*/
70-
const SQL_KEYWORD_LIKE = /^[A-Za-z_][A-Za-z0-9_$ ]{0,31}$/
72+
const REDACTED = "<non-SQL content redacted>"
73+
const SQL_TOKEN_SHAPE = /^[A-Za-z_][A-Za-z0-9_$ ]{0,31}$/
74+
const SQL_STATEMENT_KEYWORDS = new Set([
75+
"SELECT", "INSERT", "UPDATE", "DELETE", "DROP", "CREATE", "ALTER", "TRUNCATE",
76+
"GRANT", "REVOKE", "MERGE", "WITH", "BEGIN", "COMMIT", "ROLLBACK", "SET",
77+
"USE", "CALL", "EXPLAIN", "ANALYZE", "VACUUM", "COPY", "SHOW", "DESCRIBE",
78+
"DESC", "EXECUTE", "EXEC", "REPLACE", "VALUES", "LOCK", "UNLOCK", "COMMENT",
79+
"RENAME", "START", "SAVEPOINT", "RELEASE", "PREPARE", "DEALLOCATE",
80+
])
81+
function isSqlStatementType(token: string): boolean {
82+
const t = token.trim()
83+
if (!SQL_TOKEN_SHAPE.test(t)) return false
84+
return SQL_STATEMENT_KEYWORDS.has(t.split(/\s+/)[0].toUpperCase())
85+
}
7186
export function redactThreatText(text: string): string {
72-
return text
73-
.replace(/(Disallowed statement type: )(.+)$/i, (_m, prefix: string, tok: string) =>
74-
SQL_KEYWORD_LIKE.test(tok.trim()) ? `${prefix}${tok}` : `${prefix}<non-SQL content redacted>`,
75-
)
76-
.replace(/(Statement type ')([^']*)(')/i, (_m, a: string, tok: string, c: string) =>
77-
SQL_KEYWORD_LIKE.test(tok) ? `${a}${tok}${c}` : `${a}<non-SQL content redacted>${c}`,
78-
)
87+
return (
88+
text
89+
.replace(/(Disallowed statement type: )(.+)$/i, (_m, prefix: string, tok: string) =>
90+
isSqlStatementType(tok) ? `${prefix}${tok}` : `${prefix}${REDACTED}`,
91+
)
92+
// GREEDY quoted match — an attacker apostrophe inside the token would
93+
// otherwise close the match early and leak the remainder
94+
// (e.g. "Statement type 'DROP'ROOT:X:0' is not…"). Greedy capture spans
95+
// to the LAST quote; embedded quotes fail the shape check and redact.
96+
.replace(/(Statement type ')(.*)(')/i, (_m, a: string, tok: string, c: string) =>
97+
isSqlStatementType(tok) ? `${a}${tok}${c}` : `${a}${REDACTED}${c}`,
98+
)
99+
)
100+
}
101+
102+
/**
103+
* Redact raw-input echoes from a scan result's threats. For `multi_statement`
104+
* the matched pattern IS the raw input line, so it is always redacted;
105+
* injection rules keep their SQL-shaped patterns. Pure record manipulation —
106+
* safe to use from any consumer without loading the NAPI binding.
107+
*/
108+
export function redactScan(scan: Record<string, unknown>): Record<string, unknown> {
109+
const threats = (scan.threats as any[] | undefined)?.map((t: any) => ({
110+
...t,
111+
message: typeof t.message === "string" ? redactThreatText(t.message) : t.message,
112+
detail: typeof t.detail === "string" ? redactThreatText(t.detail) : t.detail,
113+
matched_pattern: t.rule === "multi_statement" ? "<redacted>" : t.matched_pattern,
114+
}))
115+
return threats ? { ...scan, threats } : scan
79116
}
80117

81118
export * as EngineCoerce from "./engine-coerce"

‎packages/opencode/src/altimate/native/sql/register.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ export function registerAllSql(): void {
3838

3939
const lint = JSON.parse(JSON.stringify(lintRaw))
4040
const semantics = JSON.parse(JSON.stringify(semanticsRaw))
41-
const safety = JSON.parse(JSON.stringify(safetyRaw))
41+
// Redact raw-input echoes (multi_statement quotes the input verbatim).
42+
const safety = EngineCoerce.redactScan(JSON.parse(JSON.stringify(safetyRaw))) as any
4243

4344
const issues: SqlAnalyzeIssue[] = []
4445

‎packages/opencode/test/altimate/threat-redaction.test.ts‎

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, expect, test, beforeAll } from "bun:test"
1+
import { describe, expect, test, beforeAll, afterAll } from "bun:test"
22
import { EngineCoerce } from "../../src/altimate/native/engine-coerce"
33

44
let coreAvailable = false
@@ -9,7 +9,7 @@ try {
99
const describeIf = coreAvailable ? describe : describe.skip
1010

1111
describe("redactThreatText", () => {
12-
test("keeps SQL-keyword-like statement types", () => {
12+
test("keeps SQL-statement-keyword types", () => {
1313
expect(EngineCoerce.redactThreatText("Disallowed statement type: DROP")).toBe("Disallowed statement type: DROP")
1414
expect(EngineCoerce.redactThreatText("Statement type 'ALTER TABLE' is not in the allowed list")).toBe(
1515
"Statement type 'ALTER TABLE' is not in the allowed list",
@@ -24,6 +24,28 @@ describe("redactThreatText", () => {
2424
expect(EngineCoerce.redactThreatText(detail)).not.toContain("ROOT:X:0")
2525
})
2626

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+
})
39+
40+
test("embedded apostrophes cannot close the quoted match early", () => {
41+
// Greedy capture spans to the LAST quote; the embedded quote fails the
42+
// shape check and everything inside is redacted.
43+
const attack = "Statement type 'DROP'ROOT:X:0:0' is not in the allowed list"
44+
const out = EngineCoerce.redactThreatText(attack)
45+
expect(out).not.toContain("ROOT:X:0")
46+
expect(out).toContain("<non-SQL content redacted>")
47+
})
48+
2749
test("leaves unrelated messages untouched", () => {
2850
expect(EngineCoerce.redactThreatText("Tautology attack detected (numeric constant comparison)")).toBe(
2951
"Tautology attack detected (numeric constant comparison)",
@@ -34,44 +56,76 @@ describe("redactThreatText", () => {
3456
// Sanity security test [6/15] regression: `altimate check /etc/passwd` must not
3557
// reflect file contents in threat messages (found by the main-only Verdaccio
3658
// sanity job after PR #1090 surfaced real threat messages).
37-
describeIf("safety handlers redact raw-input echoes (real engine)", () => {
59+
describeIf("safety consumers redact raw-input echoes (real engine)", () => {
60+
let D: typeof import("../../src/altimate/native/dispatcher")
61+
let priorTelemetry: string | undefined
62+
3863
beforeAll(async () => {
64+
priorTelemetry = process.env.ALTIMATE_TELEMETRY_DISABLED
3965
process.env.ALTIMATE_TELEMETRY_DISABLED = "true"
4066
const { registerAll } = await import("../../src/altimate/native/altimate-core")
67+
const { registerAllSql } = await import("../../src/altimate/native/sql/register")
4168
registerAll()
69+
registerAllSql()
70+
D = await import("../../src/altimate/native/dispatcher")
71+
})
72+
73+
afterAll(async () => {
74+
if (priorTelemetry === undefined) delete process.env.ALTIMATE_TELEMETRY_DISABLED
75+
else process.env.ALTIMATE_TELEMETRY_DISABLED = priorTelemetry
76+
// registerAll mutates shared Dispatcher state — reset for test isolation.
77+
;(await import("../../src/altimate/native/dispatcher")).reset()
4278
})
4379

4480
const PASSWD = "root:x:0:0:root:/root:/bin/bash\ndaemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin\n"
4581

4682
test("altimate_core.safety does not echo non-SQL file content", async () => {
47-
const { Dispatcher } = await import("../../src/altimate/native")
48-
const r = await Dispatcher.call("altimate_core.safety", { sql: PASSWD })
49-
const text = JSON.stringify((r.data as any).threats ?? [])
83+
const r = await D.call("altimate_core.safety", { sql: PASSWD })
84+
expect(r.success).toBe(true)
85+
const threats = ((r.data as any).threats ?? []) as any[]
86+
expect(threats.length).toBeGreaterThan(0)
87+
const text = JSON.stringify(threats)
5088
expect(text.toLowerCase()).not.toContain("root:x:0")
5189
expect(text).toContain("redacted")
5290
})
5391

5492
test("composite check does not echo non-SQL file content in threats", async () => {
55-
const { Dispatcher } = await import("../../src/altimate/native")
56-
const r = await Dispatcher.call("altimate_core.check", { sql: PASSWD })
57-
const threats = JSON.stringify(((r.data as any).safety?.threats ?? []))
58-
expect(threats.toLowerCase()).not.toContain("root:x:0")
93+
const r = await D.call("altimate_core.check", { sql: PASSWD })
94+
expect(r.success).toBe(true)
95+
// The sanitizer must actually be exercised — an empty threat list would
96+
// make the not-contains assertion vacuous.
97+
const threats = (((r.data as any).safety?.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("sql.analyze does not echo non-SQL file content in issues", async () => {
105+
const r = await D.call("sql.analyze", { sql: PASSWD })
106+
const text = JSON.stringify((r as any).issues ?? [])
107+
expect(text.toLowerCase()).not.toContain("root:x:0")
108+
})
109+
110+
test("altimate_core.grade nested safety threats are redacted", async () => {
111+
const r = await D.call("altimate_core.grade", { sql: PASSWD })
112+
expect(r.success).toBe(true)
113+
const text = JSON.stringify(((r.data as any).safety?.threats ?? []))
114+
expect(text.toLowerCase()).not.toContain("root:x:0")
59115
})
60116

61117
test("diff-scoping still filters pre-existing multi_statement threats despite redaction", async () => {
62118
// Regression (cursor review on #1111): redacting BEFORE base subtraction
63119
// rewrote head matched_patterns to "<redacted>" so base keys never matched
64120
// and pre-existing threats resurfaced as introduced. Redaction now runs
65121
// after subtraction: identical base/head must yield zero threats.
66-
const { Dispatcher } = await import("../../src/altimate/native")
67-
const r = await Dispatcher.call("altimate_core.check", { sql: PASSWD, base_sql: PASSWD })
122+
const r = await D.call("altimate_core.check", { sql: PASSWD, base_sql: PASSWD })
68123
expect((((r.data as any).safety?.threats ?? []) as any[]).length).toBe(0)
69124
expect((r.data as any).safety.safe).toBe(true)
70125
})
71126

72127
test("real SQL statement types are preserved in messages", async () => {
73-
const { Dispatcher } = await import("../../src/altimate/native")
74-
const r = await Dispatcher.call("altimate_core.safety", { sql: "SELECT 1; DROP TABLE users;" })
128+
const r = await D.call("altimate_core.safety", { sql: "SELECT 1; DROP TABLE users;" })
75129
const threats = ((r.data as any).threats ?? []) as any[]
76130
const messages = threats.map((t) => `${t.message} ${t.detail}`).join(" ")
77131
// SQL-keyword statement types stay useful in messages/details

0 commit comments

Comments
 (0)