Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(pi): gate unscoped git content reads
Chriseb9650800163
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.tsindex 02c78ac2ef..659f27c465 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@@ -165,7 +165,7 @@ describe('classifyBuiltinToolForAutoReview — Bash 只读命令放行', () => {}});it('git 只读子命令 auto-approve', () => {- for (const c of ['git status', 'git log --oneline', 'git diff HEAD -- README.md', 'git show abc', 'git branch', 'git config --get user.name']) {+ for (const c of ['git status', 'git log --oneline', 'git diff --stat', 'git show HEAD:README.md', 'git branch', 'git config --get user.name']) {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.tsindex 8ad139c01e..3ab62cd188 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@@ -455,6 +455,7 @@ describe('对抗语料 — dotenv 凭证路径', () => {'grep --fil=.env.local data.txt','sed --fil=.env.production data.txt','git --no-pager diff --no-index /dev/null .env',+ 'git --no-pager diff --no-index /dev/null README.md','git diff','git diff HEAD','git diff --stat -U0',@@ -465,6 +466,11 @@ describe('对抗语料 — dotenv 凭证路径', () => {'git diff -- ./','git diff -- src','git diff -- src/',+ 'git diff -- fixtures.v1',+ 'git diff -- README.md',+ 'git diff HEAD -- README.md',+ 'git diff -- src/index.ts',+ "git diff -- ':(top)README.md'","git diff -- ':(top)src'","git diff -- ':(glob)**'","git diff -- '*.ts'",@@ -473,12 +479,18 @@ describe('对抗语料 — dotenv 凭证路径', () => {"git diff -- ':(exclude,glob)**/.env*'","git diff -- . ':(exclude,glob)**/.env*'","git diff -- ':!.env'",+ "git diff -- 'notes:.env'",'git diff --no-index /dev/null .','git diff -- .env*','env -- git diff','git status --short && git diff','git grep API_KEY','git grep -- API_KEY',+ 'git grep API_KEY -- .',+ 'git grep API_KEY -- fixtures.v1',+ 'git grep .env -- data.json',+ 'git grep -- .env data.json',+ 'git grep -e API_KEY -- README.md','git grep -e-l','git grep -e -l','git grep -ne -l',@@ -488,6 +500,20 @@ describe('对抗语料 — dotenv 凭证路径', () => {'command git grep API_KEY','git diff -- .env.local | head','git show HEAD:.env',+ 'git show HEAD',+ 'git show HEAD -- README.md',+ 'git show --format=prefix:.env HEAD',+ "git show --format 'prefix:.env' HEAD",+ "git show -- 'notes:.env'",+ 'git log -p',+ 'git log --patch',+ 'git whatchanged -p',+ 'git cat-file -p HEAD',+ 'git cat-file blob HEAD',+ 'git cat-file --batch',+ 'git cat-file --batch-command',+ 'git status -v',+ 'git status -vv','git show HEAD:.env.local','git cat-file -p HEAD:.env.production','git show :0:.env',@@ -503,6 +529,10 @@ describe('对抗语料 — dotenv 凭证路径', () => {'env git --no-pager grep --untracked --no-exclude-standard .','command git grep --no-index .','git status --short && git grep --untracked .',+ 'git grep . -- --untracked',+ 'git grep -e --untracked -- README.md',+ 'git grep -e--untracked -- README.md',+ 'git grep --regexp=--no-index -- README.md',]) {expect(classifyShellCommand(command, roots, opts), command).toBe('prompt-each-time');}@@ -568,37 +598,30 @@ describe('对抗语料 — dotenv 凭证路径', () => {['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 diff --stat', 'auto-approve'],['git diff --name-only', 'auto-approve'],['git diff --stat -- .', 'auto-approve'],['git diff --name-only -- src', 'auto-approve'],- ['git diff -- README.md', 'auto-approve'],- ['git diff HEAD -- README.md', 'auto-approve'],- ['git diff -- src/index.ts', 'auto-approve'],- ["git diff -- ':(top)README.md'", 'auto-approve'],- ['git grep .env -- data.json', 'auto-approve'],- ['git grep -- .env data.json', 'auto-approve'],- ['git grep -e API_KEY -- README.md', 'auto-approve'],+ ['git show --stat HEAD', 'auto-approve'],+ ['git show --name-only HEAD', 'auto-approve'],+ ['git show --no-patch HEAD', 'auto-approve'],+ ['git show HEAD:README.md', 'auto-approve'],+ ['git cat-file -p HEAD:README.md', 'auto-approve'],+ ['git cat-file blob HEAD:README.md', 'auto-approve'],+ ['git cat-file -t HEAD', 'auto-approve'],+ ['git cat-file --batch-check', 'auto-approve'],+ ['git status --short', 'auto-approve'],+ ['git log --format=prefix:.env', 'auto-approve'],+ ['git log --stat', 'auto-approve'],+ ['git whatchanged', 'auto-approve'],+ ["git log --pretty 'prefix:.env'", 'auto-approve'],['git grep -l API_KEY', 'auto-approve'],['git grep -L API_KEY', 'auto-approve'],['git grep --files-with-matches API_KEY', 'auto-approve'],['git grep -e API_KEY -l', 'auto-approve'],['git grep -f patterns.txt -L', 'auto-approve'],- ['git show HEAD:README.md', 'auto-approve'],- ['git cat-file -p HEAD:README.md', 'auto-approve'],- ['git log --format=prefix:.env', 'auto-approve'],- ['git show --format=prefix:.env HEAD', 'auto-approve'],- ["git show --format 'prefix:.env' HEAD", 'auto-approve'],- ["git log --pretty 'prefix:.env'", 'auto-approve'],- ["git show -- 'notes:.env'", 'auto-approve'],- ["git diff -- 'notes:.env'", 'auto-approve'],['git show :README.md', 'auto-approve'],['git show :./README.md', 'auto-approve'],- ['git grep . -- --untracked', 'auto-approve'],- ['git grep -e --untracked -- README.md', 'auto-approve'],- ['git grep -e--untracked -- README.md', 'auto-approve'],- ['git grep --regexp=--no-index -- README.md', 'auto-approve'],] as const)('%s 保持既有 %s 档位', (command, expected) => {expect(classifyShellCommand(command, roots, opts)).toBe(expected);});diff --git a/packages/maker-core/src/agents/shared/auto-review.test.ts b/packages/maker-core/src/agents/shared/auto-review.test.tsindex 11adc3fb0b..5f66c546e7 100644--- a/packages/maker-core/src/agents/shared/auto-review.test.ts+++ b/packages/maker-core/src/agents/shared/auto-review.test.ts@@ -87,16 +87,16 @@ describe('classifyShellCommand — 只读放行', () => {it('git 全局目录选项后仍识别工作区内的真实只读子命令', () => {for (const c of ['git -C /repo status',- 'git -C /repo show HEAD',+ 'git -C /repo show HEAD:README.md','git -C/repo log --oneline',- 'git --namespace=review -C /repo diff HEAD -- README.md',+ 'git --namespace=review -C /repo diff --stat',]) {expect(classifyShellCommand(c, roots), c).toBe('auto-approve');}});- it('子命令自身的 -c 参数不被当作危险全局选项', () => {+ it('子命令自身的 -c 参数不被当作危险全局选项,内容输出仍进入凭证门', () => {for (const c of ['git diff -c -- README.md', 'git show -c']) {- expect(classifyShellCommand(c, roots), c).toBe('auto-approve');+ expect(classifyShellCommand(c, roots), c).toBe('prompt-each-time');}});it('git 仓库路径选项只放行工作区内的静态路径', () => {@@ -526,10 +526,10 @@ describe('reviewAction / classifyShellCommand — agent OAuth 凭证文件', ()});describe('classifyShellCommand — git --output 写文件 / curl SSRF 改路由 / wget 一律升级', () => {- it('git diff --output 写文件(无 shell >)→ prompt;显式普通 path 的 git diff 仍放行', () => {+ it('git diff --output 写文件(无 shell >)→ prompt;metadata-only diff 仍放行', () => {expect(classifyShellCommand('git diff --output ~/.bashrc HEAD^ HEAD', roots)).toBe('prompt');expect(classifyShellCommand('git diff --output=/tmp/x HEAD', roots)).toBe('prompt');- expect(classifyShellCommand('git diff HEAD -- README.md', roots)).toBe('auto-approve');+ expect(classifyShellCommand('git diff --stat -- README.md', roots)).toBe('auto-approve');});it('curl 改路由 flag(--resolve/--connect-to/--unix-socket/-x/--proxy)→ prompt(SSRF 绕过)', () => {for (const c of [@@ -575,11 +575,11 @@ describe('classifyShellCommand — procfs / 短选项绕过 / 反斜杠 / git RCit('反斜杠转义拆分 flag(find -ex\\ec)去转义后命中', () => {expect(classifyShellCommand("find . -ex\\ec sh -c 'x' {} +", roots)).toBe('prompt');});- it('git --ext-diff / 内联 -c(core.pager/diff.external)→ prompt(RCE);显式普通 path 的 git diff 仍放行', () => {+ it('git --ext-diff / 内联 -c(core.pager/diff.external)→ prompt(RCE);metadata-only diff 仍放行', () => {expect(classifyShellCommand('git diff --ext-diff', roots)).toBe('prompt');expect(classifyShellCommand('git -c core.pager=evil show HEAD', roots)).toBe('prompt');expect(classifyShellCommand('git -c diff.external=evil diff', roots)).toBe('prompt');- expect(classifyShellCommand('git diff HEAD -- README.md', roots)).toBe('auto-approve');+ expect(classifyShellCommand('git diff --stat -- README.md', roots)).toBe('auto-approve');});});@@ -778,10 +778,10 @@ describe('复审第三批:env 注入 / 显式路径 / file:// / 缩写 IP / git// 反例:公网十进制不误伤(0251 之外的规范公网)。expect(classifyShellCommand('curl http://93.184.216.34/', roots)).toBe('auto-approve');});- it('git cat-file --filters/--textconv 跑 filter(RCE)→ prompt;cat-file -p 只读放行', () => {+ it('git cat-file --filters/--textconv 跑 filter(RCE)→ prompt;显式普通对象路径仍放行', () => {expect(classifyShellCommand('git cat-file --filters HEAD:path', roots)).toBe('prompt');expect(classifyShellCommand('git cat-file --textconv HEAD:path', roots)).toBe('prompt');- expect(classifyShellCommand('git cat-file -p HEAD', roots)).toBe('auto-approve');+ expect(classifyShellCommand('git cat-file -p HEAD:README.md', roots)).toBe('auto-approve');});});@@ -860,8 +860,8 @@ describe('classifyShellCommand — 第三轮 bot 审查回归护栏', () => {for (const c of ['git grep --open-files-in-pager=./payload pattern', 'git grep -O./payload pattern']) {expect(classifyShellCommand(c, roots)).toBe('prompt');}- // 反例:显式普通 path 的 git grep 仍放行。- expect(classifyShellCommand('git grep pattern -- README.md', roots)).toBe('auto-approve');+ // 反例:files-only git grep 仍放行。+ expect(classifyShellCommand('git grep -l pattern', roots)).toBe('auto-approve');});it('git 子命令前内联 config 的等号形式(--config-env=…)升级 —— 防 core.pager RCE', () => {diff --git a/packages/maker-core/src/agents/shared/auto-review.ts b/packages/maker-core/src/agents/shared/auto-review.tsindex 9e1adc3375..85caf3ec9e 100644--- a/packages/maker-core/src/agents/shared/auto-review.ts+++ b/packages/maker-core/src/agents/shared/auto-review.ts@@ -5127,27 +5127,6 @@ function gitArgumentsReadDotenv(args: readonly string[]): boolean {return false;}-function gitGrepHasExplicitPathspec(args: readonly string[]): boolean {- const separator = args.indexOf('--');- if (separator < 0) return false;- let patternProvided = false;- for (let index = 0; index < separator; index += 1) {- const token = args[index];- if (token.startsWith('-')) {- const equalsIndex = token.indexOf('=');- const name = equalsIndex >= 0 ? token.slice(0, equalsIndex) : token;- const kind = token.startsWith('--')- ? resolveReaderLongOption('grep', name)- : [...token.slice(1)].map((option) => readerShortOptionKind('grep', option)).find(Boolean);- if (kind && equalsIndex < 0 && !/^-[^-].+/.test(token)) index += 1;- if (kind === 'data' || kind === 'data-file') patternProvided = true;- continue;- }- patternProvided = true;- }- return args.length - separator - 1 >= (patternProvided ? 1 : 2);-}-function gitGrepListsOnly(args: readonly string[]): boolean {for (let tokenIndex = 0; tokenIndex < args.length; tokenIndex += 1) {const token = args[tokenIndex];@@ -5176,54 +5155,75 @@ function gitGrepListsOnly(args: readonly string[]): boolean {return false;}-function parseGitPathspec(value: string): { exclude: boolean; pattern: string } {- const longPathspec = /^:\(([^)]*)\)(.*)$/.exec(value);- if (longPathspec) {- const magic = new Set(longPathspec[1].split(','));- return { exclude: magic.has('exclude') || magic.has('!'), pattern: longPathspec[2] };- }- if (value.startsWith(':!') || value.startsWith(':^')) {- return { exclude: true, pattern: value.slice(2) };- }- if (value.startsWith(':/')) return { exclude: false, pattern: value.slice(2) };- return { exclude: false, pattern: value };+const GIT_METADATA_ONLY_FLAGS = [+ '--stat', '--shortstat', '--numstat', '--name-only', '--name-status', '--summary', '--check', '--raw',+] as const;++function gitPatchRequested(args: readonly string[]): boolean {+ return args.some((arg) =>+ /^(?:-p|--patch|-u|-U\d*|--unified(?:=.*)?|-W|-c|--cc|--function-context|--word-diff(?:=.*)?|--word-diff-regex(?:=.*)?|--color-words(?:=.*)?|--patch-with-stat|--patch-with-raw|--binary|--inter-hunk-context(?:=.*)?)$/.test(arg));+}++function gitMetadataOnlyRequested(args: readonly string[]): boolean {+ return args.some((arg) => GIT_METADATA_ONLY_FLAGS.includes(arg as typeof GIT_METADATA_ONLY_FLAGS[number]));}-function gitDiffPathspecIsSafeFile(value: string): boolean {- const { exclude, pattern } = parseGitPathspec(value);- if (exclude) return true;- const normalized = pattern.replace(/\\/g, '/').replace(/^\.\//, '');- if (SAFE_DEVICE_PATH.test(normalized) || /^NUL$/i.test(normalized)) return true;- if (!normalized || normalized === '.' || normalized === '..' || normalized.endsWith('/')) return false;- if (/[*?\[\]{]/.test(normalized)) return false;- const basename = normalized.split('/').pop() ?? '';- return basename.includes('.');+function isSafeGitObjectPath(value: string): boolean {+ if (value.startsWith(':(') || value.startsWith(':!') || value.startsWith(':^') || value.startsWith(':/')) return false;+ const indexPath = /^:(?:[0-3]:)?(.+)$/.exec(value);+ if (indexPath) return !isDotenvCredentialPath(indexPath[1]);+ const separator = value.indexOf(':');+ if (separator <= 0) return false;+ const revision = value.slice(0, separator);+ const objectPath = value.slice(separator + 1);+ return /^[A-Za-z0-9_./@{}~^+\-]+$/.test(revision)+ && Boolean(objectPath)+ && !isDotenvCredentialPath(objectPath);+}++function gitShowHasOnlySafeObjectPaths(args: readonly string[]): boolean {+ let sawObjectPath = false;+ for (let index = 0; index < args.length; index += 1) {+ const token = args[index];+ if (token === '--') return false;+ if (token === '--format' || token === '--pretty') {+ index += 1;+ continue;+ }+ if (token.startsWith('-')) continue;+ if (!isSafeGitObjectPath(token)) return false;+ sawObjectPath = true;+ }+ return sawObjectPath;}-function gitDiffPathspecsCouldReadDotenv(pathspecs: readonly string[]): boolean {- const positive = pathspecs.filter((value) => !parseGitPathspec(value).exclude);- return positive.length === 0 || positive.some((value) => !gitDiffPathspecIsSafeFile(value));+function gitCatFileReadsUnscopedContent(args: readonly string[]): boolean {+ if (args.includes('--batch-check') || args.some((arg) => arg.startsWith('--batch-check='))) return false;+ if (args.some((arg) => arg === '--batch' || arg.startsWith('--batch=') || arg === '--batch-command' || arg.startsWith('--batch-command='))) return true;++ let contentMode = args.includes('-p');+ let objectArgs = args.filter((arg) => !arg.startsWith('-'));+ if (objectArgs[0] === 'blob') {+ contentMode = true;+ objectArgs = objectArgs.slice(1);+ } else if (objectArgs[0] === 'tree' || objectArgs[0] === 'commit' || objectArgs[0] === 'tag') {+ return false;+ }+ return contentMode && !objectArgs.some(isSafeGitObjectPath);}function gitContentReadWithoutPath(sub: string, args: readonly string[]): boolean {- if (sub === 'grep') return !gitGrepListsOnly(args) && !gitGrepHasExplicitPathspec(args);- if (sub !== 'diff') return false;- const metadataOnly = args.some((arg) => [- '--stat', '--shortstat', '--numstat', '--name-only', '--name-status', '--summary', '--check', '--raw',- ].includes(arg));- const patchRequested = args.some((arg) =>- /^(?:-p|--patch|-u|-U\d*|--unified(?:=.*)?|-W|--function-context|--word-diff(?:=.*)?|--word-diff-regex(?:=.*)?|--color-words(?:=.*)?|--patch-with-stat|--patch-with-raw|--binary|--inter-hunk-context(?:=.*)?)$/.test(arg));- if (metadataOnly && !patchRequested) return false;-- const separator = args.indexOf('--');- if (separator >= 0 && separator < args.length - 1) {- return gitDiffPathspecsCouldReadDotenv(args.slice(separator + 1));+ if (sub === 'grep') return !gitGrepListsOnly(args);+ if (sub === 'diff') return !gitMetadataOnlyRequested(args) || gitPatchRequested(args);+ if (sub === 'show') {+ if (gitShowHasOnlySafeObjectPaths(args)) return false;+ const patchSuppressed = args.includes('-s') || args.includes('--no-patch') || gitMetadataOnlyRequested(args);+ return !patchSuppressed || gitPatchRequested(args);}- if (args.includes('--no-index')) {- const operands = args.filter((arg) => !arg.startsWith('-'));- if (operands.length >= 2) return operands.some((operand) => !gitDiffPathspecIsSafeFile(operand));- }- return true;+ if (sub === 'log' || sub === 'whatchanged') return gitPatchRequested(args);+ if (sub === 'cat-file') return gitCatFileReadsUnscopedContent(args);+ if (sub === 'status') return args.some((arg) => arg === '--verbose' || /^-[^-]*v/.test(arg));+ return false;}function shellCommandReadsDotenv(