first-referrer-cookie.test.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. import { describe, expect, it } from 'vitest'
  2. import {
  3. buildFirstReferrerData,
  4. FIRST_REFERRER_COOKIE_NAME,
  5. hasPaidSignals,
  6. isExternalReferrer,
  7. isOAuthRedirectReferrer,
  8. MW_DIAG_COOKIE_NAME,
  9. parseFirstReferrerCookie,
  10. parseMwDiagCookie,
  11. serializeFirstReferrerCookie,
  12. shouldRefreshCookie,
  13. } from './first-referrer-cookie'
  14. describe('first-referrer-cookie', () => {
  15. describe('isExternalReferrer', () => {
  16. it('returns false for briven domains', () => {
  17. expect(isExternalReferrer('https://supabase.com')).toBe(false)
  18. expect(isExternalReferrer('https://www.supabase.com')).toBe(false)
  19. expect(isExternalReferrer('https://docs.supabase.com')).toBe(false)
  20. })
  21. it('returns true for external domains', () => {
  22. expect(isExternalReferrer('https://google.com')).toBe(true)
  23. expect(isExternalReferrer('https://chatgpt.com')).toBe(true)
  24. })
  25. it('returns true for http:// referrers', () => {
  26. expect(isExternalReferrer('http://google.com')).toBe(true)
  27. expect(isExternalReferrer('http://example.org/page')).toBe(true)
  28. })
  29. it('returns false for invalid values', () => {
  30. expect(isExternalReferrer('')).toBe(false)
  31. expect(isExternalReferrer('not-a-url')).toBe(false)
  32. })
  33. })
  34. describe('isOAuthRedirectReferrer', () => {
  35. // Google SSO — block entire domain
  36. it('returns true for accounts.google.com (bare)', () => {
  37. expect(isOAuthRedirectReferrer('https://accounts.google.com/')).toBe(true)
  38. })
  39. it('returns true for accounts.google.com with path', () => {
  40. expect(
  41. isOAuthRedirectReferrer('https://accounts.google.com/o/oauth2/auth?client_id=abc')
  42. ).toBe(true)
  43. })
  44. // GitHub OAuth — block bare domain only
  45. it('returns true for bare github.com/', () => {
  46. expect(isOAuthRedirectReferrer('https://github.com/')).toBe(true)
  47. })
  48. it('returns true for bare github.com (no trailing slash)', () => {
  49. expect(isOAuthRedirectReferrer('https://github.com')).toBe(true)
  50. })
  51. // GitHub genuine referrals — preserve these
  52. it('returns false for github.com with repo path', () => {
  53. expect(isOAuthRedirectReferrer('https://github.com/supabase/supabase')).toBe(false)
  54. })
  55. it('returns false for github.com with README path', () => {
  56. expect(
  57. isOAuthRedirectReferrer('https://github.com/supabase/supabase?tab=readme-ov-file')
  58. ).toBe(false)
  59. })
  60. it('returns false for github.com with discussion path', () => {
  61. expect(isOAuthRedirectReferrer('https://github.com/orgs/supabase/discussions/42949')).toBe(
  62. false
  63. )
  64. })
  65. it('returns false for github.com with blob path', () => {
  66. expect(
  67. isOAuthRedirectReferrer('https://github.com/supabase/supabase/blob/master/README.md')
  68. ).toBe(false)
  69. })
  70. // GitHub OAuth explicit path (rare, but should still be caught)
  71. it('returns true for github.com/login/oauth/authorize', () => {
  72. expect(
  73. isOAuthRedirectReferrer('https://github.com/login/oauth/authorize?client_id=abc')
  74. ).toBe(true)
  75. })
  76. // Non-OAuth domains — should not match
  77. it('returns false for google.com (search)', () => {
  78. expect(isOAuthRedirectReferrer('https://www.google.com/')).toBe(false)
  79. })
  80. it('returns false for claude.ai', () => {
  81. expect(isOAuthRedirectReferrer('https://claude.ai/')).toBe(false)
  82. })
  83. it('returns false for empty string', () => {
  84. expect(isOAuthRedirectReferrer('')).toBe(false)
  85. })
  86. it('returns false for malformed URL', () => {
  87. expect(isOAuthRedirectReferrer('not-a-url')).toBe(false)
  88. })
  89. })
  90. describe('buildFirstReferrerData', () => {
  91. it('handles malformed landing URL gracefully', () => {
  92. const data = buildFirstReferrerData({
  93. referrer: 'https://google.com',
  94. landingUrl: 'not-a-valid-url',
  95. })
  96. expect(data.referrer).toBe('https://google.com')
  97. expect(data.landing_url).toBe('not-a-valid-url')
  98. expect(data.utms).toEqual({})
  99. expect(data.click_ids).toEqual({})
  100. })
  101. it('extracts utm and click-id params from landing url', () => {
  102. const data = buildFirstReferrerData({
  103. referrer: 'https://www.google.com/',
  104. landingUrl:
  105. 'https://supabase.com/pricing?utm_source=google&utm_medium=cpc&utm_campaign=test&gclid=abc123&msclkid=xyz456',
  106. })
  107. expect(data.referrer).toBe('https://www.google.com/')
  108. expect(data.landing_url).toBe(
  109. 'https://supabase.com/pricing?utm_source=google&utm_medium=cpc&utm_campaign=test&gclid=abc123&msclkid=xyz456'
  110. )
  111. expect(data.utms).toEqual({
  112. utm_source: 'google',
  113. utm_medium: 'cpc',
  114. utm_campaign: 'test',
  115. })
  116. expect(data.click_ids).toEqual({
  117. gclid: 'abc123',
  118. msclkid: 'xyz456',
  119. })
  120. })
  121. })
  122. describe('serialize / parse', () => {
  123. it('round-trips valid cookie payloads', () => {
  124. const input = buildFirstReferrerData({
  125. referrer: 'https://www.google.com/',
  126. landingUrl: 'https://supabase.com/pricing?utm_source=google',
  127. })
  128. const encoded = serializeFirstReferrerCookie(input)
  129. const parsed = parseFirstReferrerCookie(`${FIRST_REFERRER_COOKIE_NAME}=${encoded}`)
  130. expect(parsed).toEqual(input)
  131. })
  132. it('returns null for empty string', () => {
  133. expect(parseFirstReferrerCookie('')).toBeNull()
  134. })
  135. it('parses cookie from header with multiple cookies', () => {
  136. const input = buildFirstReferrerData({
  137. referrer: 'https://google.com/',
  138. landingUrl: 'https://supabase.com/',
  139. })
  140. const encoded = serializeFirstReferrerCookie(input)
  141. const header = `session=abc123; ${FIRST_REFERRER_COOKIE_NAME}=${encoded}; theme=dark`
  142. expect(parseFirstReferrerCookie(header)).toEqual(input)
  143. })
  144. it('returns null for malformed json', () => {
  145. expect(parseFirstReferrerCookie(`${FIRST_REFERRER_COOKIE_NAME}=%7Bnot-json`)).toBeNull()
  146. })
  147. it('returns null for invalid payload shape', () => {
  148. const encoded = encodeURIComponent(JSON.stringify({ foo: 'bar' }))
  149. expect(parseFirstReferrerCookie(`${FIRST_REFERRER_COOKIE_NAME}=${encoded}`)).toBeNull()
  150. })
  151. it('parses double-encoded cookies (legacy format before serializer fix)', () => {
  152. const input = buildFirstReferrerData({
  153. referrer: 'https://www.google.com/',
  154. landingUrl: 'https://supabase.com/pricing?utm_source=google',
  155. })
  156. // Simulate the old double-encoding: encodeURIComponent(JSON.stringify(data))
  157. // followed by Next.js cookies.set() encoding it again.
  158. const doubleEncoded = encodeURIComponent(encodeURIComponent(JSON.stringify(input)))
  159. const parsed = parseFirstReferrerCookie(`${FIRST_REFERRER_COOKIE_NAME}=${doubleEncoded}`)
  160. expect(parsed).toEqual(input)
  161. })
  162. it('drops non-string values in utms/click_ids', () => {
  163. const encoded = encodeURIComponent(
  164. JSON.stringify({
  165. referrer: 'https://www.google.com/',
  166. landing_url: 'https://supabase.com/pricing',
  167. utms: { utm_source: 'google', utm_medium: 123 },
  168. click_ids: { gclid: 'abc', msclkid: null },
  169. ts: 123,
  170. })
  171. )
  172. const parsed = parseFirstReferrerCookie(`${FIRST_REFERRER_COOKIE_NAME}=${encoded}`)
  173. expect(parsed).toEqual({
  174. referrer: 'https://www.google.com/',
  175. landing_url: 'https://supabase.com/pricing',
  176. utms: { utm_source: 'google' },
  177. click_ids: { gclid: 'abc' },
  178. ts: 123,
  179. })
  180. })
  181. })
  182. describe('hasPaidSignals', () => {
  183. it('detects click IDs', () => {
  184. expect(hasPaidSignals(new URL('https://supabase.com/?gclid=abc'))).toBe(true)
  185. expect(hasPaidSignals(new URL('https://supabase.com/?fbclid=abc'))).toBe(true)
  186. expect(hasPaidSignals(new URL('https://supabase.com/?msclkid=abc'))).toBe(true)
  187. expect(hasPaidSignals(new URL('https://supabase.com/?gbraid=abc'))).toBe(true)
  188. expect(hasPaidSignals(new URL('https://supabase.com/?wbraid=abc'))).toBe(true)
  189. expect(hasPaidSignals(new URL('https://supabase.com/?rdt_cid=abc'))).toBe(true)
  190. expect(hasPaidSignals(new URL('https://supabase.com/?ttclid=abc'))).toBe(true)
  191. expect(hasPaidSignals(new URL('https://supabase.com/?twclid=abc'))).toBe(true)
  192. expect(hasPaidSignals(new URL('https://supabase.com/?li_fat_id=abc'))).toBe(true)
  193. })
  194. it('detects paid utm_medium values', () => {
  195. expect(hasPaidSignals(new URL('https://supabase.com/?utm_medium=cpc'))).toBe(true)
  196. expect(hasPaidSignals(new URL('https://supabase.com/?utm_medium=ppc'))).toBe(true)
  197. expect(hasPaidSignals(new URL('https://supabase.com/?utm_medium=paid_search'))).toBe(true)
  198. expect(hasPaidSignals(new URL('https://supabase.com/?utm_medium=paidsocial'))).toBe(true)
  199. expect(hasPaidSignals(new URL('https://supabase.com/?utm_medium=paid_social'))).toBe(true)
  200. expect(hasPaidSignals(new URL('https://supabase.com/?utm_medium=display'))).toBe(true)
  201. })
  202. it('is case-insensitive for utm_medium', () => {
  203. expect(hasPaidSignals(new URL('https://supabase.com/?utm_medium=CPC'))).toBe(true)
  204. expect(hasPaidSignals(new URL('https://supabase.com/?utm_medium=Paid_Search'))).toBe(true)
  205. })
  206. it('returns false for organic traffic', () => {
  207. expect(hasPaidSignals(new URL('https://supabase.com/'))).toBe(false)
  208. expect(hasPaidSignals(new URL('https://supabase.com/?utm_source=google'))).toBe(false)
  209. expect(hasPaidSignals(new URL('https://supabase.com/?utm_medium=email'))).toBe(false)
  210. expect(hasPaidSignals(new URL('https://supabase.com/?utm_medium=organic'))).toBe(false)
  211. })
  212. })
  213. describe('parseMwDiagCookie', () => {
  214. it('parses well-formed value with would_stamp=1 and has_cookie=0', () => {
  215. const header = `${MW_DIAG_COOKIE_NAME}=hit=1&would_stamp=1&has_cookie=0`
  216. expect(parseMwDiagCookie(header)).toEqual({
  217. hit: true,
  218. would_stamp: true,
  219. has_existing_cookie: false,
  220. })
  221. })
  222. it('parses well-formed value with would_stamp=0 and has_cookie=1', () => {
  223. const header = `${MW_DIAG_COOKIE_NAME}=hit=1&would_stamp=0&has_cookie=1`
  224. expect(parseMwDiagCookie(header)).toEqual({
  225. hit: true,
  226. would_stamp: false,
  227. has_existing_cookie: true,
  228. })
  229. })
  230. it('returns null when hit=0', () => {
  231. const header = `${MW_DIAG_COOKIE_NAME}=hit=0&would_stamp=1&has_cookie=1`
  232. expect(parseMwDiagCookie(header)).toBeNull()
  233. })
  234. it('returns object with would_stamp: false when would_stamp key is missing', () => {
  235. const header = `${MW_DIAG_COOKIE_NAME}=hit=1&has_cookie=1`
  236. expect(parseMwDiagCookie(header)).toEqual({
  237. hit: true,
  238. would_stamp: false,
  239. has_existing_cookie: true,
  240. })
  241. })
  242. it('returns object with has_existing_cookie: false when has_cookie key is missing', () => {
  243. const header = `${MW_DIAG_COOKIE_NAME}=hit=1&would_stamp=1`
  244. expect(parseMwDiagCookie(header)).toEqual({
  245. hit: true,
  246. would_stamp: true,
  247. has_existing_cookie: false,
  248. })
  249. })
  250. it('returns null for empty string input', () => {
  251. expect(parseMwDiagCookie('')).toBeNull()
  252. })
  253. it('returns null when cookie is not present in header', () => {
  254. expect(parseMwDiagCookie('session=abc123; theme=dark')).toBeNull()
  255. })
  256. it('returns null for garbage value', () => {
  257. const header = `${MW_DIAG_COOKIE_NAME}=not-a-valid-query-string`
  258. expect(parseMwDiagCookie(header)).toBeNull()
  259. })
  260. it('parses correct cookie from header with multiple cookies', () => {
  261. const header = `session=abc123; ${MW_DIAG_COOKIE_NAME}=hit=1&would_stamp=1&has_cookie=0; theme=dark`
  262. expect(parseMwDiagCookie(header)).toEqual({
  263. hit: true,
  264. would_stamp: true,
  265. has_existing_cookie: false,
  266. })
  267. })
  268. it('parses URL-encoded value from Next.js response.cookies.set()', () => {
  269. const header = `${MW_DIAG_COOKIE_NAME}=hit%3D1%26would_stamp%3D1%26has_cookie%3D0`
  270. expect(parseMwDiagCookie(header)).toEqual({
  271. hit: true,
  272. would_stamp: true,
  273. has_existing_cookie: false,
  274. })
  275. })
  276. it('parses URL-encoded value with has_cookie=1', () => {
  277. const header = `${MW_DIAG_COOKIE_NAME}=hit%3D1%26would_stamp%3D0%26has_cookie%3D1`
  278. expect(parseMwDiagCookie(header)).toEqual({
  279. hit: true,
  280. would_stamp: false,
  281. has_existing_cookie: true,
  282. })
  283. })
  284. it('parses URL-encoded value among multiple cookies', () => {
  285. const header = `session=abc; ${MW_DIAG_COOKIE_NAME}=hit%3D1%26would_stamp%3D0%26has_cookie%3D0; theme=dark`
  286. expect(parseMwDiagCookie(header)).toEqual({
  287. hit: true,
  288. would_stamp: false,
  289. has_existing_cookie: false,
  290. })
  291. })
  292. })
  293. describe('shouldRefreshCookie', () => {
  294. it('stamps when no cookie and external referrer', () => {
  295. expect(
  296. shouldRefreshCookie(false, {
  297. referrer: 'https://google.com',
  298. url: 'https://supabase.com/',
  299. })
  300. ).toEqual({ stamp: true })
  301. })
  302. it('skips when no cookie and internal referrer', () => {
  303. expect(
  304. shouldRefreshCookie(false, {
  305. referrer: 'https://supabase.com/docs',
  306. url: 'https://supabase.com/dashboard',
  307. })
  308. ).toEqual({ stamp: false })
  309. })
  310. it('skips when cookie exists and no paid signals', () => {
  311. expect(
  312. shouldRefreshCookie(true, {
  313. referrer: 'https://google.com',
  314. url: 'https://supabase.com/',
  315. })
  316. ).toEqual({ stamp: false })
  317. })
  318. it('refreshes when cookie exists but URL has paid signals', () => {
  319. expect(
  320. shouldRefreshCookie(true, {
  321. referrer: 'https://google.com',
  322. url: 'https://supabase.com/?gclid=abc123',
  323. })
  324. ).toEqual({ stamp: true })
  325. expect(
  326. shouldRefreshCookie(true, {
  327. referrer: 'https://google.com',
  328. url: 'https://supabase.com/?utm_medium=cpc&utm_source=google',
  329. })
  330. ).toEqual({ stamp: true })
  331. })
  332. it('skips when no cookie and no referrer (direct navigation)', () => {
  333. expect(shouldRefreshCookie(false, { referrer: '', url: 'https://supabase.com/' })).toEqual({
  334. stamp: false,
  335. })
  336. })
  337. it('handles malformed URL gracefully', () => {
  338. expect(
  339. shouldRefreshCookie(true, {
  340. referrer: 'https://google.com',
  341. url: 'not-a-valid-url',
  342. })
  343. ).toEqual({ stamp: false })
  344. })
  345. it('does not stamp for GitHub OAuth redirect (bare domain)', () => {
  346. const result = shouldRefreshCookie(false, {
  347. referrer: 'https://github.com/',
  348. url: 'https://supabase.com/dashboard',
  349. })
  350. expect(result.stamp).toBe(false)
  351. })
  352. it('does not stamp for Google SSO redirect', () => {
  353. const result = shouldRefreshCookie(false, {
  354. referrer: 'https://accounts.google.com/',
  355. url: 'https://supabase.com/dashboard',
  356. })
  357. expect(result.stamp).toBe(false)
  358. })
  359. it('still stamps for genuine GitHub referral with path', () => {
  360. const result = shouldRefreshCookie(false, {
  361. referrer: 'https://github.com/supabase/supabase?tab=readme-ov-file',
  362. url: 'https://supabase.com/',
  363. })
  364. expect(result.stamp).toBe(true)
  365. })
  366. it('still re-stamps existing cookie for paid signals regardless of OAuth referrer', () => {
  367. const result = shouldRefreshCookie(true, {
  368. referrer: 'https://github.com/',
  369. url: 'https://supabase.com/pricing?gclid=abc123',
  370. })
  371. expect(result.stamp).toBe(true)
  372. })
  373. })
  374. })