<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[28711] trunk: `LIKE` escape sanity:</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/28711">28711</a></dd>
<dt>Author</dt> <dd>wonderboymusic</dd>
<dt>Date</dt> <dd>2014-06-10 00:29:35 +0000 (Tue, 10 Jun 2014)</dd>
</dl>
<h3>Log Message</h3>
<pre>`LIKE` escape sanity:
* Deprecate `like_escape()`
* Add a method to `$wpdb`, `->esc_like()`, and add unit tests
`$wpdb::esc_like()` is not used yet. As such, many unit tests will throw `Unexpected deprecated notice for like_escape`. Subsequent commits will alleviate this.
Props miqrogroove.
See <a href="http://core.trac.wordpress.org/ticket/10041">#10041</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesdeprecatedphp">trunk/src/wp-includes/deprecated.php</a></li>
<li><a href="#trunksrcwpincludesformattingphp">trunk/src/wp-includes/formatting.php</a></li>
<li><a href="#trunksrcwpincludeswpdbphp">trunk/src/wp-includes/wp-db.php</a></li>
<li><a href="#trunktestsphpunittestsdbphp">trunk/tests/phpunit/tests/db.php</a></li>
<li><a href="#trunktestsphpunittestsformattingLikeEscapephp">trunk/tests/phpunit/tests/formatting/LikeEscape.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesdeprecatedphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/deprecated.php (28710 => 28711)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/deprecated.php 2014-06-09 22:26:27 UTC (rev 28710)
+++ trunk/src/wp-includes/deprecated.php 2014-06-10 00:29:35 UTC (rev 28711)
</span><span class="lines">@@ -3459,6 +3459,21 @@
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><ins>+ * Formerly used to escape strings before searching the DB. It was poorly documented and never worked as described.
+ *
+ * @since 2.5.0
+ * @deprecated 4.0.0
+ * @deprecated Use wpdb::esc_like()
+ *
+ * @param string $text The text to be escaped.
+ * @return string text, safe for inclusion in LIKE query.
+ */
+function like_escape($text) {
+ _deprecated_function( __FUNCTION__, '4.0', 'wpdb::esc_like()' );
+ return str_replace( array( "%", "_" ), array( "\\%", "\\_" ), $text );
+}
+
+/**
</ins><span class="cx"> * Determines if the URL can be accessed over SSL.
</span><span class="cx"> *
</span><span class="cx"> * Determines if the URL can be accessed over SSL by using the WordPress HTTP API to access
</span></span></pre></div>
<a id="trunksrcwpincludesformattingphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/formatting.php (28710 => 28711)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/formatting.php 2014-06-09 22:26:27 UTC (rev 28710)
+++ trunk/src/wp-includes/formatting.php 2014-06-10 00:29:35 UTC (rev 28711)
</span><span class="lines">@@ -3100,18 +3100,6 @@
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><del>- * Escapes text for SQL LIKE special characters % and _.
- *
- * @since 2.5.0
- *
- * @param string $text The text to be escaped.
- * @return string text, safe for inclusion in LIKE query.
- */
-function like_escape($text) {
- return str_replace(array("%", "_"), array("\\%", "\\_"), $text);
-}
-
-/**
</del><span class="cx"> * Convert full URL paths to absolute paths.
</span><span class="cx"> *
</span><span class="cx"> * Removes the http or https protocols and the domain. Keeps the path '/' at the
</span></span></pre></div>
<a id="trunksrcwpincludeswpdbphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/wp-db.php (28710 => 28711)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/wp-db.php 2014-06-09 22:26:27 UTC (rev 28710)
+++ trunk/src/wp-includes/wp-db.php 2014-06-10 00:29:35 UTC (rev 28711)
</span><span class="lines">@@ -1169,6 +1169,29 @@
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><ins>+ * First half of escaping for LIKE special characters % and _ before preparing for MySQL.
+ *
+ * Use this only before wpdb::prepare() or esc_sql(). Reversing the order is very bad for security.
+ *
+ * Example Prepared Statement:
+ * $wild = '%';
+ * $find = 'only 43% of planets';
+ * $like = $wild . $wpdb->esc_like( $find ) . $wild;
+ * $sql = $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_content LIKE %s", $like );
+ *
+ * Example Escape Chain:
+ * $sql = esc_sql( $wpdb->esc_like( $input ) );
+ *
+ * @since 4.0.0
+ *
+ * @param string $text The raw text to be escaped. The input typed by the user should have no extra or deleted slashes.
+ * @return string Text in the form of a LIKE phrase. The output is not SQL safe. Call prepare or real_escape next.
+ */
+ function esc_like( $text ) {
+ return addcslashes( $text, '_%\\' );
+ }
+
+ /**
</ins><span class="cx"> * Print SQL/DB error.
</span><span class="cx"> *
</span><span class="cx"> * @since 0.71
</span></span></pre></div>
<a id="trunktestsphpunittestsdbphp"></a>
<div class="modfile"><h4>Modified: trunk/tests/phpunit/tests/db.php (28710 => 28711)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/phpunit/tests/db.php 2014-06-09 22:26:27 UTC (rev 28710)
+++ trunk/tests/phpunit/tests/db.php 2014-06-10 00:29:35 UTC (rev 28711)
</span><span class="lines">@@ -109,6 +109,105 @@
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><ins>+ * @ticket 10041
+ */
+ function test_esc_like() {
+ global $wpdb;
+
+ $inputs = array(
+ 'howdy%', //Single Percent
+ 'howdy_', //Single Underscore
+ 'howdy\\', //Single slash
+ 'howdy\\howdy%howdy_', //The works
+ 'howdy\'"[[]*#[^howdy]!+)(*&$#@!~|}{=--`/.,<>?', //Plain text
+ );
+ $expected = array(
+ 'howdy\\%',
+ 'howdy\\_',
+ 'howdy\\\\',
+ 'howdy\\\\howdy\\%howdy\\_',
+ 'howdy\'"[[]*#[^howdy]!+)(*&$#@!~|}{=--`/.,<>?',
+ );
+
+ foreach ($inputs as $key => $input) {
+ $this->assertEquals($expected[$key], $wpdb->esc_like($input));
+ }
+ }
+
+ /**
+ * Test LIKE Queries
+ *
+ * Make sure $wpdb is fully compatible with esc_like() by testing the identity of various strings.
+ * When escaped properly, a string literal is always LIKE itself (1)
+ * and never LIKE any other string literal (0) no matter how crazy the SQL looks.
+ *
+ * @ticket 10041
+ * @dataProvider data_like_query
+ * @param $data string The haystack, raw.
+ * @param $like string The like phrase, raw.
+ * @param $result string The expected comparison result; '1' = true, '0' = false
+ */
+ function test_like_query( $data, $like, $result ) {
+ global $wpdb;
+ return $this->assertEquals( $result, $wpdb->get_var( $wpdb->prepare( "SELECT %s LIKE %s", $data, $wpdb->esc_like( $like ) ) ) );
+ }
+
+ function data_like_query() {
+ return array(
+ array(
+ 'aaa',
+ 'aaa',
+ '1',
+ ),
+ array(
+ 'a\\aa', // SELECT 'a\\aa' # This represents a\aa in both languages.
+ 'a\\aa', // LIKE 'a\\\\aa'
+ '1',
+ ),
+ array(
+ 'a%aa',
+ 'a%aa',
+ '1',
+ ),
+ array(
+ 'aaaa',
+ 'a%aa',
+ '0',
+ ),
+ array(
+ 'a\\%aa', // SELECT 'a\\%aa'
+ 'a\\%aa', // LIKE 'a\\\\\\%aa' # The PHP literal would be "LIKE 'a\\\\\\\\\\\\%aa'". This is why we need reliable escape functions!
+ '1',
+ ),
+ array(
+ 'a%aa',
+ 'a\\%aa',
+ '0',
+ ),
+ array(
+ 'a\\%aa',
+ 'a%aa',
+ '0',
+ ),
+ array(
+ 'a_aa',
+ 'a_aa',
+ '1',
+ ),
+ array(
+ 'aaaa',
+ 'a_aa',
+ '0',
+ ),
+ array(
+ 'howdy\'"[[]*#[^howdy]!+)(*&$#@!~|}{=--`/.,<>?',
+ 'howdy\'"[[]*#[^howdy]!+)(*&$#@!~|}{=--`/.,<>?',
+ '1',
+ ),
+ );
+ }
+
+ /**
</ins><span class="cx"> * @ticket 18510
</span><span class="cx"> */
</span><span class="cx"> function test_wpdb_supposedly_protected_properties() {
</span></span></pre></div>
<a id="trunktestsphpunittestsformattingLikeEscapephp"></a>
<div class="modfile"><h4>Modified: trunk/tests/phpunit/tests/formatting/LikeEscape.php (28710 => 28711)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/phpunit/tests/formatting/LikeEscape.php 2014-06-09 22:26:27 UTC (rev 28710)
+++ trunk/tests/phpunit/tests/formatting/LikeEscape.php 2014-06-10 00:29:35 UTC (rev 28711)
</span><span class="lines">@@ -6,6 +6,7 @@
</span><span class="cx"> class Tests_Formatting_LikeEscape extends WP_UnitTestCase {
</span><span class="cx"> /**
</span><span class="cx"> * @ticket 10041
</span><ins>+ * @expectedDeprecated like_escape
</ins><span class="cx"> */
</span><span class="cx"> function test_like_escape() {
</span><span class="cx">
</span></span></pre>
</div>
</div>
</body>
</html>