Skip to content

Conversation

hamishwillee
Copy link
Collaborator

HTMLIFrameElement.srcdoc now takes TrustedHTML. This updates the docs using similar patterns to the Element.innerHTML docs.

Related docs work can be tracked in #37518 (comment)

@hamishwillee hamishwillee requested a review from a team as a code owner October 10, 2025 06:12
@hamishwillee hamishwillee requested review from sideshowbarker and removed request for a team October 10, 2025 06:12
@github-actions github-actions bot added Content:WebAPI Web API docs size/m [PR only] 51-500 LoC changed labels Oct 10, 2025
@hamishwillee hamishwillee changed the title Tt htmliframeelement srcdoc HTMLIFrameElement.srcdoc - takes trusted types Oct 10, 2025
Copy link
Contributor

Preview URLs

Flaws (6)

Note! 1 document with no flaws that don't need to be listed. 🎉

URL: /en-US/docs/Web/API/HTMLIFrameElement
Title: HTMLIFrameElement
Flaw count: 6

  • macros:
    • Can't resolve /en-US/docs/Web/API/HTMLIFrameElement/align
    • Can't resolve /en-US/docs/Web/API/HTMLIFrameElement/frameBorder
    • Can't resolve /en-US/docs/Web/API/HTMLIFrameElement/longDesc
    • Can't resolve /en-US/docs/Web/API/HTMLIFrameElement/marginHeight
    • Can't resolve /en-US/docs/Web/API/HTMLIFrameElement/marginWidth
    • and 1 more flaws omitted
External URLs (1)

URL: /en-US/docs/Web/API/HTMLIFrameElement/srcdoc
Title: HTMLIFrameElement: srcdoc property

If the frame is not sandboxed using the Content Security Property (CSP) [`sandbox` directive](/en-US/docs/Web/HTTP/Reference/Headers/Content-Security-Policy/sandbox) (or is sandboxed but includes the [`allow-same-origin`](/en-US/docs/Web/HTTP/Reference/Headers/Content-Security-Policy/sandbox#allow-same-origin) value) then it will be same-origin with the parent.
This means that the frame will have complete access to the parent DOM and resources, and visa versa.

This is a very significant vector for [Cross-site-scripting (XSS)](/en-US/docs/Web/Security/Attacks/XSS) attacks if potentially unsafe strings provided by a user injected into a frame without first being sanitized.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This is a very significant vector for [Cross-site-scripting (XSS)](/en-US/docs/Web/Security/Attacks/XSS) attacks if potentially unsafe strings provided by a user injected into a frame without first being sanitized.
This is a very significant vector for [Cross-site-scripting (XSS)](/en-US/docs/Web/Security/Attacks/XSS) attacks if potentially unsafe strings provided by a user are injected into a frame without first being sanitized.

This means that the frame will have complete access to the parent DOM and resources, and visa versa.

This is a very significant vector for [Cross-site-scripting (XSS)](/en-US/docs/Web/Security/Attacks/XSS) attacks if potentially unsafe strings provided by a user injected into a frame without first being sanitized.
Consider the following code where a string of HTML from a user might be passed into a frame, that is then added to the document.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Consider the following code where a string of HTML from a user might be passed into a frame, that is then added to the document.
Consider the following code where a string of HTML from a user might be passed into a frame that is then added to the document.

```

If the frame is not expected to need access to your parent document, you can significantly mitigate the risk by using a CSP sandbox without the `allow-same-origin` value.
The frame will then be treated as a cross-origin resource, and and attacks will be significantly restricted.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The frame will then be treated as a cross-origin resource, and and attacks will be significantly restricted.
The frame will then be treated as a cross-origin resource, and attacks will be significantly restricted.

```

Next we create a {{domxref("TrustedTypePolicy")}} that defines a {{domxref("TrustedTypePolicy/createHTML", "createHTML()")}} for transforming an input string into {{domxref("TrustedHTML")}} instances.
Commonly implementations of `createHTML()` use a library such as [DOMPurify](https://github.com/cure53/DOMPurify) to sanitize the input as shown below:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Commonly implementations of `createHTML()` use a library such as [DOMPurify](https://github.com/cure53/DOMPurify) to sanitize the input as shown below:
Commonly, implementations of `createHTML()` use a library such as [DOMPurify](https://github.com/cure53/DOMPurify) to sanitize the input, as shown below:

```

> [!WARNING]
> While you can directly assign a string to `srcdoc` this is a [security risk](#security_considerations) if the string to be inserted might contain potentially malicious content.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
> While you can directly assign a string to `srcdoc` this is a [security risk](#security_considerations) if the string to be inserted might contain potentially malicious content.
> While you can directly assign a string to `srcdoc`, this is a [security risk](#security_considerations) if the string to be inserted might contain potentially malicious content.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Content:WebAPI Web API docs size/m [PR only] 51-500 LoC changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants