Unescaped HTML

aura:unescapedHtml

Renders an HTML value as-is, without altering its contents. Use this component to render pre-formatted HTML, for example, where the formatting is arbitrary, or expensive to calculate. The body of this component is ignored, and won’t be rendered. Only use aura:unescapedHtml with trusted or sanitized sources of data.

For Use In

Lightning Experience, Experience Builder Sites, Salesforce Mobile App, Lightning Out (Beta), Standalone Lightning App

aura:unescapedHtml outputs the value as unescaped HTML, which can introduce security vulnerabilities in your code. Use lightning:formattedRichText whenever possible. Alternatively, if you want to display plain text without HTML formatting, use lightning:formattedText instead.

Sanitize user input before rendering it unescaped. Rendering unescaped HTML can expose you to cross-site scripting (XSS) vulnerabilities.

Warning

Usage 

When your org uses Lightning Locker or Lightning Web Security, your HTML output is sanitized automatically.

Use the value attribute to pass an HTML string. The string is rendered as-is in the DOM.

1<!-- myComponent.cmp -->
2<aura:component>
3  <aura:attribute name="htmlContent" type="String" />
4  <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
5  <aura:unescapedHtml value="{!v.htmlContent}" />
6</aura:component>

This example renders as “Hello World”. The <script> tag is removed from the output.

1// myComponentController.js
2({
3  doInit: function (cmp) {
4    // htmlContent must come from a trusted, sanitized source
5    cmp.set(
6      "v.htmlContent",
7      "<strong>Hello</strong> <em>World</em><script>...</script>",
8    );
9  },
10});

Any markup you place inside <aura:unescapedHtml> is removed. Only the value attribute is rendered.

1<!-- The inner <p> is not rendered -->
2<aura:unescapedHtml value="{!v.html}">
3  <p>This content is ignored.</p>
4</aura:unescapedHtml>

Only use aura:unescapedHtml when you specifically need the browser to interpret the HTML tags.

Use Trusted or Sanitized Sources 

A trusted source means the HTML was either generated by your own code or sanitized before being stored or returned.

Trusted Sources 

Trusted sources include:

  • HTML returned from Apex that explicitly strips dangerous tags
  • Content from Salesforce rich text fields, which the Salesforce Platform sanitizes before storage
  • Static strings defined directly in your component or controller

Untrusted Sources 

Untrusted sources must be sanitized before use. Untrusted sources include:

  • User-entered form input
  • URL parameters or query strings
  • Responses from third-party APIs

Content Security Policy (CSP) Limitations 

aura:unescapedHtml does not bypass Salesforce’s Content Security Policy (CSP). Inline <script> tags embedded in the value string doesn’t execute. However, CSP does not prevent all attack vectors. Event handler attributes such as onerror or onload can still run JavaScript if the HTML isn’t sanitized.

CSP blocks <script>, but not event handlers such as onerror.

1<!-- Always sanitize to prevent XSS vulnerabilities -->
2<img src="x" onerror="alert('XSS')" />

CSP reduces some risk but isn’t a substitute for sanitization.

Choose the Right Component 

aura:unescapedHtml passes the string directly to the DOM without modification. Consider these alternatives for your use case.

  • lightning:formattedText treats its value as plain text and escapes HTML characters before rendering.
  • lightning:formattedRichText renders rich text from Salesforce fields.
ScenarioRecommended componentExample input <strong>bold</strong> renders as
Displaying plain text with no HTML formattinglightning:formattedText<strong>bold</strong> (escaped)
Displaying rich text stored in a Salesforce fieldlightning:formattedRichTextbold (sanitized by the Salesforce Platform)
Rendering arbitrary pre-formatted HTML from a trusted or sanitized sourceaura:unescapedHtmlbold (rendered as-is)

See Also 

Attributes 

NameDescriptionTypeDefaultRequired
bodyThe body of <aura:unescapedHtml> is ignored and won't be rendered.Aura.Component[]
valueThe string that should be rendered as unescaped HTML.String