-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
63 additions
and
22 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 @@ | ||
--- | ||
'gitbook': patch | ||
--- | ||
|
||
Make cookies access safer |
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,29 @@ | ||
import cookies from 'js-cookie'; | ||
|
||
import { checkIsSecurityError } from './security-error'; | ||
|
||
export function getAll(): { | ||
[key: string]: string; | ||
} { | ||
try { | ||
return cookies.get(); | ||
} catch (error) { | ||
if (checkIsSecurityError(error)) { | ||
return {}; | ||
} | ||
throw error; | ||
} | ||
} | ||
|
||
export function get(name: string): string | undefined { | ||
try { | ||
return cookies.get(name); | ||
} catch (error) { | ||
if (checkIsSecurityError(error)) { | ||
return undefined; | ||
} | ||
throw error; | ||
} | ||
} | ||
|
||
export const set = cookies.set; |
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,12 @@ | ||
/** | ||
* Test if the error is a security error returned by the browser when cookies or local storage are blocked. | ||
*/ | ||
export function checkIsSecurityError(error: unknown): error is Error { | ||
return ( | ||
error instanceof Error && | ||
// Safari | ||
(error.name === 'SecurityError' || | ||
// Firefox | ||
error.name === 'NS_ERROR_FAILURE') | ||
); | ||
} |