Skip to content

Commit

Permalink
fix(clerk-js): Validate protocol on window navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
tmilewski committed May 24, 2024
1 parent 980493f commit b91e0ef
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/unlucky-pumpkins-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/clerk-js': patch
---

Validate protocol on window navigation
8 changes: 7 additions & 1 deletion packages/clerk-js/src/core/clerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,13 @@ export class Clerk implements ClerkInterface {
return;
}

const toURL = new URL(to, window.location.href);
let toURL = new URL(to, window.location.href);

if (toURL.protocol !== 'http:' && toURL.protocol !== 'https:') {
console.warn('Clerk: Not a valid protocol. Redirecting to /');
toURL = new URL('/', window.location.href);
}

const customNavigate =
options?.replace && this.#options.routerReplace ? this.#options.routerReplace : this.#options.routerPush;

Expand Down
9 changes: 8 additions & 1 deletion packages/clerk-js/src/utils/windowNavigate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
export const CLERK_BEFORE_UNLOAD_EVENT = 'clerk:beforeunload';

export function windowNavigate(to: URL | string): void {
let toURL = new URL(to, window.location.href);

if (toURL.protocol !== 'http:' && toURL.protocol !== 'https:') {
console.warn('Clerk: Not a valid protocol. Redirecting to /');
toURL = new URL('/', window.location.href);
}

window.dispatchEvent(new CustomEvent(CLERK_BEFORE_UNLOAD_EVENT));
window.location.href = typeof to === 'string' ? to : to.href;
window.location.href = toURL.href;
}

0 comments on commit b91e0ef

Please sign in to comment.