<!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>[BuddyPress][7025] branches/1.7/bp-core: Improved sanitization in the Core component database methods</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://buddypress.trac.wordpress.org/changeset/7025">7025</a></dd>
<dt>Author</dt> <dd>boonebgorges</dd>
<dt>Date</dt> <dd>2013-05-08 20:31:14 +0000 (Wed, 08 May 2013)</dd>
</dl>

<h3>Log Message</h3>
<pre>Improved sanitization in the Core component database methods

All constructed IN clauses for integer values are now run through
wp_parse_id_list().

Fixes <a href="http://buddypress.trac.wordpress.org/ticket/4992">#4992</a> for the 1.7 branch

Props johnjamesjacoby, DJPaul</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#branches17bpcorebpcorecachephp">branches/1.7/bp-core/bp-core-cache.php</a></li>
<li><a href="#branches17bpcorebpcoreclassesphp">branches/1.7/bp-core/bp-core-classes.php</a></li>
<li><a href="#branches17bpcorebpcorefiltersphp">branches/1.7/bp-core/bp-core-filters.php</a></li>
<li><a href="#branches17bpcorebpcorefunctionsphp">branches/1.7/bp-core/bp-core-functions.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="branches17bpcorebpcorecachephp"></a>
<div class="modfile"><h4>Modified: branches/1.7/bp-core/bp-core-cache.php (7024 => 7025)</h4>
<pre class="diff"><span>
<span class="info">--- branches/1.7/bp-core/bp-core-cache.php   2013-05-08 20:27:22 UTC (rev 7024)
+++ branches/1.7/bp-core/bp-core-cache.php      2013-05-08 20:31:14 UTC (rev 7025)
</span><span class="lines">@@ -92,13 +92,8 @@
</span><span class="cx">          $object_column = $object_type . '_id';
</span><span class="cx">  }
</span><span class="cx"> 
</span><del>-       if ( !is_array( $object_ids ) ) {
-               $object_ids = preg_replace( '|[^0-9,]|', '', $object_ids );
-               $object_ids = explode( ',', $object_ids );
-       }
</del><ins>+        $object_ids = wp_parse_id_list( $object_ids );
</ins><span class="cx"> 
</span><del>-       $object_ids = array_map( 'intval', $object_ids );
-
</del><span class="cx">   $cache = array();
</span><span class="cx"> 
</span><span class="cx">  // Get meta info
</span></span></pre></div>
<a id="branches17bpcorebpcoreclassesphp"></a>
<div class="modfile"><h4>Modified: branches/1.7/bp-core/bp-core-classes.php (7024 => 7025)</h4>
<pre class="diff"><span>
<span class="info">--- branches/1.7/bp-core/bp-core-classes.php 2013-05-08 20:27:22 UTC (rev 7024)
+++ branches/1.7/bp-core/bp-core-classes.php    2013-05-08 20:31:14 UTC (rev 7025)
</span><span class="lines">@@ -301,7 +301,7 @@
</span><span class="cx">          // @todo remove need for bp_is_active() check
</span><span class="cx">          if ( empty( $include ) && ! empty( $user_id ) && bp_is_active( 'friends' ) ) {
</span><span class="cx">                  $friend_ids = friends_get_friend_user_ids( $user_id );
</span><del>-                       $friend_ids = $wpdb->escape( implode( ',', (array) $friend_ids ) );
</del><ins>+                        $friend_ids = implode( ',', wp_parse_id_list( $friend_ids ) );
</ins><span class="cx"> 
</span><span class="cx">                  if ( ! empty( $friend_ids ) ) {
</span><span class="cx">                          $sql['where'][] = "u.{$this->uid_name} IN ({$friend_ids})";
</span><span class="lines">@@ -803,6 +803,7 @@
</span><span class="cx">          }
</span><span class="cx"> 
</span><span class="cx">          if ( !empty( $exclude ) ) {
</span><ins>+                       $exclude              = implode( ',', wp_parse_id_list( $exclude ) );
</ins><span class="cx">                   $sql['where_exclude'] = "AND u.ID NOT IN ({$exclude})";
</span><span class="cx">          }
</span><span class="cx"> 
</span><span class="lines">@@ -812,20 +813,13 @@
</span><span class="cx">                  $sql['where_users'] = "AND 0 = 1";
</span><span class="cx">          } else {
</span><span class="cx">                  if ( !empty( $include ) ) {
</span><del>-                               if ( is_array( $include ) ) {
-                                       $uids = $wpdb->escape( implode( ',', (array) $include ) );
-                               } else {
-                                       $uids = $wpdb->escape( $include );
-                               }
-
-                               if ( !empty( $uids ) ) {
-                                       $sql['where_users'] = "AND u.ID IN ({$uids})";
-                               }
</del><ins>+                                $include = implode( ',',  wp_parse_id_list( $include ) );
+                               $sql['where_users'] = "AND u.ID IN ({$include})";
</ins><span class="cx">                   } elseif ( !empty( $user_id ) && bp_is_active( 'friends' ) ) {
</span><span class="cx">                          $friend_ids = friends_get_friend_user_ids( $user_id );
</span><del>-                               $friend_ids = $wpdb->escape( implode( ',', (array) $friend_ids ) );
</del><span class="cx"> 
</span><span class="cx">                          if ( !empty( $friend_ids ) ) {
</span><ins>+                                       $friend_ids = implode( ',', wp_parse_id_list( $friend_ids ) );
</ins><span class="cx">                                   $sql['where_friends'] = "AND u.ID IN ({$friend_ids})";
</span><span class="cx"> 
</span><span class="cx">                          // User has no friends, return false since there will be no users to fetch.
</span><span class="lines">@@ -911,8 +905,6 @@
</span><span class="cx">                          $user_ids[] = $user->id;
</span><span class="cx">                  }
</span><span class="cx"> 
</span><del>-                       $user_ids = $wpdb->escape( join( ',', (array) $user_ids ) );
-
</del><span class="cx">                   // Add additional data to the returned results
</span><span class="cx">                  $paged_users = BP_Core_User::get_user_extras( $paged_users, $user_ids, $type );
</span><span class="cx">          }
</span><span class="lines">@@ -957,8 +949,7 @@
</span><span class="cx">          $status_sql = bp_core_get_status_sql( 'u.' );
</span><span class="cx"> 
</span><span class="cx">          if ( !empty( $exclude ) ) {
</span><del>-                       $exclude     = wp_parse_id_list( $r['exclude'] );
-                       $exclude     = $wpdb->escape( implode( ',', $exclude ) );
</del><ins>+                        $exclude     = implode( ',', wp_parse_id_list( $r['exclude'] ) );
</ins><span class="cx">                   $exclude_sql = " AND u.id NOT IN ({$exclude})";
</span><span class="cx">          } else {
</span><span class="cx">                  $exclude_sql = '';
</span><span class="lines">@@ -979,10 +970,8 @@
</span><span class="cx">           */
</span><span class="cx">          $user_ids = array();
</span><span class="cx">          foreach ( (array) $paged_users as $user )
</span><del>-                       $user_ids[] = $user->id;
</del><ins>+                        $user_ids[] = (int) $user->id;
</ins><span class="cx"> 
</span><del>-               $user_ids = $wpdb->escape( join( ',', (array) $user_ids ) );
-
</del><span class="cx">           // Add additional data to the returned results
</span><span class="cx">          if ( $populate_extras ) {
</span><span class="cx">                  $paged_users = BP_Core_User::get_user_extras( $paged_users, $user_ids );
</span><span class="lines">@@ -1009,10 +998,11 @@
</span><span class="cx">          if ( $limit && $page )
</span><span class="cx">                  $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );
</span><span class="cx"> 
</span><ins>+               $user_ids   = implode( ',', wp_parse_id_list( $user_ids ) );
</ins><span class="cx">           $status_sql = bp_core_get_status_sql();
</span><span class="cx"> 
</span><del>-               $total_users_sql = apply_filters( 'bp_core_get_specific_users_count_sql', "SELECT COUNT(DISTINCT ID) FROM {$wpdb->users} WHERE {$status_sql} AND ID IN ( " . $wpdb->escape( $user_ids ) . " ) " );
-               $paged_users_sql = apply_filters( 'bp_core_get_specific_users_count_sql', "SELECT DISTINCT ID as id, user_registered, user_nicename, user_login, user_email FROM {$wpdb->users} WHERE {$status_sql} AND ID IN ( " . $wpdb->escape( $user_ids ) . " ) {$pag_sql}" );
</del><ins>+                $total_users_sql = apply_filters( 'bp_core_get_specific_users_count_sql', "SELECT COUNT(DISTINCT ID) FROM {$wpdb->users} WHERE {$status_sql} AND ID IN ({$user_ids})" );
+               $paged_users_sql = apply_filters( 'bp_core_get_specific_users_count_sql', "SELECT DISTINCT ID as id, user_registered, user_nicename, user_login, user_email FROM {$wpdb->users} WHERE {$status_sql} AND ID IN ({$user_ids}) {$pag_sql}" );
</ins><span class="cx"> 
</span><span class="cx">          $total_users = $wpdb->get_var( $total_users_sql );
</span><span class="cx">          $paged_users = $wpdb->get_results( $paged_users_sql );
</span><span class="lines">@@ -1067,8 +1057,6 @@
</span><span class="cx">          foreach ( (array) $paged_users as $user )
</span><span class="cx">                  $user_ids[] = $user->id;
</span><span class="cx"> 
</span><del>-               $user_ids = $wpdb->escape( join( ',', (array) $user_ids ) );
-
</del><span class="cx">           // Add additional data to the returned results
</span><span class="cx">          if ( $populate_extras )
</span><span class="cx">                  $paged_users = BP_Core_User::get_user_extras( $paged_users, $user_ids );
</span><span class="lines">@@ -1095,6 +1083,9 @@
</span><span class="cx">          if ( empty( $user_ids ) )
</span><span class="cx">                  return $paged_users;
</span><span class="cx"> 
</span><ins>+               // Sanitize user IDs
+               $user_ids = implode( ',', wp_parse_id_list( $user_ids ) );
+
</ins><span class="cx">           // Fetch the user's full name
</span><span class="cx">          if ( bp_is_active( 'xprofile' ) && 'alphabetical' != $type ) {
</span><span class="cx">                  $names = $wpdb->get_results( $wpdb->prepare( "SELECT pd.user_id as id, pd.value as fullname FROM {$bp->profile->table_name_fields} pf, {$bp->profile->table_name_data} pd WHERE pf.id = pd.field_id AND pf.name = %s AND pd.user_id IN ( {$user_ids} )", bp_xprofile_fullname_field_name() ) );
</span></span></pre></div>
<a id="branches17bpcorebpcorefiltersphp"></a>
<div class="modfile"><h4>Modified: branches/1.7/bp-core/bp-core-filters.php (7024 => 7025)</h4>
<pre class="diff"><span>
<span class="info">--- branches/1.7/bp-core/bp-core-filters.php 2013-05-08 20:27:22 UTC (rev 7024)
+++ branches/1.7/bp-core/bp-core-filters.php    2013-05-08 20:31:14 UTC (rev 7025)
</span><span class="lines">@@ -130,7 +130,7 @@
</span><span class="cx">  if ( empty( $user_ids ) )
</span><span class="cx">          return $comments;
</span><span class="cx"> 
</span><del>-       $user_ids = implode( ',', $user_ids );
</del><ins>+        $user_ids = implode( ',', wp_parse_id_list( $user_ids ) );
</ins><span class="cx"> 
</span><span class="cx">  if ( !$userdata = $wpdb->get_results( "SELECT ID as user_id, user_login, user_nicename FROM {$wpdb->users} WHERE ID IN ({$user_ids})" ) )
</span><span class="cx">          return $comments;
</span></span></pre></div>
<a id="branches17bpcorebpcorefunctionsphp"></a>
<div class="modfile"><h4>Modified: branches/1.7/bp-core/bp-core-functions.php (7024 => 7025)</h4>
<pre class="diff"><span>
<span class="info">--- branches/1.7/bp-core/bp-core-functions.php       2013-05-08 20:27:22 UTC (rev 7024)
+++ branches/1.7/bp-core/bp-core-functions.php  2013-05-08 20:31:14 UTC (rev 7025)
</span><span class="lines">@@ -141,7 +141,7 @@
</span><span class="cx">          // Always get page data from the root blog, except on multiblog mode, when it comes
</span><span class="cx">          // from the current blog
</span><span class="cx">          $posts_table_name = bp_is_multiblog_mode() ? $wpdb->posts : $wpdb->get_blog_prefix( bp_get_root_blog_id() ) . 'posts';
</span><del>-               $page_ids_sql     = implode( ',', (array) $page_ids );
</del><ins>+                $page_ids_sql     = implode( ',', wp_parse_id_list( $page_ids ) );
</ins><span class="cx">           $page_names       = $wpdb->get_results( "SELECT ID, post_name, post_parent, post_title FROM {$posts_table_name} WHERE ID IN ({$page_ids_sql}) AND post_status = 'publish' " );
</span><span class="cx"> 
</span><span class="cx">          foreach ( (array) $page_ids as $component_id => $page_id ) {
</span></span></pre>
</div>
</div>

</body>
</html>