All files / js/composables useSafeExternalUrl.ts

88.88% Statements 8/9
100% Branches 6/6
100% Functions 1/1
88.88% Lines 8/9

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 201x     7x 2x     5x 5x 5x 2x     3x            
const ALLOWED_EXTERNAL_PROTOCOLS = new Set(['http:', 'https:', 'mailto:', 'tel:']);
 
export function getSafeExternalUrl(url: string | null | undefined): string | null {
    if (!url || typeof url !== 'string') {
        return null;
    }
 
    try {
        const parsed = new URL(url, window.location.origin);
        if (!ALLOWED_EXTERNAL_PROTOCOLS.has(parsed.protocol)) {
            return null;
        }
 
        return parsed.toString();
    } catch {
        return null;
    }
}