This template ships with secure defaults, but security is contextual. Use this as a baseline.
- Never commit
.dev.vars. - Use
wrangler secret bulkfor production secrets (ex:BETTER_AUTH_SECRET) viapnpm secrets:bulkwith CI-provided env vars or aDEV_VARSfile. - Validate env vars with Zod at runtime (see
apps/api/src/env.ts).
- Prefer same-origin proxy for cookie sessions:
- browser →
apps/web(public) → Service Binding →apps/api
- browser →
- The API Worker enforces an
Originallowlist for cookie-based write requests (POST|PUT|PATCH|DELETE):- default trusted origin:
new URL(BETTER_AUTH_URL).origin - add trusted origins via
AUTH_TRUSTED_ORIGINS(comma-separated)
- default trusted origin:
- Use bearer/JWT patterns only when you understand token storage risks.
- Validate all external input with Zod (JSON bodies, query params, route params, headers).
- Treat TypeScript as compile-time only; never trust unvalidated input at runtime.
- Avoid CORS when possible by using the same-origin proxy model.
- If you must enable CORS, use an allowlist; do not use
*with credentials.- In production,
apps/apirejectsCORS_ORIGINS=*at startup.
- In production,
- In production,
apps/apienablesglobal_fetch_strictly_publicso same-zonefetch()goes through the public edge and respects Workers/routes/security.
- KV is eventually consistent: treat it as a cache or best-effort secondary store.
- Do not use KV as a source of truth for locks, inventory, or counters.
- Use Durable Objects for strong consistency and coordination.
- Authenticate/authorize DO-backed endpoints.
- Keep DO state minimal; persist important state to D1.
- Responses include
x-request-id; error envelopes includerequestId. - Enable
observabilityandupload_source_mapsin production Workers.