Skip to the page

Production and testing · mid

Security: sanitising and bypassSecurityTrust

Angular treats every value as untrusted. Bypassing sanitising is a security decision that needs a review.

The notes

What you must know

  • Interpolation escapes values. [innerHTML] is sanitised: scripts and event-handler attributes are removed.
  • URLs are checked. Templates are compiled ahead of time and never built from user input.
  • bypassSecurityTrustHtml(note) turns the protection off for that value, so any script in the note will run (XSS).
  • Prefer to sanitise with a trusted library on the server, or render rich text as safe elements, and keep any bypass tiny and documented.
  • Never build a template or HTML string from user input.
  • XSRF: HttpClient can send the XSRF token header for cookie-based sessions; the server must check it.
  • Client checks are not security. The API must check authorisation.

The red pen

Common traps: mistake → fix

  • Mistake: Angular makes XSS impossible

    Fix: only when you do not bypass it.

  • Mistake: bypassSecurityTrust sanitises the value

    Fix: it skips sanitising.

Easy to hard

Questions interviewers ask, easy to hard

  1. How does Angular protect against XSS?
  2. What does bypassSecurityTrustHtml do and when is it dangerous?
  3. How would you show user-written rich text safely?

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview