Skip to content

security: harden file permissions, fix timing attack and API key exposure#52

Open
G2Bent wants to merge 1 commit into170-carry:mainfrom
G2Bent:security/harden-file-permissions-and-api-key
Open

security: harden file permissions, fix timing attack and API key exposure#52
G2Bent wants to merge 1 commit into170-carry:mainfrom
G2Bent:security/harden-file-permissions-and-api-key

Conversation

@G2Bent
Copy link
Copy Markdown

@G2Bent G2Bent commented Mar 25, 2026

Summary

  • 修复计时侧信道攻击proxy_service.rs 中的 API Key 比较改为使用 constant_time_eq,防止攻击者通过响应时间差推断 key 内容
  • 修复 TOCTOU 竞争条件:新增 private_create_new_options() 工具函数,在 Unix 上以 mode(0o600) 原子创建临时文件,避免文件创建后 chmod 前短暂对其他用户可读的窗口(涉及 proxy_service.rsauth.rsstore.rsremote_service.rs 中的 SSH 私钥临时文件)
  • 防止 API Key 泄露至日志proxy_daemon.rs 中输出 status 时仅打印 key 的前 8 位预览,不输出完整 key
  • Windows 权限加固utils.rsset_private_permissions() 在 Windows 上调用 icacls 移除 Everyone 权限,仅保留当前用户读写权限(原先为空实现)
  • 新增 constant_time_eq = "0.3" 依赖

变更文件

文件 改动
src-tauri/Cargo.toml 新增 constant_time_eq 依赖
src-tauri/src/utils.rs 新增 private_create_new_options(),完善 Windows set_private_permissions()
src-tauri/src/proxy_service.rs 常量时间 API Key 比较,原子私有临时文件
src-tauri/src/proxy_daemon.rs 遮蔽完整 API Key 输出
src-tauri/src/auth.rs 原子私有临时文件
src-tauri/src/store.rs 原子私有临时文件,新增 write_private_file() 辅助函数
src-tauri/src/remote_service.rs SSH 私钥临时文件原子创建(消除 TOCTOU)

Test plan

  • macOS/Linux:确认敏感文件(auth token、API key、store、SSH key)创建后权限为 0o600
  • Windows:确认 icacls 调用成功限制文件权限
  • 确认代理服务功能正常(认证、转发)
  • 确认日志中不再出现完整 API Key

🤖 Generated with Claude Code

…sure

- Add `constant_time_eq` crate and use constant-time comparison in
  `is_authorized()` to prevent timing side-channel attacks on the
  proxy API key
- Redact API key from proxyd stdout (only print first 8 chars) to
  prevent the full key from being captured in systemd journal / logs
- Add `private_create_new_options()` helper in utils.rs that opens
  files with O_CREAT | mode 0o600 on Unix atomically, eliminating the
  TOCTOU window between file creation and `chmod`
- Use `private_create_new_options()` when writing auth.json, accounts
  store, api-proxy.key, and SSH private key temp files
- Add `write_private_file()` helper in store.rs for shadow/corrupt
  backup writes that also sets 0o600 from the initial open
- Improve `set_private_permissions()` on Windows to call `icacls` and
  restrict access to the current user (previously a no-op)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant