Repository navigation
feat: APP_URL/RP_ID/APP_ORIGIN 改为可选并从请求推导,APP_NAME 收敛为应用内设置 (ONE-437) - #150
Merged
Merged
Conversation
把四个原本必须配置的 Worker 变量降级为「可不配」: - 新增 `apps/worker/src/lib/app-defaults.ts` 作为唯一解析入口: `appUrl(c)` / `rpId(c)` / `appOrigin(c)`(显式 env 优先,否则用请求 origin / hostname)与 `appName(env)`(DB settings.app_name,默认 Hop)。 - `index.ts` 的 securityHeaders、CORS 白名单、`/api/config`;`auth.ts` 的 Cookie Secure、Passkey rpID/rpName 与两处 verify、`/api/admin/email/test` 全部改走该入口,删除 auth.ts / admin.ts / index.ts 里三份重复的 app_name 读取实现。 - `types.ts`:三项改为可选,`APP_NAME` 从 Env 移除。 - 新增 22 项测试覆盖「未配置按请求推导」「配置了 env 胜出」「APP_NAME 全未配 时 /api/config 与 rpName 都是 Hop,管理面改名后两处同步生效」。 - README / README.zh-CN / CLAUDE.md / .dev.vars.example 同步更新。 已配置这三项的存量部署行为不变(显式配置始终优先)。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes ONE-437(父单 ONE-435 的 Stage 1 实现)。
把 Worker 侧四个原本必须配置的环境变量降为「可不配」:
APP_URL/RP_ID/APP_ORIGIN未配置时从请求推导,APP_NAME取消 env 依赖、改为应用内设置(settings.app_name)唯一来源。完成后新部署无需在 Cloudflare 控制台配这四项;已有部署(变量已配)行为零变化。核心改动
新增集中解析入口
apps/worker/src/lib/app-defaults.ts,全仓只有这一处做推导(含 app_name 的 DB/KV 读取):appUrl(c)env.APP_URL去空白后非空 → 用它;否则请求 URL 的originrpId(c)env.RP_ID非空 → 用它;否则请求 URL 的hostnameappOrigin(c)env.APP_ORIGIN非空 → 用它;否则请求 URL 的originappName(env)settings.app_name(DB,KV 缓存 60s);无行 → 常量默认Hop显式配置始终优先(优先级铁律)。已配置的值原样返回、调用点保留原有的 trim 归一化,因此带空格的存量配置行为也与改前逐字节一致。
替换的直读点(即父单证据清单,逐个):
src/index.ts:securityHeaders(API 与 SPA 两处)、CORS 白名单、/api/config的 app_name 三级链(删 env 分支)。src/routes/auth.ts:两处 Cookie Secure 的startsWith('https');Passkey 注册 options 的rpID/rpName;两处 verify 的rpID/origin;认证 options 的rpID;getAppName三级链删除 env 分支。src/routes/redirect.ts:三处/404跳转目标 + 302 的 HSTS 判定(解析结果按请求算一次复用)。src/routes/admin.ts:getAppName三级链删 env 分支。src/types.ts:APP_URL/RP_ID/APP_ORIGIN改为可选;APP_NAME从Env移除(无保留理由)。index.ts/auth.ts/admin.ts三份重复的 app_name 读取实现,统一为 lib 里那一个(这是「全仓只许一处推导」的必要条件,不是顺手重构)。文档:
README.md/README.zh-CN.md环境变量表三项改「可选(默认从请求推导)」、APP_NAME从 env 表移除并在设置表注明为管理面设置项;CLAUDE.md、apps/worker/.dev.vars.example同步。无迁移、无 schema 变更、无 D1/KV 写操作;回滚 = revert 本 PR(纯代码,无数据影响)。
直读残留 grep 证据
即:除集中解析入口外无任何直读残留(
env.APP_NAME已 0 处)。自测证据(本地)
新增测试(
apps/worker/test/app-defaults.test.ts,22 项)覆盖:APP_URL:CORS 回显请求自身 origin(含「其他 origin 不放行」)、API 与 SPA 的 HSTS 按请求 scheme、Cookie Secure 按请求 scheme(https 有 / http 无)、/404跳转按请求 origin。RP_ID/APP_ORIGIN:Passkey 注册 options 的rpID= 请求 hostname(端口不带入)、rpName=Hop;resolver 单测同时钉住appOrigin= 请求 origin(verify 的两处调用点用的就是这两个 resolver)。APP_URL为 http 时即便请求是 https 也不发 HSTS)、/404目标、Cookie Secure、rpID。app_name:settings 有默认行 / 连默认行都没有(落到常量Hop)时/api/config与 rpName 都是Hop;管理面PUT /api/admin/settings改名后两处同步生效(覆盖 KV 缓存失效路径);残留的APP_NAMEenv 值不再影响结果。未跑成的项:Passkey verify 两处参数没有端到端断言——仓库现有测试不构造真实 WebAuthn 断言,本次未新增(需伪造 attestation),改为「resolver 单测 + 调用点统一走 resolver + grep 无直读」三重保证。如需端到端覆盖请评审时明确,我另开单补。
Passkey 语义说明(重要)
rpId改为按实际访问域名推导后,凭据仍然按域名绑定:go.example.com)访问 → 推导出的 rpID 与原来显式配置的RP_ID相同,与原行为等价;*.workers.dev或其他域名访问 → 得到不同的 rpID,因此是另一套通行密钥作用域(这些域名下无法用自定义域名下注册的凭据登录)。这是多域名部署的固有行为,不是本次回归;配置了
RP_ID/APP_ORIGIN的存量部署完全不受影响(显式配置优先)。安全视角:未配置时 CORS 白名单只放行「请求自身的 origin」,跨站 Origin 仍被拒(Origin: https://evil.example→ 不回显 ACAO),未引入新的跨域暴露面;能同时伪造 Host 与 Origin 的攻击者本就必须控制一个已路由到该 Worker 的域名,此时同源请求本身就不需要 CORS。