What you will learn
Keep data untrusted, rely on Angular sanitization, and enforce authorization on the server.
- Angular treats template values as untrusted and escapes or sanitizes them according to their DOM context. Interpolation is safer than assembling HTML strings.
- Avoid bypassSecurityTrust methods. They disable a security boundary and require a narrow, reviewed reason with trusted input.
- Client route guards improve navigation but do not enforce authorization. Every protected API operation must validate identity and permission on the server.
- Use Content Security Policy and Trusted Types as defense in depth. Keep dependencies current and never place private secrets in browser environment files.
How it works
1Receive untrusted data
2Validate at boundary
3Render in safe context
4Server authorizes mutations
Expected result
Text that looks like HTML is displayed as text, and the server independently decides whether an update is allowed.
Common mistakes
Do not trust a value because it came from your database. Do not put API keys in Angular source. Never treat a hidden button or route guard as authorization.
