Skip to content

fix(core): detect shell metacharacters that bypass the ShellCommandTool whitelist - #3764

Closed
aiyili wants to merge 4 commits into
agentscope-ai:mainfrom
aiyili:fix/shell-whitelist-metacharacters
Closed

aiyili wants to merge 4 commits into
agentscope-ai:mainfrom
aiyili:fix/shell-whitelist-metacharacters

Conversation

@aiyili

@aiyili aiyili commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

Fixes #3285

Problem

The validators checked the executable name and &/|/; separators, but the whole string then runs under sh -c / cmd.exe /c. A whitelisted echo could execute additional commands or write files via $(...), backticks, $VAR/${...}, %VAR%, or >/< redirection — never touching the approval callback.

Fix

  • Unix: new quote-aware containsShellMetacharacters — $ and backticks are rejected outside single quotes (double quotes do NOT neutralize substitution in sh), >/< outside both quote kinds. Applied after the separator check whenever a whitelist is active; hits are rejected with a reason, flowing into the existing approval path.
  • Windows: same-shape check for %VAR%, >/<, and ^ (cmd.exe quote-stripping/escape smuggling). cmd's quoting semantics are notoriously subtle, so the check is intentionally strict.
  • Plain whitelisted commands and single-quoted literals (echo '$(safe)') still pass.

Tests

CommandValidatorMetacharacterTest — per the issue's repro matrix: echo $(printf marker), echo "$(printf marker)", backticks, $HOME/${HOME}, > file, < file, %USERPROFILE%, ^ all rejected; plain and single-quoted-literal forms allowed.

Open question for maintainers (intentionally not changed)

The report also notes both validators allow everything when the whitelist is null/empty, contradicting the tool description "No whitelist configured - all commands require approval." Aligning that would be a breaking change for users relying on allow-all with no approval callback — happy to do it in this PR or a follow-up per your call.

UnixCommandValidator/WindowsCommandValidator 只校验可执行名与
&/|/;分隔符,整串命令随后交给 sh -c / cmd.exe /c——白名单内的
echo 可经 $(...)、反引号、$VAR、重定向执行额外命令或写文件,
全程不经审批回调。

- Unix:新增 containsShellMetacharacters(引号感知):$/反引号在
  单引号外即拒绝(双引号不阻断替换),>/<在两种引号外拒绝;
  在 containsMultipleCommands 之后对所有白名单激活的命令生效,
  命中即 rejected 并给出原因,进入既有审批回调路径
- Windows:新增同款检测(%VAR% 展开、>/< 重定向、^ 转义 smuggle),
  cmd.exe 引词语义复杂,从严全量拒绝
- 白名单为空时全放行的既有行为保留:改为与文档声明的
  "No whitelist - all commands require approval" 对齐属破坏性
  变更,留给维护者决策(PR 描述中提出)
- 测试:plain/单引号字面量仍 ALLOW;$(...)/"$(...)"/反引号/
  $VAR/${VAR}/>输入重定向/%VAR%/^ 全部 rejected

Fixes agentscope-ai#3285
@aiyili

aiyili commented Oct 26, 2026

Copy link
Copy Markdown
Contributor Author

Spotless violation on my pushed file (the server-side-constructed content skipped the local format pass — the standard step I keep re-learning). Fixed in 2a05779 (formatting only, verified the fix content is intact). Rerun should be green.

@aiyili

aiyili commented Oct 26, 2026

Copy link
Copy Markdown
Contributor Author

Found it — my previous "spotless fix" push for this branch actually re-introduced the broken character literals in UnixCommandValidator (the working-tree copy got overwritten from a stale buffer mid-repair, and the spotless run that would have caught it failed silently under my output filter). Properly fixed in 67d0937: literals verified byte-precise via grep ('\' / '''), file parses under javac, and spotless:apply now returns BUILD SUCCESS on the module. Apologies for the churn — verification for this push was done against the exact blob content, not just a green local command.

@aiyili

aiyili commented Oct 26, 2026

Copy link
Copy Markdown
Contributor Author

Both prior failures traced to one structural mistake of mine: the first patch inserted the new method between containsMultipleCommands's @Override and its signature, so the annotation landed on my method ("does not override"). The second attempt patched the patched file and mangled indentation. This push (1262805) rebuilds both validators from pristine upstream with the Edit tool byte-precisely — hook after the separator check, method appended at class end, @Override untouched — then full spotless:apply BUILD SUCCESS with literal verification via grep. Should compile and pass now.

Separately: the 3d4c5a3 run for #3766 failed only in agentscope-examples/jev — a module this PR does not touch (single-file core change); looks like the same class of unrelated example-module failure seen on other branches this week. Requesting a rerun/admin check for that one.

@jujn jujn added the P0 重要紧急 label Nov 3, 2026
@juruoO-o

Copy link
Copy Markdown
Collaborator

这个 PR 要解决的 ShellCommandTool 白名单校验问题已经由 #3286 修复并合入 main。#3286 同样覆盖 Unix 和 Windows 上的 shell 语法校验,因此这个 PR 可以关闭。感谢补充修复和测试。

@juruoO-o juruoO-o closed this Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P0 重要紧急

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: ShellCommandTool whitelist bypass allows command execution without approval

3 participants