5 min read

59.8MB 的疏忽:我从 npm 包里还原了 Claude Code 的完整源码

Source: http://x.com/i/article/2038919993912700928

今天浏览 npm 上的 @anthropic -ai/claude-code 包时,注意到一个文件:

Image

cli.js — 13MB,正常,打包后的产物。 cli.js.map — 59.8MB。

一个 source map 比产物本身大 4.5 倍?

打开看了一眼 JSON 结构,sourcesContent 字段里存着 4756 个文件的完整源码 — 不是 minified 的 JS,是带类型注解、带注释的原始 TypeScript。

Anthropic 在打包时没有剥除 source map 的 sourcesContent —— 也就是说,每一个 npm install @anthropic -ai/claude-code 的人,硬盘上都已经躺着一份完整源码了。

提取

提取过程没什么技术含量:

  1. npm install@anthropic-ai/[email protected]
  2. JSON.parse 读取 cli.js.map 3. 遍历 sources 和 sourcesContent 数组,按原始路径写入文件

不需要反编译器,不需要逆向工具。Source map 本来就是给浏览器 DevTools 用的调试文件,sourcesContent 里存的就是构建前的原始代码。

结果:1906 个 Claude Code 自身源文件,33MB 的原始 TypeScript/TSX,外加 2850 个 node_modules 依赖。

Image

第一眼:node_modules 里不只有公开库

2850 个 node_modules 文件,第一反应是跳过 — lodash、zod、axios 这些公开库在 npm 上随便看。

但仔细过了一遍包名列表,发现混在里面的有几个不对劲的: 包大小说明 @ant/computer-use-mcp232KBComp uter Use 完整实现

@ant/claude-for-chrome-mcp99KB

Chrome 扩展 MCP 桥接 @anthropic -ai/sandbox-runtime158KB沙箱系统(seccomp + macOS sandbox) @anthropic -ai/mcpb73KBMCP Builder CLI@ant/computer-use-input1.3KBmacOS 原生输入(enigo 绑定)

@ant/computer-use-swift1.1KBmacOS 截屏(Swift addon) @ant /* 这个 scope 在 npm 上搜不到,是 Anthropic 的内部私有包。只在内部 registry 发布,从未公开 — 直到它们被 source map 的 sourcesContent 一起打包进了 npm。

Image

混淆程度:零

这是最让人意外的部分。

我原以为内部包至少会做基本的混淆或者只保留编译后的 JS,但实际检查下来:

@ant

/* 包 — 原始 TypeScript 源码

保留了完整的 .ts 文件,包括:

  • JSDoc 注释和内部设计文档级别的说明
  • 引用了 "Vercept acquisition"(收购来的代码)和安全计划 "plan §2"
  • 完整的类型定义、接口、泛型
  • 语义化的函数名和变量名

比如

@ant/computer-use-mcp

的 toolCalls.ts 开头就写着:

Tool dispatch. Every security decision from plan §2 is enforced HERE, before any executor method is called.Enforcement order, every call: Kill switch (adapter.isDisabled()) TCC gate (adapter.ensureOsPermissions()) Tool-specific gates — ANY exception in a gate returns a tool error, executor never called Executor call

这不是反编译出来的伪代码,这是工程师写的设计文档直接留在了注释里。

Image

@anthropic

-ai/* 包 — 编译后 JS 但完全可读

sandbox-runtime、mcpb 等包是编译后的 .js,但变量名、函数名全部保留,没有做任何混淆。逻辑一目了然。

主体 src/ — 原始 TypeScript/TSX 1906 个文件,完全是构建前的源码状态。核心文件包括:

  • main.tsx — 4683 行,整个 REPL 循环和消息处理
  • Tool.ts — 工具基类定义
  • QueryEngine.ts — 与 Claude API 通信的核心引擎
  • 40+ 个工具实现(Bash、FileEdit、Grep、Agent、MCP...)
  • 100 个斜杠命令
  • 权限系统、沙箱管理、MCP 协议
  • GrowthBook feature flag 集成
  • Vim 模式、语音输入、IDE 桥接(VS Code / JetBrains)

架构一览

Image

从还原的源码可以看到 Claude Code 的完整架构:

src/ ├── main.tsx — 主入口(4683行) ├── QueryEngine.ts — API 通信核心 ├── Tool.ts — 工具基类 ├── tools/ — 40+ 个内置工具 │ ├── AgentTool/ — 子 Agent(含内置 agent 定义) │ ├── BashTool/ — Shell 执行 │ ├── FileEditTool/ — 文件编辑 │ ├── MCPTool/ — MCP 协议代理 │ └── ... ├── commands/ — 100 个斜杠命令 ├── services/ — 核心服务 │ ├── api/ — API 通信 │ ├── mcp/ — MCP 管理 │ ├── oauth/ — 认证 │ ├── compact/ — 上下文压缩 │ └── analytics/ — 遥测 + GrowthBook ├── components/ — 146 个 Ink React UI 组件 ├── utils/ — 330+ 工具函数 │ ├── swarm/ — 多 Agent 协调 │ ├── sandbox/ — 沙箱管理 │ └── permissions/ — 权限检查 ├── tasks/ — 后台任务 │ ├── DreamTask/ — "Dream" 后台推理 │ ├── LocalAgentTask/ — 本地子 Agent │ └── RemoteAgentTask/ — 远程 Agent ├── bridge/ — IDE 桥接(31 个文件) ├── skills/ — 内置 Skills 系统 ├── vim/ — Vim 模式 └── voice/ — 语音输入

内部依赖:

deps/ ├──

@ant/computer-use-mcp

— Computer Use 完整实现 ├──

@ant/claude-for-chrome-mcp

— Chrome MCP 桥接 ├──

@ant/computer-use-input

— macOS 原生输入 ├──

@ant/computer-use-swift

— macOS Swift 截屏 ├── @anthropic -ai/sandbox-runtime — 沙箱运行时 ├── @anthropic -ai/mcpb — MCP Builder CLI ├── @anthropic -ai/bedrock-sdk — AWS Bedrock 适配 ├── @anthropic -ai/foundry-sdk — Foundry SDK └── @anthropic -ai/vertex-sdk — Vertex AI 适配

这意味着什么

这不是漏洞利用,是发布流程的疏忽。

Source map 带 sourcesContent 发到 npm 是个常见事故 — webpack、esbuild 默认生成的 source map 都会包含源码内容,很多项目都犯过这个错。

但对于一个闭源商业产品来说,这基本等于不小心把 private repo 开放了:

  • 完整的产品源码和架构
  • 内部私有包(@ant/*)的实现细节
  • 安全架构设计(沙箱、权限模型、Computer Use 安全门控)
  • 收购整合的代码(Vercept)
  • Feature flag 策略和 A/B 测试配置
  • IDE 桥接协议

Anthropic 大概率会在下个版本修复 — 要么删掉 sourcesContent,要么不再附带 .map 文件。

版本号 2.1.88,发布时间距离我检查时不到 10 小时。

想看的趁早。