18 lines
541 B
TypeScript
18 lines
541 B
TypeScript
export type OriginPolicy = "*" | string[];
|
|
|
|
export function isHttpUrl(value: string): URL | undefined {
|
|
try {
|
|
const url = new URL(value);
|
|
if ((url.protocol !== "http:" && url.protocol !== "https:") || url.username || url.password || url.hostname.includes("*")) return undefined;
|
|
return url;
|
|
} catch {
|
|
return undefined;
|
|
}
|
|
}
|
|
|
|
export function approvedOrigin(value: string, policy: OriginPolicy): boolean {
|
|
const url = isHttpUrl(value);
|
|
if (!url) return false;
|
|
return policy === "*" || policy.includes(url.origin);
|
|
}
|