Use current origin for referral links - #128
Conversation
Greptile SummaryThis PR fixes referral invite links so they are built from
Confidence Score: 3/5Safe to merge only if NEXT_PUBLIC_APP_URL is guaranteed to be set in every deployment; without it, the request-origin fallback exposes a host-header injection path. The core fix (URL-encoding the referral code, removing the hardcoded domain) is correct and well-tested. The risk is the intermediate fallback: using request.nextUrl.origin means any deployment that omits NEXT_PUBLIC_APP_URL can be induced — by a manipulated Host header — to return a referral link pointing at an attacker-controlled domain. That's a present defect on the changed code path, not a theoretical one. src/app/api/referrals/code/route.ts — specifically the base URL resolution at line 25.
|
| Filename | Overview |
|---|---|
| src/app/api/referrals/code/route.ts | Replaces hardcoded production URL with a dynamic base URL derived from NEXT_PUBLIC_APP_URL or request origin; adds encodeURIComponent on the referral code. The request-origin fallback is susceptible to Host-header injection. |
| src/app/api/referrals/code/route.test.ts | Adds env-var lifecycle management (beforeEach/afterEach) and a new test covering NEXT_PUBLIC_APP_URL preference and URL-encoding of referral codes. Cleanup logic is correct. |
Sequence Diagram
sequenceDiagram
participant Client
participant GET /api/referrals/code
participant Supabase
Client->>GET /api/referrals/code: GET (with Host header)
GET /api/referrals/code->>Supabase: select referral_code, username where id=user.id
Supabase-->>GET /api/referrals/code: { referral_code, username }
note over GET /api/referrals/code: baseUrl = NEXT_PUBLIC_APP_URL<br/>|| request.nextUrl.origin ⚠️<br/>|| "https://ugig.net"
GET /api/referrals/code-->>Client: { code, link: baseUrl + "/?ref=" + encodeURIComponent(code) }
Reviews (1): Last reviewed commit: "Use current origin for referral links" | Re-trigger Greptile
|
|
||
| const code = profile.referral_code || profile.username; | ||
| const baseUrl = ( | ||
| process.env.NEXT_PUBLIC_APP_URL || request.nextUrl.origin || "https://ugig.net" |
There was a problem hiding this comment.
Host-header injection in referral link base URL
When NEXT_PUBLIC_APP_URL is not set (e.g., a self-hosted or staging deployment without the env var configured), request.nextUrl.origin is derived directly from the incoming Host HTTP header. An authenticated user can craft a request with Host: evil.com, causing the API to return link: "https://evil.com/?ref=...". If that link is displayed in the UI or copied by the user and shared, it becomes a phishing vector under the guise of a legitimate referral invite.
The safest fix is to validate request.nextUrl.origin against a known allowlist, or to rely solely on NEXT_PUBLIC_APP_URL and the hardcoded production fallback — dropping the origin fallback entirely.
Summary
NEXT_PUBLIC_APP_URLor the current request origin instead of hardcoding productionFixes #127
Validation
pnpm test:run src/app/api/referrals/code/route.test.tspnpm type-checkpnpm exec eslint src/app/api/referrals/code/route.ts src/app/api/referrals/code/route.test.tsgit diff --checkBounty / payment
Submitted for the active uGig affiliate-program testing task. SOL receive address:
27sdMYXofqoM9qR13bZhccRNYeEgYn5EoHXTSJn4QWKP.Payment fallback: PayPal cultofrozen@gmail.com