fix(linux): validate SUDO_USER and use prefix matching for interpreters

- Validate SUDO_USER via pwd.getpwnam() to prevent path injection
- Use prefix matching for interpreter detection to cover python3.10+ etc.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
PeanutSplash
2026-03-06 22:23:33 +08:00
committed by ylytdeng
parent bf77cc97d8
commit 3d58b6508c
2 changed files with 12 additions and 4 deletions

View File

@@ -116,7 +116,15 @@ def _auto_detect_db_dir_linux():
# sudo 运行时,~ 展开为 /root回退到实际用户的 home
sudo_user = os.environ.get("SUDO_USER")
if sudo_user:
sudo_home = os.path.expanduser(f"~{sudo_user}")
# 验证 SUDO_USER 是合法系统用户,防止路径注入
import pwd
try:
pw = pwd.getpwnam(sudo_user)
sudo_home = pw.pw_dir
except KeyError:
sudo_home = None
if not sudo_home:
sudo_home = os.path.expanduser(f"~{sudo_user}")
fallback = os.path.join(sudo_home, "Documents", "xwechat_files")
if fallback not in search_roots:
search_roots.append(fallback)