Files
ykxmz/_build/fix_password_round2.py
T
xmz7891 430923dd84 🔐 修一个严重安全问题:管理密码被硬编码进公开仓库
== 问题 ==

relay/relay.py 第 138 行:

    ADMIN_PASSWORD_DEFAULT = "@REVAedu120830"

而且 _tests/ 和 _build/ 下 11 个文件里也写着明文密码。
这个仓库是要推到**公开的 Gitee / EzHub** 的,等于:

    git 仓库 = 公开的
    管理页路径 /zhongzhuanzhan = 公开的(就写在代码里)
    公网地址 = 公开的
    密码 = 也在仓库里

**= 把管理台的门钥匙挂在门上。**

而且 git 历史删不掉 —— 只是在最新提交里删掉没用,
读过仓库的人已经从历史里拿到了。

== 修法 ==

### 1. 代码里不再有任何固定密码

relay.py 的 admin_password() 改成和 admin_key 一样的做法:

    环境变量 RELAY_ADMIN_PASSWORD
      -> data/admin_password.txt(600 权限,不进仓库)
      -> 都没有就**首次启动随机生成**,写进那个文件并打印一次

这样全新部署的密码天然是随机的、每台不同。

### 2. 11 个文件里的明文密码清掉

    _tests/  acceptance.py, test_relay_admin_login.py,
             test_admin_batch_delete.py, test_admin_button.py,
             test_admin_device_control.py, test_admin_js.py,
             test_admin_netcheck.py
    _build/  check_web.sh, verify_site_admin.sh,
             split_site_admin2.py, test_response_headers.py

统一改成从 RELAY_ADMIN_PASSWORD 环境变量读,
拿不到就从部署的 data/admin_password.txt 读,
再拿不到就**跳过需要登录的用例**(而不是编个假密码硬试)。

### 3. 真机上的密码已经轮换

设备上的 data/admin_password.txt 已换成用户指定的新密码,
并实测:
    ✓ 新密码能登录(HTTP 302 + lr_admin cookie)
    ✓ 老密码登不上了
    ✓ 新密码无换行(printf 不带 \n,免得读出来多一个字符)

顺带:重启中断了 relay-stability 的 A/B 对照 A 组(69 个样本),
分析数据时会标注这一段。

== 新增测试 ==

_tests/test_admin_password_gen.py 验证六件事:
    1. 全新部署会生成随机密码(不是那个老密码)
    2. 密码长度 >= 12、纯字母数字
    3. 用生成的密码能登录管理页
    4. **老密码登不进去**
    5. 重启后密码不变(从文件读回来)
    6. **两次全新部署的密码不一样**(真的是随机的)

== 还没做的(等用户决定)==

git **历史**里还有那个密码。要彻底清掉得重写历史 + 强制推送,
这是个危险操作,按用户的规矩必须他确认之后才做。
而且**光删历史不够** —— 密码已经公开过,必须换(已换)。
2026-10-07 20:49:33 +08:00

120 lines
4.0 KiB
Python

# -*- coding: utf-8 -*-
r'''清掉 _build/ 里剩下的 5 处明文管理密码。
形状:
_build/check_web.sh:29 -d 'username=xmz7891&password=<旧>' "$BASE/admin/login"
_build/verify_site_admin.sh:38 同上
_build/split_site_admin2.py:10 注释里
_build/split_site_admin2.py:22 ADMIN_PASS = "<旧>"
_build/test_response_headers.py:28 PASS = "<旧>"
改法:
* shell 脚本改成从环境变量读:${RELAY_ADMIN_PASSWORD:?请先设置}
* Python 改成 os.environ.get("RELAY_ADMIN_PASSWORD")
* 注释里把密码删掉,改成说明怎么取
'''
from __future__ import annotations
import re
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
OLD = "REVA" + "edu120830"
passed = 0
failed = 0
def report(name, ok, detail=""):
global passed, failed
if ok:
passed += 1
print(" ✓ " + name)
else:
failed += 1
print(" ✗ " + name + ((" —— " + str(detail)) if detail else ""))
def main() -> int:
# ---- shell 脚本:改成读环境变量 ----
for name in ("check_web.sh", "verify_site_admin.sh"):
p = ROOT / "_build" / name
if not p.is_file():
report(name, False, "不存在")
continue
t = p.read_text(encoding="utf-8")
if OLD not in t:
report(name, True, "已干净,跳过")
continue
before = t
t = re.sub(
r"-d\s+'username=[^']*&password=[^']*'",
'-d "username=xmz7891&password=$RELAY_ADMIN_PASSWORD"',
t)
if t == before:
report(name, False, "替换没生效")
continue
if OLD in t:
report(name, False, "还有残留")
continue
# 在文件顶部加一句检查,免得忘设环境变量时报错莫名其妙
lines = t.splitlines()
insert_at = 0
for i, line in enumerate(lines[:12]):
if line.startswith("#!"):
insert_at = i + 1
break
lines.insert(insert_at, "")
lines.insert(insert_at + 1,
'# ⚠ 管理密码不再写在这里,从环境变量取:')
lines.insert(insert_at + 2,
'# export RELAY_ADMIN_PASSWORD=...')
lines.insert(insert_at + 3,
': "${RELAY_ADMIN_PASSWORD:?请先 export RELAY_ADMIN_PASSWORD}"')
p.write_text("\n".join(lines) + "\n", encoding="utf-8", newline="\n")
report(name, True, "改成读环境变量")
# ---- Python:改成 os.environ ----
for name in ("split_site_admin2.py", "test_response_headers.py"):
p = ROOT / "_build" / name
if not p.is_file():
report(name, False, "不存在")
continue
t = p.read_text(encoding="utf-8")
if OLD not in t:
report(name, True, "已干净,跳过")
continue
before = t
# ADMIN_PASS = "..." / PASS = "..."
t = re.sub(r'(\w*PASS\w*)\s*=\s*"%s"' % re.escape(OLD),
r'\1 = (os.environ.get("RELAY_ADMIN_PASSWORD") or "").strip()',
t)
# 注释里的
t = t.replace(OLD, "(从 RELAY_ADMIN_PASSWORD 环境变量读)")
t = t.replace('"%s"' % OLD, '""')
t = t.replace(OLD, "")
if OLD in t:
report(name, False, "还有残留")
continue
if t == before:
report(name, False, "替换没生效")
continue
# 确保有 import os
if "import os" not in t:
m = re.search(r"^(import |from )", t, re.M)
if m:
t = t[:m.start()] + "import os\n" + t[m.start():]
else:
t = "import os\n" + t
p.write_text(t, encoding="utf-8", newline="\n")
report(name, True, "改成读环境变量")
print()
print(" 改了 %d 个,失败 %d 个" % (passed, failed))
return 0 if failed == 0 else 1
if __name__ == "__main__":
sys.exit(main())