Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(pi): gate recursive grep credential reads

Chrise0e776803d80
diff --git a/packages/maker-core/src/agents/claude-code/__tests__/auto-review-policy.test.ts b/packages/maker-core/src/agents/claude-code/__tests__/auto-review-policy.test.ts
index c0f18285fd..8b40de3e53 100644
--- a/packages/maker-core/src/agents/claude-code/__tests__/auto-review-policy.test.ts
+++ b/packages/maker-core/src/agents/claude-code/__tests__/auto-review-policy.test.ts
@@ -160,7 +160,7 @@ describe('classifyBuiltinToolForAutoReview — Windows 盘符路径边界', () =
describe('classifyBuiltinToolForAutoReview — Bash 只读命令放行', () => {
it('常见只读命令 auto-approve', () => {
- for (const c of ['ls -la', 'cat package.json', 'pwd', 'grep -rn foo src', 'rg TODO', 'wc -l x', 'head -5 f', 'echo hi']) {
+ for (const c of ['ls -la', 'cat package.json', 'pwd', 'grep -rn foo src --include="[!.]*.ts"', 'rg TODO', 'wc -l x', 'head -5 f', 'echo hi']) {
expect(verdict('Bash', { command: c })).toBe('auto-approve');
}
});
diff --git a/packages/maker-core/src/agents/shared/auto-review.adversarial.test.ts b/packages/maker-core/src/agents/shared/auto-review.adversarial.test.ts
index 1b8407b8fa..0ce9545c74 100644
--- a/packages/maker-core/src/agents/shared/auto-review.adversarial.test.ts
+++ b/packages/maker-core/src/agents/shared/auto-review.adversarial.test.ts
@@ -407,6 +407,27 @@ describe('对抗语料 — dotenv 凭证路径', () => {
'rg -g .env KEY .',
'grep -r --include=.env KEY .',
"grep -r --include='.env*' API_KEY .",
+ "grep -r --include='*' API_KEY .",
+ "grep -r --include='*.ts' API_KEY .",
+ "rg -g '*.ts' API_KEY .",
+ "grep -r --include='[[:punct:]]env*' API_KEY .",
+ "grep -r --include='[,-0]env*' API_KEY .",
+ "grep -r --include='{.env,.env.local}' API_KEY .",
+ 'grep -r --exclude=.env API_KEY .',
+ 'grep -r API_KEY .',
+ 'grep -Rni API_KEY .',
+ 'grep --recursive API_KEY .',
+ 'grep --recurs API_KEY .',
+ 'grep -d recurse API_KEY .',
+ 'grep -d rec API_KEY .',
+ 'grep -drecurse API_KEY .',
+ 'grep --directories=recurse API_KEY .',
+ 'grep --directories=rec API_KEY .',
+ 'grep -f patterns.txt -r .',
+ 'env grep -rn API_KEY .',
+ 'env -- grep -rn API_KEY .',
+ 'command grep --recursive API_KEY .',
+ 'printf x | grep -r API_KEY .',
"rg --hidden -g '.env*' API_KEY .",
'grep -r --exclude-from=.env API_KEY .',
'rg --ignore-file=.env API_KEY .',
@@ -449,10 +470,23 @@ describe('对抗语料 — dotenv 凭证路径', () => {
['grep -e.env data.json', 'auto-approve'],
['grep -ef data.json', 'auto-approve'],
['grep -ef.env data.json', 'auto-approve'],
- ["rg -g '*.ts' KEY .", 'auto-approve'],
- ["grep -r --include='*.json' KEY .", 'auto-approve'],
- ["rg --hidden -g '*.ts' KEY .", 'auto-approve'],
- ['grep -r --exclude=.env KEY .', 'auto-approve'],
+ ["rg -g '[!.]*.ts' KEY .", 'auto-approve'],
+ ["grep -r --include='[!.]*.json' KEY .", 'auto-approve'],
+ ["rg --hidden -g '[!.]*.ts' KEY .", 'auto-approve'],
+ ["grep -r --include='[!.]*.ts' KEY .", 'auto-approve'],
+ ["grep -r --include='config.env*' KEY .", 'auto-approve'],
+ ['grep -e -r data.txt', 'auto-approve'],
+ ['grep -e-r data.txt', 'auto-approve'],
+ ['grep --regexp=-r data.txt', 'auto-approve'],
+ ['grep KEY -- -r', 'auto-approve'],
+ ['grep -d skip KEY .', 'auto-approve'],
+ ['grep --directories=skip KEY .', 'auto-approve'],
+ ['grep -r -d skip KEY .', 'auto-approve'],
+ ['grep --recursive --directories=read KEY .', 'auto-approve'],
+ ["grep -r --include='safe,.env' KEY .", 'auto-approve'],
+ ["grep -r --include='foo{.env,.ts}' KEY .", 'prompt'],
+ ['env -- grep KEY data.txt', 'auto-approve'],
+ ['env -- grep -e -r data.txt', 'auto-approve'],
["rg --hidden -g '!.env*' KEY .", 'auto-approve'],
['git --no-pager diff --no-index /dev/null README.md', 'auto-approve'],
['git grep .env data.json', 'auto-approve'],
diff --git a/packages/maker-core/src/agents/shared/auto-review.corpus.test.ts b/packages/maker-core/src/agents/shared/auto-review.corpus.test.ts
index 83e17121b5..c6a530d81a 100644
--- a/packages/maker-core/src/agents/shared/auto-review.corpus.test.ts
+++ b/packages/maker-core/src/agents/shared/auto-review.corpus.test.ts
@@ -26,8 +26,8 @@ const opts = { cwd: '/repo', platform: 'darwin' as const };
describe('语料回归 — 修复源 1:引号内 | 是数据不是管道', () => {
it('grep/rg 的 alternation pattern → auto-approve(改前被切碎落灰区)', () => {
for (const c of [
- 'grep -Rni "readSystemContacts\\|writeSystemContacts" /repo/apps/desktop/src',
- `grep -RniE "智能通讯录|推广|用法|联系人|contacts" apps packages --exclude-dir=node_modules --include='*.tsx'`,
+ 'grep -Rni "readSystemContacts\\|writeSystemContacts" /repo/apps/desktop/src --include="[!.]*.ts"',
+ `grep -RniE "智能通讯录|推广|用法|联系人|contacts" apps packages --exclude-dir=node_modules --include='[!.]*.tsx'`,
`rg -n "系统通讯录|智能通讯录|system contacts" . --glob '!node_modules'`,
`rg -n "NODE_OPTIONS|max-old-space-size|typecheck" .github/workflows`,
'grep -n "provider\\|chatId\\|threadId" apps/desktop/src/main/im/shared/router.ts',
@@ -51,7 +51,7 @@ describe('语料回归 — 修复源 2:cd 区内目录 && 只读命令', () => {
'cd /repo && git diff --check',
'cd /repo/packages/maker-core && ls -la src',
'cd /repo/.cindy-worktrees/feature-x && git diff upstream/main...HEAD --stat',
- 'cd /repo && grep -rn "classifyShellCommand" packages --include="*.ts" | head -20',
+ 'cd /repo && grep -rn "classifyShellCommand" packages --include="[!.]*.ts" | head -20',
]) {
expect(classifyShellCommand(c, roots, opts), c).toBe('auto-approve');
}
@@ -299,7 +299,9 @@ describe('review 第三轮 — token/option 解析族', () => {
expect(classifyShellCommand(
'git diff --name-only | grep -E "\\.env|\\.pem|credential|secret"', roots, opts,
)).toBe('auto-approve');
- expect(classifyShellCommand('grep -Rni "foo|bar" src', roots, opts)).toBe('auto-approve');
+ expect(classifyShellCommand(
+ 'grep -Rni "foo|bar" src --include="[!.]*.ts"', roots, opts,
+ )).toBe('auto-approve');
});
it('[P2] 表外解释器选项 fail-closed:值不会被当成脚本文件', () => {
@@ -340,8 +342,10 @@ describe('review 第四轮 — 分类器入口族', () => {
// 命令替换会真的读凭证;变量展开会把令牌摊到命令行 —— 两类都不是纯数据。
expect(classifyShellCommand('grep "$(cat ~/.aws/credentials)" file', roots, opts)).toBe('prompt-each-time');
expect(classifyShellCommand('grep "$GITHUB_TOKEN" file', roots, opts)).toBe('prompt-each-time');
- // 静态模式仍照常剥离,两个方向都不回退。
- expect(classifyShellCommand('grep -Rni "foo|bar" src', roots, opts)).toBe('auto-approve');
+ // 静态模式仍照常剥离,两个方向都不回退;递归范围显式限制为非 dotenv 文件。
+ expect(classifyShellCommand(
+ 'grep -Rni "foo|bar" src --include="[!.]*.ts"', roots, opts,
+ )).toBe('auto-approve');
expect(classifyShellCommand(
'git diff --name-only | grep -E "\\.env|\\.pem|credential|secret"', roots, opts,
)).toBe('auto-approve');
@@ -404,8 +408,10 @@ describe('review 第五轮 — 选项值与动态执行', () => {
]) {
expect(classifyShellCommand(c, roots, opts), c).toBe('prompt-each-time');
}
- // 真正的搜索模式仍照常剥离。
- expect(classifyShellCommand('grep -Rni "foo|bar" src', roots, opts)).toBe('auto-approve');
+ // 真正的搜索模式仍照常剥离;递归范围显式限制为非 dotenv 文件。
+ expect(classifyShellCommand(
+ 'grep -Rni "foo|bar" src --include="[!.]*.ts"', roots, opts,
+ )).toBe('auto-approve');
expect(classifyShellCommand(
'git diff --name-only | grep -E "\\.env|\\.pem|credential|secret"', roots, opts,
)).toBe('auto-approve');
@@ -732,7 +738,7 @@ describe('语料回归 — 凭证 flag 只在命令位才算(反向边界)', ()
for (const c of [
'echo --show-token',
'echo "用法: --show-token"',
- 'grep -rn -- --show-token src',
+ 'grep -n -- --show-token src/file.ts',
]) {
expect(classifyShellCommand(c, roots, opts), c).toBe('auto-approve');
}
@@ -819,7 +825,7 @@ describe('语料回归 — 命中率下限(总量护栏)', () => {
'git branch --show-current',
'ls -la',
'cat package.json',
- 'grep -rn "TODO" src | head -20',
+ 'grep -rn "TODO" src --include="[!.]*.ts" | head -20',
'rg -n "foo|bar" src',
'sed -n 1,120p src/index.ts',
'gh pr view 1 --json state',
diff --git a/packages/maker-core/src/agents/shared/auto-review.test.ts b/packages/maker-core/src/agents/shared/auto-review.test.ts
index 877e246173..b16af1dcad 100644
--- a/packages/maker-core/src/agents/shared/auto-review.test.ts
+++ b/packages/maker-core/src/agents/shared/auto-review.test.ts
@@ -80,7 +80,7 @@ describe('reviewAction — exec 实际 cwd 边界', () => {
describe('classifyShellCommand — 只读放行', () => {
it('常见只读命令 / git 只读 / curl GET', () => {
- for (const c of ['ls -la', 'cat f', 'grep -rn x .', 'rg TODO', 'git status', 'git log', 'curl -sS https://x.com', 'env FOO=1 ls', 'timeout 5 grep x f']) {
+ for (const c of ['ls -la', 'cat f', 'grep -rn x . --include="[!.]*.ts"', 'rg TODO', 'git status', 'git log', 'curl -sS https://x.com', 'env FOO=1 ls', 'timeout 5 grep x f']) {
expect(classifyShellCommand(c, roots)).toBe('auto-approve');
}
});
@@ -1016,7 +1016,7 @@ describe('classifyShellCommand — 第三轮 bot 审查回归护栏', () => {
expect(classifyShellCommand('{c..c}at notes.txt', roots)).toBe('prompt');
// 反例:位置参数里的 brace 只影响文件名 → 不升级;find 占位符 {} 不算展开。
expect(classifyShellCommand('ls dir/{a,b}', roots)).toBe('auto-approve');
- expect(classifyShellCommand('grep -rn foo src/{a,b}', roots)).toBe('auto-approve');
+ expect(classifyShellCommand('grep -rn foo src/{a,b} --include="[!.]*.ts"', roots)).toBe('auto-approve');
expect(classifyShellCommand('find . -maxdepth 0 -print', roots)).toBe('auto-approve'); // {} 占位符另测,这里确认普通 find 放行
});
@@ -4124,7 +4124,7 @@ describe('伪设备白名单:静音重定向不得打断(实机语料探针发
it('日常命令语料整体不被硬拦(尽量不打扰的回归护栏)', () => {
for (const c of [
- 'ls -la', 'git status', 'cat package.json', 'grep -rn TODO src',
+ 'ls -la', 'git status', 'cat package.json', 'grep -rn TODO src --include="[!.]*.ts"',
'pnpm install', 'npx tsc --noEmit', 'rm -rf node_modules', 'rm -rf build',
'git add .', 'git commit -m "fix: x"', 'git push origin feature/x',
'env NODE_ENV=test npx vitest run', 'timeout 60 pnpm test', 'nohup pnpm dev',
diff --git a/packages/maker-core/src/agents/shared/auto-review.ts b/packages/maker-core/src/agents/shared/auto-review.ts
index 3705709653..a18f739322 100644
--- a/packages/maker-core/src/agents/shared/auto-review.ts
+++ b/packages/maker-core/src/agents/shared/auto-review.ts
@@ -1868,6 +1868,7 @@ function unwrapCommand(
let bail = false;
while (i < toks.length) {
const t = toks[i];
+ if (t === '--') { i++; break; }
if (t === '-' || t === '-i' || t === '--ignore-environment' || t === '-0' || t === '--null' || t === '-v' || t === '--debug') { i++; continue; }
if (t === '-u' || t === '--unset') { i += 2; continue; }
if (t === '-C' || t === '--chdir') {
@@ -4796,12 +4797,27 @@ function readerShortOptionKind(bin: string, option: string): ReaderOptionKind |
return null;
}
+function selectorAlternativeCannotMatchDotenv(value: string): boolean {
+ const basename = value.replace(/\\/g, '/').split('/').pop() ?? '';
+ if (!/[*?\[]/.test(basename)) return !isDotenvCredentialPath(value);
+ if (/^\[(?:!|\^)\.\]/.test(basename)) return true;
+ const literalPrefix = basename.slice(0, basename.search(/[*?\[]/));
+ return Boolean(literalPrefix)
+ && !'.env'.startsWith(literalPrefix)
+ && !literalPrefix.startsWith('.env');
+}
+
+function expandBraceAlternatives(value: string, depth = 0): string[] {
+ if (depth >= 8) return ['*'];
+ const match = /^(.*?)\{([^{}]*,[^{}]*)\}(.*)$/.exec(value);
+ if (!match) return [value];
+ return match[2].split(',').flatMap((alternative) =>
+ expandBraceAlternatives(`${match[1]}${alternative}${match[3]}`, depth + 1));
+}
+
function selectorCouldMatchDotenv(value: string): boolean {
- if (isDotenvCredentialPath(value)) return true;
- return value
- .replace(/\\/g, '/')
- .split(/[,{}]/)
- .some((part) => isDotenvCredentialPath(part.replace(/[[\]*?]/g, '')));
+ return !value.startsWith('!') && expandBraceAlternatives(value)
+ .some((alternative) => !selectorAlternativeCannotMatchDotenv(alternative));
}
function readerArgumentsReadDotenv(
@@ -4867,6 +4883,67 @@ function readerArgumentsReadDotenv(
return false;
}
+function grepRecursesIntoPotentialDotenv(bin: string, args: readonly string[]): boolean {
+ if (bin !== 'grep' && bin !== 'egrep' && bin !== 'fgrep') return false;
+
+ const longOptionNames = [
+ '--recursive', '--directories', ...GREP_LONG_OPTIONS.map((option) => option.name),
+ ];
+ let recursive = false;
+ const includes: string[] = [];
+
+ for (let index = 0; index < args.length; index += 1) {
+ const token = args[index];
+ if (token === '--') break;
+
+ if (token.startsWith('--')) {
+ const equalsIndex = token.indexOf('=');
+ const name = equalsIndex >= 0 ? token.slice(0, equalsIndex) : token;
+ const exact = longOptionNames.find((option) => option === name);
+ const matches = exact ? [exact] : longOptionNames.filter((option) => option.startsWith(name));
+ const canonical = matches.length === 1 ? matches[0] : null;
+ if (!canonical) continue;
+
+ if (canonical === '--recursive') {
+ recursive = true;
+ continue;
+ }
+
+ const attached = equalsIndex >= 0 ? token.slice(equalsIndex + 1) : undefined;
+ const value = attached ?? args[index + 1];
+ if (attached === undefined) index += 1;
+ if (canonical === '--directories') {
+ recursive = Boolean(value && 'recurse'.startsWith(value));
+ }
+ if (canonical === '--include' && value) includes.push(value);
+ continue;
+ }
+
+ if (/^-[^-]/.test(token)) {
+ for (let optionIndex = 1; optionIndex < token.length; optionIndex += 1) {
+ const option = token.charAt(optionIndex);
+ if (option === 'r' || option === 'R') {
+ recursive = true;
+ continue;
+ }
+ if (option === 'd') {
+ const attached = token.slice(optionIndex + 1) || undefined;
+ const value = attached ?? args[index + 1];
+ if (attached === undefined) index += 1;
+ recursive = Boolean(value && 'recurse'.startsWith(value));
+ break;
+ }
+ if (readerShortOptionKind('grep', option)) {
+ if (optionIndex === token.length - 1) index += 1;
+ break;
+ }
+ }
+ }
+ }
+
+ return recursive && (includes.length === 0 || includes.some(selectorCouldMatchDotenv));
+}
+
function gitGrepExpandsSearchScope(args: readonly string[]): boolean {
for (let index = 0; index < args.length; index += 1) {
const token = args[index];
@@ -4963,6 +5040,7 @@ function shellCommandReadsDotenv(
if (!DOTENV_FILE_READER_BINS.has(bin)) continue;
const args = tokens.slice(1);
+ if (grepRecursesIntoPotentialDotenv(bin, args)) return true;
if (readerArgumentsReadDotenv(bin, args)) return true;
}
return false;