我目前正在使用 PDO 和一些整数值的准备好的语句(请参阅PDO::PARAM_INT)。这意味着我这样称呼 PDO:
$stmt = $conn->prepare("SELECT `lastMove` FROM ".GAMES_TABLE.
" WHERE `id`=:gameID AND (`whiteUserID` = ".USER_ID.
" OR `blackUserID` = ".USER_ID.") LIMIT 1");
$stmt->bindValue(':gameID', $_POST['gameID'], PDO::PARAM_INT);
$stmt->execute();
我需要做任何检查$_POST['gameID']吗?可能存在哪些漏洞?
PDO 和整数
Krzysztof Kotowicz 发表了很好的评论。他说 PDO 将整数视为字符串。所以我启用了 MySQL general_log:
- 打开 /etc/mysql/my.cnf
- 设置
general_log为 1 并将路径添加到general_log_file(例如/var/log/mysql/mysql.log) - 重启你的 MySQL 服务器:
sudo /etc/init.d/mysql restart - 看一下文件:
tail -f /var/log/mysql/mysql.log - 调用以下代码段:
测试.php:
$stmt = $conn->prepare("SELECT `lastMove` FROM ".GAMES_TABLE.
" WHERE `id`=:gameID AND (`whiteUserID` = :uid".
" OR `blackUserID` = :uid) LIMIT 1");
$stmt->bindValue(':gameID', "abc", PDO::PARAM_INT);
$stmt->bindValue(':uid', "1'", PDO::PARAM_INT);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
这是结果:
111114 17:32:54 241 Connect chessuser@localhost on chess
241 Query SELECT `user_id` FROM `chess_users` WHERE `user_id`='1' LIMIT 1
241 Query SELECT `lastMove` FROM chess_games WHERE `id`='abc' AND (`whiteUserID` = '1\'' OR `blackUserID` = '1\'') LIMIT 1
241 Quit
所以很明显,它没有在 PHP 端成为 int (PHP 版本 5.3.2-1ubuntu4.10)