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 scoped dotenv searches

Chris681b6e6424a8
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 5abbe86433..8ad139c01e 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
@@ -443,6 +443,8 @@ describe('对抗语料 — dotenv 凭证路径', () => {
"rg --hidden -g '.env*' API_KEY .",
"rg --hidden -g '!.env*' -g '.env' API_KEY .",
'rg --hidden --no-ignore API_KEY .',
+ 'rg -. --no-ignore API_KEY .',
+ 'env -- rg -. --no-ignore API_KEY .',
'rg -uu API_KEY .',
'rg -uuu API_KEY .',
'env -- rg --hidden --no-ignore API_KEY .',
@@ -456,14 +458,33 @@ describe('对抗语料 — dotenv 凭证路径', () => {
'git diff',
'git diff HEAD',
'git diff --stat -U0',
+ 'git diff --stat -U0 -- .',
'git diff --stat --word-diff',
'git diff --stat --binary',
+ 'git diff -- .',
+ 'git diff -- ./',
+ 'git diff -- src',
+ 'git diff -- src/',
+ "git diff -- ':(top)src'",
+ "git diff -- ':(glob)**'",
+ "git diff -- '*.ts'",
+ "git diff -- ':(exclude).env'",
+ "git diff -- ':(exclude).env*'",
+ "git diff -- ':(exclude,glob)**/.env*'",
+ "git diff -- . ':(exclude,glob)**/.env*'",
+ "git diff -- ':!.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 -e-l',
+ 'git grep -e -l',
+ 'git grep -ne -l',
+ 'git grep -f -L',
+ 'git grep --regexp -l',
+ 'git grep --file -L',
'command git grep API_KEY',
'git diff -- .env.local | head',
'git show HEAD:.env',
@@ -526,6 +547,9 @@ describe('对抗语料 — dotenv 凭证路径', () => {
['rg --no-ignore KEY .', 'auto-approve'],
['rg -u KEY .', 'auto-approve'],
['rg --hidden --no-hidden KEY .', 'auto-approve'],
+ ['rg -. --no-hidden --no-ignore KEY .', 'auto-approve'],
+ ["rg -. -g '[!.]*.ts' KEY .", 'auto-approve'],
+ ['rg -e-. data.txt', 'auto-approve'],
['rg -uu --no-hidden KEY .', 'auto-approve'],
['rg -e --hidden data.txt', 'auto-approve'],
['rg -- --hidden data.txt', 'auto-approve'],
@@ -547,14 +571,20 @@ describe('对抗语料 — dotenv 凭证路径', () => {
['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 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'],
@@ -563,8 +593,6 @@ describe('对抗语料 — dotenv 凭证路径', () => {
["git log --pretty 'prefix:.env'", 'auto-approve'],
["git show -- 'notes:.env'", 'auto-approve'],
["git diff -- 'notes:.env'", 'auto-approve'],
- ["git diff -- ':(exclude).env'", 'auto-approve'],
- ["git diff -- ':!.env'", 'auto-approve'],
['git show :README.md', 'auto-approve'],
['git show :./README.md', 'auto-approve'],
['git grep . -- --untracked', 'auto-approve'],
diff --git a/packages/maker-core/src/agents/shared/auto-review.ts b/packages/maker-core/src/agents/shared/auto-review.ts
index 2d780b7615..9e1adc3375 100644
--- a/packages/maker-core/src/agents/shared/auto-review.ts
+++ b/packages/maker-core/src/agents/shared/auto-review.ts
@@ -5033,6 +5033,7 @@ function rgSearchesPotentialDotenv(args: readonly string[]): boolean {
if (!/^-[^-]/.test(token)) continue;
for (let optionIndex = 1; optionIndex < token.length; optionIndex += 1) {
const option = token.charAt(optionIndex);
+ if (option === '.') { hidden = true; continue; }
if (option === 'u') { unrestricted += 1; if (unrestricted >= 2) hidden = true; continue; }
const kind = readerShortOptionKind('rg', option);
if (!kind) continue;
@@ -5101,6 +5102,12 @@ function isGitRevisionDotenvOperand(value: string): boolean {
&& isDotenvCredentialPath(value.slice(revisionPathSeparator + 1));
}
+function isGitExcludePathspec(value: string): boolean {
+ const longPathspec = /^:\(([^)]*)\)(.*)$/.exec(value);
+ if (longPathspec) return /(?:^|,)(?:exclude|!)(?:,|$)/.test(longPathspec[1]);
+ return value.startsWith(':!') || value.startsWith(':^');
+}
+
function gitArgumentsReadDotenv(args: readonly string[]): boolean {
let pathspecOnly = false;
for (let index = 0; index < args.length; index += 1) {
@@ -5113,6 +5120,7 @@ function gitArgumentsReadDotenv(args: readonly string[]): boolean {
index += 1;
continue;
}
+ if (isGitExcludePathspec(token)) continue;
if (shellOperandCouldMatchDotenv(token, isGitDotenvOperand)) return true;
if (!pathspecOnly && isGitRevisionDotenvOperand(token)) return true;
}
@@ -5141,31 +5149,81 @@ function gitGrepHasExplicitPathspec(args: readonly string[]): boolean {
}
function gitGrepListsOnly(args: readonly string[]): boolean {
- for (const token of args) {
+ for (let tokenIndex = 0; tokenIndex < args.length; tokenIndex += 1) {
+ const token = args[tokenIndex];
if (token === '--') break;
- if (token === '--files-with-matches' || token === '--files-without-match' || token === '--name-only') return true;
+ if (token.startsWith('--')) {
+ const equalsIndex = token.indexOf('=');
+ const name = equalsIndex >= 0 ? token.slice(0, equalsIndex) : token;
+ const kind = resolveReaderLongOption('grep', name);
+ if (kind === 'data' || kind === 'data-file') {
+ if (equalsIndex < 0) tokenIndex += 1;
+ continue;
+ }
+ if (token === '--files-with-matches' || token === '--files-without-match' || token === '--name-only') return true;
+ continue;
+ }
if (!/^-[^-]/.test(token)) continue;
- for (let index = 1; index < token.length; index += 1) {
- const option = token.charAt(index);
- if (option === 'e' || option === 'f') break;
+ for (let optionIndex = 1; optionIndex < token.length; optionIndex += 1) {
+ const option = token.charAt(optionIndex);
+ if (option === 'e' || option === 'f') {
+ if (optionIndex === token.length - 1) tokenIndex += 1;
+ break;
+ }
if (option === 'l' || option === 'L') return true;
}
}
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 };
+}
+
+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 gitDiffPathspecsCouldReadDotenv(pathspecs: readonly string[]): boolean {
+ const positive = pathspecs.filter((value) => !parseGitPathspec(value).exclude);
+ return positive.length === 0 || positive.some((value) => !gitDiffPathspecIsSafeFile(value));
+}
+
function gitContentReadWithoutPath(sub: string, args: readonly string[]): boolean {
if (sub === 'grep') return !gitGrepListsOnly(args) && !gitGrepHasExplicitPathspec(args);
if (sub !== 'diff') return false;
- const separator = args.indexOf('--');
- if (separator >= 0 && separator < args.length - 1) return false;
- if (args.includes('--no-index') && args.filter((arg) => !arg.startsWith('-')).length >= 2) 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));
- return !metadataOnly || patchRequested;
+ if (metadataOnly && !patchRequested) return false;
+
+ const separator = args.indexOf('--');
+ if (separator >= 0 && separator < args.length - 1) {
+ return gitDiffPathspecsCouldReadDotenv(args.slice(separator + 1));
+ }
+ if (args.includes('--no-index')) {
+ const operands = args.filter((arg) => !arg.startsWith('-'));
+ if (operands.length >= 2) return operands.some((operand) => !gitDiffPathspecIsSafeFile(operand));
+ }
+ return true;
}
function shellCommandReadsDotenv(