Skip to:
Content

BuddyPress.org

Changeset 7048


Ignore:
Timestamp:
05/09/2013 02:11:57 PM (12 years ago)
Author:
boonebgorges
Message:

Improved sanitization in Activity component database methods

  • All integer array params are filtered through wp_parse_id_list()
  • Standardized LIKE clause processing

Adds tests for touched methods

Fixes #4995

Props DJPaul, johnjamesjacoby

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-activity/bp-activity-classes.php

    r6948 r7048  
    174174        if ( $search_terms ) {
    175175            $search_terms = $wpdb->escape( $search_terms );
    176             $where_conditions['search_sql'] = "a.content LIKE '%%" . like_escape( $search_terms ) . "%%'";
     176            $where_conditions['search_sql'] = "a.content LIKE '%%" . esc_sql( like_escape( $search_terms ) ) . "%%'";
    177177        }
    178178
     
    253253
    254254        // Get the fullnames of users so we don't have to query in the loop
    255         $activity_user_ids = array();
    256         if ( bp_is_active( 'xprofile' ) && $activities ) {
    257             foreach ( (array) $activities as $activity ) {
    258                 if ( (int) $activity->user_id )
    259                     $activity_user_ids[] = $activity->user_id;
    260             }
    261 
    262             $activity_user_ids = implode( ',', array_unique( (array) $activity_user_ids ) );
    263             if ( !empty( $activity_user_ids ) ) {
     255        if ( bp_is_active( 'xprofile' ) && !empty( $activities ) ) {
     256            $activity_user_ids = wp_list_pluck( $activities, 'user_id' );
     257            $activity_user_ids = implode( ',', wp_parse_id_list( $activity_user_ids ) );
     258
     259            if ( !empty( $activity_user_ids ) ) {               
    264260                if ( $names = $wpdb->get_results( "SELECT user_id, value AS user_fullname FROM {$bp->profile->table_name_data} WHERE field_id = 1 AND user_id IN ({$activity_user_ids})" ) ) {
    265261                    foreach ( (array) $names as $name )
     
    374370
    375371        if ( !empty( $item_id ) )
    376             $where_args[] = $wpdb->prepare( "item_id = %s", $item_id );
     372            $where_args[] = $wpdb->prepare( "item_id = %d", $item_id );
    377373
    378374        if ( !empty( $secondary_item_id ) )
    379             $where_args[] = $wpdb->prepare( "secondary_item_id = %s", $secondary_item_id );
     375            $where_args[] = $wpdb->prepare( "secondary_item_id = %d", $secondary_item_id );
    380376
    381377        if ( !empty( $action ) )
     
    439435
    440436        if ( !empty( $item_id ) )
    441             $where_args[] = $wpdb->prepare( "item_id = %s", $item_id );
     437            $where_args[] = $wpdb->prepare( "item_id = %d", $item_id );
    442438
    443439        if ( !empty( $secondary_item_id ) )
    444             $where_args[] = $wpdb->prepare( "secondary_item_id = %s", $secondary_item_id );
     440            $where_args[] = $wpdb->prepare( "secondary_item_id = %d", $secondary_item_id );
    445441
    446442        if ( !empty( $date_recorded ) )
     
    471467    }
    472468
    473     function delete_activity_item_comments( $activity_ids ) {
     469    function delete_activity_item_comments( $activity_ids = array() ) {
    474470        global $bp, $wpdb;
    475471
    476         if ( is_array( $activity_ids ) )
    477             $activity_ids = implode ( ',', array_map( 'absint', $activity_ids ) );
    478         else
    479             $activity_ids = implode ( ',', array_map( 'absint', explode ( ',', $activity_ids ) ) );
     472        $activity_ids = implode( ',', wp_parse_id_list( $activity_ids ) );
    480473
    481474        return $wpdb->query( "DELETE FROM {$bp->activity->table_name} WHERE type = 'activity_comment' AND item_id IN ({$activity_ids})" );
    482475    }
    483476
    484     function delete_activity_meta_entries( $activity_ids ) {
     477    function delete_activity_meta_entries( $activity_ids = array() ) {
    485478        global $bp, $wpdb;
    486479
    487         if ( is_array( $activity_ids ) )
    488             $activity_ids = implode ( ',', array_map( 'absint', $activity_ids ) );
    489         else
    490             $activity_ids = implode ( ',', array_map( 'absint', explode ( ',', $activity_ids ) ) );
     480        $activity_ids = implode( ',', wp_parse_id_list( $activity_ids ) );
    491481
    492482        return $wpdb->query( "DELETE FROM {$bp->activity->table_name_meta} WHERE activity_id IN ({$activity_ids})" );
  • trunk/tests/testcases/activity/class.BP_Activity_Activity.php

    r7047 r7048  
    7474    }
    7575
    76     public function test_get_meta_query() {
     76    public function test_get_with_meta_query() {
    7777        $a1 = $this->factory->activity->create();
    7878        $a2 = $this->factory->activity->create();
     
    9090        $this->assertEquals( $ids, array( $a1 ) );
    9191    }
     92
     93    public function test_get_with_search_terms() {
     94        $a1 = $this->factory->activity->create( array(
     95            'content' => 'Boone is a cool guy',
     96        ) );
     97        $a2 = $this->factory->activity->create( array(
     98            'content' => 'No he isn\'t',
     99        ) );
     100
     101        $activity = BP_Activity_Activity::get( array(
     102            'search_terms' => 'cool',
     103        ) );
     104        $ids = wp_list_pluck( $activity['activities'], 'id' );
     105        $this->assertEquals( $ids, array( $a1 ) );
     106    }
     107
     108    public function test_get_id_with_item_id() {
     109        $a1 = $this->factory->activity->create( array(
     110            'item_id' => 523,
     111        ) );
     112        $a2 = $this->factory->activity->create( array(
     113            'item_id' => 1888,
     114        ) );
     115
     116        $activity = BP_Activity_Activity::get_id( false, false, false, 523, false, false, false, false );
     117        $this->assertEquals( $a1, $activity );
     118    }
     119
     120    public function test_get_id_with_secondary_item_id() {
     121        $a1 = $this->factory->activity->create( array(
     122            'secondary_item_id' => 523,
     123        ) );
     124        $a2 = $this->factory->activity->create( array(
     125            'secondary_content' => 1888,
     126        ) );
     127
     128        $activity = BP_Activity_Activity::get_id( false, false, false, false, 523, false, false, false );
     129        $this->assertEquals( $a1, $activity );
     130    }
     131
     132    public function test_delete_with_item_id() {
     133        $a1 = $this->factory->activity->create( array(
     134            'item_id' => 523,
     135        ) );
     136        $a2 = $this->factory->activity->create( array(
     137            'item_id' => 1888,
     138        ) );
     139
     140        $activity = BP_Activity_Activity::delete( array(
     141            'item_id' => 523,
     142        ) );
     143        $this->assertEquals( array( $a1 ), $activity );
     144    }
     145
     146    public function test_delete_with_secondary_item_id() {
     147        $a1 = $this->factory->activity->create( array(
     148            'secondary_item_id' => 523,
     149        ) );
     150        $a2 = $this->factory->activity->create( array(
     151            'secondary_item_id' => 1888,
     152        ) );
     153
     154        $activity = BP_Activity_Activity::delete( array(
     155            'secondary_item_id' => 523,
     156        ) );
     157        $this->assertEquals( array( $a1 ), $activity );
     158    }
    92159}
Note: See TracChangeset for help on using the changeset viewer.