How do you handle security in an Angular app (XSS, tokens, CSRF)?

Angular testing, tooling and architecture · Experienced

Model answer

Angular escapes values in templates by default, which prevents most cross-site scripting. Avoid bypassSecurityTrustHtml and similar methods unless you sanitise the content yourself, and never build HTML strings from user input.

For authentication, prefer HttpOnly secure cookies where possible, or keep tokens in memory and short-lived, because tokens in local storage can be stolen by an XSS bug. Use HTTPS, validate everything on the server, and protect cookie-based sessions against CSRF (Angular supports an XSRF token header). Keep dependencies updated.

Learn this topic

Angular Tutorial: Beginner to Advanced .

Sign in to add your own answer.

More questions