为什么富文本内容中,标签的style属性被clean_xss过滤掉了,style有xss隐患?
富文本编辑器
安全

1个回答默认排序 投票数排序
YANG001
YANG001
这家伙很懒,什么也没写~
1年前

XSS

html中有很多属性都是安全的,因为它们被视为文本,永远不会执行它,比如:alt,align,rows,title,...,如上图,这其中并不应该包含style,如果确实要允许style属性,可以对clean_xss进行重写,或者写一个新的clean_xss_custom

// app/common.php文件

/**
 * 清理XSS
 * 通常只用于富文本,比 filter 慢
 * @param string $string
 * @return string
 */
function clean_xss(string $string): string
{
    // return (new AntiXSS())->xss_clean($string);

    // 修改为
    $antiXss = new AntiXSS();
    $antiXss->removeEvilAttributes(['style']); // 允许 style 属性
    $antiXss->setReplacement('cleanXss'); // 检查到xss代码之后使用cleanXss替换它
    return $antiXss->xss_clean($string);
}

做出以上修改后:

将被过滤为:
<li style="list-style-image: url(alert&#40;0&#41;)">

也就是说,对于XSS代码部分的任然有过滤/转义作用,并且允许了style属性;

但任然建议仅当您有充分的理由时才使用此方法。

关于voku/anti-xss还有更多使用说明和案例,请 参考这里

Zxcvbnm
Zxcvbnm回复YANG001
这家伙很懒,什么也没写~
1年前

我这里是表单提交的富文本字段整个html标签全部都被过滤掉了,要怎么设置才能在某个控制器内不过滤呢?

YANG001
YANG001回复Zxcvbnm
这家伙很懒,什么也没写~
1年前
请先登录
0
0
1
3