Skip to content

Commit 248efae

Browse files
committed
Added safe pipe to fix angular xss warnings
1 parent 7ac9f6d commit 248efae

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

projects/demo/src/app/app.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import en from '@angular/common/locales/en';
1111
registerLocaleData(en);
1212

1313
import { AppComponent } from './app.component';
14-
import { NgxDiff2htmlModule } from 'ngx-diff2html';
14+
//import { NgxDiff2htmlModule } from 'ngx-diff2html';
15+
import { NgxDiff2htmlModule } from 'projects/ngx-diff2html/src/public-api';
1516

1617
@NgModule({
1718
declarations: [

projects/ngx-diff2html/src/lib/ngx-diff2html.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { DiffFormat, DiffStyle } from './ngx-diff2html.model';
55
@Component({
66
selector: 'ngx-diff2html',
77
template: `
8-
<div [innerHtml]="diffHTML"></div>
8+
<div [innerHtml]="diffHTML | safe:'html'"></div>
99
`,
1010
styles: []
1111
})

projects/ngx-diff2html/src/lib/ngx-diff2html.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { NgModule } from '@angular/core';
22
import { NgxDiff2htmlComponent } from './ngx-diff2html.component';
3+
import { SafePipe } from '../pipes/safe.pipe';
34

45

56

67
@NgModule({
78
declarations: [
8-
NgxDiff2htmlComponent
9+
NgxDiff2htmlComponent,
10+
SafePipe
911
],
1012
imports: [
1113
],
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Stolen from: https://github.com/embarq/safe-pipe/blob/master/projects/safe-pipe/src/lib/safe-pipe.pipe.ts
3+
*/
4+
import { Pipe, PipeTransform } from '@angular/core';
5+
import { DomSanitizer, SafeStyle, SafeResourceUrl, SafeScript, SafeHtml, SafeUrl } from '@angular/platform-browser';
6+
7+
export type SafePipeType = 'html' | 'style' | 'script' | 'url' | 'resourceUrl';
8+
9+
@Pipe({
10+
name: 'safe',
11+
pure: true
12+
})
13+
export class SafePipe implements PipeTransform {
14+
constructor(protected sanitizer: DomSanitizer) { }
15+
16+
public transform(value: string, type: SafePipeType): SafeHtml | SafeStyle | SafeScript | SafeUrl | SafeResourceUrl {
17+
switch (type) {
18+
case 'html':
19+
return this.sanitizer.bypassSecurityTrustHtml(value);
20+
case 'style':
21+
return this.sanitizer.bypassSecurityTrustStyle(value);
22+
case 'script':
23+
return this.sanitizer.bypassSecurityTrustScript(value);
24+
case 'url':
25+
return this.sanitizer.bypassSecurityTrustUrl(value);
26+
case 'resourceUrl':
27+
return this.sanitizer.bypassSecurityTrustResourceUrl(value);
28+
default:
29+
throw new Error(`SafePipe unable to bypass security for invalid type: ${type}`);
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)