1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-11-24 20:06:55 +01:00
unleash.unleash/src/lib/util/validateOrigin.ts
Christopher Kolstad efcf04487d
chore: make it build with strict null checks set to true (#9554)
As part of preparation for ESM and node/TSC updates, this PR will make
Unleash build with strictNullChecks set to true, since that's what's in
our tsconfig file. Hence, this PR also removes the `--strictNullChecks
false` flag in our compile tasks in package.json.

TL;DR - Clean up your code rather than turning off compiler security
features :)
2025-03-19 10:01:49 +01:00

28 lines
641 B
TypeScript

export const validateOrigin = (origin: string | undefined): boolean => {
if (origin === undefined) {
return false;
}
if (origin === '*') {
return true;
}
if (origin?.includes('*')) {
return false;
}
try {
const parsed = new URL(origin);
return typeof parsed.origin === 'string' && parsed.origin === origin;
} catch {
return false;
}
};
export const validateOrigins = (origins: string[]): string | undefined => {
for (const origin of origins) {
if (!validateOrigin(origin)) {
return `Invalid origin: ${origin}`;
}
}
};