Skip to content
Closed
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') {
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');
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