All files / apps / web/src / lib redirect.ts

100% Statements 7/7
100% Branches 8/8
100% Functions 1/1
100% Lines 6/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20        1x       11x   8x 8x     8x     11x    
// Here, not in features/auth: the /signin route reads it outside its lazy component, and importing
// the auth feature there would put the Firebase SDK in the entry chunk of every page.
 
/** Where sign-in goes when ?redirect= is missing or points off this site: home, which opens #general. */
export const HOME_PATH = '/'
 
/** Only a path on this site, so ?redirect= cannot send someone elsewhere after sign-in. */
export function safeRedirect(value: unknown): string {
  if (typeof value !== 'string' || !value.startsWith('/')) return HOME_PATH
  // Resolved the way location.assign reads it: `//host`, `/\host` and `/\t/host` leave the site.
  const url = new URL(value, window.location.origin)
  const path = `${url.pathname}${url.search}${url.hash}`
  // The path is checked again: dot segments can resolve /.//host to a pathname that starts with //.
  const isOnSite =
    url.origin === window.location.origin &&
    new URL(path, window.location.origin).origin === window.location.origin
 
  return isOnSite ? path : HOME_PATH
}