@@ -14,6 +14,7 @@ import {
1414 toolsChars ,
1515 verifierTools ,
1616 isCredentialPath ,
17+ holdsCredentials ,
1718 type AiVerifyEvidence ,
1819 type VerifierLlm ,
1920 type VerifierStep ,
@@ -39,9 +40,9 @@ import type { AiReviewFile } from "../../src/altimate/review/ai-review"
3940import { runAiReview } from "../../src/altimate/review/ai-review"
4041import { createDispatcherRunner } from "../../src/altimate/review/runner"
4142import { LLM } from "../../src/session/llm"
42- import { mkdirSync , writeFileSync } from "fs"
43+ import { mkdirSync , symlinkSync , writeFileSync } from "fs"
4344import { execFileSync } from "child_process"
44- import { grepFixedAt , projectGrepSpecs } from "../../src/altimate/review/git"
45+ import { grepFixedAt , makeContentResolver , projectGrepSpecs } from "../../src/altimate/review/git"
4546import path from "path"
4647import { tmpdir } from "../fixture/fixture"
4748import { runReview , type ReviewRunner } from "../../src/altimate/review/orchestrate"
@@ -1138,6 +1139,178 @@ describe("runAiVerify: the bounded tool loop", () => {
11381139 } )
11391140} )
11401141
1142+ describe ( "credential files: refused by name, by the path actually read, and by content" , ( ) => {
1143+ test . each ( [
1144+ "profiles.yml" ,
1145+ "config/Profiles.YAML" ,
1146+ ".env" ,
1147+ "dbt/.env.local" ,
1148+ ".envrc" ,
1149+ ".env-prod" ,
1150+ "deploy/prod.env" ,
1151+ ".netrc" ,
1152+ ".pgpass" ,
1153+ ".npmrc" ,
1154+ ".pypirc" ,
1155+ ".git-credentials" ,
1156+ "home/.ssh/id_ed25519" ,
1157+ "keyfile.json" ,
1158+ "secrets/keyfile-bq.json" ,
1159+ "gcp/my-service-account.json" ,
1160+ "gcp/service_account_key.json" ,
1161+ "credentials.json" ,
1162+ "aws/credentials-prod.json" ,
1163+ "certs/client.pem" ,
1164+ "tls/server.key" ,
1165+ "apple/AuthKey.p8" ,
1166+ "store.p12" ,
1167+ "infra/prod.tfvars" ,
1168+ "infra/terraform.tfvars.json" ,
1169+ ] ) ( "%s is a credential path" , ( p ) => expect ( isCredentialPath ( p ) ) . toBe ( true ) )
1170+
1171+ test . each ( [
1172+ "models/profiles_summary.sql" ,
1173+ "models/env.yml" ,
1174+ "models/environment.sql" ,
1175+ "models/accounts.json" ,
1176+ "macros/keys.sql" ,
1177+ "docs/credentials.md" ,
1178+ "seeds/service_accounts.csv" ,
1179+ ] ) ( "%s is not a credential path" , ( p ) => expect ( isCredentialPath ( p ) ) . toBe ( false ) )
1180+
1181+ test ( "content holding a private key or a service-account key is credentials, whatever the name" , ( ) => {
1182+ expect ( holdsCredentials ( "x\n-----BEGIN RSA PRIVATE KEY-----\nMII..." ) ) . toBe ( true )
1183+ expect ( holdsCredentials ( "-----BEGIN PRIVATE KEY-----" ) ) . toBe ( true )
1184+ expect ( holdsCredentials ( '{ "type": "service_account", "project_id": "p" }' ) ) . toBe ( true )
1185+ expect ( holdsCredentials ( "select private_key_id from keys -- BEGIN" ) ) . toBe ( false )
1186+ expect ( holdsCredentials ( "-----BEGIN PUBLIC KEY-----" ) ) . toBe ( false )
1187+ } )
1188+
1189+ const git = ( cwd : string , ...args : string [ ] ) =>
1190+ execFileSync ( "git" , [ "-c" , "user.email=t@t" , "-c" , "user.name=t" , ...args ] , { cwd, stdio : "ignore" } )
1191+ const runner = {
1192+ async impact ( ) {
1193+ return { hasManifest : true , severity : "SAFE" , directCount : 0 , transitiveCount : 0 , testCount : 0 }
1194+ } ,
1195+ async grade ( ) {
1196+ return { grade : "B" }
1197+ } ,
1198+ async check ( ) {
1199+ return { issues : [ ] , ran : true }
1200+ } ,
1201+ async equivalence ( ) {
1202+ return { decided : true , equivalent : true }
1203+ } ,
1204+ async detectPii ( ) {
1205+ return { columns : [ ] }
1206+ } ,
1207+ } as ReviewRunner
1208+ /** The verifier evidence runReview builds over the real git content resolver. */
1209+ const evidenceFor = async ( root : string , changedFiles : Parameters < typeof runReview > [ 0 ] [ "changedFiles" ] , head ?: string ) => {
1210+ const renames = new Map ( changedFiles . flatMap ( ( f ) => ( f . oldPath ? [ [ f . path , f . oldPath ] as [ string , string ] ] : [ ] ) ) )
1211+ let ev : AiVerifyEvidence | undefined
1212+ await runReview ( {
1213+ changedFiles,
1214+ config : { ...DEFAULT_REVIEW_CONFIG } ,
1215+ rubric : DEFAULT_RUBRIC ,
1216+ mode : "comment" ,
1217+ runner,
1218+ forceTier : "full" ,
1219+ aiVerify : true ,
1220+ getContent : makeContentResolver ( { base : "HEAD~1" , head, cwd : root , renames, gitRoot : root } ) ,
1221+ prFiles : changedFiles . map ( ( f ) => f . path ) ,
1222+ generatedAt : "2026-11-23T00:00:00Z" ,
1223+ aiReview : async ( input ) => ( ( ev = input . evidence ) , { findings : [ ] , status : { state : "ok" } } ) ,
1224+ } )
1225+ return ev !
1226+ }
1227+
1228+ test ( "the old side of a renamed credential file is refused (read_file, compiled_sql, equivalence)" , async ( ) => {
1229+ await using tmp = await tmpdir ( )
1230+ const root = tmp . path
1231+ git ( root , "init" , "-q" )
1232+ writeFileSync ( path . join ( root , "profiles.yml" ) , "prod:\n password: SECRET123\n" )
1233+ writeFileSync ( path . join ( root , ".env" ) , "select 'SECRET456' as id\n" )
1234+ mkdirSync ( path . join ( root , "models" ) )
1235+ writeFileSync ( path . join ( root , "models" , "a.sql" ) , "select 1 as id\n" )
1236+ git ( root , "add" , "." )
1237+ git ( root , "commit" , "-qm" , "base" )
1238+ git ( root , "mv" , "profiles.yml" , "profiles.yml.example" )
1239+ writeFileSync ( path . join ( root , "profiles.yml.example" ) , "prod:\n password: \"{{ env_var('PW') }}\"\n" )
1240+ git ( root , "mv" , ".env" , "models/env_model.sql" )
1241+ git ( root , "commit" , "-qam" , "head" )
1242+ const ev = await evidenceFor (
1243+ root ,
1244+ [
1245+ { path : "profiles.yml.example" , oldPath : "profiles.yml" , status : "renamed" , diff : "@@ -2 +2 @@\n- password: SECRET123\n+ password: x\n" } ,
1246+ { path : "models/env_model.sql" , oldPath : ".env" , status : "renamed" , diff : "" } ,
1247+ ] ,
1248+ "HEAD" ,
1249+ )
1250+ const tools = verifierTools ( ev )
1251+ const old = await tools . read_file . run ( { path : "profiles.yml.example" , side : "old" } )
1252+ expect ( old ) . toBe ( "not available: may hold credentials" )
1253+ expect ( await tools . read_file . run ( { path : "profiles.yml.example" , side : "new" } ) ) . toContain ( "env_var('PW')" )
1254+ // A model renamed from a credential file: neither the engine's old SQL nor compiled output is shown.
1255+ expect ( await ev . compiledSql ( "models/env_model.sql" , "old" ) ) . toBeUndefined ( )
1256+ expect ( await tools . compiled_sql . run ( { path : "models/env_model.sql" , side : "old" } ) ) . not . toContain ( "SECRET456" )
1257+ expect ( await tools . equivalence . run ( { path : "models/env_model.sql" } ) ) . toStartWith ( "not available" )
1258+ expect ( await tools . read_file . run ( { path : "models/env_model.sql" , side : "old" } ) ) . toBe ( "not available: may hold credentials" )
1259+ } )
1260+
1261+ test ( "working-tree mode never follows a symlink to a credential file; other symlinks still read" , async ( ) => {
1262+ await using tmp = await tmpdir ( )
1263+ const root = tmp . path
1264+ git ( root , "init" , "-q" )
1265+ writeFileSync ( path . join ( root , "profiles.yml" ) , "prod:\n password: SECRET123\n" )
1266+ mkdirSync ( path . join ( root , "docs" ) )
1267+ writeFileSync ( path . join ( root , "docs" , "readme.md" ) , "hello\n" )
1268+ git ( root , "add" , "." )
1269+ git ( root , "commit" , "-qm" , "base" )
1270+ symlinkSync ( "../profiles.yml" , path . join ( root , "docs" , "link.md" ) )
1271+ symlinkSync ( "readme.md" , path . join ( root , "docs" , "alias.md" ) )
1272+ const resolver = makeContentResolver ( { base : "HEAD" , cwd : root , gitRoot : root } )
1273+ expect ( await resolver ( "docs/link.md" , "new" ) ) . toBeUndefined ( )
1274+ expect ( await resolver ( "docs/alias.md" , "new" ) ) . toBe ( "hello\n" )
1275+ const ev = await evidenceFor ( root , [
1276+ { path : "docs/link.md" , status : "added" , diff : "@@ -0,0 +1 @@\n+../profiles.yml\n" } ,
1277+ { path : "docs/alias.md" , status : "added" , diff : "@@ -0,0 +1 @@\n+readme.md\n" } ,
1278+ ] )
1279+ const tools = verifierTools ( ev )
1280+ expect ( await tools . read_file . run ( { path : "docs/link.md" } ) ) . not . toContain ( "SECRET123" )
1281+ expect ( await tools . read_file . run ( { path : "docs/alias.md" } ) ) . toStartWith ( "1: hello" )
1282+ } )
1283+
1284+ test ( "read_file refuses a file whose content is a private key, whatever it is called" , async ( ) => {
1285+ await using tmp = await tmpdir ( )
1286+ const root = tmp . path
1287+ git ( root , "init" , "-q" )
1288+ writeFileSync ( path . join ( root , "notes.md" ) , "x\n" )
1289+ git ( root , "add" , "." )
1290+ git ( root , "commit" , "-qm" , "base" )
1291+ writeFileSync ( path . join ( root , "notes.md" ) , "-----BEGIN OPENSSH PRIVATE KEY-----\nb3BlbnNzaC1rZXktdjE\n" )
1292+ const ev = await evidenceFor ( root , [ { path : "notes.md" , status : "modified" , diff : "@@ -1 +1,2 @@\n-x\n+k\n" } ] )
1293+ expect ( await verifierTools ( ev ) . read_file . run ( { path : "notes.md" } ) ) . toBe ( "not available: may hold credentials" )
1294+ } )
1295+
1296+ test ( "grepFixedAt returns canonical non-ASCII paths, so the credential filter still matches" , async ( ) => {
1297+ await using tmp = await tmpdir ( )
1298+ const root = tmp . path
1299+ git ( root , "init" , "-q" )
1300+ mkdirSync ( path . join ( root , "models" , "café" ) , { recursive : true } )
1301+ writeFileSync ( path . join ( root , "models" , "café" , "profiles.yml" ) , "password: SECRET123\n" )
1302+ writeFileSync ( path . join ( root , "models" , "café" , "a.sql" ) , "select password_hash\n" )
1303+ git ( root , "add" , "." )
1304+ git ( root , "commit" , "-qm" , "init" )
1305+ for ( const ref of [ "HEAD" , undefined ] ) {
1306+ const hits = await grepFixedAt ( root , ref , "password" , [ "models" ] )
1307+ expect ( hits ?. map ( ( h ) => h . path ) . sort ( ) ) . toEqual ( [ "models/café/a.sql" , "models/café/profiles.yml" ] )
1308+ const tools = verifierTools ( { ...evidence ( ) , grepProject : async ( ) => hits } )
1309+ expect ( await tools . grep_project . run ( { pattern : "password" } ) ) . toBe ( "models/café/a.sql:1: select password_hash" )
1310+ }
1311+ } )
1312+ } )
1313+
11411314describe ( "runAiReview with the verifier on (harness path, scripted LLM.stream)" , ( ) => {
11421315 afterEach ( ( ) => mock . restore ( ) )
11431316
0 commit comments