mirror of
https://github.com/windmill-labs/windmill.git
synced 2026-12-04 00:06:39 +00:00
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
16 lines
535 B
TypeScript
16 lines
535 B
TypeScript
const SAFE_PROTOCOLS = ['http:', 'https:', 'mailto:']
|
|
|
|
/**
|
|
* The href a link built from untrusted content may carry, or undefined for one that must be
|
|
* dropped: `javascript:`, `data:` and any other non-navigating scheme.
|
|
* Relative links resolve against `base` (the page), so they stay.
|
|
*/
|
|
export function safeHref(href: string | undefined, base: string): string | undefined {
|
|
if (!href) return undefined
|
|
try {
|
|
return SAFE_PROTOCOLS.includes(new URL(href, base).protocol) ? href : undefined
|
|
} catch {
|
|
return undefined
|
|
}
|
|
}
|