如何允许 href='javascript:myFunc()'使用 HTML 净化器?

发布于 2024-11-01 06:34:14 字数 688 浏览 0 评论 0原文

我有一些在本地编写的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浊酒尽余欢 2024-11-08 06:34:14

简单的答案:不要在其上使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文