Skip to content

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

Open
alan-agius4 wants to merge 1 commit into
angular:mainfrom
alan-agius4:script-host-element-2
Open

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

Conversation

@alan-agius4
Copy link
Copy Markdown
Contributor

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

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') {
Copy link
Copy Markdown
Contributor

@SkyZeroZx SkyZeroZx May 13, 2026

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.

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

Labels

action: review The PR is still awaiting reviews from at least one requested reviewer 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.

3 participants