【问题标题】:Codeigniter: Submit textarea with <script> insideCodeigniter:提交带有 <script> 的文本区域
【发布时间】:2023-03-06 11:14:01
【问题描述】:

因为我不知道在哪里搜索这个 - 也许这里有人可以帮助我。

我需要用户在 textarea 中添加类似的内容:

<script> var foo  bar; </script> 
<script type="text/javascript" src="http://foobar.de/mylist.js"></script>

但 Codeigniter 似乎有一个内置的代码注入保护 - 所以提交后我得到的只是:

[removed] var foo  bar; [removed] 
[removed][removed]

我该如何改变呢?我知道它不安全,但我需要解析出 URL。

作为替代,我需要一个 jQuery 函数来解析这个 URL。我对 regEx 不是很熟悉。 ^^

我的 PHP 解析器看起来像这样(从某处复制 ^^):

$reg_exUrl = '/\b(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)[-A-Z0-9+&@#\/%=~_|$?!:,.]*[A-Z0-9+&@#\/%=~_|$]/i';
if (preg_match($reg_exUrl, $_POST['code'], $matches)) {
        $jsUrl = $matches[0];
}

【问题讨论】:

  • 您可以尝试将$config['global_xss_filtering'] = FALSE; 设置为false。这个配置可以在 application/config/config.php 中找到
  • 或者使用$this-&gt;input-&gt;post('textareaName', false),这将在获取该文本区域的值时禁用 XSS 过滤。
  • 谢谢。 Gavin 的解决方案会很好 - 所以我只能在这种情况下使用它 - 没有不安全的所有表格 - 但不幸的是它不起作用。 :(

标签: php jquery regex codeigniter code-injection


【解决方案1】:

@Gavin 的评论效果很好。可能是您没有看到它,因为脚本标签会使它不可见(如果您尝试echoprint 结果)。

function index()
{
    echo form_open();
    echo form_textarea('test');
    echo form_submit('', 'Submit');
    echo form_close();

    if($this->input->post())
    {
        echo "<pre>";

        $textified = str_replace('<', '&lt;', $this->input->post('test', false));
        echo "textified string = $textified <br><br>";

        // find URL. Regex from http://stackoverflow.com/a/2721152/183254
        preg_match_all('/\b(?:(?:https?):\/\/|www\.|ftp\.)[-A-Z0-9+&@#\/%=~_|$?!:,.]*[A-Z0-9+&@#\/%=~_|$]/i', $textified, $result, PREG_PATTERN_ORDER);

        echo "URL in textified string: " . $result[0][0];
    }
}

编辑:

以上内容似乎取决于设置的$config['global_xss_filtering'] = FALSE;。当$config['global_xss_filtering'] = TRUE; 时,无论如何都会过滤所有字段,并且没有办法让选择性字段不通过过滤器。因此,如果将其设置为TRUE,则上述内容将不起作用,就像您的情况一样。

$this-&gt;input-&gt;post() 的默认设置是 XSS 过滤为 false,因此上例中的 false 也是多余的。

似乎唯一的方法是离开$config['global_xss_filtering'] = FALSE; 并在您确实想要 XSS 过滤的所有输入字段上设置$this-&gt;input-&gt;post('filtered_item', true);,并仅对您在其中的字段使用$this-&gt;input-&gt;post('unfiltered_item');需要脚本标签。

http://ellislab.com/forums/viewthread/182774/#871558

【讨论】:

  • 谢谢,但这对我不起作用——我不知道为什么。我仍然有字符串中的所有 [removed] - 并且没有
  • 谢谢。你是对的。将 XSS 过滤器设置为 false 时,它​​可以工作 - 但无法更改所有其他输入。所以我决定制作一个 jQuery 解决方案,它可以从字符串中解析出我需要的 url...
猜你喜欢
  • 1970-01-01
  • 2015-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多