UniHunt / src /lib /url.ts
ashnhash's picture
Cursor
Ground course search in Brave Search so result URLs are live official pages.
8c17b26
Raw History Blame Contribute Delete
710 Bytes
/**
* Course URLs originate from Brave Search, model output, and manual user entry,
* and are rendered straight into an anchor `href`. Restricting them to http(s)
* keeps `javascript:` and `data:` URLs from becoming a click-to-execute vector.
*/
export function isHttpUrl(value: string): boolean {
try {
const { protocol } = new URL(value);
return protocol === "http:" || protocol === "https:";
} catch {
return false;
}
}
/** Returns the URL when it is a usable http(s) link, or an empty string. */
export function safeHttpUrl(value: unknown): string {
if (typeof value !== "string") return "";
const trimmed = value.trim();
return trimmed && isHttpUrl(trimmed) ? trimmed : "";
}