PDO准备语句有多安全?

8 浏览
0 Comments

PDO准备语句有多安全?

最近开始使用PDO准备语句,据我了解,它会自动处理所有的转义和安全性问题。

例如,假设$_POST['title']是一个表单字段。

$title = $_POST['title'];
$query = "insert into blog(userID, title) values (?, ?)"
$st = $sql->prepare($query);
$st->bindParam(1, $_SESSION['user']['userID'], PDO::PARAM_INT);
$st->bindParam(2, $title);
$st->execute();

这样真的安全吗?我需要做其他什么吗?还有什么其他要考虑的事情吗?

谢谢。

0
0 Comments

PDO prepared statements offer a high level of security against SQL injections. By using constants like PDO::PARAM_INT, the safety level can be further enhanced. However, there is some confusion regarding the statement that using constants makes it "especially" safe. It is important to clarify the safety difference between using constants and not using them.

The reason for the confusion is that the statement implies that without constants, the safety level is not 100%. If this is the case, then it cannot be considered safe at all. On the other hand, if the safety level is indeed 100% with or without constants, there is no need to emphasize the use of constants as being "especially" safe.

To address this issue, it is necessary to provide a clear explanation of the safety difference between using constants and not using them. This will help developers understand the level of security they can achieve with PDO prepared statements.

In conclusion, while PDO prepared statements are generally safe against SQL injections, the use of constants like PDO::PARAM_INT can enhance the security level. However, it is important to clarify the safety difference between using constants and not using them to avoid any confusion.

0
0 Comments

PDO prepared statements可以防止SQL注入攻击,但并不意味着它是完全安全的。它仍然存在一些安全风险,如拒绝服务攻击和跨站脚本攻击。

拒绝服务攻击是指恶意用户通过大量创建行来占用服务器资源,从而导致系统崩溃或变慢。PDO prepared statements并不能阻止这种类型的攻击。

而跨站脚本攻击是指如果标题内容被回显给其他用户,那么恶意用户可以通过在标题中嵌入HTML代码来注入恶意脚本,从而在其他用户的页面上执行该脚本。这种攻击与SQL注入攻击不同,但同样是一种安全威胁。

安全不仅仅是防止SQL注入攻击,还需要考虑其他类型的攻击。

在处理博客帖子标题时,如果其他用户可以查看这些帖子,那么就存在潜在的跨站脚本攻击风险。恶意用户可以通过在标题中嵌入恶意脚本来影响其他用户的页面。

为了解决这个问题,可以使用HTML过滤器或编码函数来确保在将标题回显给其他用户之前,将任何可能的恶意脚本进行转义或过滤。这样可以防止恶意脚本的执行,保护用户的安全。

需要注意的是,PDO prepared statements并不能解决所有的安全问题,开发人员在编写代码时还需要注意其他安全方面的考虑,以确保应用程序的安全性。

0
0 Comments

PDO prepared statements的安全性是一个很重要的问题。在使用PDO prepared statements时,可以避免SQL注入的风险,但并不是完全安全的。原因在于,PDO prepared statements只能替代SQL表达式中的单个字面值,无法替代值列表、动态表名、动态列名以及其他类型的SQL语法。在这些情况下,仍然需要谨慎编写代码以避免SQL注入。

PDO prepared statements的工作原理是在调用prepare()方法时将查询发送到数据库服务器,然后在调用execute()方法时发送参数值。参数值与查询文本形式是分开的,因此不存在SQL注入的机会(前提是PDO::ATTR_EMULATE_PREPARES为false)。

然而,即使使用PDO prepared statements,也需要注意一些细节。例如,在使用LIKE操作符时,需要对用于匹配的字符进行转义以避免注入。

关于"provided PDO::ATTR_EMULATE_PREPARES is false"的说法,这是否意味着PDO模拟准备语句不如数据库驱动程序的原生准备语句安全?如果是这样,为什么呢?

最近Drupal的一个漏洞(链接见上文)是一个很好的例子,展示了由于PDO prepared statements的限制而导致的注入问题。因此,升级Drupal是非常重要的。

,PDO prepared statements是一种有效的防止SQL注入的方法,但并不是绝对安全的。在使用PDO prepared statements时,需要注意其限制,并谨慎编写代码以避免SQL注入的风险。

0