Skip to content

fix(middleware): validate ref query param before storing as referral_code cookie#78

Open
forgou37 wants to merge 1 commit into
profullstack:masterfrom
forgou37:fix/ref-cookie-validation
Open

fix(middleware): validate ref query param before storing as referral_code cookie#78
forgou37 wants to merge 1 commit into
profullstack:masterfrom
forgou37:fix/ref-cookie-validation

Conversation

@forgou37
Copy link
Copy Markdown

@forgou37 forgou37 commented Jun 7, 2026

Problem

proxy.ts stores the raw ?ref= query parameter into a referral_code cookie with no validation:

const ref = request.nextUrl.searchParams.get('ref');
if (ref) {
  response.cookies.set('referral_code', ref, { ... });
}

Two issues:

  1. Referral fraud — any value can be injected via a crafted URL, letting an attacker claim credits for any referrer code they choose.
  2. Cookie overflow — an arbitrarily long ?ref= value bloats the cookie header. Browsers cap total cookie size per domain (~4 KB); a large referral_code value can cause the sb-auth-token auth cookie to be dropped, logging users out.

Fix

Adds a strict allowlist regex (/^[a-zA-Z0-9_-]{1,64}$/) before writing the cookie. Values that do not match are silently ignored — same UX for legitimate users, attack surface closed.

Test

# Valid ref — stored
curl -I 'https://your-domain.com/?ref=promo2026'

# Invalid ref — ignored
curl -I 'https://your-domain.com/?ref=../../../etc/passwd'
curl -I "https://your-domain.com/?ref=$(python3 -c 'print("a"*5000)')"

…code cookie

Without validation an attacker can craft a URL with an arbitrary ?ref= value
to inject a fake referral code (referral fraud) or send an oversized value
that bloats the cookie header and risks dropping the auth cookie.

Add a strict alphanumeric + hyphen/underscore allowlist capped at 64 characters.
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