Make WordPress Core

Changeset 56867


Ignore:
Timestamp:
10/12/2023 02:54:10 PM (7 months ago)
Author:
joemcgill
Message:

Grouped backports to the 6.1 branch.

  • REST API: Limit search_columns for users without list_users.
  • Comments: Prevent users who can not see a post from seeing comments on it.
  • Application Passwords: Prevent the use of some pseudo protocols in application passwords.
  • Restrict media shortcode ajax to certain type
  • REST API: Ensure no-cache headers are sent when methods are overriden.
  • Prevent unintended behavior when certain objects are unserialized.

Merges [56833], [56834], [56835], [56836], [56837], and [56838] to the 6.1 branch.
Props xknown, jorbin, Vortfu, joehoyle, timothyblynjacobs, peterwilsoncc, ehtis, tykoted, martinkrcho, paulkevan, dd32, antpb, rmccue.

Location:
branches/6.1
Files:
17 edited

Legend:

Unmodified
Added
Removed
  • branches/6.1

  • branches/6.1/src/wp-admin/includes/ajax-actions.php

    r55771 r56867  
    38533853    $shortcode = wp_unslash( $_POST['shortcode'] );
    38543854
     3855    // Only process previews for media related shortcodes:
     3856    $found_shortcodes = get_shortcode_tags_in_content( $shortcode );
     3857    $media_shortcodes = array(
     3858        'audio',
     3859        'embed',
     3860        'playlist',
     3861        'video',
     3862        'gallery',
     3863    );
     3864
     3865    $other_shortcodes = array_diff( $found_shortcodes, $media_shortcodes );
     3866
     3867    if ( ! empty( $other_shortcodes ) ) {
     3868        wp_send_json_error();
     3869    }
     3870
    38553871    if ( ! empty( $_POST['post_ID'] ) ) {
    38563872        $post = get_post( (int) $_POST['post_ID'] );
     
    38593875    // The embed shortcode requires a post.
    38603876    if ( ! $post || ! current_user_can( 'edit_post', $post->ID ) ) {
    3861         if ( 'embed' === $shortcode ) {
     3877        if ( in_array( 'embed', $found_shortcodes, true ) ) {
    38623878            wp_send_json_error();
    38633879        }
  • branches/6.1/src/wp-admin/includes/class-wp-comments-list-table.php

    r54378 r56867  
    637637
    638638        $this->user_can = current_user_can( 'edit_comment', $comment->comment_ID );
     639
     640        $edit_post_cap = $post ? 'edit_post' : 'edit_posts';
     641        if (
     642            current_user_can( $edit_post_cap, $comment->comment_post_ID ) ||
     643            (
     644                empty( $post->post_password ) &&
     645                current_user_can( 'read_post', $comment->comment_post_ID )
     646            )
     647        ) {
     648            // The user has access to the post
     649        } else {
     650            return false;
     651        }
    639652
    640653        echo "<tr id='comment-$comment->comment_ID' class='$the_comment_class'>";
  • branches/6.1/src/wp-admin/includes/class-wp-list-table.php

    r54378 r56867  
    812812            $pending_comments_number
    813813        );
     814
     815        $post_object   = get_post( $post_id );
     816        $edit_post_cap = $post_object ? 'edit_post' : 'edit_posts';
     817        if (
     818            current_user_can( $edit_post_cap, $post_id ) ||
     819            (
     820                empty( $post_object->post_password ) &&
     821                current_user_can( 'read_post', $post_id )
     822            )
     823        ) {
     824            // The user has access to the post and thus can see comments
     825        } else {
     826            return false;
     827        }
    814828
    815829        if ( ! $approved_comments && ! $pending_comments ) {
  • branches/6.1/src/wp-admin/includes/dashboard.php

    r54638 r56867  
    10921092        echo '<ul id="the-comment-list" data-wp-lists="list:comment">';
    10931093        foreach ( $comments as $comment ) {
    1094             _wp_dashboard_recent_comments_row( $comment );
     1094
     1095            $comment_post = get_post( $comment->comment_post_ID );
     1096            if (
     1097                current_user_can( 'edit_post', $comment->comment_post_ID ) ||
     1098                (
     1099                    empty( $comment_post->post_password ) &&
     1100                    current_user_can( 'read_post', $comment->comment_post_ID )
     1101                )
     1102            ) {
     1103                _wp_dashboard_recent_comments_row( $comment );
     1104            }
    10951105        }
    10961106        echo '</ul>';
  • branches/6.1/src/wp-admin/includes/user.php

    r53458 r56867  
    607607 *
    608608 * @since 5.6.0
     609 * @since 6.2.0 Allow insecure HTTP connections for the local environment.
     610 * @since 6.3.2 Validates the success and reject URLs to prevent javascript pseudo protocol being executed.
    609611 *
    610612 * @param array   $request {
     
    622624    $error = new WP_Error();
    623625
    624     if ( ! empty( $request['success_url'] ) ) {
    625         $scheme = wp_parse_url( $request['success_url'], PHP_URL_SCHEME );
    626 
    627         if ( 'http' === $scheme ) {
     626    if ( isset( $request['success_url'] ) ) {
     627        $validated_success_url = wp_is_authorize_application_redirect_url_valid( $request['success_url'] );
     628        if ( is_wp_error( $validated_success_url ) ) {
    628629            $error->add(
    629                 'invalid_redirect_scheme',
    630                 __( 'The success URL must be served over a secure connection.' )
     630                $validated_success_url->get_error_code(),
     631                $validated_success_url->get_error_message()
    631632            );
    632633        }
    633634    }
    634635
    635     if ( ! empty( $request['reject_url'] ) ) {
    636         $scheme = wp_parse_url( $request['reject_url'], PHP_URL_SCHEME );
    637 
    638         if ( 'http' === $scheme ) {
     636    if ( isset( $request['reject_url'] ) ) {
     637        $validated_reject_url = wp_is_authorize_application_redirect_url_valid( $request['reject_url'] );
     638        if ( is_wp_error( $validated_reject_url ) ) {
    639639            $error->add(
    640                 'invalid_redirect_scheme',
    641                 __( 'The rejection URL must be served over a secure connection.' )
     640                $validated_reject_url->get_error_code(),
     641                $validated_reject_url->get_error_message()
    642642            );
    643643        }
     
    668668    return true;
    669669}
     670
     671/**
     672 * Validates the redirect URL protocol scheme. The protocol can be anything except http and javascript.
     673 *
     674 * @since 6.3.2
     675 *
     676 * @param string $url - The redirect URL to be validated.
     677 *
     678 * @return true|WP_Error True if the redirect URL is valid, a WP_Error object otherwise.
     679 */
     680function wp_is_authorize_application_redirect_url_valid( $url ) {
     681    $bad_protocols = array( 'javascript', 'data' );
     682    if ( empty( $url ) ) {
     683        return true;
     684    }
     685
     686    // Based on https://www.rfc-editor.org/rfc/rfc2396#section-3.1
     687    $valid_scheme_regex = '/^[a-zA-Z][a-zA-Z0-9+.-]*:/';
     688    if ( ! preg_match( $valid_scheme_regex, $url ) ) {
     689        return new WP_Error(
     690            'invalid_redirect_url_format',
     691            __( 'Invalid URL format.' )
     692        );
     693    }
     694
     695    /**
     696     * Filters the list of invalid protocols used in applications redirect URLs.
     697     *
     698     * @since 6.3.2
     699     *
     700     * @param string[]  $bad_protocols Array of invalid protocols.
     701     * @param string    $url The redirect URL to be validated.
     702     */
     703    $invalid_protocols = array_map( 'strtolower', apply_filters( 'wp_authorize_application_redirect_url_invalid_protocols', $bad_protocols, $url ) );
     704
     705    $scheme   = wp_parse_url( $url, PHP_URL_SCHEME );
     706    $host     = wp_parse_url( $url, PHP_URL_HOST );
     707    $is_local = 'local' === wp_get_environment_type();
     708
     709    // validates if the proper URI format is applied to the $url
     710    if ( empty( $host ) || empty( $scheme ) || in_array( strtolower( $scheme ), $invalid_protocols, true ) ) {
     711        return new WP_Error(
     712            'invalid_redirect_url_format',
     713            __( 'Invalid URL format.' )
     714        );
     715    }
     716
     717    if ( 'http' === $scheme && ! $is_local ) {
     718        return new WP_Error(
     719            'invalid_redirect_scheme',
     720            __( 'The URL must be served over a secure connection.' )
     721        );
     722    }
     723
     724    return true;
     725}
  • branches/6.1/src/wp-includes/Requests/Hooks.php

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

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

    r52328 r56867  
    230230    }
    231231
     232    public function __wakeup() {
     233        throw new \LogicException( __CLASS__ . ' should never be unserialized' );
     234    }
     235
    232236    /**
    233237     * Merge a request's data with the default data
  • branches/6.1/src/wp-includes/class-wp-block-patterns-registry.php

    r54133 r56867  
    189189    }
    190190
     191    public function __wakeup() {
     192        if ( ! $this->registered_patterns ) {
     193            return;
     194        }
     195        if ( ! is_array( $this->registered_patterns ) ) {
     196            throw new UnexpectedValueException();
     197        }
     198        foreach ( $this->registered_patterns as $value ) {
     199            if ( ! is_array( $value ) ) {
     200                throw new UnexpectedValueException();
     201            }
     202        }
     203        $this->registered_patterns_outside_init = array();
     204    }
     205
    191206    /**
    192207     * Utility method to retrieve the main instance of the class.
  • branches/6.1/src/wp-includes/class-wp-block-type-registry.php

    r54133 r56867  
    169169    }
    170170
     171    public function __wakeup() {
     172        if ( ! $this->registered_block_types ) {
     173            return;
     174        }
     175        if ( ! is_array( $this->registered_block_types ) ) {
     176            throw new UnexpectedValueException();
     177        }
     178        foreach ( $this->registered_block_types as $value ) {
     179            if ( ! $value instanceof WP_Block_Type ) {
     180                throw new UnexpectedValueException();
     181            }
     182        }
     183    }
     184
    171185    /**
    172186     * Utility method to retrieve the main instance of the class.
  • branches/6.1/src/wp-includes/class-wp-theme.php

    r54236 r56867  
    719719
    720720    /**
     721     * Perform reinitialization tasks.
     722     *
     723     * Prevents a callback from being injected during unserialization of an object.
     724     *
     725     * @return void
     726     */
     727    public function __wakeup() {
     728        if ( $this->parent && ! $this->parent instanceof self ) {
     729            throw new UnexpectedValueException();
     730        }
     731        if ( $this->headers && ! is_array( $this->headers ) ) {
     732            throw new UnexpectedValueException();
     733        }
     734        foreach ( $this->headers as $value ) {
     735            if ( ! is_string( $value ) ) {
     736                throw new UnexpectedValueException();
     737            }
     738        }
     739        $this->headers_sanitized = array();
     740    }
     741
     742    /**
    721743     * Adds theme data to cache.
    722744     *
     
    17791801        return strnatcasecmp( $a->name_translated, $b->name_translated );
    17801802    }
     1803
     1804    private static function _check_headers_property_has_correct_type( $headers ) {
     1805        if ( ! is_array( $headers ) ) {
     1806            return false;
     1807        }
     1808        foreach ( $headers as $key => $value ) {
     1809            if ( ! is_string( $key ) || ! is_string( $value ) ) {
     1810                return false;
     1811            }
     1812        }
     1813        return true;
     1814    }
    17811815}
  • branches/6.1/src/wp-includes/media.php

    r55771 r56867  
    24542454        }
    24552455    } elseif ( ! empty( $atts['exclude'] ) ) {
     2456        $post_parent_id = $id;
    24562457        $attachments = get_children(
    24572458            array(
     
    24662467        );
    24672468    } else {
     2469        $post_parent_id = $id;
    24682470        $attachments = get_children(
    24692471            array(
     
    24762478            )
    24772479        );
     2480    }
     2481
     2482    if ( ! empty( $post_parent_id ) ) {
     2483        $post_parent = get_post( $post_parent_id );
     2484
     2485        // terminate the shortcode execution if user cannot read the post or password-protected
     2486        if (
     2487        ( ! is_post_publicly_viewable( $post_parent->ID ) && ! current_user_can( 'read_post', $post_parent->ID ) )
     2488        || post_password_required( $post_parent ) ) {
     2489            return '';
     2490        }
    24782491    }
    24792492
     
    28042817    }
    28052818
     2819    if ( ! empty( $args['post_parent'] ) ) {
     2820        $post_parent = get_post( $id );
     2821
     2822        // terminate the shortcode execution if user cannot read the post or password-protected
     2823        if ( ! current_user_can( 'read_post', $post_parent->ID ) || post_password_required( $post_parent ) ) {
     2824            return '';
     2825        }
     2826    }
     2827
    28062828    if ( empty( $attachments ) ) {
    28072829        return '';
  • branches/6.1/src/wp-includes/rest-api.php

    r54518 r56867  
    10691069
    10701070    if ( ! $result ) {
     1071        add_filter( 'rest_send_nocache_headers', '__return_true', 20 );
    10711072        return new WP_Error( 'rest_cookie_invalid_nonce', __( 'Cookie check failed' ), array( 'status' => 403 ) );
    10721073    }
  • branches/6.1/src/wp-includes/rest-api/class-wp-rest-server.php

    r54339 r56867  
    360360
    361361        /**
    362          * Filters whether to send nocache headers on a REST API request.
    363          *
    364          * @since 4.4.0
    365          *
    366          * @param bool $rest_send_nocache_headers Whether to send no-cache headers.
    367          */
    368         $send_no_cache_headers = apply_filters( 'rest_send_nocache_headers', is_user_logged_in() );
    369         if ( $send_no_cache_headers ) {
    370             foreach ( wp_get_nocache_headers() as $header => $header_value ) {
    371                 if ( empty( $header_value ) ) {
    372                     $this->remove_header( $header );
    373                 } else {
    374                     $this->send_header( $header, $header_value );
    375                 }
    376             }
    377         }
    378 
    379         /**
    380362         * Filters whether the REST API is enabled.
    381363         *
     
    431413         * header.
    432414         */
     415        $method_overridden = false;
    433416        if ( isset( $_GET['_method'] ) ) {
    434417            $request->set_method( $_GET['_method'] );
    435418        } elseif ( isset( $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] ) ) {
    436419            $request->set_method( $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] );
     420            $method_overridden = true;
    437421        }
    438422
     
    493477         */
    494478        $served = apply_filters( 'rest_pre_serve_request', false, $result, $request, $this );
     479
     480        /**
     481         * Filters whether to send nocache headers on a REST API request.
     482         *
     483         * @since 4.4.0
     484         * @since 6.x.x Moved the block to catch the filter added on rest_cookie_check_errors() from rest-api.php
     485         *
     486         * @param bool $rest_send_nocache_headers Whether to send no-cache headers.
     487         */
     488        $send_no_cache_headers = apply_filters( 'rest_send_nocache_headers', is_user_logged_in() );
     489
     490        // send no cache headers if the $send_no_cache_headers is true
     491        // OR if the HTTP_X_HTTP_METHOD_OVERRIDE is used but resulted a 4xx response code.
     492        if ( $send_no_cache_headers || ( true === $method_overridden && strpos( $code, '4' ) === 0 ) ) {
     493            foreach ( wp_get_nocache_headers() as $header => $header_value ) {
     494                if ( empty( $header_value ) ) {
     495                    $this->remove_header( $header );
     496                } else {
     497                    $this->send_header( $header, $header_value );
     498                }
     499            }
     500        }
    495501
    496502        if ( ! $served ) {
  • branches/6.1/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php

    r54317 r56867  
    319319
    320320        if ( ! empty( $prepared_args['search'] ) ) {
     321            if ( ! current_user_can( 'list_users' ) ) {
     322                $prepared_args['search_columns'] = array( 'ID', 'user_login', 'user_nicename', 'display_name' );
     323            }
    321324            $prepared_args['search'] = '*' . $prepared_args['search'] . '*';
    322325        }
  • branches/6.1/src/wp-includes/shortcodes.php

    r54319 r56867  
    167167    }
    168168    return false;
     169}
     170
     171/**
     172 * Returns a list of registered shortcode names found in the given content.
     173 *
     174 * Example usage:
     175 *
     176 *     get_shortcode_tags_in_content( '[audio src="file.mp3"][/audio] [foo] [gallery ids="1,2,3"]' );
     177 *     // array( 'audio', 'gallery' )
     178 *
     179 * @since 6.3.2
     180 *
     181 * @param string $content The content to check.
     182 * @return string[] An array of registered shortcode names found in the content.
     183 */
     184function get_shortcode_tags_in_content( $content ) {
     185    if ( false === strpos( $content, '[' ) ) {
     186        return array();
     187    }
     188
     189    preg_match_all( '/' . get_shortcode_regex() . '/', $content, $matches, PREG_SET_ORDER );
     190    if ( empty( $matches ) ) {
     191        return array();
     192    }
     193
     194    $tags = array();
     195    foreach ( $matches as $shortcode ) {
     196        $tags[] = $shortcode[2];
     197
     198        if ( ! empty( $shortcode[5] ) ) {
     199            $deep_tags = get_shortcode_tags_in_content( $shortcode[5] );
     200            if ( ! empty( $deep_tags ) ) {
     201                $tags = array_merge( $tags, $deep_tags );
     202            }
     203        }
     204    }
     205
     206    return $tags;
    169207}
    170208
Note: See TracChangeset for help on using the changeset viewer.