2016-09-24 18 views
12

我用<div [innerHTML]="body"></div>來轉義HTML傳遞給我的模板,當我傳給bodydiv與屬性id,角投:Angular 2:消毒HTML剝離了一些內容與div ID - 這是錯誤或功能?

警告:清理HTML剝離某些內容(見http://g.co/ng/security#xss)。 警告:消毒HTML會剝離某些內容(請參閱http://g.co/ng/security#xss)。 警告:消毒HTML會剝離某些內容(請參閱http://g.co/ng/security#xss)。

See. plunker

那麼爲什麼說這個?什麼可能是危險iddiv?這個bug可以嗎?

回答

4

這是因爲id屬性不安全。

這不是我的答案,但它會回答你的問題:https://security.stackexchange.com/questions/88973/why-do-id-attributes-need-stricter-validation


對於idname,這些屬性經常用作DOM參考點。

如果攻擊者可以欺騙這些參考點,她可能會欺騙現有腳本,從非設計的位置獲取和設置值,這可能是危險的,具體取決於使用的上下文。從我


注:他對name屬性會談後的休息,但如果你不已經是頭上


這也你會得到這一切背後的想法適用於HTML表單,其中name用於標識名稱/值對。例如,如果一個網站在輸出時沒有對特定的表單字段進行編碼,但由於表單字段是服務器生成的,並且表單通過使用令牌來防止CSRF,因此無法通過常規手段進行利用。但是,攻擊者可能能夠誘使用戶訪問帶有在name中使用的參數的URL,其中包含在提交表單時執行的XSS有效內容。

例如正常使用:

https://example.com/product?item_name=watch&qty=1 

這使得形式

<form> 

<input type="hidden" name="watch" value="1" /> 
<input type="hidden" name="shop_name" value="Bob's Supplies" /> 
<input type="hidden" name="anti-csrf" value="asdjasodhoai" /> 

<input type="submit" value="Click here to buy" /> 

</form> 

然後獲取輸出

Thank you for buying from Bob's Supplies. 

然而,攻擊者可以發送一個鏈接給用戶,像這樣:

https://example.com/product?item_name=shop_name&qty=<script>alert('xss')</script> 

由於申請是正確此時HTML編碼它呈現形式

<form> 

<input type="hidden" name="shop_name" value="&lt;script&gt;alert(&#039;xss&#039;)&lt;/script&gt;" /> 
<input type="hidden" name="shop_name" value="Bob's Supplies" /> 
<input type="hidden" name="anti-csrf" value="asdjasodhoai" /> 

<input type="submit" value="Click here to buy" /> 

</form> 

這然後獲取輸出

Thank you for buying from <script>alert('xss')</script>. 

,因爲這個頁面不HTML編碼shop_name參數,因爲它是可信和應用程序框架總是在重複的情況下取第一個值。非常有意思,但這是我首先想到的一點。

19

簡單的辦法就是寫管道像

import { Pipe, PipeTransform } from "@angular/core"; 
import { DomSanitizer, SafeHtml } from '@angular/platform-browser'; 

@Pipe({ 
    name: 'sanitizeHtml' 
}) 
export class SanitizeHtmlPipe implements PipeTransform { 

    constructor(private _sanitizer:DomSanitizer) { 
    } 

    transform(v:string):SafeHtml { 
    return this._sanitizer.bypassSecurityTrustHtml(v); 
    } 
} 

添加在HTML文件中添加一堆像

<td *ngIf="i>0" [innerHTML]="entry.attributes[i] | sanitizeHtml"></td> 
+1

與卡爾從這個問題的答案在一起:http://stackoverflow.com/問題/ 39007130/the-pipe-could-be-found-angular2-custom-pipe對於Sanitizer的這個問題很好的解決方法 – sanyooh

+0

@sanyooh這個工作很好,但我得到'undefined'在我的' innerHTML]'顯示內容。 '

' – fidev

+0

SafeHtml?應從 –