question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Should Dom Sanitization allow Angular Elements?

See original GitHub issue

I’m submitting a…


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[ ] Performance issue
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
[ ] Other... Please describe:

Current behavior

Angular Elements are great for creating standalone units, capable of running in a non Angular context.

This use case is great, but I also think it makes sense to run Angular Elements inside regular Angular applications. IMO, the main advantage is the ability to add Angular content to the DOM dynamically at runtime. An example use case is some form of a content publishing system with highly configurable UI.

The fact that Angular Elements can inherit the injector from the parent Angular app makes this super flexible. In addition to more dynamic DOM, it also opens the door to lazy loading of the Javascript backing the element (without the restriction of routing).

Previously this was very hard since template changes are not possible at runtime when doing AoT.

Expected behavior

It would be great if we could seamlessly bind Angular Elements to the DOM at runtime.

Minimal reproduction of the problem with instructions

Assume a case where you are binding a piece of html to the DOM at runtime (innerHTML)

content = "<my-angular-element></<my-angular-element>"
<div [innerHTML]="content"></div>

The code above will not work by default since the Angular DOM sanitizer will strip out the unknown element tag.

The current workaround is to manually bypass the template sanitization by running bypassSecurityTrustHtml. However, this turns off sanitization for the entire html fragment. Ideally we should still be able to run sanitization over anything that is not whitelisted.

Does it seem reasonable for the sanitizer to whitelist Angular Elements that are part of the hosting application?

One challenge of course is that the current api does not keep track of the registered element tags at the application level, but maybe it could?

What is the motivation / use case for changing the behavior?

Main use case is better support for dynamic Angular applications, without opting out of the important performance gains from AoT.

Thought?

cc: @robwormald @mhevery @IgorMinar

Environment


Angular version: X.Y.Z


Browser:
- [ ] Chrome (desktop) version XX
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
 
For Tooling issues:
- Node version: XX  
- Platform:  

Others:

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
robwormaldcommented, Oct 4, 2018

My personal belief is that Angular Elements should not be treated as “special”… so the question is probably broader, to Custom Elements in general. We should definitely have a look at this, but just to set expectations, its not likely to happen soon, as messing with the Sanitizer requires Google Security reviews and such 😃

Note on a.io we just forgo the sanitizer, as we expect our own content to be trusted: https://github.com/angular/angular/blob/master/aio/src/app/layout/doc-viewer/doc-viewer.component.ts#L137

For an extra modicum of security, rather than going straight to innerHTML from a string of HTML, throw it into an HTML Template first.

0reactions
angular-automatic-lock-bot[bot]commented, Jul 12, 2021

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

DomSanitizer - Angular
DomSanitizer helps preventing Cross Site Scripting Security bugs (XSS) by sanitizing values to be safe to use in the different DOM contexts.
Read more >
Angular 2 Security — The DomSanitizer Service - Netanel Basal
Sanitization modifies the input, turning it into a value that is safe to insert into the DOM. In Angular sanitization depends on context,...
Read more >
Security - Angular - w3resource
Angular sanitizes untrusted values for HTML, styles, and URLs; sanitizing resource URLs isn't possible because they contain arbitrary code. In ...
Read more >
Correct way Provide DomSanitizer to Component with Angular ...
You don't need to declare providers : [ DomSanitizer ] anymore. Just need to import DomSanitizer as shown below, import { DomSanitizer ...
Read more >
Using Angular innerHtml to display user-generated content ...
Angular comes with a built-in html sanitizer DomSanitizer , as a security feature, thats used whenever you use [innerHtml] . Its a great...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found