准备好的语句和 $_GET (有安全问题吗?)

发布于 2025-01-08 10:28:08 字数 996 浏览 1 评论 0原文

可能的重复:
准备好的语句如何防范 SQL 注入攻击?

如果我将 $_GET 与 PDO 一起使用,我还需要转义它吗?我的理解是,这不受 SQL 注入的影响,但我仍然对不逃避它感到不安。那么有人可以看看这个小代码块并告诉我它是否安全吗?

<?php
$hostname = 'localhost';
$username = 'root';
$password = 'root';
$database = 'database';
try {
    $dbh = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $dbh->prepare("SELECT * FROM comments WHERE pid = :pid");
    $pid = $_GET['pid'];
    $stmt->bindParam(':pid', $pid, PDO::PARAM_STR);
    $stmt->execute();
    $result = $stmt->fetchAll();
    }
catch(PDOException $e)
    {
    echo $e->getMessage();
    }
    $stmt->execute();
    echo $stmt->rowCount();
$dbh = null;
?>

再说一次,我关心的是 $_GET。如有任何帮助,我们将不胜感激,谢谢。

Possible Duplicate:
How prepared statements can protect from SQL injection attacks?

If I'm using $_GET with PDO do I still need to escape it? My understanding is that this is immune to SQL injection, however I still feel uneasy about not escaping it. So could someone please look at this little block of code and tell me if it is secure?

<?php
$hostname = 'localhost';
$username = 'root';
$password = 'root';
$database = 'database';
try {
    $dbh = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $dbh->prepare("SELECT * FROM comments WHERE pid = :pid");
    $pid = $_GET['pid'];
    $stmt->bindParam(':pid', $pid, PDO::PARAM_STR);
    $stmt->execute();
    $result = $stmt->fetchAll();
    }
catch(PDOException $e)
    {
    echo $e->getMessage();
    }
    $stmt->execute();
    echo $stmt->rowCount();
$dbh = null;
?>

Again, it's the $_GET I'm concerned about. Any help is appreciated, thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

行至春深 2025-01-15 10:28:08

是的,准备好的语句功能正如它所说的那样。但既然你问了,我们就明确一点,这并不是故事的结局。我正在查看2010 年 OWASP 十大应用程序安全风险

例如:

  • 每个远程用户是否都有权访问与每个 PID 相关的数据?如果没有,则无法检查用户是否已获得授权就是 OWASP 2010-A4-不安全直接对象引用的明显示例。
  • 您可能并不认真对待以明文形式对密码进行硬编码,因为这是 OWASP 2010-A7-不安全加密存储的一个明显示例。
  • 除了回显行计数之外,您没有说明您可能对 $stmt 做什么,但是当然,如​​果您显示数据库中的任何内容,您将首先小心地转义 HTML 实体。否则,您将创建一个清晰的 OWASP 2010-A2-跨站点脚本 (XSS) 示例。
  • 顺便说一句,通常最好显式指定列(或聚合函数)而不是“SELECT *”。

Yes, the prepared statement feature does what it says. But since you asked, let's be clear that it's not the end of the story. I'm looking at the OWASP Top Ten Application Security Risks 2010.

For example:

  • Is every remote user authorized to access data associated with every PID? If not, failing to check that the user is authorized is a clear example of OWASP 2010-A4-Insecure Direct Object References.
  • You're probably not serious about hardcoding the password in cleartext, because that is a clear example of OWASP 2010-A7-Insecure Cryptographic Storage.
  • You don't say what you might do with $stmt apart from echoing the rowcount, but of course if you display any content from the database you'll be careful to escape HTML entities first. Otherwise you would create a clear example of OWASP 2010-A2-Cross-Site Scripting (XSS).
  • By the way, it's generally better to specify columns (or aggregate functions) explicitly rather than to "SELECT *".
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文