Make WordPress Core

Changeset 56859


Ignore:
Timestamp:
10/12/2023 02:43:19 PM (8 months ago)
Author:
davidbaumwald
Message:

Grouped backports to the 4.6 branch.

  • Comments: Prevent users who can not see a post from seeing comments on it.
  • Shortcodes: Restrict media shortcode ajax to certain type.
  • REST API: Ensure no-cache headers are sent when methods are overridden.
  • Prevent unintended behavior when certain objects are unserialized.

Merges [56834], [56835], [56836], and [56838] to the 4.6 branch.
Props xknown, jorbin, joehoyle, timothyblynjacobs, peterwilsoncc, ehtis, tykoted, antpb, rmccue.

Location:
branches/4.6/src
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • branches/4.6/src/wp-admin/includes/ajax-actions.php

    r55783 r56859  
    30533053    $shortcode = wp_unslash( $_POST['shortcode'] );
    30543054
     3055    // Only process previews for media related shortcodes:
     3056    $found_shortcodes = get_shortcode_tags_in_content( $shortcode );
     3057    $media_shortcodes = array(
     3058        'audio',
     3059        'embed',
     3060        'playlist',
     3061        'video',
     3062        'gallery',
     3063    );
     3064
     3065    $other_shortcodes = array_diff( $found_shortcodes, $media_shortcodes );
     3066
     3067    if ( ! empty( $other_shortcodes ) ) {
     3068        wp_send_json_error();
     3069    }
     3070
    30553071    if ( ! empty( $_POST['post_ID'] ) ) {
    30563072        $post = get_post( (int) $_POST['post_ID'] );
     
    30593075    // the embed shortcode requires a post
    30603076    if ( ! $post || ! current_user_can( 'edit_post', $post->ID ) ) {
    3061         if ( 'embed' === $shortcode ) {
     3077        if ( in_array( 'embed', $found_shortcodes, true ) ) {
    30623078            wp_send_json_error();
    30633079        }
  • branches/4.6/src/wp-admin/includes/class-wp-comments-list-table.php

    r37674 r56859  
    495495        $this->user_can = current_user_can( 'edit_comment', $comment->comment_ID );
    496496
     497        $edit_post_cap = $post ? 'edit_post' : 'edit_posts';
     498        if (
     499            current_user_can( $edit_post_cap, $comment->comment_post_ID ) ||
     500            (
     501                empty( $post->post_password ) &&
     502                current_user_can( 'read_post', $comment->comment_post_ID )
     503            )
     504        ) {
     505            // The user has access to the post
     506        } else {
     507            return false;
     508        }
     509
    497510        echo "<tr id='comment-$comment->comment_ID' class='$the_comment_class'>";
    498511        $this->single_row_columns( $comment );
  • branches/4.6/src/wp-admin/includes/class-wp-list-table.php

    r38146 r56859  
    656656        $pending_phrase = sprintf( _n( '%s pending comment', '%s pending comments', $pending_comments ), $pending_comments_number );
    657657
    658         // No comments at all.
     658        $post_object   = get_post( $post_id );
     659        $edit_post_cap = $post_object ? 'edit_post' : 'edit_posts';
     660        if (
     661            current_user_can( $edit_post_cap, $post_id ) ||
     662            (
     663                empty( $post_object->post_password ) &&
     664                current_user_can( 'read_post', $post_id )
     665            )
     666        ) {
     667            // The user has access to the post and thus can see comments
     668        } else {
     669            return false;
     670        }
     671
    659672        if ( ! $approved_comments && ! $pending_comments ) {
    660673            printf( '<span aria-hidden="true">—</span><span class="screen-reader-text">%s</span>',
  • branches/4.6/src/wp-admin/includes/dashboard.php

    r38028 r56859  
    917917
    918918        echo '<ul id="the-comment-list" data-wp-lists="list:comment">';
    919         foreach ( $comments as $comment )
    920             _wp_dashboard_recent_comments_row( $comment );
     919        foreach ( $comments as $comment ) {
     920            $comment_post = get_post( $comment->comment_post_ID );
     921            if (
     922                current_user_can( 'edit_post', $comment->comment_post_ID ) ||
     923                (
     924                    empty( $comment_post->post_password ) &&
     925                    current_user_can( 'read_post', $comment->comment_post_ID )
     926                )
     927            ) {
     928                _wp_dashboard_recent_comments_row( $comment );
     929            }
     930        }
    921931        echo '</ul>';
    922932
  • branches/4.6/src/wp-includes/Requests/Hooks.php

    r38049 r56859  
    6666        return true;
    6767    }
     68
     69    public function __wakeup() {
     70        throw new \LogicException( __CLASS__ . ' should never be unserialized' );
     71    }
    6872}
  • branches/4.6/src/wp-includes/Requests/IRI.php

    r38728 r56859  
    704704    }
    705705
     706    public function __wakeup() {
     707        $class_props = get_class_vars( __CLASS__ );
     708        $string_props = array( 'scheme', 'iuserinfo', 'ihost', 'port', 'ipath', 'iquery', 'ifragment' );
     709        $array_props = array( 'normalization' );
     710        foreach ( $class_props as $prop => $default_value ) {
     711            if ( in_array( $prop, $string_props, true ) && ! is_string( $this->$prop ) ) {
     712                throw new UnexpectedValueException();
     713            } elseif ( in_array( $prop, $array_props, true ) && ! is_array( $this->$prop ) ) {
     714                throw new UnexpectedValueException();
     715            }
     716            $this->$prop = null;
     717        }
     718    }
     719
    706720    /**
    707721     * Set the entire IRI. Returns true on success, false on failure (if there
  • branches/4.6/src/wp-includes/Requests/Session.php

    r38049 r56859  
    228228    }
    229229
     230    public function __wakeup() {
     231        throw new \LogicException( __CLASS__ . ' should never be unserialized' );
     232    }
     233
    230234    /**
    231235     * Merge a request's data with the default data
  • branches/4.6/src/wp-includes/class-wp-theme.php

    r39810 r56859  
    531531
    532532    /**
     533     * Perform reinitialization tasks.
     534     *
     535     * Prevents a callback from being injected during unserialization of an object.
     536     *
     537     * @return void
     538     */
     539    public function __wakeup() {
     540        if ( $this->parent && ! $this->parent instanceof self ) {
     541            throw new UnexpectedValueException();
     542        }
     543        if ( $this->headers && ! is_array( $this->headers ) ) {
     544            throw new UnexpectedValueException();
     545        }
     546        foreach ( $this->headers as $value ) {
     547            if ( ! is_string( $value ) ) {
     548                throw new UnexpectedValueException();
     549            }
     550        }
     551        $this->headers_sanitized = array();
     552    }
     553
     554    /**
    533555     * Adds theme data to cache.
    534556     *
     
    14411463        return strnatcasecmp( $a->display( 'Name', false, true ), $b->display( 'Name', false, true ) );
    14421464    }
     1465
     1466    private static function _check_headers_property_has_correct_type( $headers ) {
     1467        if ( ! is_array( $headers ) ) {
     1468            return false;
     1469        }
     1470        foreach ( $headers as $key => $value ) {
     1471            if ( ! is_string( $key ) || ! is_string( $value ) ) {
     1472                return false;
     1473            }
     1474        }
     1475        return true;
     1476    }
    14431477}
  • branches/4.6/src/wp-includes/media.php

    r55783 r56859  
    16601660        }
    16611661    } elseif ( ! empty( $atts['exclude'] ) ) {
     1662        $post_parent_id = $id;
    16621663        $attachments = get_children( array( 'post_parent' => $id, 'exclude' => $atts['exclude'], 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $atts['order'], 'orderby' => $atts['orderby'] ) );
    16631664    } else {
     1665        $post_parent_id = $id;
    16641666        $attachments = get_children( array( 'post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $atts['order'], 'orderby' => $atts['orderby'] ) );
     1667    }
     1668
     1669    if ( ! empty( $post_parent_id ) ) {
     1670        $post_parent = get_post( $post_parent_id );
     1671
     1672        // terminate the shortcode execution if user cannot read the post or password-protected
     1673        if (
     1674        ( ! is_post_publicly_viewable( $post_parent->ID ) && ! current_user_can( 'read_post', $post_parent->ID ) )
     1675        || post_password_required( $post_parent ) ) {
     1676            return '';
     1677        }
    16651678    }
    16661679
     
    19641977    }
    19651978
     1979    if ( ! empty( $args['post_parent'] ) ) {
     1980        $post_parent = get_post( $id );
     1981
     1982        // terminate the shortcode execution if user cannot read the post or password-protected
     1983        if ( ! current_user_can( 'read_post', $post_parent->ID ) || post_password_required( $post_parent ) ) {
     1984            return '';
     1985        }
     1986    }
     1987
    19661988    if ( empty( $attachments ) ) {
    19671989        return '';
  • branches/4.6/src/wp-includes/rest-api.php

    r46496 r56859  
    595595
    596596    if ( ! $result ) {
     597        add_filter( 'rest_send_nocache_headers', '__return_true', 20 );
    597598        return new WP_Error( 'rest_cookie_invalid_nonce', __( 'Cookie nonce is invalid' ), array( 'status' => 403 ) );
    598599    }
  • branches/4.6/src/wp-includes/rest-api/class-wp-rest-server.php

    r38037 r56859  
    244244
    245245        /**
    246          * Send nocache headers on authenticated requests.
    247          *
    248          * @since 4.4.0
    249          *
    250          * @param bool $rest_send_nocache_headers Whether to send no-cache headers.
    251          */
    252         $send_no_cache_headers = apply_filters( 'rest_send_nocache_headers', is_user_logged_in() );
    253         if ( $send_no_cache_headers ) {
    254             foreach ( wp_get_nocache_headers() as $header => $header_value ) {
    255                 $this->send_header( $header, $header_value );
    256             }
    257         }
    258 
    259         /**
    260246         * Filters whether the REST API is enabled.
    261247         *
     
    315301         * header.
    316302         */
     303        $method_overridden = false;
    317304        if ( isset( $_GET['_method'] ) ) {
    318305            $request->set_method( $_GET['_method'] );
    319306        } elseif ( isset( $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] ) ) {
    320307            $request->set_method( $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] );
     308            $method_overridden = true;
    321309        }
    322310
     
    376364         */
    377365        $served = apply_filters( 'rest_pre_serve_request', false, $result, $request, $this );
     366
     367        /**
     368         * Filters whether to send nocache headers on a REST API request.
     369         *
     370         * @since 4.4.0
     371         * @since 6.x.x Moved the block to catch the filter added on rest_cookie_check_errors() from rest-api.php
     372         *
     373         * @param bool $rest_send_nocache_headers Whether to send no-cache headers.
     374         */
     375        $send_no_cache_headers = apply_filters( 'rest_send_nocache_headers', is_user_logged_in() );
     376
     377        // send no cache headers if the $send_no_cache_headers is true
     378        // OR if the HTTP_X_HTTP_METHOD_OVERRIDE is used but resulted a 4xx response code.
     379        if ( $send_no_cache_headers || ( true === $method_overridden && strpos( $code, '4' ) === 0 ) ) {
     380            foreach ( wp_get_nocache_headers() as $header => $header_value ) {
     381                $this->send_header( $header, $header_value );
     382            }
     383        }
    378384
    379385        if ( ! $served ) {
  • branches/4.6/src/wp-includes/shortcodes.php

    r37865 r56859  
    186186
    187187/**
    188  * Search content for shortcodes and filter shortcodes through their hooks.
     188 * Returns a list of registered shortcode names found in the given content.
     189 *
     190 * Example usage:
     191 *
     192 *     get_shortcode_tags_in_content( '[audio src="file.mp3"][/audio] [foo] [gallery ids="1,2,3"]' );
     193 *     // array( 'audio', 'gallery' )
     194 *
     195 * @since 6.3.2
     196 *
     197 * @param string $content The content to check.
     198 * @return string[] An array of registered shortcode names found in the content.
     199 */
     200function get_shortcode_tags_in_content( $content ) {
     201    if ( false === strpos( $content, '[' ) ) {
     202        return array();
     203    }
     204
     205    preg_match_all( '/' . get_shortcode_regex() . '/', $content, $matches, PREG_SET_ORDER );
     206    if ( empty( $matches ) ) {
     207        return array();
     208    }
     209
     210    $tags = array();
     211    foreach ( $matches as $shortcode ) {
     212        $tags[] = $shortcode[2];
     213
     214        if ( ! empty( $shortcode[5] ) ) {
     215            $deep_tags = get_shortcode_tags_in_content( $shortcode[5] );
     216            if ( ! empty( $deep_tags ) ) {
     217                $tags = array_merge( $tags, $deep_tags );
     218            }
     219        }
     220    }
     221
     222    return $tags;
     223}
     224
     225/**
     226 * Searches content for shortcodes and filter shortcodes through their hooks.
    189227 *
    190228 * If there are no shortcode tags defined, then the content will be returned
Note: See TracChangeset for help on using the changeset viewer.