admin管理员组文章数量:1418022
I'm playing with bypassSecurityTrust*
functions of Angular. Goal is to get a script
tag to execute on the page. But it either keeps sanitizing with the message
WARNING: sanitizing HTML stripped some content
or I see in the console a
SafeHtmlImpl {changingThisBreaksApplicationSecurity: "<script>alert(1)</script>
.
Goal is to get this working.
What I currently use and tried:
@Pipe({ name: 'safeHtml'})
export class SafeHtmlPipe implements PipeTransform {
constructor(private sanitized: DomSanitizer) {}
transform(value: string): string {
console.log(this.sanitized.sanitize(SecurityContext.NONE, value))
return this.sanitized.sanitize(SecurityContext.NONE, value);
}
}
@Component({
selector: 'app-demo',
templateUrl: './demoponent.html',
styleUrls: ['./demoponent.css']
})
export class DemoComponent implements OnInit {
name: string;
html: string;
constructor(private sanitizer: DomSanitizer) {
this.name = 'Angular2';
this.html = "<script> alert(8) </script>";
}
ngOnInit() {
}
}
and the template html:
<div [innerHtml]="html | safeHtml"></div>
I tried both sanitize
with SecurityContext.NONE
which should work looking at the code and bypassSecurityTrustHtml(value)
. The above code was inspired by this answer.
Any ideas on how to execute that JavaScript?
I'm playing with bypassSecurityTrust*
functions of Angular. Goal is to get a script
tag to execute on the page. But it either keeps sanitizing with the message
WARNING: sanitizing HTML stripped some content
or I see in the console a
SafeHtmlImpl {changingThisBreaksApplicationSecurity: "<script>alert(1)</script>
.
Goal is to get this working.
What I currently use and tried:
@Pipe({ name: 'safeHtml'})
export class SafeHtmlPipe implements PipeTransform {
constructor(private sanitized: DomSanitizer) {}
transform(value: string): string {
console.log(this.sanitized.sanitize(SecurityContext.NONE, value))
return this.sanitized.sanitize(SecurityContext.NONE, value);
}
}
@Component({
selector: 'app-demo',
templateUrl: './demo.ponent.html',
styleUrls: ['./demo.ponent.css']
})
export class DemoComponent implements OnInit {
name: string;
html: string;
constructor(private sanitizer: DomSanitizer) {
this.name = 'Angular2';
this.html = "<script> alert(8) </script>";
}
ngOnInit() {
}
}
and the template html:
<div [innerHtml]="html | safeHtml"></div>
I tried both sanitize
with SecurityContext.NONE
which should work looking at the code and bypassSecurityTrustHtml(value)
. The above code was inspired by this answer.
Any ideas on how to execute that JavaScript?
Share Improve this question edited Sep 17, 2018 at 20:07 Machavity♦ 31.7k27 gold badges95 silver badges105 bronze badges asked Apr 19, 2018 at 14:09 user857990user857990 1,2203 gold badges15 silver badges31 bronze badges 3-
I don't think the sanitizer is the thing that is stopping this from working. It's the regular HTML spec that says
<scripts>
s added in innerHTML won't be executed. See :Can scripts be inserted with innerHTML? and my answer to this question: Why doesn't a browser run a <script> in an HTML fragment retrieved via fetch API?. – zero298 Commented Apr 19, 2018 at 14:17 - So how could I put the string on the page? – user857990 Commented Apr 24, 2018 at 8:24
-
<div id="attack8"> {{ html | safeHtml }} </div>
doesn't work either. Again the changingThisBreaksApplicationSecurity message. – user857990 Commented Apr 24, 2018 at 8:29
1 Answer
Reset to default 2So yes, innerHtml can't insert script tags, but it doesn't stop it from one of the many other ways to inject JavaScript.
Working example:
import { Component, Pipe, PipeTransform, SecurityContext} from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser'
@Pipe({ name: 'safeHtml'})
export class SafeHtmlPipe implements PipeTransform {
constructor(private sanitized: DomSanitizer) {}
transform(value: string) {
console.log(this.sanitized.bypassSecurityTrustHtml(value));
return this.sanitized.bypassSecurityTrustHtml(value);
}
}
@Component({
selector: 'app-demo',
template: `
<div [innerHtml]="html | safeHtml">
</div>
`
})
export class DemoComponent {
html: string;
h_html: string;
constructor(private sanitizer: DomSanitizer) {
this.html = "<svg onload=\"alert(1)\"> blah </svg>"
this.h_html = sanitizer.sanitize(SecurityContext.HTML, "<svg onload=\"alert(2)\"> blah </svg>');
}
}
What doesn't work is
return this.sanitized.sanitize(SecurityContext.HTML, value);
or using
<div [innerHtml]="h_tmpl"></div>
Not sure why. Should behave the same afaiu.
本文标签: javascriptAngularDomSanitizerbypassSecurity scriptStack Overflow
版权声明:本文标题:javascript - Angular, DomSanitizer, bypassSecurity script - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745283073a2651538.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论