Question presented to candidate: "A security review found that user profiles accept HTML and links. Explain which vulnerabilities matter most in an Angular front end, how Angular protects against XSS by default, and when, if ever, you would use DomSanitizer's bypass methods."
What a strong answer should cover:
- XSS as the main client-side threat; Angular's model: all bound values are untrusted by default.
- Contextual sanitization: interpolation escapes text; HTML, URL and resource-URL contexts are sanitized or rejected (
unsafe:prefix, stripped<script>/onerror, NG0904 for resource URLs). - DomSanitizer:
sanitize()for manual cleaning;bypassSecurityTrust*only for values you control, and the risk of using it on user input. - Other defences: CSP (including
autoCspand nonces), Trusted Types, AOT and never compiling user templates, XSRF handling in HttpClient, SSRallowedHosts. - Practical hygiene: no direct
innerHTMLvia ElementRef, dependency updates, server-side validation.
Clarifying questions expected:
- "Do users need rich text, or would plain text plus markdown rendered safely do?"
- "Is the app server-rendered? That adds host-header and request-forgery concerns."
Code / implementation expected: Yes — bindings that show sanitization, and one justified bypassSecurityTrustResourceUrl.