-
Notifications
You must be signed in to change notification settings - Fork 2
fix referrer encoding #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
0fb5c73
04100dc
0dbd313
9dc4f41
f4a454d
6753e2a
85c0a2e
8b6f64f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,17 +36,13 @@ export async function proxy(request: NextRequest) { | |
| externalReferrer && | ||
| request.method === "GET" | ||
| ) { | ||
| response.cookies.set( | ||
| INITIAL_REFERRER_COOKIE, | ||
| encodeURIComponent(externalReferrer), | ||
| { | ||
| httpOnly: false, | ||
| maxAge: INITIAL_REFERRER_MAX_AGE, | ||
| path: "/", | ||
| sameSite: "lax", | ||
| secure: request.nextUrl.protocol === "https:", | ||
| } | ||
| ); | ||
| response.cookies.set(INITIAL_REFERRER_COOKIE, externalReferrer, { | ||
| httpOnly: false, | ||
| maxAge: INITIAL_REFERRER_MAX_AGE, | ||
| path: "/", | ||
| sameSite: "lax", | ||
| secure: request.nextUrl.protocol === "https:", | ||
| }); | ||
|
Comment on lines
+39
to
+45
|
||
| } | ||
|
|
||
| return response; | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,5 +1,7 @@ | ||||||||||||||||||||||||||||||||||||
| "use client"; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| import { normaliseReferrer } from "@/utils/referrer"; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
dnywh marked this conversation as resolved.
|
||||||||||||||||||||||||||||||||||||
| const UTM_STORAGE_KEY = "attribution_params"; | ||||||||||||||||||||||||||||||||||||
| const INITIAL_REFERRER_KEY = "initial_referrer"; | ||||||||||||||||||||||||||||||||||||
| const INITIAL_REFERRER_COOKIE = "initial_referrer"; | ||||||||||||||||||||||||||||||||||||
|
|
@@ -46,11 +48,7 @@ const getCookie = (name: string): string | null => { | |||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| if (!value) return null; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||
| return decodeURIComponent(value); | ||||||||||||||||||||||||||||||||||||
| } catch { | ||||||||||||||||||||||||||||||||||||
| return value; | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| return normaliseReferrer(value) ?? null; | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
| return normaliseReferrer(value) ?? null; | |
| const normalisedValue = normaliseReferrer(value); | |
| if (!normalisedValue.trim()) return null; | |
| return normalisedValue; |
Copilot
AI
Apr 19, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
storedInitialReferrer comes from localStorage.getItem(...) (type string | null), but the new conditional uses truthiness. This changes behavior vs the previous ?? logic: an empty string will now be treated as missing and fall back to the cookie. Use an explicit null check (e.g., storedInitialReferrer !== null) so empty-string values don’t change control flow unintentionally.
| const initialReferrer = storedInitialReferrer | |
| ? normaliseReferrer(storedInitialReferrer) | |
| : cookieReferrer; | |
| if (!storedInitialReferrer && initialReferrer) { | |
| storeInitialReferrer(initialReferrer); | |
| } else if ( | |
| storedInitialReferrer && | |
| const initialReferrer = | |
| storedInitialReferrer !== null | |
| ? normaliseReferrer(storedInitialReferrer) | |
| : cookieReferrer; | |
| if (storedInitialReferrer === null && initialReferrer) { | |
| storeInitialReferrer(initialReferrer); | |
| } else if ( | |
| storedInitialReferrer !== null && |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| const encodedReferrerPrefix = /^https?%(?:25)*3a%(?:25)*2f%(?:25)*2f/i; | ||
|
|
||
| export function normaliseReferrer(referrer: string): string; | ||
| export function normaliseReferrer(referrer: undefined): undefined; | ||
| export function normaliseReferrer( | ||
| referrer: string | undefined | ||
| ): string | undefined; | ||
| export function normaliseReferrer(referrer: string | undefined) { | ||
|
Comment on lines
+5
to
+10
|
||
| if (referrer === undefined) return undefined; | ||
|
|
||
| let current = referrer; | ||
|
|
||
| for (let i = 0; i < 3 && encodedReferrerPrefix.test(current); i += 1) { | ||
| try { | ||
| const decoded = decodeURIComponent(current); | ||
| if (decoded === current) break; | ||
| current = decoded; | ||
| } catch { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| return current; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
normaliseReferreris applied toformData.get("initial_referrer"), which is user-controllable. Because it can decode percent-encoded payloads, it can introduce control characters (e.g.%0d%0a) that then get logged and persisted to Supabase user metadata. Consider sanitizing the normalized value (strip control chars / enforce a max length) and/or validating it as anhttp(s)URL before storing/logging.