History
了解wpdb https://codex.wordpress.org/Class_Reference/wpdb类,原本a fork https://core.trac.wordpress.org/browser/trunk/b2-include/wp-db.php?rev=112 of Justin Vincent 的 ezSQL 库 http://justinvincent.com/ezsql, 被介绍早在2003年 https://codex.wordpress.org/Version_0.71, only 几个月 https://web.archive.org/web/20150315075138/http://dev.mysql.com/doc/refman/4.1/en/news-4-1-0.htmlMySQL 准备语句协议发布后超过一年前 http://news.php.net/php.announce/50PHP 有any用于访问它的 API 支持:WordPress 因此做了它当时唯一能做的事情,并要求将任何变量直接连接到动态 SQL 中。
那时,ezSQL(以及 WordPress)used https://core.trac.wordpress.org/browser/trunk/b2-include/wp-db.php?rev=114#L67 PHP's mysql_escape_string() https://secure.php.net/manual/en/function.mysql-escape-string.php函数来转义此类文字,因此不知道数据库连接的实际编码(这一威胁仅在 PHP 本身中得到解决)就在几个月前 https://secure.php.net/releases/4_3_0.php).
这仍然是 WordPress 针对 SQL 注入的(唯一)防御近两年来 https://codex.wordpress.org/Version_1.5,于是它switched https://core.trac.wordpress.org/changeset/1545使用(更不安全)addslashes() https://secure.php.net/manual/en/function.addslashes.php逃跑——这种情况随后仍然存在四年多了 https://codex.wordpress.org/Version_2.8 until mysql_real_escape_string() https://secure.php.net/manual/en/function.mysql-real-escape-string.php was finally utilised https://core.trac.wordpress.org/changeset/10597(尽管wpdb::prepare() https://developer.wordpress.org/reference/classes/wpdb/prepare/曾经是介绍 https://core.trac.wordpress.org/changeset/5778 in 临时的 https://codex.wordpress.org/Version_2.3)!
那时不是为了another5年 https://codex.wordpress.org/Version_3.9直到WordPress开始出现using https://core.trac.wordpress.org/changeset/27250一个司机capable本机语句准备(即MySQLi https://secure.php.net/manual/en/book.mysqli.php):现在就在不到两年前,正如@naththedeveloper已经指出了 https://stackoverflow.com/questions/38206366/what-can-i-do-to-get-actual-prepared-statements-in-wordpress#comment63837372_38206366, 中的讨论票#21663 https://core.trac.wordpress.org/ticket/21663表明使用本机准备好的语句甚至可能不在待办事项列表中。
现状
不幸的是,由于 MySQLi 对象是受保护成员 https://core.trac.wordpress.org/browser/tags/4.5.3/src/wp-includes/wp-db.php?rev=37837#L556 of the wpdb
没有 getter 的类(并且在内部分配实例化期间 https://core.trac.wordpress.org/browser/tags/4.5.3/src/wp-includes/wp-db.php?rev=37837#L1467),您无法将本机准备好的语句与 WordPress 的数据库连接一起使用(至少,不修改/替换wpdb
与你自己的班级)。
我能做些什么来得到真实的东西吗?
当然,您可以打开自己的/单独的数据库连接,通过该连接您可以完全灵活地按照您的意愿进行操作。
或者我的担心是没有根据的?
尽管本机语句准备很出色(并且我同意它确实应该成为任何新项目中针对 SQL 注入的默认防御策略),但它确实还具有它的缺点 http://blog.ulf-wendel.de/2008/pdo_mysqlnd-prepared-statements-again/#procon。小心转义变量以用作 SQL 文字(例如,通过 PDO 的预准备语句模拟完成;这就是wpdb::prepare()
这些天尝试交付)should能够抵抗所有已知的攻击...