Skip to content

fix(security): harden DB/SQL permissions, backup filenames, CONNECT proxy, and deeplink risk rules - #9835

Merged
allenxu09 merged 1 commit into
farion1231:mainfrom
YUZHEthefool:ao/ccswitch-audit/fix-security-hardening-misc
Nov 25, 2026
Merged

allenxu09 merged 1 commit into
farion1231:mainfrom
YUZHEthefool:ao/ccswitch-audit/fix-security-hardening-misc

Conversation

@YUZHEthefool

@YUZHEthefool YUZHEthefool commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Human Note / 本人说明

rebase

Summary / 概述

Addresses four defense-in-depth security hardening and edge-case bugs across storage permissions, backup filename validation, HTTP CONNECT proxying, and deep-link risk classification:

  1. Private 0o600 file permissions on Unix (src-tauri/src/database/backup.rs, src-tauri/src/database/mod.rs):
    • Switch export_sql from atomic_write to atomic_write_private (0o600) because SQL dumps contain provider API keys and tokens.
    • Restrict cc-switch.db to 0o600 on Unix inside Database::init.
  2. Strict single-component backup filename validation (src-tauri/src/database/backup.rs):
    • Centralize validate_backup_filename across restore_from_backup, rename_backup, and delete_backup, requiring exactly one Component::Normal .db filename and rejecting \0, /, \, .., bare .db, and (on Windows via cfg!(windows)) : for drive-relative paths (C:foo.db) and NTFS alternate data streams.
    • Continue rejecting : cross-platform for newly created rename target names in rename_backup, while preserving Unix compatibility for listing, restoring, renaming, and deleting existing legacy colon-named backups (e.g., 上线前-14:30.db).
  3. HTTP CONNECT proxy credentials & bounded header reads (src-tauri/src/proxy/hyper_client.rs):
    • RFC 3986 percent-decode parsed.username() and parsed.password() before Base64-encoding Proxy-Authorization: Basic ..., fixing 407 auth failures with encoded characters (e.g., %40, %3A, %23).
    • Reject \r/\n in target_host, require proper HTTP/1.x 200 status parsing, detect premature EOF before the terminating blank line, and cap CONNECT response headers at 64 lines and 16 KiB total via read_connect_line_bounded.
  4. Expanded deep-link risk detection (src/utils/deeplinkRisk.ts):
    • Add ALL_PROXY, SSL_CERT_FILE, SSL_CERT_DIR, REQUESTS_CA_BUNDLE, CURL_CA_BUNDLE, GIT_SSH_COMMAND, GIT_EXEC_PATH, ZDOTDIR, PROMPT_COMMAND, COMSPEC, and PATHEXT to ENV_HIJACK_PATTERNS.
    • Extend classifyCommand to detect inline code execution via scripting runtimes (node/bun -e/-p/--eval/--print, deno eval, python/python3/py -c, perl -e/-E, ruby -e, php -r), stopping option scanning at the first positional script name, -, --, or Python -m, so script-owned flags (e.g. python3 server.py -c config.toml) and value-bearing interpreter flags (e.g. python3 -Wonce, perl -Mfeature) are not flagged.

Related Issue / 关联 Issue

None

Validation / 验证

  • pnpm typecheck, pnpm format:check, pnpm knip, and pnpm build:renderer — passed.
  • pnpm test:unit --maxWorkers=4 --minWorkers=1 — 340 files and 4,476 tests passed.
  • cargo fmt --manifest-path src-tauri/Cargo.toml --all -- --check — passed.
  • cargo clippy --offline --locked --manifest-path src-tauri/Cargo.toml --all-targets -- -D warnings — passed.
  • Full backend suite with nextest — 5,361 tests passed across the initial run and a failed-test rerun; 12 skipped. The initial 29 failures were resolved by creating the temporary test home and setting NO_PROXY / no_proxy to localhost,127.0.0.1,::1 so local test servers bypass the system proxy. All 29 passed on rerun, with no code changes.
  • Rebased onto main at 3473487169a64a7eff222a1c08111be14fb7e909. Preserved both the upstream connection-pool tests and the original CONNECT tests; all added and removed lines match the original PR.

Checklist / 检查清单

  • Reproduced every sub-issue via failing tests before implementing fixes
  • Verified cargo clippy --all-targets -- -D warnings, cargo fmt --check, Rust tests, Vitest, typecheck, and Prettier pass
  • Rebased onto the validated main baseline and confirmed no conflicts with current main (bf4cc69fe).

@github-actions github-actions Bot added frontend Frontend (React/TypeScript) backend Backend (Rust/Tauri) proxy Related to proxy/API forwarding labels Nov 21, 2026
@YUZHEthefool
YUZHEthefool force-pushed the ao/ccswitch-audit/fix-security-hardening-misc branch from 2e17eee to 58de7ef Compare November 22, 2026 03:32

@farion1231 farion1231 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感谢这个 PR!里面有几项是实打实的修复:

  • CONNECT 代理凭据先做百分号解码再 Base64(hyper_client.rs#L600-L605)修了一个真 bug:之前密码含 @ : # 时会 407,现在和 reqwest 走代理时的行为一致了;
  • 导出 SQL 改用私有写入;
  • 环境变量黑名单新增的几项也都合理。

有一个需要先修的问题,另外建议去掉两项:

[P2] Unix 上带冒号的旧备份无法还原、改名或删除(backup.rs#L1004-L1021)
旧版改名时只拒绝 .. / \ 和 NUL,不拒绝 :,macOS/Linux 用户可能已经把备份改名成 上线前-14:30.db 这种带时间的名字。新的 validate_backup_filename 在所有平台都拒绝 :,而还原、改名、删除都会先过这道校验(#L1035、#L1105、#L1168):这类备份在列表里仍然显示,但点任何操作都报 Invalid backup filename,用户只能自己进目录处理。

冒号只在 Windows 上有危险(盘符相对路径和 NTFS 备用数据流),在 Unix 上就是普通字符。最小修法是让这一条只在 Windows 生效:

|| (cfg!(windows) && filename.contains(':'))

新建名字那里(#L1132)继续全平台拒绝冒号也没问题,只要已有的备份还能还原、改名和删除。最好再补一个测试:放一个带冒号的旧备份,覆盖列表和这三个操作。

建议去掉:osascript 的换行转义(terminal/mod.rs#L441)
AppleScript 字符串字面量里本来就可以有原样换行,引号和反斜杠转义对了就跳不出字面量。把换行改成 \n 后,AppleScript 求值又会还原成真换行再交给终端,语义完全一样。所以这一项堵不住任何注入,反而让人以为这里已经加固过了。

建议收窄或去掉:内联执行识别(deeplinkRisk.ts#L225-L288)
现在会扫描整个参数数组,脚本自己的参数也会被当成解释器的开关。比如 python3 server.py -c config.toml 里的 -c 是 server.py 的参数,确认框却会把它标成内联执行。如果要保留,扫描应该在遇到脚本名或 -- 时停下,并补一个"脚本带自己参数"的反例测试。

修掉 P2,并去掉或收窄上面两项,就可以合并。改完 ping 我,我会尽快复审。

@YUZHEthefool
YUZHEthefool force-pushed the ao/ccswitch-audit/fix-security-hardening-misc branch from 58de7ef to 905d083 Compare November 23, 2026 09:48
@YUZHEthefool YUZHEthefool changed the title fix(security): harden DB/SQL permissions, backup filenames, osascript escaping, CONNECT proxy, and deeplink risk rules fix(security): harden DB/SQL permissions, backup filenames, CONNECT proxy, and deeplink risk rules Nov 23, 2026
@YUZHEthefool

Copy link
Copy Markdown
Collaborator Author

@farion1231 感谢细致复审!已基于最新 main rebase 并按你的三点建议全部修改完毕(提交 905d0832):

  1. [P2] Unix 已有含冒号备份兼容性(src-tauri/src/database/backup.rs、src-tauri/src/database/backup/tests.rs):
    • 将 validate_backup_filename 中对 : 的拒绝收敛为 cfg!(windows) && filename.contains(':');
    • rename_backup 对新建目标名继续全平台拒绝 :;
    • 新增 Unix 集成测试 unix_colon_named_backup_supports_list_restore_rename_and_delete,覆盖带冒号旧备份(如 上线前-14:30.db)的 list_backups、restore_from_backup、rename_backup(含拒绝新目标名带冒号)与 delete_backup。
  2. 移除 osascript 换行转义(src-tauri/src/session_manager/terminal/mod.rs):
    • 已彻底还原该文件至 main,从本 PR 中移除 escape_osascript 的换行转义改动及对应测试。
  3. 收窄脚本解释器内联执行识别(src/utils/deeplinkRisk.ts、src/utils/deeplinkRisk.test.ts):
    • 通过 interpreterFlagsBeforeScript 仅扫描脚本名 / - / -- / Python -m 之前的解释器自身选项,并避开紧贴值的短参数(如 python3 -Wonce、perl -Mfeature、ruby -rset、php -dmemory_limit=256M);
    • 补充了正例(前置解释器参数 + 内联执行)以及“脚本带自身参数”的反例测试(包括 python3 server.py -c config.toml、python3 -m mcp_server -c config.toml、node dist/index.js -e production --eval=false -p 3000、deno run server.ts eval 等)。

本地验证

  • cargo fmt --manifest-path src-tauri/Cargo.toml --all -- --check — 通过
  • cargo clippy --manifest-path src-tauri/Cargo.toml --all-targets -- -D warnings — 通过
  • cargo test --manifest-path src-tauri/Cargo.toml --lib "database::permission_tests" — 1/1 通过
  • cargo test --manifest-path src-tauri/Cargo.toml --lib "database::backup::tests" — 51/51 通过
  • cargo test --manifest-path src-tauri/Cargo.toml --lib "proxy::hyper_client::tests" — 8/8 通过
  • vitest run src/utils/deeplinkRisk.test.ts tests/components/DeepLinkImportDialog.test.tsx — 59/59 通过(21 + 38)
  • bun run typecheck & bun run format:check — 通过

麻烦有空再看一眼,谢谢!

@YUZHEthefool
YUZHEthefool force-pushed the ao/ccswitch-audit/fix-security-hardening-misc branch from 905d083 to 50468db Compare November 25, 2026 04:59

@funkpopo funkpopo left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已完整复审最新提交(50468db),LGTM,可以直接合并。

上一轮 @farion1231 提出的三点都已妥善处理:

  1. Unix 含冒号旧备份兼容(backup.rs / backup/tests.rs):validate_backup_filename 已将 : 拒绝收敛到 cfg!(windows),rename_backup 对新建目标名继续全平台禁用 :(同时也补了纯 .db 空名校验),并补齐了 list / restore / rename / delete 的 Unix 回归测试。
  2. osascript 换行转义:src-tauri/src/session_manager/terminal/mod.rs 已彻底还原,不再包含在本次改动中。
  3. 脚本内联执行识别(deeplinkRisk.ts / deeplinkRisk.test.ts):通过 interpreterFlagsBeforeScript 将扫描边界收敛在脚本名、-、-- 及 Python -m 之前,并正确跳过带值解释器选项及紧贴值短参数(如 -Wonce、-Mfeature、-rset、-dmemory_limit=256M),脚本自带参数的反例测试覆盖充分。

其余几项改动也核对无问题:

  • Database::init 在 configure_connection(WAL)前收紧 cc-switch.db 权限至 0o600(失败仅 warn 不阻断启动),export_sql 切换至 atomic_write_private。
  • connect_via_proxy 的 userinfo 百分号解码(含空用户名配密码场景)、target_host CRLF 校验、RFC 9110 状态行解析、提前 EOF 拦截以及 64 行 / 16 KiB 响应头上限实现严谨,边界测试清晰。
  • ENV_HIJACK_PATTERNS 新增条目合理,不影响合法供应商预设。

当前前端 CI 已通过,待三端 Backend Checks 跑绿后即可合入。

@allenxu09
allenxu09 merged commit d2bb4c8 into farion1231:main Nov 25, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend Backend (Rust/Tauri) frontend Frontend (React/TypeScript) proxy Related to proxy/API forwarding

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants