-
Notifications
You must be signed in to change notification settings - Fork 437
fix(clerk-js): Prevent duplicate __client_uat cookies in iframe contexts #7875
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 all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@clerk/clerk-js': patch | ||
| --- | ||
|
|
||
| Fix `__client_uat` cookie being set on two different domain scopes when app is loaded in both iframe and non-iframe contexts. `getCookieDomain()` now falls back to `hostname` instead of `undefined` when the eTLD+1 probe fails, and the eTLD+1 probe uses the same `SameSite`/`Secure` attributes as the actual cookie to ensure consistent behavior across contexts. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,19 @@ import { createCookieHandler } from '@clerk/shared/cookie'; | |
| let cachedETLDPlusOne: string; | ||
| const eTLDCookie = createCookieHandler('__clerk_test_etld'); | ||
|
|
||
| export function getCookieDomain(hostname = window.location.hostname, cookieHandler = eTLDCookie) { | ||
| /** | ||
| * @param hostname - The hostname to determine the eTLD+1 for. | ||
| * @param cookieHandler - The cookie handler to use for the eTLD+1 probe. | ||
| * @param cookieAttributes - Optional cookie attributes (sameSite, secure) to use | ||
| * during the eTLD+1 probe. These should match the attributes that will be used | ||
| * when setting the actual cookie, so the probe accurately reflects whether a | ||
| * domain-scoped cookie can be set in the current context. | ||
| */ | ||
| export function getCookieDomain( | ||
| hostname = window.location.hostname, | ||
| cookieHandler = eTLDCookie, | ||
| cookieAttributes?: { sameSite?: string; secure?: boolean }, | ||
| ) { | ||
| // only compute it once per session to avoid unnecessary cookie ops | ||
| if (cachedETLDPlusOne) { | ||
| return cachedETLDPlusOne; | ||
|
|
@@ -28,17 +40,22 @@ export function getCookieDomain(hostname = window.location.hostname, cookieHandl | |
| // we know for sure that the first entry is definitely a TLD, skip it | ||
| for (let i = hostnameParts.length - 2; i >= 0; i--) { | ||
| const eTLD = hostnameParts.slice(i).join('.'); | ||
| cookieHandler.set('1', { domain: eTLD }); | ||
| cookieHandler.set('1', { ...cookieAttributes, domain: eTLD }); | ||
|
|
||
| const res = cookieHandler.get(); | ||
| if (res === '1') { | ||
| cookieHandler.remove({ domain: eTLD }); | ||
| cookieHandler.remove({ ...cookieAttributes, domain: eTLD }); | ||
| cachedETLDPlusOne = eTLD; | ||
| return eTLD; | ||
| } | ||
|
|
||
| cookieHandler.remove({ domain: eTLD }); | ||
| cookieHandler.remove({ ...cookieAttributes, domain: eTLD }); | ||
| } | ||
|
|
||
| return; | ||
| // Fallback to hostname to ensure domain-scoped cookie rather than host-only. | ||
| // In restricted contexts (e.g. cross-origin iframes), the set() will silently | ||
| // fail — which is preferable to creating a host-only cookie that conflicts | ||
| // with domain-scoped cookies set by non-iframe contexts. | ||
| cachedETLDPlusOne = hostname; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm. I think it makes sense, but I wonder if this could potentially set a cookie on a different subdomain that would still create 2 separate cookies in some cases. 🤔 |
||
| return hostname; | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
🧩 Analysis chain
🏁 Script executed:
Repository: clerk/javascript
Length of output: 2746
Add explicit return type and complete JSDoc documentation for the exported
getCookieDomainfunction.This public API lacks an explicit return type annotation. Its JSDoc documentation is also incomplete, missing
@returns,@throws, and@exampletags required by the TypeScript and JSDoc guidelines.Suggested patch
📝 Committable suggestion
🤖 Prompt for AI Agents