1- import { describe , expect , test , beforeAll } from "bun:test"
1+ import { describe , expect , test , beforeAll , afterAll } from "bun:test"
22import { EngineCoerce } from "../../src/altimate/native/engine-coerce"
33
44let coreAvailable = false
99const describeIf = coreAvailable ? describe : describe . skip
1010
1111describe ( "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