Skip to content

fix(gateway): send sign-in to den-web and approve the gateway return URL without an org - #3207

Merged
benjaminshafii merged 2 commits into
devfrom
fix/gateway-handoff-return
Jul 27, 2026
Merged

fix(gateway): send sign-in to den-web and approve the gateway return URL without an org#3207
benjaminshafii merged 2 commits into
devfrom
fix/gateway-handoff-return

Conversation

@benjaminshafii

Copy link
Copy Markdown
Member

Two defects found by driving the real sign-in flow against the deployed gateway. Both block sign-in entirely; neither was caught by unit tests.

1. Sign-in pointed at the gateway instead of den-web

resolveRequestBaseUrl routes every /api/* path to baseUrl, and gateway mode set baseUrl to the gateway origin — but the gateway only proxies /api/den/*. Measured on the live gateway:

Request Gateway den-web
/api/auth/get-session 200 <!doctype html> 200 null

So better-auth calls received SPA HTML, and the sign-in button navigated to the gateway origin instead of the Den sign-in UI.

Now the gateway's same-origin override is scoped to the Den API only: apiBaseUrl = <gatewayOrigin>/api/den, while baseUrl stays the configured Den web origin (already defaults to https://app.openworklabs.com). This required resolveDenBaseUrls to stop forcing apiBaseUrl to be derived from baseUrl; non-gateway callers that pass no apiBaseUrl still derive it exactly as before. Note this is a deliberate contract change: an explicitly passed apiBaseUrl is now honored rather than ignored.

Verified on the deployed gateway — the button now opens:

https://app.openworklabs.com/?mode=sign-up&webAuth=1&webAuthReturn=https%3A%2F%2Fgateway-rlch.onrender.com

2. The gateway return URL could never be approved for a new user

resolveApprovedWebHandoffReturnUrl returned early when the session had no activeOrganizationId, before ever consulting DEN_GATEWAY_ORIGIN. That guard is right for signed preview origins, which are per-organization, but DEN_GATEWAY_ORIGIN is a single operator-configured deployment constant. A freshly signed-up user has no active org yet, so sign-in through the gateway always failed with "The Cloud web handoff return URL is not approved for this organization."

Reproduced end to end in production: signed up ben+cloudgw@openworklabs.com on den-web, entered the emailed code, reached "Signed in as ..." and then hit exactly that error.

The configured gateway origin is now matched independently of the org. The signed-preview path is unchanged and still requires multi_org, an active organization, and per-org signed preview URLs. Still exact-origin only, no wildcards; unset DEN_GATEWAY_ORIGIN approves nothing. This is safe because the origin is operator-configured rather than user input, and approving a redirect target grants nothing by itself — the Cloud gate still independently requires capabilities.cloud === true.

Why unit tests missed both

The slice-3 tests called approveWebHandoffReturnUrl with gatewayOrigin passed in directly, so they never exercised the wrapper that computes it. The new test asserts the wrapper approves the gateway origin with activeOrganizationId: null, and it fails on the pre-fix code:

(fail) approves the configured gateway web returnUrl without an active organization
Expected: "https://web.openworklabs.com/signin"
Received: null

Tests

Command Result
cd apps/app && bun test --isolate tests/ 472 pass / 0 fail
den-api desktop-handoff-public-url + cloud-instance-route 35 pass / 0 fail
pnpm --filter @openwork-ee/den-gateway run test 13 pass / 0 fail
tsc --noEmit (app, den-api) 0 errors

Added coverage: gateway baseUrl/apiBaseUrl split, auth URL origin + webAuthReturn, /api/auth vs /v1 request routing, gateway MCP URL, and the handoff approvals above.

Not verified yet

The full signed-in journey (instance provisioning, the Cloud capability gate in the UI) is still unproven — it was blocked behind these two bugs. I am continuing that against the deployed gateway now and will post the frames.

@vercel

vercel Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
openwork-app Ready Ready Preview, Comment Jul 27, 2026 9:40pm
openwork-den Ready Ready Preview, Comment Jul 27, 2026 9:40pm
openwork-den-worker-proxy Ready Ready Preview, Comment Jul 27, 2026 9:40pm
openwork-landing Ready Ready Preview, Comment, Open in v0 Jul 27, 2026 9:40pm
1 Skipped Deployment
Project Deployment Actions Updated (UTC)
openwork-diagnostics Skipped Skipped Jul 27, 2026 9:40pm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant