Skip to content

safeWriteFile: O_NOFOLLOW 不保护 symbiotic parent directories #64

Description

@luojiyin1987

问题

safeWriteFile 使用 open() + O_NOFOLLOW 防止最终路径组件是符号链接时写入。但 Linux O_NOFOLLOW 只拒绝 path 最后一个组件是 symlink 的情况,对父目录中的符号链接仍会跟随

例如:

  • /tmp/link/a.mdlink 是指向 /etc 的目录符号链接
  • O_NOFOLLOW 会跟随 link 进入 /etc,只检查 a.md 是否是符号链接

loadMdFiles 也存在相同行为:isSymbolicLink() 只检查文件自身,不检查父目录中是否有符号链接。当前行为已通过测试文档化(symlink-security.spec.ts)。

影响

如果项目的安全目标是“--fix 模式不应该写入 symlink 目录指向的外部文件”,当前防护不足。

可能方案

  • 扫描阶段:拒绝任何路径组件中包含 symlink 的文件
  • 写入阶段:逐级用 openat(通过 open() 逐级解析)做路径保护,或使用 realpath + 白名单校验

Node.js 中均需额外实现,建议评估风险等级后决定优先级(P1/P2)。

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions