All files / app-leaderboard/src/pages preferences.ts

90.9% Statements 10/11
100% Branches 12/12
100% Functions 2/2
90% Lines 9/10

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 21 22 23 24 25 26 27 28 29      12x 12x       175x                 12x 7x 7x 7x 3x   4x          
import { isRuleset, type RulesetId } from '../rulesets.ts';
 
/** Cookie holding the viewer's display ruleset; every page shows its figures under it. */
export const RULESET_COOKIE = 'lb-ruleset';
export const DEFAULT_RULESET: RulesetId = 'lr2';
 
/** The display ruleset from the cookie, or LR2 when it is missing or not a ruleset. */
export function resolveDisplayRuleset(cookieValue: string | undefined): RulesetId {
  return isRuleset(cookieValue) ? cookieValue : DEFAULT_RULESET;
}
 
/**
 * Where to go after switching the ruleset: back to the Leaderboard page the switch was made on. Anything that is not a
 * page on this site (another host, `//evil`, a backslash trick, the API) goes to the top page instead, so the switch
 * cannot be used as an open redirect.
 */
export function safeReturnPath(next: string | undefined): string {
  if (next === undefined || !next.startsWith('/') || next.includes('\\')) return '/';
  try {
    const url = new URL(next, 'https://leaderboard.invalid');
    if (url.origin !== 'https://leaderboard.invalid' || url.pathname === '/api' || url.pathname.startsWith('/api/')) {
      return '/';
    }
    return `${url.pathname}${url.search}`;
  } catch {
    return '/';
  }
}