Skip to content

Commit ac3e9dc

Browse files
authored
Mark icon svg as trusted HTML (#82)
This prevents CSP errors in some contexts. The alternative is to create a specific policy for feather-icons but this seems unecessary since we're already integrating with Angular. The icons are embedded as static string resources so this should be no problem for safety.
1 parent e989624 commit ac3e9dc

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

projects/angular-feather/src/lib/feather.component.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import {
66
ChangeDetectorRef,
77
OnChanges,
88
SimpleChanges,
9+
SecurityContext,
910
} from '@angular/core';
11+
import { DomSanitizer } from '@angular/platform-browser';
1012
import { Icons } from './icons.provider';
1113
import { uppercamelcase } from './utils';
1214

@@ -21,7 +23,8 @@ export class FeatherComponent implements OnChanges {
2123
constructor(
2224
@Inject(ElementRef) private elem: ElementRef,
2325
@Inject(ChangeDetectorRef) private changeDetector: ChangeDetectorRef,
24-
@Inject(Icons) private icons: Icons
26+
@Inject(Icons) private icons: Icons,
27+
@Inject(DomSanitizer) private sanitizer: DomSanitizer,
2528
) {}
2629

2730
ngOnChanges(changes: SimpleChanges) {
@@ -36,7 +39,9 @@ export class FeatherComponent implements OnChanges {
3639
);
3740
}
3841

39-
this.elem.nativeElement.innerHTML = svg;
42+
// Since the icons are precompiled we can trust them as safe html.
43+
this.elem.nativeElement.innerHTML = this.sanitizer.sanitize(SecurityContext.HTML, this.sanitizer.bypassSecurityTrustHtml(svg));
44+
4045
this.changeDetector.markForCheck();
4146
}
4247
}

0 commit comments

Comments
 (0)