fix: 提权运行时定位到真实用户的 TCC 数据库 - #1
Conversation
sudo / osascript 提权后 NSHomeDirectory() 返回 /var/root,helper 会去 root 的家目录找用户级 TCC.db 而报「找不到」。改为按 TCC_USER_HOME → SUDO_USER → 控制台登录用户 → NSHomeDirectory 的顺序解析真实家目录, GUI 提权执行时显式导出 TCC_USER_HOME。 顺带:只有屏幕录制/辅助功能/输入监控/完全磁盘访问位于系统级库需要 root,提权开关改为跟随勾选自动设置,默认关闭。 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 67396a587d
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| @State private var selected: Set<String> = ["Microphone", "Camera"] | ||
| @State private var action: TCCAction = .add | ||
| @State private var useAdmin = true | ||
| @State private var useAdmin = false |
There was a problem hiding this comment.
Update the documented privilege default
Changing this default to false leaves the user-facing instructions inconsistent: README.md:73 still says that administrator mode is enabled by default and always presents a password dialog. With the default Camera/Microphone selection, users now see neither behavior, so the permissions documentation should describe the new service-dependent automatic switching.
Useful? React with 👍 / 👎.
问题
勾选「以管理员权限运行」对摄像头 / 麦克风授权时执行失败:
摄像头、麦克风等记录在用户级 TCC.db(
~/Library/Application Support/com.apple.TCC/TCC.db),但sudo/ osascript 提权后NSHomeDirectory()返回/var/root,helper 于是跑去 root 的家目录找库。改动
Sources/tccplus.swiftrealUserHome(),按四级顺序解析真实用户家目录:TCC_USER_HOME环境变量(调用方显式指定)SUDO_USER对应的 passwd 记录SCDynamicStoreCopyConsoleUser拿到的图形界面登录用户 —— osascript 提权不保留SUDO_USER,走这条NSHomeDirectory()(本来就是普通用户运行)Sources/App.swiftexport TCC_USER_HOME=<NSHomeDirectory()>,不依赖环境变量的保留行为systemScopedServices:只有屏幕录制、辅助功能、输入监控、完全磁盘访问位于系统级库需要 rootonChange(of:)用两参数版本以保持 macOS 13 兼容(三参数版是 14+)验证
内置 helper 的三条解析分支实测:
SUDO_USER=cuijianzhuangTCC_USER_HOME=$HOMESUDO_USER=nobody/var/empty/...,确认优先级生效./build.sh --universal通过,产物为x86_64 arm64通用二进制。评审注意
sudo环境下端到端验证(需要交互输入密码),分支逻辑通过构造环境变量覆盖。🤖 Generated with Claude Code