mirror of
https://github.com/pterodactyl/panel.git
synced 2026-12-01 03:45:53 -05:00
32 lines
748 B
TypeScript
32 lines
748 B
TypeScript
import { isString } from '@/lib/objects';
|
|
|
|
export type RedirectSearch = {
|
|
redirect?: string;
|
|
};
|
|
|
|
const redirectOrigin = 'http://redirect.invalid';
|
|
|
|
export const parseRedirectSearch = (search: { redirect?: unknown }): RedirectSearch => {
|
|
const redirect = isString(search.redirect) ? search.redirect.trim() : '';
|
|
|
|
if (!redirect.startsWith('/')) {
|
|
return {};
|
|
}
|
|
|
|
let url: URL;
|
|
|
|
try {
|
|
url = new URL(redirect, redirectOrigin);
|
|
} catch {
|
|
return {};
|
|
}
|
|
|
|
if (url.origin !== redirectOrigin || url.pathname === '/auth' || url.pathname.startsWith('/auth/')) {
|
|
return {};
|
|
}
|
|
|
|
const path = `${url.pathname}${url.search}${url.hash}`;
|
|
|
|
return path === '/' ? {} : { redirect: path };
|
|
};
|