# Sharing a local app with outside testers (Cloudflare Tunnel + app-level gate) Use when a local web app (Next.js/Node, backed by a local model endpoint) must be reachable by friends/testers on a public subdomain of a Cloudflare-managed domain, while staying on this Mac. Tailscale serve is tailnet-only; this is the public path. ## Order 1. **App gate first (worker job, behind an env flag such as `APP_ACIK=1`, so the local demo mode stays unchanged):** access-code screen verified server-side, signed httpOnly+Secure+SameSite=Lax cookie; every page and every `/api/*` returns 401 without it; wrong-code rate limit per IP; daily total live-model quota persisted in a file outside the repo (survives restarts, safe under concurrent requests, resets at local midnight), with a calm "remaining today" line and a fallback to recorded answers when spent; message length cap; model/model options never taken from the client; error texts leak nothing; security headers (CSP incl. `frame-ancestors 'none'`, X-Frame-Options, Referrer-Policy, nosniff); `robots.txt` disallow; dev/test pages 404; footer "school project / not affiliated with ". Client IP from `CF-Connecting-IP` only, so the app must listen on `127.0.0.1` only. One server process per quota file. 2. **Tunnel (coordinator, with the DNS-capable Cloudflare key; never print it):** create a remotely managed tunnel over the API (`POST /accounts//cfd_tunnel` with `config_src: cloudflare` and a random `tunnel_secret`), `PUT .../configurations` with exactly one ingress rule `. -> http://127.0.0.1:` plus a final `http_status:404`, fetch `.../token` into `~/./tunnel.token` (mode 600). DNS: `CNAME -> .cfargotunnel.com`, proxied. Cloudflare Access may be disabled on the account; the app gate is then the only gate. 3. **Run:** `cloudflared tunnel --no-autoupdate run --token "$(cat token)"`. For persistence across reboots, write the LaunchAgent plist (RunAtLoad/KeepAlive) and give the user the one-line `launchctl bootstrap gui/$(id -u) ` to run in their own Terminal; installing a persistent service is the user's decision. 4. **Security review before giving out the code** (by a security reviewer agent or person; context only: first release, what is exposed, form = access code + free-text questions to the model). Two findings recur: - Privacy text must match reality: if the model endpoint logs prompts locally and sends them to a provider, say so instead of "not stored". - An agent-backed model endpoint can pick up instruction files from its working directory and pass them to the model with every public question. Run it from an empty working directory with all tools disabled. 5. **External check after the tunnel is up (from the public URL, not localhost):** without code `/`, `/api/chat`, `/.env.local`, `/v1/models` -> 401; another subdomain/path reaches nothing; N wrong codes -> 429 on attempt N+1 (proves the real IP is read; wait out the window before testing the right code); with code all pages 200 and a real live question answers. Report results back into the security report, then hand the user URL + code: sharing the code is the user's release decision. 6. **Fast failure on provider limits:** set `agent.api_max_retries: 1` in the model-endpoint profile's `config.yaml` (default 3 with rate-limit waits up to 600 s leaves testers watching a spinner for minutes). ## Updating the live version without breaking it Never run `npm run build` in the folder a running `next start` serves: `.next` is replaced underneath it and every page's JS chunks start returning 500 for the whole build. Release recipe (a few seconds of downtime, no 500 window): 1. Merge the finished branches into `main` in the main checkout, resolve conflicts there, `npx tsc --noEmit` (type check writes nothing to `.next`). 2. `git worktree add --detach .worktrees/yayinN main` (`main` is already checked out, so a plain add fails), then clone dependencies from the currently serving worktree with APFS copy-on-write: `cp -c -R /node_modules .worktrees/yayinN/` (instant, no extra disk) and `cp -c /.env.local` (compare env files with `cmp -s`, never print them). 3. `npm run build` there, start it on a spare port, curl every route plus one real assistant request (e.g. the table question that was broken) before touching the live ports. 4. Switch each live server: take the PID from `lsof -tiTCP: -sTCP:LISTEN`, kill it, and `exec` the new server from the new worktree as a `persist_on_release` background process with the same env (secrets via `"$(cat )"`). If the public/local difference is a runtime env flag, one build serves both ports. 5. Re-run the external check (gated routes 401 without code, landing 200) and the tailnet URL. ## Adding or renaming a hostname on the same tunnel Keep the old hostname working until shared links have moved. GET `.../cfd_tunnel//configurations`, insert a copy of the existing rule with the new `hostname` before the final `http_status:404`, PUT it back; then create `CNAME -> .cfargotunnel.com` (proxied). Tunnel config and DNS can need different keys: probe which assigned key can read `zones//dns_records` before concluding DNS is blocked. Verify both hostnames (landing 200, gated routes 401). A 301 from the old name is a later, separate step. ## Public-facing landing next to a gated app Let only the landing route and exactly the build assets it uses through the gate (derive the list from the build output; everything else stays 401), redirect a code-less `/` to it, and point "Open app" at the code screen with a validated same-origin `next=` path (reject absolute URLs, `//`, `/\`, `/api/`). For an always-on copy, export the landing as a static folder under the host site's base path (no React needed) so it survives the Mac being off.