此代码是否容易受到SQL注入攻击?

时间:2011-08-18 01:25:28

标签: php security wordpress-plugin wordpress

此代码是否容易受到SQL注入攻击?

$sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author, comment_author_email, comment_date_gmt, comment_approved, comment_type, comment_author_url, SUBSTRING(comment_content,1,70) AS com_excerpt FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) WHERE comment_approved = '1' AND comment_type = '' AND post_password = '' ORDER BY comment_date_gmt DESC LIMIT 5";

2 个答案:

答案 0 :(得分:2)

假设$wpdb对象从外部是不可触摸的(在Wordpress中通常是正确的),我会说你对这个特定查询是安全的。

您只需要担心传入从外部源接收的任何参数。

Wordpress提供了几种处理查询中用户输入的方法。见http://codex.wordpress.org/Data_Validation#Database

答案 1 :(得分:1)

这取决于我看不到的一些事情 - 或者根据你发布的代码知道的知识不足。 要容易受到SQL注入攻击,您必须在数据库中输入未转义的字符串。 (编辑:通常是未转义的,但用户可定义的字符串)。

我无法在代码中看到您已经转义字符串的任何地方。 PHP为此提供了一个功能: $ string = mysql_real_escape_string($ string); 然后该字符串在数据库查询中应该是安全的。

例如,不要使用:

$name = $_GET['name'];
mysql_query("INSERT INTO table_name VALUES ('$name')");

改为使用:

$name = mysql_real_escape_string($_GET['name']);
mysql_query("INSERT INTO table_name VALUES ('$name')");

如果“mysql_real_escape_string”函数中没有漏洞,那么“应该”保护SQL注入。

相关问题