Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

docs(auth): 口令的存放、取用与两台机器共用同一份

longmeidao23f812454ed3
diff --git a/README.en.md b/README.en.md
index 54ad58832..90737a7df 100644
--- a/README.en.md
+++ b/README.en.md
@@ -107,8 +107,9 @@ and writes `<data root>/config.toml`. The default data root is `peach-data/` nex
replication switches are all edited in that settings file, explained item by item in
[`docs/OPERATIONS.md`](docs/OPERATIONS.md). The tray is started with `peach-tray`, without arguments.
-Four facts:
+Five facts:
+- Listening on `127.0.0.1` needs no token. Reaching the service from a phone or any other device on the LAN (`--host 0.0.0.0`, and everything the tray starts) does: `peach init` has already written one to `<data root>/secrets/auth-token`, `peach token` prints it, and each device pastes it into the login page once. `peach serve` refuses to start when it binds a non-loopback address without a token, because that puts the whole collection and the write endpoints on the local network.
- `-e` is a hard requirement: the wheel contains only the packages under `src/`, and the repository-root `migrations/` and `web/` are not in it, so under a non-editable install `peach init` fails outright because it cannot find the migrations directory.
- When `--data-root` points somewhere else, `peach serve` looks for the data root only via `PEACH_DATA_ROOT` and the `peach-data/` directories a few levels above the repository, so set `PEACH_DATA_ROOT` as well; the default data root needs no such step.
- The declared roots of the three sources in the settings file default to `local = R:\media`, `115 = B:/` and `pikpak = A:/`. These are example drive letters and must be changed to your own paths under `[media.locations]` and `[media.mounts]`; leaving them means every source is offline, not an error. CloudDrive is not required — any cloud drive that mounts as a local path works; 115 and PikPak are recommendations, not requirements.
diff --git a/README.md b/README.md
index b9200d9c4..0dcb32437 100644
--- a/README.md
+++ b/README.md
@@ -101,8 +101,9 @@ peach-app/
之后 `peach serve` 就能起服务;监听地址、端口、媒体盘符映射和复制开关都在那个设置文件里改,
逐项说明见 [`docs/OPERATIONS.md`](docs/OPERATIONS.md)。托盘由 `peach-tray` 启动,不带参数。
-四个事实:
+五个事实:
+- 只监听 `127.0.0.1` 不需要口令。要让手机或局域网里别的设备访问(`--host 0.0.0.0`、托盘起的服务都算),就必须有访问口令:`peach init` 已经在 `<数据根>/secrets/auth-token` 生成一份,`peach token` 打印出来,设备第一次访问时贴进登录页。绑非回环地址却读不到口令时 `peach serve` 拒绝启动,因为那等于把整个馆藏和写接口摆在同网段上。
- `-e` 是硬性要求:wheel 只含 `src/` 下的包,仓库根的 `migrations/` 与 `web/` 不在里面,非可编辑安装下 `peach init` 找不到迁移目录会直接报错。
- `--data-root` 指到别处时,`peach serve` 只按 `PEACH_DATA_ROOT` 和仓库上方几层的 `peach-data/` 找数据根,所以要同时设 `PEACH_DATA_ROOT`;用默认数据根没有这一步。
- 设置文件里三个来源的声明根默认是 `local = R:\media`、`115 = B:/`、`pikpak = A:/`,这只是示例盘符,必须在 `[media.locations]` 与 `[media.mounts]` 里改成自己的路径;不改的结果是全部来源脱盘,而不是报错。CloudDrive 不是必需,任何能挂成本地路径的网盘都行;115 与 PikPak 是推荐项,不是要求。
diff --git a/SECURITY.md b/SECURITY.md
index f6e9c40a5..75a0941ab 100644
--- a/SECURITY.md
+++ b/SECURITY.md
@@ -2,8 +2,10 @@
Peach is a single-user, local-first, self-hosted application: one deployment serves one person, with
no accounts, roles or tenant isolation, so the threat model covers only other devices on the same LAN
-being let in by accident. The token gate is off whenever `peach serve` runs without `--token`, so
-binding to a LAN address requires a token. The service binds to loopback (`127.0.0.1`) by default
+being let in by accident. `peach init` generates a token under `<data root>/secrets/`, and `peach
+serve` refuses to start when it binds a non-loopback address and cannot read one; the gate is open
+whenever the token is empty, which is why that refusal is a hard stop and not a warning. `peach
+token` prints it, `peach token --rotate` replaces it. The service binds to loopback (`127.0.0.1`) by default
and uses HTTPS with a self-signed project CA generated by `peach init`; only the CA certificate is
distributed, never a private key. Never expose the service to the public internet — there is no rate
limiting, auditing or second factor. Report vulnerabilities through the repository's GitHub
@@ -20,8 +22,15 @@ Peach 是单用户、本地优先的自托管应用:一个部署服务一个
口令闸门在 `src/peach/routes_auth.py`,三个 `require_*` 依赖是唯一的判定入口。
-- 口令由 `peach serve --token <口令>` 给出。**不给 `--token` 时闸门整体放行**,任何请求都不需要
- 凭证。绑定到局域网地址就必须同时配口令。
+- 口令存在 `<数据根>/secrets/auth-token`,由 `peach init` 生成(两条 init 路径都生成),
+ `peach token` 打印,`peach token --rotate` 换一个。取用顺序是 `--token` > `PEACH_TOKEN` >
+ 这个文件。口令不进 `config.toml`——那份文件是可以贴进 issue 的本机坐标;也不靠
+ `--token <口令>` 长期传递——同机任何进程都能从进程表读到别人的命令行。
+- **口令为空时闸门整体放行**,任何请求都不需要凭证。所以 `peach serve` 绑非回环地址却取不到
+ 口令时**拒绝启动**(`src/peach/cli.py` 的 `_serve_token`),不是打一句告警:托盘在后台起服务,
+ 告警没有人会看见。只监听 `127.0.0.1` 时允许没有口令。
+- 两台机器互取复核结果时发的是自己的口令(`ReviewMirror(token=settings.token)`),reader 要和
+ writer 用同一份口令文件,复制过去即可。
- 比较用 `hmac.compare_digest`,不做长度或前缀短路。
- `POST /login` 提交表单后换到 HttpOnly cookie `tok`(`SameSite=Lax`,有效期一年,HTTPS 下带
`Secure`)。脚本用 `X-Token` 请求头。`?t=` 查询参数也被接受,但它会把口令留在访问日志、
diff --git a/docs/OPERATIONS.md b/docs/OPERATIONS.md
index a42743f8a..75886ab7d 100644
--- a/docs/OPERATIONS.md
+++ b/docs/OPERATIONS.md
@@ -107,13 +107,15 @@ launchctl kickstart -k gui/$(id -u)/gg.lmd.peach.tray
## 网络、证书与 mDNS
- 托盘管理 HTTP `0.0.0.0:80` 和当前路由选出的 LAN IPv4 上的 HTTPS 443,显式参数、`PEACH_LAN_ADDRESS`、`lan_ipv4()` 依次覆盖;服务日志写入本机 `peach-data/logs`。
+- 托盘起的服务全是非回环绑定,所以这台机器必须有访问口令,否则 `peach serve` 直接退出、托盘把那条服务显示成未运行。`peach token` 打印 `<数据根>/secrets/auth-token`(没有就现生成),`peach token --rotate` 换一个。换完要重启服务——进程只在启动时读一次——已登录的设备也要重新登录。
+- 设备第一次访问跳登录页,把口令贴进去换成一年期的 HttpOnly cookie。reader 取 writer 的复核结果时发的是自己的口令,所以两台机器要用同一份 `auth-token`,复制过去即可。
- `peach serve` 按平台发布固定 mDNS 主机名,不在源码钉家庭 IP,仍保留 `Zeroconf()` 全合格网卡监听;mDNS 验收必须包含单元测试、运行态 health、DNS-SD、主机名解析和真实 LAN 客户端。
- 双机广播分工固定:macOS 是 `peach.local`,Windows 是 `peach-writer.local`,默认值收敛到 `peach.config.MDNS_NAME`,`PEACH_MDNS_NAME` 只做临时覆盖。服务可以同时跑,但两边同时写入会很快冲突转只读。
- `.local` 使用本地 CA,不使用 Let's Encrypt。证书与私钥保存在本机 `peach-data/secrets`;TLS 私钥禁用 ACL 继承,只允许实际服务身份、SYSTEM 和 Administrators。macOS/iOS 只安装并信任 `peach-local-ca.crt`,不分发任何私钥。
- 两台机器各有独立的本机 CA(secrets 按设计不共享):iPhone/iPad 必须信任「当前正在服务的那台」的 CA,换机器服务后要装对应的 `peach-local-ca.crt` 并开完全信任,指纹用 `openssl x509 -noout -fingerprint` 核对。
- 对本机服务的 HTTP 探测必须 `trust_env=False`:代理客户端会设置系统级 HTTP 代理,httpx 默认经 `urllib.getproxies()` 读它,探测 `127.0.0.1` 的请求被送进代理并由代理回 503,服务活着却被判「未运行」。修复在 `peach.tray.ServiceManager.healthy`,`test_health_check_never_goes_through_a_proxy` 守门,新写的健康检查同样适用。
- macOS 系统代理的例外列表必须包含 `*.local`、`localhost`、`127.0.0.1` 和本机局域网网段:代理核心解析不了 mDNS 名字,浏览器打开 `.local` 会被代理回 503(终端直连正常)。用 `networksetup -setproxybypassdomains` 设置、`scutil --proxy` 的 `ExceptionsList` 复查;代理客户端重设系统代理后这一列表可能被清掉。
-- FastAPI 是唯一 Web server,不得恢复平行 `http.server` 或动态 legacy loader;配置 `--token` 时通过 `/login` POST 取得 HttpOnly cookie,旧 `?t=` 只做一次兼容重定向。
+- FastAPI 是唯一 Web server,不得恢复平行 `http.server` 或动态 legacy loader;口令通过 `/login` POST 换成 HttpOnly cookie,`?t=` 只做一次性场合,它会把口令留在访问日志和浏览历史里。
- 切换服务前检查 80、443、8900 端口和实际进程归属。9999 已移出这份清单:服务运行期不再连接 Stash(ADR-0021)。
## 媒体解析与转码
diff --git a/docs/PRODUCT_BACKLOG.md b/docs/PRODUCT_BACKLOG.md
index ebd8a51a1..ea4de4287 100644
--- a/docs/PRODUCT_BACKLOG.md
+++ b/docs/PRODUCT_BACKLOG.md
@@ -33,7 +33,7 @@
18. 口味导入引导:`/taste` 上传(Takeout ZIP、browserexport 兼容文件)与 `scripts/taste_history.py` 直读本机浏览器库两条路都能用,但没有面向陌生人的文档。需要一页「各浏览器怎么导出、多台设备怎么各自刷新」教程,把脚本折进 `peach` CLI(见第 7 条),并写明定时刷新的安装方式。
19. README 瘦身:把「依赖维护」「开发」两节移到 `CONTRIBUTING.md`,「目录」并入 `docs/ARCHITECTURE.md`,「主要页面」「关注与候选」压成一张表;README 只留是什么、边界、前置条件、安装、下载、文档入口与许可证。中英两份同步。
-20. 局域网入口默认无鉴权:`routes_auth._authorized` 在口令为空时整体放行,而托盘 `build_service_specs` 起的正是 `--host 0.0.0.0`(Windows 80、macOS 8900)与局域网 `:443`,三条命令都不带 `--token`;口令只能由 `peach serve --token` 给出,`config.toml` 没有这个字段。于是按 README 装完、用托盘自启动的陌生用户,同网段任何设备都能读整库并调写接口——SECURITY.md 写的「绑定局域网就必须配口令」在自带入口上不成立。要做三件事:非回环监听且无有效口令时**拒绝启动**而不是告警(托盘在后台起服务,告警没人看得到);`peach init` 生成至少 256 bit 的随机口令存进 `peach-data/secrets/`,不写 `config.toml`、也不以 `--token <口令>` 长期留在进程命令行里;托盘与服务读同一个文件。局域网明文口只做跳转、手机端一次性配对码排在这三件之后。这改的是现有部署的访问方式,实施前要在同一轮拿到用户确认。
+20. 局域网访问还差两件。一是局域网明文口只做跳到 HTTPS 的重定向,不再提供完整应用面:Windows 的 80 和 macOS 的 8900 现在都是完整的 HTTP 服务,口令、cookie 和媒体流在同网段上是明文。二是配对体验:设备第一次访问要人把 43 个字符手输或粘贴进登录页,改成桌面端显示一次性配对码或局域网 HTTPS 地址的二维码,扫完直接写设备 cookie。口令的生成、取用与「绑非回环地址却没有口令就拒绝启动」在 `src/peach/auth.py` 与 `cli._serve_token`,不在这条里。
21. 全新安装的自动门槛:现有 `Test` 装的是 `-e ".[build,vision,maintenance-115,naming]"` 全套可选依赖、开着 pip 缓存、只跑单元测试,证明不了「陌生用户按 README 装完能用」。补三条互相独立的冒烟:① minimal source——全新 venv、`--no-cache-dir` 只装默认依赖、`peach init`(连跑两次验幂等)、`migrate status`、**离开仓库根目录**再 `peach serve`,请求 `/healthz`、`/`、`/api/items`,覆盖 3.12/3.14 × Windows/macOS 以及无 FFmpeg/OpenSSL/Node 的机器;② wheel——`python -m build` 后在不 checkout 源码的 job 里装 `dist/*.whl` 走同一条链路,它通过才能去掉 README 的 `-e` 硬要求(依赖第 12 条);③ artifact-only——只下载刚构建的制品、不 checkout 源码地跑起来(依赖第 15 条)。消费方一律不许 checkout:工作目录会替漏文件的制品兜底,那是假通过。失败场景也要覆盖:数据根不可写、端口被占、账本损坏、未配置媒体目录、无 FFmpeg、非回环监听但无口令、两个 writer 同时起。
22. `peach doctor` 与分级 `/healthz`:`doctor`(另带 `--json`)逐项报版本、数据根可写性、配置文件合法性、数据库能否打开、schema 版本与待执行迁移、FFmpeg/ffprobe/OpenSSL 路径、挂载点可达性、端口占用、是否处在「局域网暴露但无口令」状态、后台任务最近一次失败;输出脱敏,不带口令、cookie、站点凭据和完整媒体路径。`/healthz` 相应从布尔改成分项状态(`database`/`schema`/`configured`/`ffmpeg`/`media_mounts`/`security`),与第 13 条一起做。
23. 性能基准:用 SFW 合成数据生成 1k/10k/100k/500k 四档库,nightly 测冷启动到 `/healthz`、目录页与详情页 p95、两字以上搜索 p95、本地 SSD 与网盘挂载的 Range 首字节、空闲 RSS、后台扫描时前台退化倍数、备份期间读请求不失败。门槛用「相对上一次基线下降超过 20%」,不给绝对毫秒数——不同机器不可比。数据集与第 16 条的演示数据集共用。