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: address review — enforce dotenv shell boundaries

Chrisd51b9f73f61c
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 c29c09948b..f712531f8e 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
@@ -381,15 +381,32 @@ describe('对抗语料 — 变体矩阵', () => {
/** Dotenv files are credential-bearing paths, including when shell tools read them. */
describe('对抗语料 — dotenv 凭证路径', () => {
- it('绝对或显式相对 dotenv 路径不得直接放行', () => {
+ it('命令分段中的 dotenv 文件操作数一律要求确认', () => {
for (const command of [
- 'cat /repo/.env',
- 'grep KEY /repo/.env.local',
+ 'cat .env',
+ 'cat /repo/.env && true',
+ "cat /repo/.env | sed -n '1p'",
+ 'cat /repo/.env ',
+ 'cat /repo/.env # inspect configuration',
+ 'cat "/repo/.env.local"; true',
'cat ./.env.production.local',
+ 'grep KEY .env.local && true',
+ 'sed -n 1p .env.test',
]) {
expect(classifyShellCommand(command, roots, opts)).toBe('prompt-each-time');
}
- expect(classifyShellCommand('jq .env data.json', roots, opts)).toBe('auto-approve');
+ });
+
+ it('普通文件与数据表达式仍保持只读直通', () => {
+ for (const command of [
+ 'cat .environment',
+ 'cat config.env.local',
+ 'echo .env',
+ 'jq .env data.json',
+ 'grep .env data.json',
+ ]) {
+ expect(classifyShellCommand(command, roots, opts)).toBe('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 acc1e69a50..112cebda65 100644
--- a/packages/maker-core/src/agents/shared/auto-review.ts
+++ b/packages/maker-core/src/agents/shared/auto-review.ts
@@ -38,6 +38,7 @@
*/
import {
+ isDotenvCredentialPath,
isSensitiveCredentialPath,
SENSITIVE_CREDENTIAL_PATH_PATTERNS,
} from './sensitive-credential-paths.js';
@@ -161,6 +162,19 @@ const SAFE_READONLY_BINS: ReadonlySet<string> = new Set([
'jq', 'yq', 'base64', 'md5', 'md5sum', 'sha256sum', 'cksum',
]);
+/** Read-only commands whose positional operands may expose file contents. */
+const DOTENV_FILE_READER_BINS: ReadonlySet<string> = new Set([
+ 'cat', 'head', 'tail', 'wc', 'stat', 'file', 'realpath', 'readlink',
+ 'grep', 'egrep', 'fgrep', 'rg', 'ag', 'find', 'tree', 'du',
+ 'diff', 'cmp', 'sort', 'uniq', 'cut', 'tr', 'nl', 'tac',
+ 'jq', 'yq', 'base64', 'md5', 'md5sum', 'sha256sum', 'cksum', 'sed',
+]);
+
+/** These readers consume their first positional argument as an expression, not a file. */
+const FIRST_DATA_ARGUMENT_BINS: ReadonlySet<string> = new Set([
+ 'grep', 'egrep', 'fgrep', 'rg', 'ag', 'sed', 'jq', 'yq',
+]);
+
/** 命令包裹器:剥掉后信任绑定到内层真实命令。`sudo`/`doas` 不在此列(提权本身危险)。 */
const COMMAND_WRAPPERS: ReadonlySet<string> = new Set([
'env', 'nohup', 'nice', 'ionice', 'stdbuf', 'timeout', 'time', 'command', 'builtin',
@@ -4720,12 +4734,45 @@ export function commandExecutableNames(command: string): string[] {
return [...names];
}
+function shellCommandReadsDotenv(command: string): boolean {
+ for (const segment of splitTopLevelSegments(command)) {
+ const tokens = unwrapWrappers(stripShellControlTokens(tokenize(segment)));
+ const bin = executableName(tokens[0] ?? '');
+ if (!DOTENV_FILE_READER_BINS.has(bin)) continue;
+
+ let dataArgumentIndex = -1;
+ if (FIRST_DATA_ARGUMENT_BINS.has(bin)) {
+ for (let index = 1; index < tokens.length; index += 1) {
+ const token = tokens[index];
+ if (token === '--') {
+ dataArgumentIndex = index + 1;
+ break;
+ }
+ if (!token.startsWith('-')) {
+ dataArgumentIndex = index;
+ break;
+ }
+ }
+ }
+
+ for (let index = 1; index < tokens.length; index += 1) {
+ if (index === dataArgumentIndex) continue;
+ if (isDotenvCredentialPath(tokens[index])) return true;
+ }
+ }
+ return false;
+}
+
export function classifyShellCommand(
command: string,
workspaceRoots: string[],
opts: ShellReviewOptions = {},
): ReviewVerdict {
if (typeof command !== 'string' || command.trim().length === 0) return 'prompt';
+ // The shared path matcher deliberately accepts only complete path values. Shell
+ // commands need argument-aware scanning so a trailing pipe/comment cannot hide a
+ // dotenv operand, while jq/grep expressions such as jq .env data.json stay data.
+ if (shellCommandReadsDotenv(command)) return 'prompt-each-time';
// 两档风险模式都跑以下变体;明确红线优先,命中才 prompt-each-time:
// - deEscaped(去引号 + 去反斜杠转义):防 su'do' / su\do / rm -r'f' 这类把关键词拆开的绕过。
// - quotesOnly(只去引号、保留 `\`):Windows `\` 路径的凭证检测 —— `cat C:\Users\me\.ssh\id_rsa`
diff --git a/packages/maker-core/src/agents/shared/sensitive-credential-paths.ts b/packages/maker-core/src/agents/shared/sensitive-credential-paths.ts
index a9991c8088..7d7b1c0208 100644
--- a/packages/maker-core/src/agents/shared/sensitive-credential-paths.ts
+++ b/packages/maker-core/src/agents/shared/sensitive-credential-paths.ts
@@ -5,6 +5,11 @@
* bridge runs in a standalone child process and embeds these specs into its
* generated extension instead of maintaining a second handwritten regex list.
*/
+export const DOTENV_CREDENTIAL_PATH_PATTERN_SPEC = {
+ source: String.raw`(?:^|[\\/])\.env(?:\.[^\\/]+)?$`,
+ flags: "i",
+} as const;
+
export const SENSITIVE_CREDENTIAL_PATH_PATTERN_SPECS = [
{
source: String.raw`(?:^|[\\/\s'"~])\.(?:ssh|aws|gnupg|kube|docker|azure|claude|codex)\b`,
@@ -14,10 +19,7 @@ export const SENSITIVE_CREDENTIAL_PATH_PATTERN_SPECS = [
source: String.raw`(?:^|[\\/\s'"~])\.(?:netrc|npmrc|pgpass|pypirc|git-credentials)\b`,
flags: "i",
},
- {
- source: String.raw`(?:^|[\\/])\.env(?:\.[^\\/]+)?$`,
- flags: "i",
- },
+ DOTENV_CREDENTIAL_PATH_PATTERN_SPEC,
{ source: String.raw`[\\/]\.cargo[\\/]credentials(?:\.toml)?\b`, flags: "i" },
{
source: String.raw`[\\/]\.m2[\\/]settings(?:-security)?\.xml\b`,
@@ -135,6 +137,17 @@ export const REVIEW_SENSITIVE_CREDENTIAL_GLOB_PATTERNS = [
"**/.xdt-server/**",
] as const;
+const DOTENV_CREDENTIAL_PATH_PATTERN = new RegExp(
+ DOTENV_CREDENTIAL_PATH_PATTERN_SPEC.source,
+ DOTENV_CREDENTIAL_PATH_PATTERN_SPEC.flags,
+);
+
+export function isDotenvCredentialPath(target: string): boolean {
+ return (
+ typeof target === "string" && DOTENV_CREDENTIAL_PATH_PATTERN.test(target)
+ );
+}
+
export const SENSITIVE_CREDENTIAL_PATH_PATTERNS: readonly RegExp[] =
SENSITIVE_CREDENTIAL_PATH_PATTERN_SPECS.map(
({ source, flags }) => new RegExp(source, flags),