CódigoSin intentar
isSafeUrl (allowlist contra SSRF)
Dificultad: fácil
Escribe isSafeUrl(input) para un servidor que hace fetch de URLs enviadas por usuarios. Devuelve true solo para URLs https: válidas cuyo host sea exactamente uno de ALLOWED_HOSTS. Los parecidos como api.stripe.com.evil.com deben fallar. Pista: parsea con new URL() en lugar de comprobar la cadena.
Ver solución de referencia
// Hosts the server is allowed to call on a user's behalf.export const ALLOWED_HOSTS = ['api.stripe.com', 'images.example.com'];
export function isSafeUrl(input: string): boolean { let url: URL; try { url = new URL(input); } catch { return false; // not a valid absolute URL }
if (url.protocol !== 'https:') return false;
// Compare the parsed hostname exactly. A string check like input.includes('api.stripe.com') // would let through https://api.stripe.com.evil.com or https://evil.com/?api.stripe.com return ALLOWED_HOSTS.includes(url.hostname);}
Ctrl Enter