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

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.

PR Close #68926
This commit is contained in:
Alan Agius
2026-05-13 10:12:28 +00:00
committed by Alex Rickabaugh
parent b8f1f72765
commit d86e4e7b2a
@@ -21,6 +21,7 @@ import {stringify} from '../../util/stringify';
import {assertFirstCreatePass, assertHasParent, assertLView} from '../assert';
import {attachPatchData} from '../context_discovery';
import {getNodeInjectable, getOrCreateNodeInjectorForNode} from '../di';
import {RuntimeError, RuntimeErrorCode} from '../../errors';
import {throwMultipleComponentError} from '../errors';
import {ComponentDef, ComponentTemplate, DirectiveDef, RenderFlags} from '../interfaces/definition';
import {
@@ -177,6 +178,12 @@ export function locateHostElement(
// projection.
const preserveContent = preserveHostContent || encapsulation === ViewEncapsulation.ShadowDom;
const rootElement = renderer.selectRootElement(elementOrSelector, preserveContent);
if (rootElement.tagName.toLowerCase() === 'script') {
throw new RuntimeError(
RuntimeErrorCode.UNSAFE_VALUE_IN_SCRIPT,
ngDevMode && `"<script>" tag is not allowed as a component host element.`,
);
}
applyRootElementTransform(rootElement as HTMLElement);
return rootElement;
}