This repository was archived by the owner on Aug 29, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 72
feat: disable cookie access under restricted sandboxes #677
Merged
+33
−1
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
26d8641
feat: disable cookie access under restricted sandboxes
josegonzalez 76f4e14
Execute CookieStorage.enabled only once
b15a35a
changelog
5d49ce4
Merge branch 'dev' into iframe-sandbox-support
Marc-Andre-Rivet d264181
Merge branch 'dev' into iframe-sandbox-support
Marc-Andre-Rivet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,8 +1,31 @@ | ||
| import * as R from 'ramda'; | ||
|
|
||
| const __1day = 86400 * 1000; | ||
| const __20years = 86400 * 1000 * 365 * 20; | ||
|
|
||
| export default class CookieStorage { | ||
| // From https://github.com/Modernizr/Modernizr/blob/f4d3aa0b3c9eeb7338e8d89ed77929a8e969c502/feature-detects/cookies.js#L1 | ||
| // try..catch because some in situations `document.cookie` is exposed but throws a | ||
| // SecurityError if you try to access it; e.g. documents created from data URIs | ||
| // or in sandboxed iframes (depending on flags/context) | ||
| public static enabled = R.once((): boolean => { | ||
| try { | ||
| // Create cookie | ||
| document.cookie = 'cookietest=1'; | ||
| let ret = document.cookie.indexOf('cookietest=') !== -1; | ||
| // Delete cookie | ||
| document.cookie = 'cookietest=1; expires=Thu, 01-Jan-1970 00:00:01 GMT'; | ||
| return ret; | ||
| } catch (e) { | ||
| return false; | ||
| } | ||
| }); | ||
|
Contributor
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. @josegonzalez As I'm nearing a release and would want to get this in -- and haven't looked at it soon enough, took the liberty of updating this code so that the |
||
|
|
||
| public static delete(id: string, domain: string = '', path: string = '/') { | ||
| if (!CookieStorage.enabled()) { | ||
| return; | ||
| } | ||
|
|
||
| let expires = new Date(Date.now() - __1day).toUTCString(); | ||
|
|
||
| document.cookie = `${id}=;expires=${expires};domain=${domain};path=${path}`; | ||
|
|
@@ -13,6 +36,10 @@ export default class CookieStorage { | |
| return; | ||
| } | ||
|
|
||
| if (!CookieStorage.enabled()) { | ||
| return; | ||
| } | ||
|
|
||
| id = id.toLowerCase(); | ||
|
|
||
| let cookies = document.cookie.split(';').map(cookie => { | ||
|
|
@@ -28,6 +55,10 @@ export default class CookieStorage { | |
| } | ||
|
|
||
| public static set(id: string, value: string, domain: string = '', path: string = '/') { | ||
| if (!CookieStorage.enabled()) { | ||
| return; | ||
| } | ||
|
|
||
| let expires = new Date(Date.now() + __20years).toUTCString(); | ||
|
|
||
| let entry = `${id}=${value};expires=${expires};domain=${domain};path=${path}`; | ||
|
|
@@ -38,4 +69,4 @@ export default class CookieStorage { | |
|
|
||
| document.cookie = entry; | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@josegonzalez Added a changelog entry