Always validate and sanitize user-provided inputs before using them in security-sensitive operations to prevent XSS attacks and unsafe redirections.
Always validate and sanitize user-provided inputs before using them in security-sensitive operations to prevent XSS attacks and unsafe redirections.
When handling URLs from user input:
When inserting content into the DOM:
Example (improved URL validation):
function loadUrl(url) {
try {
// Parse and validate URL
const urlObj = new URL(url, window.location.origin);
// Only allow http and https protocols
if (urlObj.protocol !== 'http:' && urlObj.protocol !== 'https:') {
console.error('Invalid URL protocol');
return;
}
// Use the validated URL
iframe.src = urlObj.toString();
} catch (e) {
console.error('Invalid URL format', e);
}
}
This practice helps prevent client-side open redirect vulnerabilities and cross-site scripting (XSS) attacks that could compromise user security.
Enter the URL of a public GitHub repository