Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(core): reject script element as a dynamic component host - #68713

Merged
leonsenft merged 1 commit into
angular:mainfrom
alan-agius4:script-host-element-2
May 18, 2026
Merged

fix(core): reject script element as a dynamic component host#68713
leonsenft merged 1 commit into
angular:mainfrom
alan-agius4:script-host-element-2

Conversation

@alan-agius4

@alan-agius4 alan-agius4 commented May 13, 2026

Copy link
Copy Markdown
Contributor

To enhance application security and prevent accidental or malicious script execution, this change ensures that dynamically mounting a component via createComponent directly onto a <script> element throws a runtime error in development mode. SVG <script> elements are also rejected. The error message is designed to be fully tree-shakable under production builds where ngDevMode is disabled.

More context in: #68689 (comment)

To enhance application security and prevent accidental or malicious script execution, this change ensures that dynamically mounting a component via createComponent directly onto a <script> element throws a runtime error in development mode. SVG <script> elements are also rejected. The error message is designed to be fully tree-shakable under production builds where ngDevMode is disabled.
@alan-agius4
alan-agius4 requested a review from AndrewKushnir May 13, 2026 12:12
@alan-agius4 alan-agius4 added action: review The PR is still awaiting reviews from at least one requested reviewer target: patch This PR is targeted for the next patch release labels May 13, 2026
@angular-robot angular-robot Bot added the area: core Issues related to the framework runtime label May 13, 2026
@ngbot ngbot Bot added this to the Backlog milestone May 13, 2026
})
class MySvgSink {}

const svgScriptHost = document.createElementNS('http://www.w3.org/2000/svg', 'script');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I understand this; I would only validate that the host is a script.

We are not validating a case as if it were

<svg>
<script> </script>
</svg>

For example

It will not throw an error; it currently has the previously described form (and is also considered validated HTML according to https://developer.mozilla.org/en-US/docs/Web/API/SVGScriptElement)

    const svgHost = document.createElementNS(SVG_NS, 'svg');
    const text = document.createElementNS(SVG_NS, 'text');
    const script = document.createElementNS(SVG_NS, 'script');
    svgHost.append(text, script);
    this.ref = createComponent(SvgScriptChildPov, {
      environmentInjector: this.env,
      hostElement: svgHost,
    });

encapsulation === ViewEncapsulation.ShadowDom ||
encapsulation === ViewEncapsulation.ExperimentalIsolatedShadowDom;
const rootElement = renderer.selectRootElement(elementOrSelector, preserveContent);
if (rootElement.tagName.toLowerCase() === 'script') {

@SkyZeroZx SkyZeroZx May 13, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Perhaps a simple way would be to prevent any child of the host from having a script (considering we are prohibiting scripts at both the compiler and SVG levels, which would be the overall and retroactive goal if any future HTML spec allows it).

function containsScriptElement(root: Element): boolean {
  return root.localName === 'script' || root.querySelector('script') !== null;
}

@SkyZeroZx

Copy link
Copy Markdown
Contributor

@alan-agius4 After investigating and running some more tests, I believe this issue is a bit more complex than simply a script host element problem.

I've updated the details here https://issuetracker.google.com/u/1/issues/510537066 with a more complete example. I think this issue should be reclassified.

@alan-agius4 alan-agius4 added action: merge The PR is ready for merge by the caretaker and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels May 18, 2026
@alan-agius4

alan-agius4 commented May 18, 2026

Copy link
Copy Markdown
Contributor Author

This behavior is working as intended.

When a developer uses createComponent and manually supplies a raw native hostElement, they are explicitly stepping outside of Angular’s managed template boundary.

Angular’s automatic SecurityContext sanitization relies heavily on the compiler analyzing templates. When you bypass templates by programmatically injecting a native element iframe, object, embed and coupling it to a host binding [attr.data], you assume the responsibility of ensuring that the inputs passed to that element are safe.

If an application must dynamically create components this way with untrusted URLs, the data must be explicitly sanitized at the creation boundary using the DomSanitizer.

Because this relies on the developer actively bypassing the template engine via hostElement, this is not classify as a framework-level vulnerability.

@SkyZeroZx, will also reply in the ticket.

@alan-agius4 alan-agius4 added target: patch This PR is targeted for the next patch release action: review The PR is still awaiting reviews from at least one requested reviewer and removed target: patch This PR is targeted for the next patch release action: review The PR is still awaiting reviews from at least one requested reviewer labels May 18, 2026
@leonsenft
leonsenft merged commit 0011664 into angular:main May 18, 2026
54 checks passed
@leonsenft

Copy link
Copy Markdown
Contributor

This PR was merged into the repository. The changes were merged into the following branches:

@alan-agius4
alan-agius4 deleted the script-host-element-2 branch May 18, 2026 20:23
@angular-automatic-lock-bot

Copy link
Copy Markdown

This pull request has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot Bot locked and limited conversation to collaborators Jun 18, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

action: merge The PR is ready for merge by the caretaker area: core Issues related to the framework runtime target: patch This PR is targeted for the next patch release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants