Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/neat-donuts-teach.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@gitbook/react-contentkit': patch
---

Fix potential invalid URL error in react-contentkit
22 changes: 15 additions & 7 deletions packages/gitbook/src/components/Insights/cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
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;
}
}
14 changes: 6 additions & 8 deletions packages/gitbook/src/lib/local-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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') {
Expand Down
5 changes: 5 additions & 0 deletions packages/react-contentkit/src/ElementWebframe.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading