Skip to content

Commit b68bfaf

Browse files
authored
feat(den): support configurable Better Auth trusted origins (anomalyco#958)
* feat(den): support dedicated Better Auth trusted origins * fix(den): unify trusted origins env key naming
1 parent 3ae7735 commit b68bfaf

8 files changed

Lines changed: 31 additions & 4 deletions

File tree

‎.github/workflows/deploy-den.yml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ jobs:
101101
DEN_GOOGLE_CLIENT_ID: ${{ secrets.DEN_GOOGLE_CLIENT_ID }}
102102
DEN_GOOGLE_CLIENT_SECRET: ${{ secrets.DEN_GOOGLE_CLIENT_SECRET }}
103103
DEN_BETTER_AUTH_URL: ${{ vars.DEN_BETTER_AUTH_URL }}
104+
DEN_BETTER_AUTH_TRUSTED_ORIGINS: ${{ vars.DEN_BETTER_AUTH_TRUSTED_ORIGINS }}
104105
DEN_RENDER_WORKER_PLAN: ${{ vars.DEN_RENDER_WORKER_PLAN }}
105106
DEN_RENDER_WORKER_OPENWORK_VERSION: ${{ vars.DEN_RENDER_WORKER_OPENWORK_VERSION }}
106107
DEN_CORS_ORIGINS: ${{ vars.DEN_CORS_ORIGINS }}
@@ -152,6 +153,7 @@ jobs:
152153
google_client_id = os.environ.get("DEN_GOOGLE_CLIENT_ID") or ""
153154
google_client_secret = os.environ.get("DEN_GOOGLE_CLIENT_SECRET") or ""
154155
better_auth_url = os.environ.get("DEN_BETTER_AUTH_URL") or "https://app.openwork.software"
156+
configured_better_auth_trusted_origins = os.environ.get("DEN_BETTER_AUTH_TRUSTED_ORIGINS") or ""
155157
156158
if bool(github_client_id) != bool(github_client_secret):
157159
raise RuntimeError(
@@ -237,10 +239,16 @@ jobs:
237239
],
238240
)
239241
242+
better_auth_trusted_origins = build_cors_origins(
243+
configured_better_auth_trusted_origins,
244+
cors_origins.split(","),
245+
)
246+
240247
env_vars = [
241248
{"key": "DATABASE_URL", "value": os.environ["DEN_DATABASE_URL"]},
242249
{"key": "BETTER_AUTH_SECRET", "value": os.environ["DEN_BETTER_AUTH_SECRET"]},
243250
{"key": "BETTER_AUTH_URL", "value": better_auth_url},
251+
{"key": "DEN_BETTER_AUTH_TRUSTED_ORIGINS", "value": better_auth_trusted_origins},
244252
{"key": "GITHUB_CLIENT_ID", "value": github_client_id},
245253
{"key": "GITHUB_CLIENT_SECRET", "value": github_client_secret},
246254
{"key": "GOOGLE_CLIENT_ID", "value": google_client_id},

‎packaging/docker/README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ Optional env vars (via `.env` or `export`):
7878
- `DEN_BETTER_AUTH_SECRET` — Better Auth secret (auto-generated if unset)
7979
- `DEN_PUBLIC_HOST` — host name/IP used for default auth URL + printed LAN/public URLs (defaults to your machine hostname)
8080
- `DEN_BETTER_AUTH_URL` — browser-facing auth base URL (defaults to `http://$DEN_PUBLIC_HOST:<DEN_WEB_PORT>`)
81-
- `DEN_CORS_ORIGINS` — trusted origins for Better Auth + Express CORS (defaults include hostname, localhost, loopback, and detected LAN IPv4)
81+
- `DEN_BETTER_AUTH_TRUSTED_ORIGINS` — trusted origins for Better Auth (defaults to `DEN_CORS_ORIGINS`)
82+
- `DEN_CORS_ORIGINS` — trusted origins for Express CORS (defaults include hostname, localhost, loopback, and detected LAN IPv4)
8283
- `DEN_PROVISIONER_MODE` — `stub` or `render` (defaults to `stub`)
8384
- `DEN_WORKER_URL_TEMPLATE` — stub worker URL template with `{workerId}` placeholder
8485

‎packaging/docker/den-dev-up.sh‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ if [ -z "${DEN_CORS_ORIGINS:-}" ]; then
102102
"${LAN_IPV4:+http://$LAN_IPV4:$DEN_API_PORT}")"
103103
fi
104104

105+
DEN_BETTER_AUTH_TRUSTED_ORIGINS="${DEN_BETTER_AUTH_TRUSTED_ORIGINS:-$DEN_CORS_ORIGINS}"
106+
105107
mkdir -p "$RUNTIME_DIR"
106108
RUNTIME_FILE="$ROOT_DIR/tmp/.den-dev-env-$DEV_ID"
107109

@@ -120,13 +122,15 @@ echo "Starting Docker Compose project: $PROJECT" >&2
120122
echo "- DEN_API_PORT=$DEN_API_PORT" >&2
121123
echo "- DEN_WEB_PORT=$DEN_WEB_PORT" >&2
122124
echo "- DEN_BETTER_AUTH_URL=$DEN_BETTER_AUTH_URL" >&2
125+
echo "- DEN_BETTER_AUTH_TRUSTED_ORIGINS=$DEN_BETTER_AUTH_TRUSTED_ORIGINS" >&2
123126
echo "- DEN_CORS_ORIGINS=$DEN_CORS_ORIGINS" >&2
124127
echo "- DEN_PROVISIONER_MODE=$DEN_PROVISIONER_MODE" >&2
125128

126129
if ! DEN_API_PORT="$DEN_API_PORT" \
127130
DEN_WEB_PORT="$DEN_WEB_PORT" \
128131
DEN_BETTER_AUTH_SECRET="$DEN_BETTER_AUTH_SECRET" \
129132
DEN_BETTER_AUTH_URL="$DEN_BETTER_AUTH_URL" \
133+
DEN_BETTER_AUTH_TRUSTED_ORIGINS="$DEN_BETTER_AUTH_TRUSTED_ORIGINS" \
130134
DEN_CORS_ORIGINS="$DEN_CORS_ORIGINS" \
131135
DEN_PROVISIONER_MODE="$DEN_PROVISIONER_MODE" \
132136
DEN_WORKER_URL_TEMPLATE="$DEN_WORKER_URL_TEMPLATE" \

‎packaging/docker/docker-compose.den-dev.yml‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
# DEN_BETTER_AUTH_SECRET — Better Auth secret (auto-generated by den-dev-up.sh)
1212
# DEN_PUBLIC_HOST — host used to build default auth origin + LAN/public URLs
1313
# DEN_BETTER_AUTH_URL — browser-facing auth origin (default: http://<DEN_PUBLIC_HOST>:<DEN_WEB_PORT>)
14-
# DEN_CORS_ORIGINS — comma-separated trusted origins for Better Auth + CORS (auto-populated by den-dev-up.sh)
14+
# DEN_BETTER_AUTH_TRUSTED_ORIGINS — Better Auth trusted origins (defaults to DEN_CORS_ORIGINS)
15+
# DEN_CORS_ORIGINS — comma-separated trusted origins for Express CORS (auto-populated by den-dev-up.sh)
1516
# DEN_PROVISIONER_MODE — stub or render (default: stub)
1617
# DEN_WORKER_URL_TEMPLATE — worker URL template used by stub provisioning
1718

@@ -91,6 +92,7 @@ services:
9192
DATABASE_URL: mysql://root:password@mysql:3306/openwork_den
9293
BETTER_AUTH_SECRET: ${DEN_BETTER_AUTH_SECRET:-dev-den-local-auth-secret-please-override-1234567890}
9394
BETTER_AUTH_URL: ${DEN_BETTER_AUTH_URL:-http://localhost:3005}
95+
DEN_BETTER_AUTH_TRUSTED_ORIGINS: ${DEN_BETTER_AUTH_TRUSTED_ORIGINS:-${DEN_CORS_ORIGINS:-http://localhost:3005,http://127.0.0.1:3005,http://localhost:8788,http://127.0.0.1:8788}}
9496
PORT: "8788"
9597
CORS_ORIGINS: ${DEN_CORS_ORIGINS:-http://localhost:3005,http://127.0.0.1:3005,http://localhost:8788,http://127.0.0.1:8788}
9698
PROVISIONER_MODE: ${DEN_PROVISIONER_MODE:-stub}

‎services/den/.env.example‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
DATABASE_URL=
22
BETTER_AUTH_SECRET=
33
BETTER_AUTH_URL=http://localhost:8788
4+
DEN_BETTER_AUTH_TRUSTED_ORIGINS=http://localhost:3005,http://localhost:5173
45
GITHUB_CLIENT_ID=
56
GITHUB_CLIENT_SECRET=
67
GOOGLE_CLIENT_ID=

‎services/den/README.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@ The script prints the exact URLs and `docker compose ... down` command to use fo
3030
- `DATABASE_URL` MySQL connection URL
3131
- `BETTER_AUTH_SECRET` 32+ char secret
3232
- `BETTER_AUTH_URL` public base URL Better Auth uses for OAuth redirects and callbacks
33+
- `DEN_BETTER_AUTH_TRUSTED_ORIGINS` optional comma-separated trusted origins for Better Auth origin validation (defaults to `CORS_ORIGINS`)
3334
- `GITHUB_CLIENT_ID` optional OAuth app client ID for GitHub sign-in
3435
- `GITHUB_CLIENT_SECRET` optional OAuth app client secret for GitHub sign-in
3536
- `GOOGLE_CLIENT_ID` optional OAuth app client ID for Google sign-in
3637
- `GOOGLE_CLIENT_SECRET` optional OAuth app client secret for Google sign-in
3738
- `PORT` server port
38-
- `CORS_ORIGINS` comma-separated list of trusted browser origins (used for Better Auth origin validation + Express CORS)
39+
- `CORS_ORIGINS` comma-separated list of trusted browser origins for Express CORS
3940
- `PROVISIONER_MODE` `stub` or `render`
4041
- `WORKER_URL_TEMPLATE` template string with `{workerId}`
4142
- `RENDER_API_BASE` Render API base URL (default `https://api.render.com/v1`)
@@ -121,6 +122,7 @@ Optional GitHub Actions variable:
121122
- `DEN_RENDER_WORKER_PLAN` (defaults to `standard`)
122123
- `DEN_RENDER_WORKER_OPENWORK_VERSION` pins the `openwork-orchestrator` npm version installed in workers; the worker build bundles the matching `opencode` release asset into the Render image
123124
- `DEN_CORS_ORIGINS` (defaults to `https://app.openwork.software,https://api.openwork.software,<render-service-url>`)
125+
- `DEN_BETTER_AUTH_TRUSTED_ORIGINS` (defaults to `DEN_CORS_ORIGINS`)
124126
- `DEN_RENDER_WORKER_PUBLIC_DOMAIN_SUFFIX` (defaults to `openwork.studio`)
125127
- `DEN_RENDER_CUSTOM_DOMAIN_READY_TIMEOUT_MS` (defaults to `240000`)
126128
- `DEN_BETTER_AUTH_URL` (defaults to `https://app.openwork.software`)

‎services/den/src/auth.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const socialProviders = {
2727
export const auth = betterAuth({
2828
baseURL: env.betterAuthUrl,
2929
secret: env.betterAuthSecret,
30-
trustedOrigins: env.corsOrigins.length > 0 ? env.corsOrigins : undefined,
30+
trustedOrigins: env.betterAuthTrustedOrigins.length > 0 ? env.betterAuthTrustedOrigins : undefined,
3131
socialProviders: Object.keys(socialProviders).length > 0 ? socialProviders : undefined,
3232
database: drizzleAdapter(db, {
3333
provider: "mysql",

‎services/den/src/env.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const schema = z.object({
44
DATABASE_URL: z.string().min(1),
55
BETTER_AUTH_SECRET: z.string().min(32),
66
BETTER_AUTH_URL: z.string().min(1),
7+
DEN_BETTER_AUTH_TRUSTED_ORIGINS: z.string().optional(),
78
GITHUB_CLIENT_ID: z.string().optional(),
89
GITHUB_CLIENT_SECRET: z.string().optional(),
910
GOOGLE_CLIENT_ID: z.string().optional(),
@@ -55,13 +56,21 @@ const corsOrigins = parsed.CORS_ORIGINS?.split(",")
5556
.map((origin) => normalizeOrigin(origin))
5657
.filter(Boolean);
5758

59+
const betterAuthTrustedOrigins =
60+
parsed.DEN_BETTER_AUTH_TRUSTED_ORIGINS?.split(",")
61+
.map((origin) => normalizeOrigin(origin))
62+
.filter(Boolean) ??
63+
corsOrigins ??
64+
[];
65+
5866
const polarFeatureGateEnabled =
5967
(parsed.POLAR_FEATURE_GATE_ENABLED ?? "false").toLowerCase() === "true";
6068

6169
export const env = {
6270
databaseUrl: parsed.DATABASE_URL,
6371
betterAuthSecret: parsed.BETTER_AUTH_SECRET,
6472
betterAuthUrl: parsed.BETTER_AUTH_URL,
73+
betterAuthTrustedOrigins,
6574
github: {
6675
clientId: parsed.GITHUB_CLIENT_ID?.trim() || undefined,
6776
clientSecret: parsed.GITHUB_CLIENT_SECRET?.trim() || undefined,

0 commit comments

Comments
 (0)