Fix CoinPay OAuth origin fallback#237
Conversation
Greptile SummaryThis PR extracts a shared
Confidence Score: 5/5Safe to merge; the open-redirect vector from the previous review is correctly closed by the trustedOnly mode and VERCEL_URL fallback. The core logic change — replacing the bare request.nextUrl.origin fallback with a mode that rejects non-local, non-env-configured origins — correctly addresses the open-redirect concern. The redirect_uri computed at initiation and at token exchange will be consistent across all documented environment configurations. The one notable undocumented addition (APP_URL) is unlikely to cause problems in practice but deserves attention before the pattern spreads to other callers. src/lib/app-url.ts — the undocumented APP_URL env-var fallback and the absence of a test for that path. Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A["GET /api/auth/coinpay"] --> B["getAppUrl with trustedOnly"]
B --> C{Configured env var?}
C -- Yes --> D["Use configured URL"]
C -- No --> E{VERCEL_URL present?}
E -- Yes --> F["Use Vercel deployment URL"]
E -- No --> G{Request from localhost?}
G -- Yes --> H["Use request origin"]
G -- No --> I["Use default ugig.net"]
D & F & H & I --> J["Build redirectUri\nappUrl + /api/callback/oauth"]
J --> K["Redirect to CoinPay OAuth\nStore state cookie"]
K --> L["CoinPay redirects back"]
L --> M["GET /api/callback/oauth"]
M --> N["getAppUrl with trustedOnly\nsame resolution"]
N --> O["Token exchange with\nmatching redirectUri"]
O --> P{Success?}
P -- No --> Q["Redirect to login with error"]
P -- Yes --> R["Find or create user\nLink OAuth identity"]
R --> S["Generate magic link\nRedirect to auth confirm"]
Reviews (4): Last reviewed commit: "Harden CoinPay OAuth app URL fallback" | Re-trigger Greptile |
|
Addressed the Host-header redirect concern from the Greptile review in What changed:
Validation:
|
b5974d2 to
919fb52
Compare
Summary
NEXT_PUBLIC_APP_URLwhen configured, otherwise from the current request origin.Fixes #236.
uGig gig: https://ugig.net/gigs/abd6b2a0-e728-48cf-a46f-f99e419ed94e
Note: this replaces my closed duplicate PR #235; #235 overlapped with existing PR #128, so I closed it and moved to this non-overlapping bug/fix.
Validation
node_modules\.bin\vitest.cmd run src/app/api/auth/coinpay/route.test.ts src/app/api/callback/oauth/route.test.tsnode_modules\.bin\tsc.cmd --noEmitnode_modules\.bin\prettier.cmd --check src/app/api/auth/coinpay/route.ts src/app/api/auth/coinpay/route.test.ts src/app/api/callback/oauth/route.ts src/app/api/callback/oauth/route.test.tsgit diff --check -- src/app/api/auth/coinpay/route.ts src/app/api/auth/coinpay/route.test.ts src/app/api/callback/oauth/route.ts src/app/api/callback/oauth/route.test.tsNote:
corepack pnpm install --frozen-lockfilepopulated dependencies, but the repo postinstall script fails on this Windows shell because it invokes barepnpmand Unixtrue; direct local binaries were used for validation after install.