sdk: "./sdk" export throws at import time — native module resolved outside its own security boundary
还没有人认领这个 Issue。
- 主要语言
- Shell
- 星标
- 11.2k
- 派生
- 1.9k
- 平均合并
- 14 小时 16 分钟
- 30 天内合并 PR
- 6
描述
Package: @github/copilot
Version affected: 1.0.11
Platform: darwin-arm64
Summary
package.json's "exports" map points the documented import path @github/copilot/sdk at sdk/index.js. That bundle's own require() wrapper enforces a same-directory security check — it resolves the requested module's real path, computes it relative to the bundle's own directory (sdk/), and throws if the relative path starts with .. (i.e. the target lives outside sdk/).
The native pty.node addon this bundle needs at import time lives in the package root's prebuilds/<platform>/ directory — one level outside sdk/. So the check the bundle enforces on itself is violated by the bundle's own required dependency, and import "@github/copilot/sdk" (or any import of sdk/index.js, including by absolute path) throws before a CopilotClient can even be constructed.
Reproduction
// Fails on darwin-arm64, @github/copilot@1.0.11:
import("/opt/homebrew/lib/node_modules/@github/copilot/sdk/index.js")
.then(m => console.log("OK", Object.keys(m)))
.catch(e => console.error("ERR", e.message));
The surfaced error is misleading: it reads as Cannot find module './prebuilds/<platform>/pty.node', which looks like an ordinary missing-file error. It is not — it's the last of six internal resolution attempts inside a try/catch loop, and it overwrites the real, earlier error: Requiring module outside of application is a security concern, thrown by the custom require() wrapper on the 5th attempt. Confirmed by instrumenting Module._resolveFilename directly.
Impact
Anyone following the package's own documented entry point (@github/copilot/sdk) for the CLI's JSON-RPC SDK hits this immediately — the import throws, not a runtime session error.
Workaround found
Importing the older copilot-sdk/index.js bundle directly by absolute path instead of the documented @github/copilot/sdk specifier works — it exposes the identical public API (CopilotClient, CopilotSession, defineTool, approveAll, SYSTEM_PROMPT_SECTIONS), uses a plain createRequire(import.meta.url) with no directory-boundary check, and is unaffected.
Suggested fix directions
- Either place
sdk/'s security check's allowed root one level higher (package root, notsdk/), or shipprebuilds/(or a copy/symlink of the needed native module) insidesdk/so the resolved path never crosses the boundary the check enforces. - Fix the swallowed-error behavior regardless: the security-check error should not be silently overwritten by a later, unrelated "module not found" from a subsequent resolution attempt in the same try/catch loop — it hides the real cause from anyone hitting this for the first time.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 package.json 的 exports map 和 sdk/index.js 开始,然后使用所示命令重现 darwin-arm64 的导入。跟踪解析到 prebuilds//pty.node 的尝试以及边界检查,包括被吞掉的错误。完成标准是 @github/copilot/sdk 能够成功导入,并且失败时保留原始的安全检查原因。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- javascript, node.js
- 领域
- cli, security
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 活跃
- 描述清晰度
- 基本清楚
- 新手友好度
- 55/100