mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-04-25 16:56:22 +02:00
28 lines
764 B
TypeScript
28 lines
764 B
TypeScript
export function sanitizeHref(href?: string): string | undefined {
|
|
if (!href) return undefined;
|
|
const candidate = href.trim();
|
|
if (!candidate) return undefined;
|
|
|
|
if (
|
|
candidate.startsWith("/") ||
|
|
candidate.startsWith("./") ||
|
|
candidate.startsWith("../") ||
|
|
candidate.startsWith("?") ||
|
|
candidate.startsWith("#")
|
|
) {
|
|
if (candidate.startsWith("//")) return undefined;
|
|
// biome-ignore lint/suspicious/noControlCharactersInRegex: intentionally matching control characters
|
|
if (/[\u0000-\u001F\u007F]/.test(candidate)) return undefined;
|
|
return candidate;
|
|
}
|
|
|
|
try {
|
|
const url = new URL(candidate);
|
|
if (url.protocol === "http:" || url.protocol === "https:") {
|
|
return url.toString();
|
|
}
|
|
} catch {
|
|
return undefined;
|
|
}
|
|
return undefined;
|
|
}
|