如何允许 href='javascript:myFunc()'使用 HTML 净化器?
我有一些在本地编写的 HTML,想通过 HTML 净化器运行它。它完全是我生成的,所以我知道不存在 XSS 漏洞。我试图通过净化器运行它,但是无论我如何尝试, href='javascript:myFunc()' 都会被解析出来。
我当前的设置是:
$string = file_get_contents($myHTMLFile);
$schemes = array (
'http' => true,
'https' => true,
'mailto' => true,
'ftp' => true,
'nntp' => true,
'news' => true,
'javascript' => true,
);
$config = HTMLPurifier_Config::createDefault();
$config->set('URL.AllowedSchemes', array($schemes));
$purifier = new HTMLPurifier($config);
$string = $purifier->purify($string);
这根本不起作用 - 所有 javascript 都被删除。
我已经查看了所有各种 HTML Purifier 配置设置,但找不到我需要的内容。有答案吗?
提前致谢
I have some HTML that I wrote locally and want to run it through HTML purifier. It is entirely generated by me so I know there are no XSS vulnerabilities. I am trying to run it through the purifier, but href='javascript:myFunc()' is parsed out no matter what I try.
My current setup is:
$string = file_get_contents($myHTMLFile);
$schemes = array (
'http' => true,
'https' => true,
'mailto' => true,
'ftp' => true,
'nntp' => true,
'news' => true,
'javascript' => true,
);
$config = HTMLPurifier_Config::createDefault();
$config->set('URL.AllowedSchemes', array($schemes));
$purifier = new HTMLPurifier($config);
$string = $purifier->purify($string);
This isn't working at all - all javascript is stripped out.
I have looked through all the various HTML Purifier config settings but can't find what I need. Are there any answers?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简单的答案:不要在其上使用 HTML Purifier。请改用 HTML Tidy 之类的东西。
更复杂的答案:不要使用 javascript: URL;相反,将类属性附加到您想要具有 JavaScript 的链接,然后让 JavaScript 处理程序查找具有此类的链接并将 JavaScript 代码附加为 onClick 处理程序。
更复杂的答案:创建一个新的 javascript URI 方案并对其进行编程以仅接受您的函数的固定字符串。
Simple answer: Don't use HTML Purifier on it. Use something like HTML Tidy instead.
More complex answer: Don't use javascript: URLs; instead, attach class attributes to links you would like to have JavaScript, and then have a JavaScript handler look for links that have this class and attach the JavaScript code as an onClick handler.
Even more complex answer: Create a new javascript URI scheme and program it only to accept the fixed string which is your function.