-
Notifications
You must be signed in to change notification settings - Fork 27.2k
fix(core): reject script element as a dynamic component host #68713
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,9 @@ import {NgIf} from '@angular/common'; | |
| import {DomSanitizer} from '@angular/platform-browser'; | ||
| import { | ||
| Component, | ||
| createComponent, | ||
| Directive, | ||
| EnvironmentInjector, | ||
| inject, | ||
| provideZoneChangeDetection, | ||
| TemplateRef, | ||
|
|
@@ -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'); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
| } | ||
| }); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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).