2015-05-21 23 views
0

有php代碼,它返回svg-image。從PHP SVG圖像返回並通過<img>執行內部JavaScript代碼

public function actionJsTest() { 
    header('Content-Type: image/svg+xml'); 
    $svg = ''; 
    $svg .= '<svg width="100%" height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">'; 
    $svg .= '<script type="text/javascript">'; 
    $svg .= '<![CDATA['; 
    $svg .= 'alert();'; 
    $svg .= ']]>'; 
    $svg .= '</script>'; 
    $svg .= '<rect x="0" y="0" height="10" width="10" style="stroke-width:1; stroke:black; fill:red"></rect>'; 
    $svg .= '<circle cx="250" cy="250" r="50" stroke="red" stroke-width="1" fill="red"></circle>'; 
    $svg .= '</svg>'; 

    echo $svg; 
} 

當我們直接在視圖中調用php函數時,它的工作正確並且警報觸發。

當我們使用標籤從html頁面顯示svg時: <img src="http://givemesvg.php"/> - SVG顯示正確,但警報不會觸發。

這是瀏覽器安全策略還是漏掉了一些東西?謝謝。

回答

1

圖片cannot run script。這同樣適用於html <img>標記,SVG <image>標記和CSS背景圖像。