{ "id": "api/platform-browser/DomSanitizer", "title": "DomSanitizer", "contents": "\n\n
\n
\n
\n \n API > @angular/platform-browser\n
\n \n
\n \n
\n

DomSanitizerlink

\n \n \n \n \n \n
\n \n \n\n
\n \n
\n

DomSanitizer helps preventing Cross Site Scripting Security bugs (XSS) by sanitizing\nvalues to be safe to use in the different DOM contexts.

\n\n

See more...

\n
\n \n
\n

Security risklink

\n

Calling any of the bypassSecurityTrust... APIs disables Angular's built-in\nsanitization for the value passed in. Carefully check and audit all values and code paths going\ninto this call. Make sure any user data is appropriately escaped for this security context.\nFor more detail, see the Security Guide.

\n\n
\n\n \n \n
\n\nabstract class DomSanitizer implements Sanitizer {\n abstract sanitize(context: SecurityContext, value: string | SafeValue): string | null\n abstract bypassSecurityTrustHtml(value: string): SafeHtml\n abstract bypassSecurityTrustStyle(value: string): SafeStyle\n abstract bypassSecurityTrustScript(value: string): SafeScript\n abstract bypassSecurityTrustUrl(value: string): SafeUrl\n abstract bypassSecurityTrustResourceUrl(value: string): SafeResourceUrl\n}\n\n\n \n \n\n
\n\n\n \n\n \n \n
\n

Descriptionlink

\n

For example, when binding a URL in an <a [href]=\"someValue\"> hyperlink, someValue will be\nsanitized so that an attacker cannot inject e.g. a javascript: URL that would execute code on\nthe website.

\n

In specific situations, it might be necessary to disable sanitization, for example if the\napplication genuinely needs to produce a javascript: style link with a dynamic value in it.\nUsers can bypass security by constructing a value with one of the bypassSecurityTrust...\nmethods, and then binding to that value from the template.

\n

These situations should be very rare, and extraordinary care must be taken to avoid creating a\nCross Site Scripting (XSS) security bug!

\n

When using bypassSecurityTrust..., make sure to call the method as early as possible and as\nclose as possible to the source of the value, to make it easy to verify no security bug is\ncreated by its use.

\n

It is not required (and not recommended) to bypass security if the value is safe, e.g. a URL that\ndoes not start with a suspicious protocol, or an HTML snippet that does not contain dangerous\ncode. The sanitizer leaves safe values intact.

\n\n \n
\n\n \n\n\n\n\n\n\n\n\n\n\n\n\n
\n

Methodslink

\n \n \n\n \n \n \n \n \n \n \n \n \n \n\n \n \n \n\n \n \n
\n
\n

\n sanitize()\n \n link

\n \n
\n
\n

Sanitizes a value for use in the given SecurityContext.

\n\n
\n
\n \n\n abstract sanitize(context: SecurityContext, value: string | SafeValue): string | null\n\n \n\n
Parameters
\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
\n \n context\n SecurityContext\n \n \n
\n \n value\n string | SafeValue\n \n \n
\n\n \n
Returns
\n

string | null

\n\n \n\n\n \n\n \n
\n
\n

If value is trusted for the context, this method will unwrap the contained safe value and use\nit directly. Otherwise, value will be sanitized to be safe in the given context, for example\nby replacing URLs that have an unsafe protocol part (such as javascript:). The implementation\nis responsible to make sure that the value can definitely be safely used in the given context.

\n\n
\n\n \n \n\n \n \n \n \n \n \n \n \n \n \n\n \n \n \n\n \n \n
\n
\n

\n bypassSecurityTrustHtml()\n \n link

\n \n
\n
\n

Bypass security and trust the given value to be safe HTML. Only use this when the bound HTML\nis unsafe (e.g. contains <script> tags) and the code should be executed. The sanitizer will\nleave safe HTML intact, so in most situations this method should not be used.

\n\n
\n
\n \n\n abstract bypassSecurityTrustHtml(value: string): SafeHtml\n\n \n\n
Parameters
\n \n \n \n \n \n \n \n \n \n
\n \n value\n string\n \n \n
\n\n \n
Returns
\n

SafeHtml

\n\n \n\n\n \n\n \n
\n
\n

WARNING: calling this method with untrusted user data exposes your application to XSS\nsecurity risks!

\n\n
\n\n \n \n\n \n \n \n \n \n \n \n \n \n \n\n \n \n \n\n \n \n
\n
\n

\n bypassSecurityTrustStyle()\n \n link

\n \n
\n
\n

Bypass security and trust the given value to be safe style value (CSS).

\n\n
\n
\n \n\n abstract bypassSecurityTrustStyle(value: string): SafeStyle\n\n \n\n
Parameters
\n \n \n \n \n \n \n \n \n \n
\n \n value\n string\n \n \n
\n\n \n
Returns
\n

SafeStyle

\n\n \n\n\n \n\n \n
\n
\n

WARNING: calling this method with untrusted user data exposes your application to XSS\nsecurity risks!

\n\n
\n\n \n \n\n \n \n \n \n \n \n \n \n \n \n\n \n \n \n\n \n \n
\n
\n

\n bypassSecurityTrustScript()\n \n link

\n \n
\n
\n

Bypass security and trust the given value to be safe JavaScript.

\n\n
\n
\n \n\n abstract bypassSecurityTrustScript(value: string): SafeScript\n\n \n\n
Parameters
\n \n \n \n \n \n \n \n \n \n
\n \n value\n string\n \n \n
\n\n \n
Returns
\n

SafeScript

\n\n \n\n\n \n\n \n
\n
\n

WARNING: calling this method with untrusted user data exposes your application to XSS\nsecurity risks!

\n\n
\n\n \n \n\n \n \n \n \n \n \n \n \n \n \n\n \n \n \n\n \n \n
\n
\n

\n bypassSecurityTrustUrl()\n \n link

\n \n
\n
\n

Bypass security and trust the given value to be a safe style URL, i.e. a value that can be used\nin hyperlinks or <img src>.

\n\n
\n
\n \n\n abstract bypassSecurityTrustUrl(value: string): SafeUrl\n\n \n\n
Parameters
\n \n \n \n \n \n \n \n \n \n
\n \n value\n string\n \n \n
\n\n \n
Returns
\n

SafeUrl

\n\n \n\n\n \n\n \n
\n
\n

WARNING: calling this method with untrusted user data exposes your application to XSS\nsecurity risks!

\n\n
\n\n \n \n\n \n \n \n \n \n \n \n \n \n \n\n \n \n \n\n \n \n
\n
\n

\n bypassSecurityTrustResourceUrl()\n \n link

\n \n
\n
\n

Bypass security and trust the given value to be a safe resource URL, i.e. a location that may\nbe used to load executable code from, like <script src>, or <iframe src>.

\n\n
\n
\n \n\n abstract bypassSecurityTrustResourceUrl(value: string): SafeResourceUrl\n\n \n\n
Parameters
\n \n \n \n \n \n \n \n \n \n
\n \n value\n string\n \n \n
\n\n \n
Returns
\n

SafeResourceUrl

\n\n \n\n\n \n\n \n
\n
\n

WARNING: calling this method with untrusted user data exposes your application to XSS\nsecurity risks!

\n\n
\n\n \n
\n\n\n\n \n\n\n
\n
\n\n\n" }