【问题标题】:Joomla's JInput strips HTML with every filterJoomla 的 JInput 使用每个过滤器去除 HTML
【发布时间】:2013-10-17 12:36:54
【问题描述】:

我正在尝试在 Joomla 2.5 中将 HTML 文本安全地保存到数据库中,因此我使用 JInput 来获取表单数据。

根据developer.joomla.org,有HTML过滤器:

HTML - 返回一个完整的 HTML 实体和标签的字符串,受制于 过滤器中的白名单或黑名单。

根据docs.joomla.org,有这些过滤器应该(从逻辑上说。他们没有在那里解释)通过HTML标签:

RAW、HTML、SAFE_HTML

在 JInput 用于过滤的代码 JFilterInput::clean 中,没有 SAFE_HTML 过滤器。我不知道它在一个文档中做了什么以及为什么另一个文档中缺少 RAW 过滤器。除此之外,所有这些过滤器都会去除 HTML 标签。

只需 $_POST:

$_POST['shortDescription'];

返回

<b>Hello <i>world</i></b>

当我使用 JInput 时:

$input->get('shortDescription', '', 'RAW');
$input->get('shortDescription', '', 'HTML');
$input->get('shortDescription', '', 'SAFE_HTML');

全部返回

Hello world

没有 HTML 标签。那是为了什么?需要安全存储 HTML 时如何使用?

【问题讨论】:

    标签: php joomla joomla2.5


    【解决方案1】:

    我用这个方法绕过了它:

    public function getHtmlInput($htmlText)
    {
        $input_options = JFilterInput::getInstance(
            array(
                'img','p','a','u','i','b','strong','span','div','ul','li','ol','h1','h2','h3','h4','h5',
                'table','tr','td','th','tbody','theader','tfooter','br'
            ),
            array(
                'src','width','height','alt','style','href','rel','target','align','valign','border','cellpading',
                'cellspacing','title','id','class'
            )
        );
    
        $postData = new JInput($_POST, array('filter' => $input_options));
    
        return $postData->get($htmlText, '', 'HTML');
    }
    

    用法:

    $this->getHtmlInput('documentation');
    

    我希望这在 Joomla 3 中得到解决...

    【讨论】:

    • 我刚刚在 Joomla 中检查过这个! 3.2.2 似乎“$my_var = $jinput->get('my_field', 'my default', 'html');”仍然会错误地剥离 HTML。不过,我将深入研究 Joomla 代码
    【解决方案2】:

    你应该这样做:

    $jinput = JFactory::getApplication()->input;
    $html = JComponentHelper::filterText($jinput->post->get('shortDescription', '', 'raw'));
    

    【讨论】:

      【解决方案3】:

      这是一篇旧帖子,但我想我会投入 2 美分,因为它可能会帮助人们找到此帖子以寻找解决方案。

      使用 html 编辑器,它仍然使用 HTML 过滤器去除 html。为了解决这个问题,我使用 ARRAY 作为过滤器,然后将结果内爆。

      简单易懂。

      【讨论】:

      • 这实质上是用这种方法绕过了 Joomla 的内置保护。要非常小心,相信任何使用您的 HTML 编辑器的人。否则,您将接受托管各种恶意内容。
      • @JohnRix 是正确的,无论如何,我都会很粗略地让某人从面向前端的表单提交 html。我的解决方案假设这是一个管理员方面的事情,你知道它不会是恶意的。
      【解决方案4】:

      (在 Joomla 3.x 的上下文中)JInputFilter 实例的默认配置是在白名单模式下运行,具有白名单标签和属性的空数组,即。最严格的 HTML 过滤模式,可以有效地摆脱一切。

      这显然不是开箱即用,但它选择了安全性而不是便利性,并让开发人员有意识地决定放宽安全性以通过使用接受接收内容中的标签和属性一个备用的 JInputFilter 实例,或者:

      A)具有指定的标签白名单(@Jon 最终在他自己的答案中做了什么)

      $filter = JInputFilter::getInstance(array('img', ...), array('src', ...));
      

      B) 配置为在黑名单模式下运行

      $filter = JInputFilter::getInstance([], [], 1, 1);
      

      顺便说一句,除非您禁用 $xssAuto 选项(请参阅下面的用法),否则 Joomla 将强制执行以下黑名单,而不管 JInputFilter 实例配置为哪种模式:

      标签:'applet'、'body'、'bgsound'、'base'、'basefont'、'embed'、'frame'、'frameset'、'head'、'html '、'id'、'iframe'、'ilayer'、'layer'、'link'、'meta'、'name'、'object'、'script'、'style'、'title'、'xml'

      属性:'action'、'background'、'codebase'、'dynsrc'、'lowsrc'

      作为参考,这里是JFilterInput::getInstance方法的使用信息:

      /**
       * Returns an input filter object, only creating it if it doesn't already exist.
       *
       * @param   array    $tagsArray   List of user-defined tags
       * @param   array    $attrArray   List of user-defined attributes
       * @param   integer  $tagsMethod  WhiteList method = 0, BlackList method = 1
       * @param   integer  $attrMethod  WhiteList method = 0, BlackList method = 1
       * @param   integer  $xssAuto     Only auto clean essentials = 0, Allow clean blacklisted tags/attr = 1
       * @param   integer  $stripUSC    Strip 4-byte unicode characters = 1, no strip = 0, ask the database driver = -1
       *
       * @return  JFilterInput  The JFilterInput object.
       *
       * @since   11.1
       */
      public static function &getInstance($tagsArray = array(), $attrArray = array(), $tagsMethod = 0, $attrMethod = 0, $xssAuto = 1, $stripUSC = -1)
      

      Joomla 还在管理界面的“全局配置”页面的“文本过滤器”选项卡上提供可配置的过滤规则。在这里,您可以配置操作模式,以及按用户组过滤的标签和属性。要在您自己的代码中利用这一点,请根据@xavip 的回答使用JComponentHelper::filterText() 方法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-26
        • 1970-01-01
        • 1970-01-01
        • 2013-03-16
        • 1970-01-01
        • 2015-07-10
        相关资源
        最近更新 更多