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
10 changes: 10 additions & 0 deletions packages/core/src/render3/component_ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,15 @@ function createHostElement(componentDef: ComponentDef<unknown>, renderer: Render
return createElementNode(renderer, tagName, namespace);
}

function assertNotScriptHostElement(tagName: string | null | undefined): void {
if (tagName?.toLowerCase() === 'script') {
throw new RuntimeError(
RuntimeErrorCode.UNSAFE_VALUE_IN_SCRIPT,
ngDevMode && `"<script>" tag is not allowed as a component host element.`,
);
}
}

/**
* Infers the tag name that should be used for a component based on its definition.
* @param componentDef Definition for which to resolve the tag name.
Expand Down Expand Up @@ -303,6 +312,7 @@ export class ComponentFactory<T> {
const hostElement = rootSelectorOrNode
? locateHostElement(hostRenderer, rootSelectorOrNode, cmpDef.encapsulation, rootViewInjector)
: createHostElement(cmpDef, hostRenderer);
assertNotScriptHostElement(hostElement?.tagName);

const sharedStylesHost = rootViewInjector.get(SHARED_STYLES_HOST, null);
const styleHost = getStyleHost(
Expand Down
7 changes: 0 additions & 7 deletions packages/core/src/render3/instructions/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {ViewEncapsulation} from '../../metadata/view';
import {validateAgainstEventProperties} from '../../sanitization/sanitization';

import {ProfilerEvent} from '../../../primitives/devtools';
import {RuntimeError, RuntimeErrorCode} from '../../errors';
import {normalizeDebugBindingName, normalizeDebugBindingValue} from '../../ng_reflect';
import {assertIndexInRange, assertNotSame} from '../../util/assert';
import {escapeCommentText} from '../../util/dom';
Expand Down Expand Up @@ -181,12 +180,6 @@ 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
13 changes: 13 additions & 0 deletions packages/core/test/acceptance/security_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,19 @@ describe('innerHTML processing', () => {
});

describe('Component host element validation', () => {
it('should throw an error when dynamically creating a component with a script selector', () => {
@Component({
selector: 'script',
template: '',
})
class ScriptHost {}

const environmentInjector = TestBed.inject(EnvironmentInjector);
expect(() => {
createComponent(ScriptHost, {environmentInjector});
}).toThrowError(/"<script>" tag is not allowed as a component host element/);
});

it('should throw an error when dynamically mounting a component onto a script tag', () => {
@Component({
selector: 'my-sink',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@
"areAnimationSupported",
"arrRemove",
"assertNotDestroyed",
"assertNotScriptHostElement",
"assertTypeDefined",
"attachPatchData",
"balancePreviousStylesIntoKeyframes",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@
"areAnimationSupported",
"arrRemove",
"assertNotDestroyed",
"assertNotScriptHostElement",
"assertTypeDefined",
"attachPatchData",
"baseElement",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@
"areAnimationSupported",
"arrRemove",
"assertNotDestroyed",
"assertNotScriptHostElement",
"assertTypeDefined",
"attachPatchData",
"bind",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@
"assertAllValuesPresent",
"assertControlPresent",
"assertNotDestroyed",
"assertNotScriptHostElement",
"assertPlatform",
"assertTypeDefined",
"attachPatchData",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@
"assertAllValuesPresent",
"assertControlPresent",
"assertNotDestroyed",
"assertNotScriptHostElement",
"assertPlatform",
"assertTypeDefined",
"attachPatchData",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@
"areAnimationSupported",
"arrRemove",
"assertNotDestroyed",
"assertNotScriptHostElement",
"assertTypeDefined",
"attachPatchData",
"baseElement",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@
"arrRemove",
"arrayEquals",
"assertNotDestroyed",
"assertNotScriptHostElement",
"assertTypeDefined",
"attachPatchData",
"baseElement",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@
"areAnimationSupported",
"arrRemove",
"assertNotDestroyed",
"assertNotScriptHostElement",
"assertTypeDefined",
"attachPatchData",
"baseElement",
Expand Down
Loading