dqb78642 2010-03-09 21:01
浏览 74
已采纳

PHP:显示信息的最佳安全实践?

In PHP, I know that using parameterized queries is the best way to prevent SQL injection.

But what about sanitizing user input that will be used for other purposes, such as:

  • Displaying back to a user (potential cross-site scripting vector)
  • Addressing an email or filling in the message body

Is htmlentities() the best way to sanitize for non-database usage? What is considered to be best practice here?

  • 写回答

4条回答 默认 最新

  • duandi6531 2010-03-09 21:04
    关注

    In php the best xss filter is:

    htmlspecialchars($_POST['param'],ENT_QUOTES);
    

    The reason why you also have to encode quotes is becuase you don't need <> to exploit some xss. for instance this is vulnerable to xss:

    print('<A HREF="http://www.xssed.com/'.htmlspecialchars($_REQUEST[xss]).'">link</a>');
    

    You don't need <> to execute javascript in this case because you can use onmouseover, here is an example attack:

    $_REQUEST[xss]='" onMouseOver="alert(/xss/)"';
    

    the ENT_QUOTES takes care of the double quotes.

    E-mail is a bit different, javascript shouldn't be executed by the mail client, and if it is then your site isn't affected due to the Same Origin Policy. But to be on the safe side I would still use htmlspecialchars($var,ENT_QUOTES);. HOWEVER, PHP's mail() function can succumb to a different type of vulnerability, its called CRLF injection. Here is an example vulnerability against PHP-Nuke. If you have a function call like this: mail($fmail, $subject, $message, $header); Then you must make sure that a user cannot inject into $header.

    Vulnerable code:

    $header="From: \"$_GET[name]\" <$ymail>
    X-Mailer: PHP";
    

    patched:

    $_GET[name]=str_replace(array("","
    "),$_GET[name]);
    $header="From: \"$_GET[name]\" <$ymail>
    X-Mailer: PHP";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)