pin*_*hic 4 html php html-parsing htmlpurifier
根据HTML Purifier smoketest,偶尔会丢弃"格式错误"的URI以留下无属性的锚标记,例如
<a href="javascript:document.location='http://www.google.com/'">XSS</a> 变 <a>XSS</a>
...以及偶尔被剥离到协议,例如
<a href="http://1113982867/">XSS</a> 变 <a href="http:/">XSS</a>
虽然这本身没有问题,但它有点难看.我没有试图用正则表达式去除这些,而是希望使用HTML Purifier自己的库功能/注入器/插件/ whathaveyou.
有条件地删除HTMLPurifier中的属性很容易.这里的库为类HTMLPurifier_AttrTransform提供了方法confiscateAttr().
虽然我不亲自使用的功能confiscateAttr(),我使用HTMLPurifier_AttrTransform按照这个线程添加target="_blank"到所有的锚.
// more configuration stuff up here
$htmlDef = $htmlPurifierConfiguration->getHTMLDefinition(true);
$anchor = $htmlDef->addBlankElement('a');
$anchor->attr_transform_post[] = new HTMLPurifier_AttrTransform_Target();
// purify down here
Run Code Online (Sandbox Code Playgroud)
HTMLPurifier_AttrTransform_Target 当然,这是一个非常简单的课程.
class HTMLPurifier_AttrTransform_Target extends HTMLPurifier_AttrTransform
{
public function transform($attr, $config, $context) {
// I could call $this->confiscateAttr() here to throw away an
// undesired attribute
$attr['target'] = '_blank';
return $attr;
}
}
Run Code Online (Sandbox Code Playgroud)
那部分就像一个魅力,自然而然.
也许我不是在眯着眼睛盯着HTMLPurifier_TagTransform,或者我正在寻找错误的地方,或者一般都不理解它,但我似乎无法想办法有条件地删除元素.
说,有效的事情:
// more configuration stuff up here
$htmlDef = $htmlPurifierConfiguration->getHTMLDefinition(true);
$anchor = $htmlDef->addElementHandler('a');
$anchor->elem_transform_post[] = new HTMLPurifier_ElementTransform_Cull();
// add target as per 'point of reference' here
// purify down here
Run Code Online (Sandbox Code Playgroud)
使用Cull类扩展具有confiscateElement()能力或可比性的东西,其中我可以检查缺少的href属性或href具有内容的属性http:/.
我知道我可以创建一个过滤器,但是示例(Youtube.php和ExtractStyleBlocks.php)建议我使用正则表达式,如果可能的话,我真的宁愿避免使用.我希望有一个板载或准板载解决方案,利用HTML Purifier的出色解析功能.
不幸的是,null在一个儿童班回来HTMLPurifier_AttrTransform并没有削减它.
任何人都有任何聪明的想法,还是我坚持使用正则表达式?:)
成功!感谢Ambush Commander和mcgrailm在另一个问题上,我现在使用一个非常简单的解决方案:
// a bit of context
$htmlDef = $this->configuration->getHTMLDefinition(true);
$anchor = $htmlDef->addBlankElement('a');
// HTMLPurifier_AttrTransform_RemoveLoneHttp strips 'href="http:/"' from
// all anchor tags (see first post for class detail)
$anchor->attr_transform_post[] = new HTMLPurifier_AttrTransform_RemoveLoneHttp();
// this is the magic! We're making 'href' a required attribute (note the
// asterisk) - now HTML Purifier removes <a></a>, as well as
// <a href="http:/"></a> after HTMLPurifier_AttrTransform_RemoveLoneHttp
// is through with it!
$htmlDef->addAttribute('a', 'href*', new HTMLPurifier_AttrDef_URI());
Run Code Online (Sandbox Code Playgroud)
它有效,它有效,bahahahaHAHAHAHAnhͥͤͫğͮ͑̆ͦó̓̉ͬ͋hͧ̆̈̉ğ̈͐̈a̾̈̑ͨô̔̄̑̇ḡh̘̝͊̐ͩͥ̋ͤ͛g̦̣̙̙̒ͥ̐̔o̤̣hg͓̈͋̇̓̆ä͖̩̯̥͕̐ͮ̒o̶ͬ̽̍ͮ̾ͮ͢҉̩͉̘͓̙̦̩̹͍̹̠̕g̵̡͔̙͉̠̙̩͚͑ͥ̓͛̋͗̍̽͋͑̈̚...... !*躁狂的笑声,潺潺的声音,脸上带着微笑的龙骨*
| 归档时间: |
|
| 查看次数: |
3008 次 |
| 最近记录: |