我扒了 Claude Code 的源码,发现 Anthropic 藏了一个"卧底模式"
系列:从泄露源码看 Claude Code 的内部世界(第一篇)
最近,Claude Code v2.1.88 的源码通过 npm 包的 sourcemap 意外泄露。
这不是黑客攻击,也不是内鬼泄密——只是 Anthropic 的工程师在打包时忘了关掉 sourcemap,导致完整的 TypeScript 源码随着公开发布的 npm 包一起流出。
我把这份源码下载下来仔细翻了一遍,发现了一个让我觉得很妙的设计:Undercover Mode(卧底模式)。
问题从哪里来
Anthropic 内部用 Claude Code 写代码。这很正常,很多 AI 公司都用自家产品。
但问题在于:Claude Code 提交代码时,会在 git commit message 里留下署名。比如:
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
这在内部仓库完全没问题。但 Anthropic 的工程师也会给开源项目提交代码——比如 anthropics/claude-code 这个公开仓库本身。
这就尴尬了。
如果 Claude 在给开源项目提交 PR 时,commit message 里写了:
Fix bug found while testing with Claude Capybara1-shotted by claude-opus-4-7
那 Capybara 和 opus-4-7 这两个还没发布的内部代号,就这么公开泄露了。
Anthropic 的解法:让 AI 伪装成人类
源码文件 src/utils/undercover.ts,第一行注释就开门见山:
Undercover mode — safety utilities for contributing topublic/open-source repos.When active, Claude Code adds safety instructions to commit/PR prompts andstrips all attribution to avoid leaking internal model codenames, projectnames, or other Anthropic-internal information. The model isnot told whatmodel it is.
翻译过来:当 Claude Code 检测到自己在公开仓库里工作,它会自动切换进”卧底模式”——不告诉模型自己是什么模型,抹掉所有 Anthropic 内部信息,伪装成一个普通的人类开发者。
触发逻辑:默认开启,无法强制关闭
激活逻辑写得很清楚:
exportfunctionisUndercover(): boolean {if (process.env.USER_TYPE === 'ant') {if (isEnvTruthy(process.env.CLAUDE_CODE_UNDERCOVER)) returntrue// Auto: active unless we've positively confirmed we're in an allowlisted// internal repo. 'external', 'none', and null (check not yet run) all// resolve to ON. The check is primed in setup.ts; only 'internal' → OFF.returngetRepoClassCached() !== 'internal' }returnfalse}
逻辑是这样的:
-
只对 Anthropic 内部员工( USER_TYPE === 'ant')生效,普通用户不涉及 -
可以用环境变量 CLAUDE_CODE_UNDERCOVER=1强制开启 - 但没有强制关闭的选项
-
默认状态:只要不能确认是内部私有仓库,就自动开启
注释里特别说明了设计意图:
There is NO force-OFF. This guards against model codename leaks — if we’re not confident we’re in an internal repo, we stay undercover.
没有关闭开关,这是刻意的。 宁可误判,也不能漏判。
内部私有仓库白名单
commitAttribution.ts 里有一份硬编码的仓库白名单,只有在这些仓库里,卧底模式才会关闭:
constINTERNAL_MODEL_REPOS = ['github.com/anthropics/claude-cli-internal','github.com/anthropics/anthropic','github.com/anthropics/apps','github.com/anthropics/casino','github.com/anthropics/dbt','github.com/anthropics/dotfiles','github.com/anthropics/terraform-config',// ... 共约 20 个]
注意注释里特别强调:
NOTE: This is intentionally a repo allowlist, not an org-wide check.The anthropics and anthropic-experimental orgs contain PUBLIC repos(e.g. anthropics/claude-code, anthropic-experimental/sandbox-runtime).Undercover mode must stay ON in those to prevent codename leaks.Only add repos here that are confirmed PRIVATE.
就算是 anthropics 这个组织下的仓库,也不能自动信任——因为里面有公开仓库。必须是明确确认为私有的仓库,才能加进白名单。
顺带一提,这份白名单本身也是一次意外泄露:我们现在知道了 Anthropic 内部有哪些私有仓库,包括 casino(疑似 A/B 测试平台)、trellis、forge-web、mycro_manifests 等。
卧底模式下,AI 被告知什么
激活后,系统会向模型注入一段特殊的指令:
## UNDERCOVER MODE — CRITICALYou are operating UNDERCOVER in a PUBLIC/OPEN-SOURCE repository. Your commitmessages, PR titles, and PR bodies MUST NOT contain ANY Anthropic-internalinformation. Do not blow your cover.NEVER include in commit messages or PR descriptions:- Internal model codenames (animal names like Capybara, Tengu, etc.)- Unreleased model version numbers (e.g., opus-4-7, sonnet-4-8)- Internal repo or project names (e.g., claude-cli-internal, anthropics/…)- Internal tooling, Slack channels, or short links (e.g., go/cc, #claude-code-…)- The phrase "Claude Code"orany mention that you are an AI- Any hint of what model or version you are- Co-Authored-By lines orany other attribution
然后给了正反两个示例:
GOOD:- "Fix race condition in file watcher initialization"- "Add support for custom key bindings"- "Refactor parser for better error messages"BAD (never write these):- "Fix bug found while testing with Claude Capybara"- "1-shotted by claude-opus-4-6"- "Generated with Claude Code"- "Co-Authored-By: Claude Opus 4.6 <…>"
“1-shotted by claude-opus-4-6” 这条 BAD 示例,显然是真实发生过的事故。有人真的这么写了,然后被紧急撤回,然后这条规则被写进了代码。
还有一个”首次提示”弹窗
shouldShowUndercoverAutoNotice() 函数表明,当卧底模式被自动触发时,系统会弹出一个一次性提示,告诉内部员工:“你现在在公开仓库里,卧底模式已启动。”
这个提示只显示一次,之后记录在配置里(hasSeenUndercoverAutoNotice),不再重复打扰。
这个设计说明了什么
表面上,这只是一个防止信息泄露的工程保护。但往深了想,它揭示了几件事:
第一,Anthropic 员工真的在用 Claude Code 给开源项目贡献代码。 不然不需要这套机制。这意味着 Claude Code 的很多功能,是在真实开发场景下被打磨出来的。
第二,Anthropic 有大量还没发布的模型代号。 指令里明确提到”animal names like Capybara, Tengu”——这是两个不同的未发布模型代号。Capybara(水豚)在其他代码注释里出现过,是当前内部测试中的某个版本;Tengu(天狗,日本神话生物)则是另一个。
第三,这套机制的设计哲学是”默认不信任”。 没有关闭开关,宁可误判也不能漏判——这和 Anthropic 在安全上的一贯风格一致。
第四,这是一次真实的工程事故预防。 “BAD”示例里那条 1-shotted by claude-opus-4-6,不像是凭空想象出来的,更像是某个工程师真的提交了这样的 commit,然后被紧急撤回,然后这条规则被写进了代码。
最后
有意思的是,这篇文章里引用的所有代码,本身就是另一次信息泄露的产物——sourcemap 忘了关。
Anthropic 为了防止 AI 在 commit message 里泄露内部信息,专门写了一套卧底模式;但他们自己,把整份源码打包进了公开的 npm 包。
防住了 AI,没防住自己。
本文基于Claude Code v2.1.88反编译源码分析,所有源码版权归Anthropic PBC所有,仅供技术研究和学习参考。
下一篇:从源码里挖出 9 个藏在 Claude Code 里、还没上线的隐藏功能。
夜雨聆风