【问题标题】:Return from PHP SVG-image and execute internal javascript via <img> tag从 PHP SVG-image 返回并通过 <img> 标签执行内部 javascript
【发布时间】:2023-03-30 05:19:01
【问题描述】:

有php-code,返回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 时: &lt;img src="http://givemesvg.php"/&gt; - SVG 显示正确,但不会触发警报。

这是浏览器安全策略还是遗漏了什么?谢谢。

【问题讨论】:

    标签: javascript php html security svg


    【解决方案1】:

    图片cannot run script。这同样适用于 html &lt;img&gt; 标签、SVG &lt;image&gt; 标签和 CSS 背景图片。

    【讨论】:

      猜你喜欢
      • 2012-11-26
      • 2012-05-02
      • 2020-10-29
      • 2023-03-14
      • 1970-01-01
      • 2016-11-26
      • 2010-12-14
      • 1970-01-01
      相关资源
      最近更新 更多