You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> This property parses its input as HTML, writing the result into the DOM.
13
+
> APIs like this are known as [injection sinks](/en-US/docs/Web/API/Trusted_Types_API#concepts_and_usage), and are potentially a vector for [cross-site-scripting (XSS)](/en-US/docs/Web/Security/Attacks/XSS) attacks, if the input originally came from an attacker.
14
+
>
15
+
> You can mitigate this risk by always assigning `TrustedHTML` objects instead of strings and [enforcing trusted types](/en-US/docs/Web/API/Trusted_Types_API#using_a_csp_to_enforce_trusted_types).
16
+
> See [Security considerations](#security_considerations) for more information.
17
+
11
18
The **`innerHTML`** property of the {{domxref("ShadowRoot")}} interface gets or sets the HTML markup to the DOM tree inside the `ShadowRoot`.
12
19
20
+
## Value
21
+
22
+
Getting the property returns a string containing the HTML serialization of the shadow root's descendants.
23
+
24
+
Setting the property accepts either a {{domxref("TrustedHTML")}} object or a string.
25
+
It parses this value as HTML and replaces all the element's descendants with the result.
26
+
When set to the `null` value, that `null` value is converted to the empty string (`""`), so `shadowRoot.innerHTML = null` is equivalent to `shadowRoot.innerHTML = ""`.
27
+
28
+
### Exceptions
29
+
30
+
-`SyntaxError` {{domxref("DOMException")}}
31
+
- : Thrown if an attempt was made to set the value of `innerHTML` using a string which is not properly-formed HTML.
32
+
-`TypeError`
33
+
- : Thrown if the property is set to a string when [Trusted Types](/en-US/docs/Web/API/Trusted_Types_API) are [enforced by a CSP](/en-US/docs/Web/API/Trusted_Types_API#using_a_csp_to_enforce_trusted_types) and no default policy is defined.
34
+
35
+
## Description
36
+
37
+
`innerHTML` gets a serialization of the nested child DOM elements within the shadow root, or sets HTML or XML that should be parsed to replace the DOM tree within the shadow root.
38
+
13
39
Note that some browsers serialize the `<` and `>` characters as `<` and `>` when they appear in attribute values (see [Browser compatibility](#browser_compatibility)).
14
40
This is to prevent a potential security vulnerability ([mutation XSS](https://www.securitum.com/mutation-xss-via-mathml-mutation-dompurify-2-0-17-bypass.html)) in which an attacker can craft input that bypasses a [sanitization function](/en-US/docs/Web/Security/Attacks/XSS#sanitization), enabling a cross-site scripting (XSS) attack.
15
41
16
-
##Value
42
+
### Security considerations
17
43
18
-
A string.
44
+
The `innerHTML` property is a possible vector for [Cross-site-scripting (XSS)](/en-US/docs/Web/Security/Attacks/XSS) attacks, where potentially unsafe strings provided by a user are injected into the DOM without first being sanitized.
45
+
While the property does prevent {{HTMLElement("script")}} elements from executing when they are injected, it is susceptible to many other ways that attackers can craft HTML to run malicious JavaScript.
46
+
For example, the following example would execute the code in the `error` event handler, because the {{htmlelement("img")}} `src` value is not a valid image URL:
19
47
20
-
When set to the `null` value, that `null` value is converted to the empty string (`""`), so `sr.innerHTML = null` is equivalent to `sr.innerHTML = ""`.
48
+
```js
49
+
constname="<img src='x' onerror='alert(1)'>";
50
+
shadowRoot.innerHTML= name; // shows the alert
51
+
```
52
+
53
+
You can mitigate these issues by always assigning {{domxref("TrustedHTML")}} objects instead of strings, and [enforcing trusted type](/en-US/docs/Web/API/Trusted_Types_API#using_a_csp_to_enforce_trusted_types) using the [`require-trusted-types-for`](/en-US/docs/Web/HTTP/Reference/Headers/Content-Security-Policy/require-trusted-types-for) CSP directive.
54
+
This ensures that the input is passed through a transformation function, which has the chance to [sanitize](/en-US/docs/Web/Security/Attacks/XSS#sanitization) the input to remove potentially dangerous markup before it is injected.
21
55
22
56
## Examples
23
57
58
+
### Reading the HTML contents of an element
59
+
60
+
Reading `innerHTML` causes the user agent to serialize the shadow root's descendants.
61
+
62
+
Given the following HTML:
63
+
64
+
```html
65
+
<divclass="host">
66
+
<templateshadowrootmode="open">
67
+
<p>My name is Joe</p>
68
+
</template>
69
+
</div>
70
+
```
71
+
72
+
You can get and log the markup for the shadow root as shown:
73
+
74
+
```js
75
+
constshadowHost=document.querySelector("#host");
76
+
constshadowRoot=shadowHost.shadowRoot;
77
+
constcontents=shadowRoot.innerHTML;
78
+
console.log(contents); // "\n <p>My name is Joe</p>\n"
79
+
```
80
+
24
81
### Setting the innerHTML of a Shadow root
25
82
83
+
In this example we'll replace an element's DOM by assigning HTML to the element's `innerHTML` property.
84
+
To mitigate the risk of XSS, we'll first create a `TrustedHTML` object from the string containing the HTML, and then assign that object to `innerHTML`.
85
+
86
+
Trusted types are not yet supported on all browsers, so first we define the [trusted types tinyfill](/en-US/docs/Web/API/Trusted_Types_API#trusted_types_tinyfill).
87
+
This acts as a transparent replacement for the trusted types JavaScript API:
88
+
26
89
```js
27
-
let customElem =document.querySelector("my-shadow-dom-element");
shadow.innerHTML="<strong>This element should be more important!</strong>";
94
+
Next we create a {{domxref("TrustedTypePolicy")}} that defines a {{domxref("TrustedTypePolicy/createHTML", "createHTML()")}} for transforming an input string into {{domxref("TrustedHTML")}} instances.
95
+
Commonly implementations of `createHTML()` use a library such as [DOMPurify](https://github.com/cure53/DOMPurify) to sanitize the input as shown below:
// Inject the TrustedHTML (which contains a trusted string)
117
+
shadowRoot.innerHTML= trustedHTML;
118
+
```
119
+
120
+
> [!WARNING]
121
+
> While you can directly assign a string to `innerHTML` this is a [security risk](#security_considerations) if the string to be inserted might contain potentially malicious content.
0 commit comments