Skip to content

Commit 5b9b6ae

Browse files
anandgupta42claude
andcommitted
fix: whole-phrase keyword allowlist + non-vacuous consumer tests
- `isSqlStatementType` now requires EVERY word of the preserved phrase to come from the SQL keyword vocabulary (statement keywords first, auxiliary words like TABLE/INDEX/IF/EXISTS after) — checking only the first word preserved attacker tails such as "SET SECRET PASSWORD" verbatim. Since preserved text is a subset of the fixed vocabulary, no attacker-specific content can survive. Tests cover keyword-prefixed tails and compound types ("DROP INDEX IF EXISTS" stays useful). - `sql.analyze` test asserts non-empty safety issues + the redaction marker (was vacuous if the scan produced nothing). - Grade test corrected to the REAL EvalResult shape: its `safety` section is `{ method, score }` — there are no nested threats to echo (the handler redaction stays as defense-in-depth). The test now asserts the whole result is echo-free minus the by-contract input-echo fields (`sql`, `lint.sql`), which the CLI never renders. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 7b39204 commit 5b9b6ae

2 files changed

Lines changed: 40 additions & 5 deletions

File tree

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,22 @@ const SQL_STATEMENT_KEYWORDS = new Set([
7878
"DESC", "EXECUTE", "EXEC", "REPLACE", "VALUES", "LOCK", "UNLOCK", "COMMENT",
7979
"RENAME", "START", "SAVEPOINT", "RELEASE", "PREPARE", "DEALLOCATE",
8080
])
81+
// Auxiliary words allowed AFTER a statement keyword ("ALTER TABLE",
82+
// "DROP INDEX IF EXISTS"). Every word of a preserved phrase must come from
83+
// the keyword vocabulary — checking only the first word would preserve
84+
// attacker tails like "SET SECRET PASSWORD" verbatim.
85+
const SQL_AUX_KEYWORDS = new Set([
86+
"TABLE", "INDEX", "VIEW", "DATABASE", "SCHEMA", "COLUMN", "TRIGGER",
87+
"FUNCTION", "PROCEDURE", "SEQUENCE", "MATERIALIZED", "TEMPORARY", "TEMP",
88+
"IF", "NOT", "EXISTS", "OR", "INTO", "TRANSACTION", "WORK", "CASCADE",
89+
"UNIQUE", "ROLE", "USER", "EXTENSION", "TYPE",
90+
])
8191
function isSqlStatementType(token: string): boolean {
8292
const t = token.trim()
8393
if (!SQL_TOKEN_SHAPE.test(t)) return false
84-
return SQL_STATEMENT_KEYWORDS.has(t.split(/\s+/)[0].toUpperCase())
94+
const words = t.split(/\s+/).map((w) => w.toUpperCase())
95+
if (!SQL_STATEMENT_KEYWORDS.has(words[0])) return false
96+
return words.slice(1).every((w) => SQL_STATEMENT_KEYWORDS.has(w) || SQL_AUX_KEYWORDS.has(w))
8597
}
8698
export function redactThreatText(text: string): string {
8799
return (

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ describe("redactThreatText", () => {
3535
expect(EngineCoerce.redactThreatText("Statement type 'PRIVATE NOTE' is not in the allowed list")).toBe(
3636
"Statement type '<non-SQL content redacted>' is not in the allowed list",
3737
)
38+
// A keyword PREFIX must not preserve an arbitrary tail.
39+
expect(EngineCoerce.redactThreatText("Disallowed statement type: SET SECRET PASSWORD")).toBe(
40+
"Disallowed statement type: <non-SQL content redacted>",
41+
)
42+
expect(EngineCoerce.redactThreatText("Disallowed statement type: DROP DEAD FRED")).toBe(
43+
"Disallowed statement type: <non-SQL content redacted>",
44+
)
45+
// Compound statement types made purely of keywords stay useful.
46+
expect(EngineCoerce.redactThreatText("Disallowed statement type: DROP INDEX IF EXISTS")).toBe(
47+
"Disallowed statement type: DROP INDEX IF EXISTS",
48+
)
3849
})
3950

4051
test("embedded apostrophes cannot close the quoted match early", () => {
@@ -103,15 +114,27 @@ describeIf("safety consumers redact raw-input echoes (real engine)", () => {
103114

104115
test("sql.analyze does not echo non-SQL file content in issues", async () => {
105116
const r = await D.call("sql.analyze", { sql: PASSWD })
106-
const text = JSON.stringify((r as any).issues ?? [])
117+
const safetyIssues = (((r as any).issues ?? []) as any[]).filter((i) => i.type === "safety")
118+
// The sanitizer must actually be exercised — no safety issues would make
119+
// the not-contains assertion vacuous.
120+
expect(safetyIssues.length).toBeGreaterThan(0)
121+
const text = JSON.stringify(safetyIssues)
107122
expect(text.toLowerCase()).not.toContain("root:x:0")
123+
expect(text).toContain("redacted")
108124
})
109125

110-
test("altimate_core.grade nested safety threats are redacted", async () => {
126+
test("altimate_core.grade result does not echo non-SQL file content", async () => {
127+
// EvalResult's safety section is { method, score } — no threat scan to
128+
// echo (the handler's redactScan is defense-in-depth if that changes).
129+
// Assert the whole result is echo-free, minus the `sql` field, which by
130+
// contract carries back the caller's own input.
111131
const r = await D.call("altimate_core.grade", { sql: PASSWD })
112132
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")
133+
// `sql` (EvalResult) and `lint.sql` (LintResult) carry back the caller's
134+
// own input by contract and are never rendered by the CLI grade check.
135+
const { sql: _echoedInput, ...rest } = r.data as any
136+
if (rest.lint) rest.lint = { ...rest.lint, sql: undefined }
137+
expect(JSON.stringify(rest).toLowerCase()).not.toContain("root:x:0")
115138
})
116139

117140
test("diff-scoping still filters pre-existing multi_statement threats despite redaction", async () => {

0 commit comments

Comments
 (0)