Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/core/src/render3/instructions/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -180,6 +181,12 @@ export function locateHostElement(
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;
}

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;
}
Expand Down
50 changes: 50 additions & 0 deletions packages/core/test/acceptance/security_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import {NgIf} from '@angular/common';
import {DomSanitizer} from '@angular/platform-browser';
import {
Component,
createComponent,
Directive,
EnvironmentInjector,
inject,
provideZoneChangeDetection,
TemplateRef,
Expand Down Expand Up @@ -852,3 +854,51 @@ describe('innerHTML processing', () => {
expect(fixture.nativeElement.innerHTML).not.toContain('action');
});
});

describe('Component host element validation', () => {
it('should throw an error when dynamically mounting a component onto a script tag', () => {
@Component({
selector: 'my-sink',
template: '',
})
class MySink {}

const scriptHost = document.createElement('script');
document.head.appendChild(scriptHost);

try {
const environmentInjector = TestBed.inject(EnvironmentInjector);
expect(() => {
createComponent(MySink, {
environmentInjector,
hostElement: scriptHost,
});
}).toThrowError(/"<script>" tag is not allowed as a component host element/);
} finally {
scriptHost.remove();
}
});

it('should throw an error when dynamically mounting a component onto an SVG script tag', () => {
@Component({
selector: 'my-svg-sink',
template: '',
})
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,
    });

document.head.appendChild(svgScriptHost);

try {
const environmentInjector = TestBed.inject(EnvironmentInjector);
expect(() => {
createComponent(MySvgSink, {
environmentInjector,
hostElement: svgScriptHost,
});
}).toThrowError(/"<script>" tag is not allowed as a component host element/);
} finally {
svgScriptHost.remove();
}
});
});
Loading