有PHP代码,它返回svg-image。

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显示正确,但不会触发警报。

是浏览器安全策略还是错过了什么?谢谢。

最佳答案

图像cannot run script。这同样适用于html <img>标记,SVG <image>标记和CSS背景图像。

09-10 06:51
查看更多