【问题标题】:PHP input sanitizer?PHP输入消毒剂?
【发布时间】:2010-04-30 14:15:06
【问题描述】:

有哪些好的 PHP html(输入)消毒剂?

最好是,如果内置了某些东西 - 我希望我们这样做。

更新

根据请求,通过 cmets,输入应该允许 HTML(并且显然要防止 XSS 和 SQL 注入等)。

【问题讨论】:

  • 我认为这个问题需要更多信息;您是在谈论允许用户直接输入 HTML,并对其进行清理以删除诸如<script> 之类的标签吗?同时,我想说一些基本的建议阅读对于任何寻找类似信息的人来说都是“基本 PHP 安全”一书中关于过滤输入的部分 - books.google.ca/…
  • 出于什么特定目的您需要消毒?
  • @Boris,您提供的链接似乎只与 XSS 相关。 SQL 注入呢?
  • SQL 注入依赖于 SQL 服务器,你不应该依赖第三方库,而应该依赖你的 sql 软件提供的函数,比如 MySQL 的 mysql_real_escape_string()

标签: php input xss sanitization input-sanitization


【解决方案1】:

html 净化器 -> http://htmlpurifier.org/

【讨论】:

  • 嗯。它说它消除了 XSS 攻击,但我的 XSS 攻击字符串(来自ha.ckers.org/xss.html)干净利落。
【解决方案2】:

我一直使用PHP 的addslashes() 和stripslashes() 函数,但我也只看到了内置的filter_var() 函数(link)。貌似built-in filters不少。

【讨论】:

  • 你使用 PHP 的 addlashes() 和 stripslashes() 函数是为了什么目的?
【解决方案3】:

如果您想运行使用$_GET['user'] 的查询,一个不错的解决方案是使用mysql_real_escape_string() 执行类似的操作:

<?php

    $user = mysql_real_escape_string($_GET['user']);
    $SQL = "SELECT * FROM users WHERE username = '$name'";

    //run $SQL now
    ...
?>

如果您想将文本存储在数据库中,然后将其打印到网页上,请考虑使用htmlentities

[Edit]或者如awshepard所说,你可以使用addslashes()stripslashes()函数[/Edit]

下面是一个关于防止 XSS 攻击的清理示例:

<?php
    $str = "A 'quote' is <b>bold</b>";

    //Outputs: A 'quote' is <b>bold</b>
    echo $str;

    // Outputs: A 'quote' is &lt;b&gt;bold&lt;/b&gt;
    echo htmlentities($str);

    // Outputs: A &#039;quote&#039; is &lt;b&gt;bold&lt;/b&gt;
    echo htmlentities($str, ENT_QUOTES);
?>

【讨论】:

  • 您的 MySQL 示例不会让您接受 SQL 注入(您的示例应该使用准备好的语句)
  • 我不使用准备好的语句。我在示例中使用 mysql_real_escape_string(),在我的 PHP 代码中很多,直到现在我无法找到一种方法来利用我编写的代码。也许我没有看到可以绕过 mysql_real_escape_string() 的可能攻击向量,所以如果你有一个例子,请告诉我。我总是想学点新东西。
  • @Dr Optix,StackOverflow 确认 mysql_real_escape_string() 并不总能阻止 SQL 注入 --> stackoverflow.com/questions/1220182/…
【解决方案4】:

使用

 $input_var=sanitize_input($_POST);

功能在下面,几乎可以清理您需要的所有东西

function sanitize($var, $santype = 1){
     if ($santype == 1) {return strip_tags($var);}
     if ($santype == 2) {return htmlentities(strip_tags($var),ENT_QUOTES,'UTF-8');}
     if ($santype == 3) 
     {
      if (!get_magic_quotes_gpc()) {
       return addslashes(htmlentities(strip_tags($var),ENT_QUOTES,'UTF-8'));
      } 
      else {
         return htmlentities(strip_tags($var),ENT_QUOTES,'UTF-8');
      }
     }
    }

    function sanitize_input($input,$escape_mysql=false,$sanitize_html=true,
             $sanitize_special_chars=true,$allowable_tags='<br><b><strong><p>')
    {
      unset($input['submit']); //we use 'submit' variable for all of our form

      $input_array = $input;

      //array is not referenced when passed into foreach
      //this is why we create another exact array
      foreach ($input as $key=>$value)
      {
       if(!empty($value))
       {
        $input_array[$key]=strtolower($input_array[$key]);
        //stripslashes added by magic quotes
        if(get_magic_quotes_gpc()){$input_array[$key]=sanitize($input_array[$key]);} 

        if($sanitize_html){$input_array[$key] = strip_tags($input_array[$key],$allowable_tags);}

        if($sanitize_special_chars){$input_array[$key] = htmlspecialchars($input_array[$key]);}    

        if($escape_mysql){$input_array[$key] = mysql_real_escape_string($input_array[$key]);}
       }
      }

      return $input_array;

    }

记住:它不会清理多维数组,你需要递归地修改它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-06
    • 2021-03-06
    • 2016-07-28
    • 1970-01-01
    • 1970-01-01
    • 2021-04-02
    相关资源
    最近更新 更多