Skip to main content
InterviewHubPRO
Section Overview
Q29

Question 29 of 50

Part 3: Advanced Internals, Scale & Production Architecture (Senior / Lead)

58%
HTML5 & Web APIs (50 Qs)•Part 3: Advanced Internals, Scale & Production Architecture (Senior / Lead)

Cross-Site Scripting (XSS) Prevention: Context-Aware Escaping and Content Security Policy (CSP).

senior LevelMost AskedCode ExampleSecurity & AuthSystem DesignHTTP & REST APIs

In Simple Words: Preventing malicious scripts from stealing session cookies or redirecting users:

  1. Never use element.innerHTML with untrusted data: Always use textContent or sanitize with DOMPurify (DOMPurify.sanitize(userInput)).
  2. Content Security Policy (CSP): An HTTP response header instructing the browser which domains are permitted to execute scripts:
    http
    Content-Security-Policy: default-src 'self'; script-src 'self' https://trusted-cdn.com; object-src 'none';
    
    Blocks inline scripts (<script>alert(1)</script>) and untrusted third-party script injection!