gpt4 book ai didi

php - 在 PHP 中清理用户定义的 CSS

转载 作者:IT王子 更新时间:2023-10-29 00:12:30 24 4
gpt4 key购买 nike

我想允许用户在我的论坛上为他们的个人资料使用他们自己的样式表,但我担心可能存在安全漏洞。有人有清理 CSS 的任何提示吗?

基本流程:用户在表单中输入CSS -> 保存到DB -> 输出为内联CSS

最佳答案

HTMLPurifierCSSTidy做你正在寻找的东西。

HTMLPurifier 主要用于清理 HTML,但也可以选择使用 CSSTidy 提取样式 block 。

HTMLPurifier 文档中有一个示例(可惜,我已经用完了每个帖子的两个链接。)

还有一个:

require_once './htmlpurifier/library/HTMLPurifier.auto.php';
require_once './csstidy/class.csstidy.php';

// define some css
$input_css = "
body {
margin: 0px;
padding: 0px;
/* JS injection */
background-image: url(javascript:alert('Injected'));
}
a {
color: #ccc;
text-decoration: none;
/* dangerous proprietary IE attribute */
behavior:url(hilite.htc);
/* dangerous proprietary FF attribute */
-moz-binding: url('http://virus.com/htmlBindings.xml');
}
.banner {
/* absolute position can be used for phishing */
position: absolute;
top: 0px;
left: 0px;
}
";

// Create a new configuration object
$config = HTMLPurifier_Config::createDefault();
$config->set('Filter.ExtractStyleBlocks', TRUE);

// Create a new purifier instance
$purifier = new HTMLPurifier($config);

// Turn off strict warnings (CSSTidy throws some warnings on PHP 5.2+)
$level = error_reporting(E_ALL & ~E_STRICT);

// Wrap our CSS in style tags and pass to purifier.
// we're not actually interested in the html response though
$html = $purifier->purify('<style>'.$input_css.'</style>');

// Revert error reporting
error_reporting($level);

// The "style" blocks are stored seperately
$output_css = $purifier->context->get('StyleBlocks');

// Get the first style block
echo $output_css[0];

输出是:

body {
margin:0;
padding:0;
}

a {
color:#ccc;
text-decoration:none;
}

.banner {
}

关于php - 在 PHP 中清理用户定义的 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3241616/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com