Various fixes (#2683)

This commit is contained in:
Greg Bergé
2025-01-07 16:54:10 +01:00
committed by GitHub
parent d2bc5672e0
commit 648f0e9e84
4 changed files with 31 additions and 15 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@gitbook/react-contentkit': patch
---
Fix potential invalid URL error in react-contentkit
@@ -20,13 +20,21 @@ export function setCookiesTracking(enabled: boolean) {
* Return `undefined` if state is not known.
*/
export function isCookiesTrackingDisabled() {
const state = cookies.get(GRANTED_COOKIE);
try {
const state = cookies.get(GRANTED_COOKIE);
if (state === 'yes') {
return false;
} else if (state === 'no') {
return true;
if (state === 'yes') {
return false;
} else if (state === 'no') {
return true;
}
return undefined;
} catch (error) {
// If there is a security error, we consider cookies as disabled
if (error instanceof Error && error.name === 'SecurityError') {
return true;
}
throw error;
}
return undefined;
}
+6 -8
View File
@@ -2,11 +2,10 @@
* Get an item from local storage safely.
*/
export function getItem<T>(key: string, defaultValue: T): T {
if (typeof localStorage === 'undefined') {
return defaultValue;
}
try {
if (typeof localStorage === 'undefined') {
return defaultValue;
}
const stored = localStorage.getItem(key);
return stored ? (JSON.parse(stored) as T) : defaultValue;
} catch (error) {
@@ -21,11 +20,10 @@ export function getItem<T>(key: string, defaultValue: T): T {
* Set an item in local storage safely.
*/
export function setItem(key: string, value: unknown) {
if (typeof localStorage === 'undefined') {
return;
}
try {
if (typeof localStorage === 'undefined') {
return;
}
localStorage.setItem(key, JSON.stringify(value));
} catch (error) {
if (error instanceof Error && error.name === 'SecurityError') {
@@ -58,6 +58,11 @@ export function ElementWebframe(props: ContentKitClientElementProps<ContentKitWe
}
const message = event.data;
if (!URL.canParse(event.origin)) {
return;
}
const origin = new URL(event.origin);
// For security reasons, only iframe from our integrations domains are allowed