-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(clerk-js): Set localhost SameSite=None cookies as Secure (#3604)
This change removes the specific fix we did a while ago for Cypress. The real reason our cookies where getting deleted in Cypress Chrome is because Chrome requires Secure when setting SameSite=None cookies. Safari needs a bit of a different handling because it does not consider localhost to be a secure context, so in Safari localhost cookies cannot have the Secure attribute. So the new logic about the Secure attribute on cookies goes as follows: - If you are in a https location then add Secure on cookies. - If you are not in https: 1. If the browser is Safari, then do not add Secure (safari considers secure only https origins). 2. If the window.isSecureContext property exists, then respect it (safari does not work correctly with this, that's why we added a condition before this) 3. Lastly, if the window location is localhost and SameSite is set to None, add the Secure attribute (that's required by Chrome)
- Loading branch information
Showing
7 changed files
with
136 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@clerk/clerk-js": patch | ||
--- | ||
|
||
Set the localhost cookies with the Secure attribute |
94 changes: 94 additions & 0 deletions
94
packages/clerk-js/src/core/auth/__tests__/getSecureAttribute.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { getSecureAttribute } from '../getSecureAttribute'; | ||
|
||
describe('getSecureAttribute', () => { | ||
let windowSpy: any; | ||
|
||
beforeEach(() => { | ||
windowSpy = jest.spyOn(window, 'window', 'get'); | ||
}); | ||
|
||
afterEach(() => { | ||
windowSpy.mockRestore(); | ||
}); | ||
|
||
it('returns true if the protocol is https', () => { | ||
windowSpy.mockImplementation(() => ({ | ||
location: new URL('https://www.clerk.com'), | ||
safari: undefined, | ||
isSecureContext: undefined, | ||
})); | ||
expect(getSecureAttribute('None')).toBe(true); | ||
}); | ||
|
||
it('returns false if the protocol is not https and the SameSite is not None', () => { | ||
windowSpy.mockImplementation(() => ({ | ||
location: new URL('http://www.clerk.com'), | ||
safari: undefined, | ||
isSecureContext: undefined, | ||
})); | ||
expect(getSecureAttribute('Lax')).toBe(false); | ||
}); | ||
|
||
it('returns false if the protocol is not https and the browser is Safari', () => { | ||
windowSpy.mockImplementation(() => ({ | ||
location: new URL('http://www.clerk.com'), | ||
safari: { dummyValue: true }, | ||
isSecureContext: undefined, | ||
})); | ||
expect(getSecureAttribute('None')).toBe(false); | ||
}); | ||
|
||
it('returns true if isSecureContext is true', () => { | ||
windowSpy.mockImplementation(() => ({ | ||
location: new URL('http://www.clerk.com'), | ||
safari: undefined, | ||
isSecureContext: true, | ||
})); | ||
expect(getSecureAttribute('None')).toBe(true); | ||
}); | ||
|
||
it('returns false if isSecureContext is false', () => { | ||
windowSpy.mockImplementation(() => ({ | ||
location: new URL('http://www.clerk.com'), | ||
safari: undefined, | ||
isSecureContext: false, | ||
})); | ||
expect(getSecureAttribute('None')).toBe(false); | ||
}); | ||
|
||
it('returns true if the protocol is http and the hostname is localhost and sameSite is None', () => { | ||
windowSpy.mockImplementation(() => ({ | ||
location: new URL('http://localhost'), | ||
safari: undefined, | ||
isSecureContext: undefined, | ||
})); | ||
expect(getSecureAttribute('None')).toBe(true); | ||
}); | ||
|
||
it('returns false if the protocol is http and the hostname is localhost and sameSite is not None', () => { | ||
windowSpy.mockImplementation(() => ({ | ||
location: new URL('http://localhost'), | ||
safari: undefined, | ||
isSecureContext: undefined, | ||
})); | ||
expect(getSecureAttribute('Lax')).toBe(false); | ||
}); | ||
|
||
it('returns false if the protocol is http and the hostname is not localhost', () => { | ||
windowSpy.mockImplementation(() => ({ | ||
location: new URL('http://www.clerk.com'), | ||
safari: undefined, | ||
isSecureContext: undefined, | ||
})); | ||
expect(getSecureAttribute('None')).toBe(false); | ||
}); | ||
|
||
it('returns false in case window.safari is undefined and isSecureContext is true on localhost for Lax cookie', () => { | ||
windowSpy.mockImplementation(() => ({ | ||
location: new URL('http://localhost'), | ||
safari: undefined, | ||
isSecureContext: true, | ||
})); | ||
expect(getSecureAttribute('Lax')).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
type SameSiteAttributeType = 'None' | 'Lax' | 'Strict'; | ||
|
||
// The Secure attribute is required for SameSite=None on Chrome and Firefox | ||
// Also, localhost is considered secure for testing purposes by Chrome and Firefox | ||
// Safari does not support the Secure attribute on localhost, although it returns true for isSecureContext | ||
// ref: https://bugs.webkit.org/show_bug.cgi?id=232088#c8 | ||
export const getSecureAttribute = (sameSite: SameSiteAttributeType): boolean => { | ||
if (window.location.protocol === 'https:') { | ||
return true; | ||
} | ||
|
||
// If the cookie is not SameSite=None, then the Secure attribute is not required | ||
if (sameSite !== 'None') { | ||
return false; | ||
} | ||
|
||
// This is because Safari does not support the Secure attribute on localhost | ||
if (typeof (window as any).safari !== 'undefined') { | ||
return false; | ||
} | ||
|
||
// This is a useful way to check if the current context is secure | ||
// This is needed for example in environments like Firefox Remote Control | ||
// where the `localhost` is not considered secure | ||
if (typeof window.isSecureContext !== 'undefined') { | ||
return window.isSecureContext; | ||
} | ||
|
||
return window.location.hostname === 'localhost'; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters