Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,14 +221,13 @@ For local development, put secrets in `apps/worker/.dev.vars` (gitignored).

## wrangler.toml Variables

All set via the Cloudflare dashboard (Variables and Secrets). `keep_vars = true` prevents deployments from overwriting them. The four ordinary variables `APP_NAME` / `APP_URL` / `RP_ID` / `APP_ORIGIN` are set via the CF dashboard only and are **not migrated** by CI; their values must **never change** — `RP_ID` / `APP_ORIGIN` are the WebAuthn passkey relying-party identifiers, and changing them invalidates every user's passkeys.
All set via the Cloudflare dashboard (Variables and Secrets). `keep_vars = true` prevents deployments from overwriting them. `APP_URL` / `RP_ID` / `APP_ORIGIN` are optional and read only through `src/lib/app-defaults.ts` — never directly via `env.*` in routes. Unset, they are derived from the request (origin / hostname); configured, they win. `APP_NAME` is **not** an env var: the app name comes from the admin setting `app_name` (default `Hop`). A configured `RP_ID` / `APP_ORIGIN` must **never change** — they are the WebAuthn relying-party identifiers, and changing them invalidates every user's passkeys.

| Var | Required | Description |
|---|---|---|
| `APP_URL` | Yes | Canonical origin (`https://yourdomain.com`) |
| `APP_NAME` | Yes | Default app name (overridable in admin UI) |
| `RP_ID` | Yes | WebAuthn relying party ID (domain only, no protocol) |
| `APP_ORIGIN` | Yes | WebAuthn origin (full URL with protocol) |
| `APP_URL` | No | Canonical origin (`https://yourdomain.com`). Default: request origin — used for CORS, HSTS, the refresh cookie's `Secure` flag and the `/404` redirect. |
| `RP_ID` | No | WebAuthn relying party ID (domain only, no protocol). Default: request hostname. |
| `APP_ORIGIN` | No | WebAuthn origin (full URL with protocol). Default: request origin. |
| `TABLE_PREFIX` | No | Three-state prefix for all D1 table names. Unset → default `hop` (`hop_users`, `hop_links`, …); a custom value → that value (case preserved, `[^a-zA-Z0-9_]` stripped); explicit empty string → no prefix (deployer accepts table-collision risk). Must be configured **before** running the setup route — changing it after initial setup requires renaming all tables manually. On GitHub Actions, `none` is the reserved word for the explicit empty string (unset/empty both become the default `hop`). A silent switch (database initialised under a different prefix) aborts setup/migrations with an error. |

Email sender config and API keys are managed in the **admin UI settings**, not here.
Expand Down
31 changes: 17 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Repository variables (the deploy workflow reads these):
| `D1_DATABASE_ID` | D1 database ID from the previous step |
| `D1_DATABASE_NAME` | (Optional) Only needed if you customized the D1 database name in the previous step; otherwise, no configuration is required |
| `TABLE_PREFIX` | (Optional) Production environment variable. The CI reserved word `none` means explicit no prefix. Must equal the Worker's `TABLE_PREFIX` plain-text variable (step 4). |
| `APP_URL` | Production environment variable used by the smoke test; must point at the production URL and equal the Worker's `APP_URL` variable (step 4). |
| `APP_URL` | Production URL used by the smoke test (step 6). Independent of the optional Worker variable of the same name (step 4) — the Worker no longer needs it. |

The deploy workflow (`.github/workflows/deploy.yml`) injects the resource IDs into `wrangler.toml` at deploy time via `sed`, so no IDs are ever committed to the repository.

Expand All @@ -79,18 +79,21 @@ Go to [Cloudflare dashboard](https://dash.cloudflare.com) → **Workers & Pages

| Name | Type | Required | Example / Notes |
|---|---|---|---|
| `APP_URL` | Variable | Yes | `https://yourdomain.com` — must equal the GitHub `APP_URL` variable (step 3) |
| `APP_NAME` | Variable | Yes | `Hop` — default app name, overridable in admin UI |
| `RP_ID` | Variable | Yes | `yourdomain.com` — WebAuthn relying party ID (domain only) |
| `APP_ORIGIN` | Variable | Yes | `https://yourdomain.com` — WebAuthn origin (full URL) |
| `APP_URL` | Variable | No | Canonical base URL (e.g. `https://yourdomain.com`). Optional — defaults to the origin of each request. Set it only to pin a URL that differs from the one clients hit. |
| `RP_ID` | Variable | No | WebAuthn relying party ID (domain only). Optional — defaults to the request hostname. |
| `APP_ORIGIN` | Variable | No | WebAuthn origin (full URL). Optional — defaults to the origin of each request. |
| `TABLE_PREFIX` | Variable | No | Three-state table prefix. Unset → default `hop` (`hop_users`, `hop_links`, …). A custom value → that value, case preserved, `[^a-zA-Z0-9_]` stripped. Explicit empty string → no prefix (deployer accepts the table-collision risk). Must equal the GitHub `TABLE_PREFIX` variable (step 3). Set **before** first setup and never change it once data exists. |
| `TOTP_ENCRYPTION_KEY` | Secret | Yes | `openssl rand -hex 32` — encrypts TOTP credentials; rotating it invalidates every bound TOTP. The **only** secret configured in the Cloudflare console and never handled by CI. |

> `keep_vars = true` is set in `wrangler.toml`, so deployments will never overwrite these values.
>
> `JWT_SECRET` and `SETUP_SECRET` are **not** configured here — CI uploads them from the GitHub Secrets added in step 3. `TOTP_ENCRYPTION_KEY` is the only secret that stays exclusively in the Cloudflare console.
>
> The ordinary variables `APP_NAME` / `APP_URL` / `RP_ID` / `APP_ORIGIN` are set via the **Cloudflare dashboard** only and are **not migrated** by CI; their values must **never change** — `RP_ID` / `APP_ORIGIN` are the WebAuthn passkey relying-party identifiers, and changing them invalidates every user's passkeys. `APP_URL` and `TABLE_PREFIX` are also read by CI as GitHub variables (step 3) for the smoke test and prefix resolution, so keep those two GitHub variables equal to these Worker variables.
> `APP_URL` / `RP_ID` / `APP_ORIGIN` are optional: when unset the Worker derives them from the incoming request (see [Environment Variables](#environment-variables)). An explicit value always wins, so a deployment that already configures them behaves exactly as before. `APP_NAME` is **no longer an env var** — the app name is the admin UI setting `app_name` (default `Hop`).
>
> `RP_ID` / `APP_ORIGIN` are the WebAuthn passkey relying-party identifiers. If you configure them, their values must **never change** — changing them invalidates every user's passkeys. With them unset, the rpID is derived from the hostname the user actually visits, so credentials stay bound to that hostname: visiting through your custom domain behaves like the old explicit config, while visiting through another hostname (e.g. `*.workers.dev`) yields a different rpID and therefore different passkey scope — inherent to a multi-hostname deployment, not a regression.
>
> The GitHub `TABLE_PREFIX` variable (step 3) still feeds prefix resolution; keep it equal to the Worker's `TABLE_PREFIX`.
>
> Email provider settings (Resend API key, SMTP credentials, etc.) are configured in the **admin UI** after first login — no Cloudflare dashboard config needed.

Expand Down Expand Up @@ -175,9 +178,10 @@ Navigate to your deployed URL and register — the first account is automaticall
```bash
# 1. Create apps/worker/.dev.vars
cat > apps/worker/.dev.vars << 'EOF'
APP_URL=http://localhost:8787
APP_NAME=Hop
RP_ID=localhost
# APP_URL / RP_ID / APP_ORIGIN are optional — unset, they are derived from the
# request URL (http://localhost:8787 → rpID "localhost"). APP_ORIGIN is only
# needed for passkey testing, where the page runs on the Vite dev server
# (:5173) while /api is proxied to the Worker (:8787).
APP_ORIGIN=http://localhost:5173
JWT_SECRET=dev-secret-at-least-32-characters-long
TOTP_ENCRYPTION_KEY=0000000000000000000000000000000000000000000000000000000000000000
Expand Down Expand Up @@ -300,10 +304,9 @@ All variables and secrets are managed in the **Cloudflare dashboard** (Workers &

| Variable | Type | Required | Description |
|---|---|---|---|
| `APP_URL` | Var | Yes | Canonical base URL (e.g. `https://go.example.com`) |
| `APP_NAME` | Var | Yes | Default app name; overridable in admin UI |
| `RP_ID` | Var | Yes | WebAuthn relying party ID (domain without protocol) |
| `APP_ORIGIN` | Var | Yes | WebAuthn origin (full URL) |
| `APP_URL` | Var | No | Canonical base URL (e.g. `https://go.example.com`). Optional — defaults to the request origin (used for CORS, HSTS, the refresh cookie's `Secure` flag and the `/404` redirect). |
| `RP_ID` | Var | No | WebAuthn relying party ID (domain without protocol). Optional — defaults to the request hostname. |
| `APP_ORIGIN` | Var | No | WebAuthn origin (full URL). Optional — defaults to the request origin. |
| `TABLE_PREFIX` | Var | No | Three-state prefix for all D1 table names. Unset → default `hop` (`hop_users`, `hop_links`, …); custom value → that value (case preserved); explicit empty → no prefix. Set once before the first migration — changing it later requires renaming tables. Only alphanumeric and underscore characters are used. |
| `JWT_SECRET` | Secret | Yes | HS256 signing key, ≥ 32 chars |
| `TOTP_ENCRYPTION_KEY` | Secret | Yes | Hex string from `openssl rand -hex 32` |
Expand All @@ -317,7 +320,7 @@ All variables and secrets are managed in the **Cloudflare dashboard** (Workers &
|---|---|---|
| `registration_enabled` | `true` / `false` | Allow new user registration (default: `false`) |
| `require_email_verification` | `true` / `false` | Require new users to verify their email before signing in (default: `false`) |
| `app_name` | string | Override displayed app name |
| `app_name` | string | App name shown in the UI, emails and passkey prompts. Default `Hop`. There is no env var for it — this setting is the single source. |
| `email_provider` | `resend` / `smtp` | Which email backend to use (default: `resend`) |
| `resend_api_key` | string | Resend API key |
| `email_from_domain` | domain | Sender domain for Resend, e.g. `example.com` |
Expand Down
30 changes: 16 additions & 14 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
| `D1_DATABASE_ID` | 上一步中的 D1 database ID |
| `D1_DATABASE_NAME` | (可选)仅当上一步自定义了 D1 数据库名称时才需配置为对应名称,否则无须配置。 |
| `TABLE_PREFIX` | (可选)production 环境变量。CI 中保留字 `none` 表示显式无前缀。必须与 Worker 的同名明文变量一致(见第 4 步)。 |
| `APP_URL` | production 环境变量,smoke test 使用,须指向生产 URL,并与 Worker 的 `APP_URL` 变量一致(见第 4 步)。 |
| `APP_URL` | 生产 URL,供 smoke test 使用(见第 6 步)。与第 4 步中同名的 Worker 变量无关——Worker 已不再需要它。 |

部署工作流(`.github/workflows/deploy.yml`)在部署时通过 `sed` 将这些资源 ID 注入 `wrangler.toml`,因此 ID 不会提交到仓库。

Expand All @@ -80,18 +80,21 @@

| 名称 | 类型 | 必填 | 示例 / 说明 |
|---|---|---|---|
| `APP_URL` | Variable | 是 | `https://yourdomain.com` — 须与 GitHub 中的 `APP_URL` 变量一致(第 3 步) |
| `APP_NAME` | Variable | 是 | `Hop` — 默认应用名称,可在管理界面覆盖 |
| `RP_ID` | Variable | 是 | `yourdomain.com` — WebAuthn 依赖方 ID(仅域名) |
| `APP_ORIGIN` | Variable | 是 | `https://yourdomain.com` — WebAuthn 来源(完整 URL) |
| `APP_URL` | Variable | 否 | 规范基础 URL(例如 `https://yourdomain.com`)。可选——未配置时按每个请求的 origin 推导。仅当需要固定一个与客户端实际访问地址不同的 URL 时才配置。 |
| `RP_ID` | Variable | 否 | WebAuthn 依赖方 ID(仅域名)。可选——未配置时按请求 hostname 推导。 |
| `APP_ORIGIN` | Variable | 否 | WebAuthn 来源(完整 URL)。可选——未配置时按请求 origin 推导。 |
| `TABLE_PREFIX` | Variable | 否 | 三态表前缀。未配置 → 默认 `hop`(`hop_users`、`hop_links` 等);配置自定义值 → 该值(保留大小写,剔除 `[^a-zA-Z0-9_]`);显式配置为空字符串 → 无前缀(部署者自担撞表风险)。须与 GitHub 中的 `TABLE_PREFIX` 变量一致(第 3 步)。必须在**首次** setup 前设置,一旦有数据后不得更改。 |
| `TOTP_ENCRYPTION_KEY` | Secret | 是 | `openssl rand -hex 32` — 加密 TOTP 凭据,轮换会使已绑定的 TOTP 全部失效。这是**唯一**在 Cloudflare 控制台配置的 Secret,不经 CI。 |

> `wrangler.toml` 中设置了 `keep_vars = true`,因此部署不会覆盖这些值。
>
> `JWT_SECRET` 与 `SETUP_SECRET` **不在此处配置** — 由 CI 从第 3 步添加的 GitHub Secret 上传。只有 `TOTP_ENCRYPTION_KEY` 仅存在于 Cloudflare 控制台、从不经 CI。
>
> 普通变量 `APP_NAME` / `APP_URL` / `RP_ID` / `APP_ORIGIN` 仅在 **Cloudflare 控制台** 设置,不迁移;其值**严禁变更**——`RP_ID` / `APP_ORIGIN` 是 WebAuthn 通行密钥的依赖方标识,一旦变更将导致所有用户的通行密钥失效。`APP_URL` 与 `TABLE_PREFIX` 同时作为 GitHub 变量(第 3 步)供 smoke test 与前缀解析读取,需与这里的 Worker 变量保持同值。
> `APP_URL` / `RP_ID` / `APP_ORIGIN` 均为可选:未配置时 Worker 按请求推导(见[环境变量](#环境变量))。显式配置始终优先,因此已配置这三项的存量部署行为与改前完全一致。`APP_NAME` **不再是环境变量**——应用名称唯一来源是管理面设置 `app_name`(默认 `Hop`)。
>
> `RP_ID` / `APP_ORIGIN` 是 WebAuthn 通行密钥的依赖方标识。若配置,其值**严禁变更**——一旦变更将导致所有用户的通行密钥失效。若未配置,rpID 按用户实际访问的 hostname 推导,凭据仍按域名绑定:经正式自定义域名访问与原显式配置等价;经其他域名(例如 `*.workers.dev`)访问会得到不同的 rpID,从而形成不同的通行密钥作用域——这是多域名部署的固有行为,不是本次变更的回归。
>
> GitHub 中的 `TABLE_PREFIX` 变量(第 3 步)仍供前缀解析读取,需与 Worker 的同名变量保持同值。
>
> 邮件提供商设置(Resend API 密钥、SMTP 凭据等)在首次登录后通过**管理界面**配置 — 无需在 Cloudflare 控制台中配置。

Expand Down Expand Up @@ -159,9 +162,9 @@ curl -X POST https://your-worker.workers.dev/api/setup \
```bash
# 1. 创建 apps/worker/.dev.vars
cat > apps/worker/.dev.vars << 'EOF'
APP_URL=http://localhost:8787
APP_NAME=Hop
RP_ID=localhost
# APP_URL / RP_ID / APP_ORIGIN 均可选——不配时按请求 URL 推导
# (http://localhost:8787 → rpID "localhost")。只有测试通行密钥时才需要
# APP_ORIGIN:此时页面由 Vite dev server(:5173)提供,而 /api 代理到 Worker(:8787)。
APP_ORIGIN=http://localhost:5173
JWT_SECRET=dev-secret-at-least-32-characters-long
TOTP_ENCRYPTION_KEY=0000000000000000000000000000000000000000000000000000000000000000
Expand Down Expand Up @@ -284,10 +287,9 @@ GET /:slug → 302 跳转到目标 URL

| 变量 | 类型 | 必填 | 描述 |
|---|---|---|---|
| `APP_URL` | 变量 | 是 | 规范基础 URL(例如 `https://go.example.com`) |
| `APP_NAME` | 变量 | 是 | 默认应用名称;可在管理界面覆盖 |
| `RP_ID` | 变量 | 是 | WebAuthn 依赖方 ID(不含协议的域名) |
| `APP_ORIGIN` | 变量 | 是 | WebAuthn 来源(完整 URL) |
| `APP_URL` | 变量 | 否 | 规范基础 URL(例如 `https://go.example.com`)。可选——未配置时用请求 origin(用于 CORS、HSTS、refresh cookie 的 `Secure` 标记与 `/404` 跳转)。 |
| `RP_ID` | 变量 | 否 | WebAuthn 依赖方 ID(不含协议的域名)。可选——未配置时用请求 hostname。 |
| `APP_ORIGIN` | 变量 | 否 | WebAuthn 来源(完整 URL)。可选——未配置时用请求 origin。 |
| `TABLE_PREFIX` | 变量 | 否 | 所有 D1 表名的三态前缀。未配置 → 默认 `hop`(`hop_users`、`hop_links` 等);自定义值 → 该值(保留大小写);显式空 → 无前缀。在第一次迁移前设置一次 — 之后更改需手动重命名所有表。只使用字母数字和下划线字符。 |
| `JWT_SECRET` | 密钥 | 是 | HS256 签名密钥,≥ 32 字符 |
| `TOTP_ENCRYPTION_KEY` | 密钥 | 是 | 来自 `openssl rand -hex 32` 的十六进制字符串 |
Expand All @@ -301,7 +303,7 @@ GET /:slug → 302 跳转到目标 URL
|---|---|---|
| `registration_enabled` | `true` / `false` | 允许新用户注册(默认:`false`) |
| `require_email_verification` | `true` / `false` | 要求新用户在登录前验证其邮箱地址(默认:`false`) |
| `app_name` | 字符串 | 覆盖显示的应用名称 |
| `app_name` | 字符串 | 界面、邮件与通行密钥提示中显示的应用名称,默认 `Hop`。没有对应的环境变量——本设置是唯一来源。 |
| `email_provider` | `resend` / `smtp` | 使用哪个邮件后端(默认:`resend`) |
| `resend_api_key` | 字符串 | Resend API 密钥 |
| `email_from_domain` | 域名 | Resend 的发件人域名,例如 `example.com` |
Expand Down
14 changes: 8 additions & 6 deletions apps/worker/.dev.vars.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
# Copy this file to apps/worker/.dev.vars and fill in real values.
# .dev.vars is gitignored — never commit real secrets.

# ─── Ordinary variables ───────────────────────────────────────────────────────
# Production values are set via the Cloudflare dashboard (Variables) and are
# NOT migrated by CI. Their values must never change:
# RP_ID / APP_ORIGIN are the WebAuthn passkey relying-party identifiers —
# changing them invalidates every user's passkeys.
# ─── Ordinary variables (all optional) ────────────────────────────────────────
# Leave these empty to derive them from the request URL: APP_URL/APP_ORIGIN from
# the request origin, RP_ID from the request hostname. An explicit value always
# wins. APP_NAME is gone — the app name is the admin UI setting `app_name`
# (default "Hop").
#
# APP_ORIGIN is only needed for passkey testing locally: the page is served by
# the Vite dev server (http://localhost:5173) while /api is proxied to :8787.
APP_URL=
APP_NAME=
RP_ID=
APP_ORIGIN=

Expand Down
Loading
Loading