From d4c02d548eb9a462ee32d1271781ae7aeca5204e Mon Sep 17 00:00:00 2001 From: davidbaumwald Date: Thu, 12 Oct 2023 18:28:44 +0000 Subject: [PATCH] Grouped backports to the 5.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. - REST API: Limit `search_columns` for users without `list_users`. - Prevent unintended behavior when certain objects are unserialized. - Application Passwords: Prevent the use of some pseudo protocols in application passwords. Merges [56833], [56834], [56835], [56836], [56837], and [56838] to the 5.6 branch. Props xknown, jorbin, Vortfu, joehoyle, timothyblynjacobs, peterwilsoncc, ehtis, tykoted, martinkrcho, paulkevan, dd32, antpb, rmccue. Built from https://develop.svn.wordpress.org/branches/5.6@56882 git-svn-id: http://core.svn.wordpress.org/branches/5.6@56393 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/ajax-actions.php | 18 ++++- .../includes/class-wp-comments-list-table.php | 13 +++ wp-admin/includes/class-wp-list-table.php | 14 ++++ wp-admin/includes/dashboard.php | 12 ++- wp-admin/includes/user.php | 80 ++++++++++++++++--- wp-includes/Requests/Hooks.php | 6 +- wp-includes/Requests/IRI.php | 14 ++++ wp-includes/Requests/Session.php | 4 + .../class-wp-block-patterns-registry.php | 15 ++++ wp-includes/class-wp-block-type-registry.php | 14 ++++ wp-includes/class-wp-theme.php | 34 ++++++++ wp-includes/media.php | 22 +++++ wp-includes/rest-api.php | 1 + wp-includes/rest-api/class-wp-rest-server.php | 42 +++++----- .../class-wp-rest-users-controller.php | 3 + wp-includes/shortcodes.php | 40 +++++++++- 16 files changed, 298 insertions(+), 34 deletions(-) diff --git a/wp-admin/includes/ajax-actions.php b/wp-admin/includes/ajax-actions.php index a272b2d259..2acad02179 100644 --- a/wp-admin/includes/ajax-actions.php +++ b/wp-admin/includes/ajax-actions.php @@ -3766,13 +3766,29 @@ function wp_ajax_parse_media_shortcode() { $shortcode = wp_unslash( $_POST['shortcode'] ); + // Only process previews for media related shortcodes: + $found_shortcodes = get_shortcode_tags_in_content( $shortcode ); + $media_shortcodes = array( + 'audio', + 'embed', + 'playlist', + 'video', + 'gallery', + ); + + $other_shortcodes = array_diff( $found_shortcodes, $media_shortcodes ); + + if ( ! empty( $other_shortcodes ) ) { + wp_send_json_error(); + } + if ( ! empty( $_POST['post_ID'] ) ) { $post = get_post( (int) $_POST['post_ID'] ); } // The embed shortcode requires a post. if ( ! $post || ! current_user_can( 'edit_post', $post->ID ) ) { - if ( 'embed' === $shortcode ) { + if ( in_array( 'embed', $found_shortcodes, true ) ) { wp_send_json_error(); } } else { diff --git a/wp-admin/includes/class-wp-comments-list-table.php b/wp-admin/includes/class-wp-comments-list-table.php index 563187ffe4..ae574989ce 100644 --- a/wp-admin/includes/class-wp-comments-list-table.php +++ b/wp-admin/includes/class-wp-comments-list-table.php @@ -622,6 +622,19 @@ class WP_Comments_List_Table extends WP_List_Table { } $this->user_can = current_user_can( 'edit_comment', $comment->comment_ID ); + $edit_post_cap = $post ? 'edit_post' : 'edit_posts'; + if ( + current_user_can( $edit_post_cap, $comment->comment_post_ID ) || + ( + empty( $post->post_password ) && + current_user_can( 'read_post', $comment->comment_post_ID ) + ) + ) { + // The user has access to the post + } else { + return false; + } + echo ""; $this->single_row_columns( $comment ); echo "\n"; diff --git a/wp-admin/includes/class-wp-list-table.php b/wp-admin/includes/class-wp-list-table.php index e6dc3b6ebc..c5a680014e 100644 --- a/wp-admin/includes/class-wp-list-table.php +++ b/wp-admin/includes/class-wp-list-table.php @@ -727,6 +727,20 @@ class WP_List_Table { $pending_comments_number ); + $post_object = get_post( $post_id ); + $edit_post_cap = $post_object ? 'edit_post' : 'edit_posts'; + if ( + current_user_can( $edit_post_cap, $post_id ) || + ( + empty( $post_object->post_password ) && + current_user_can( 'read_post', $post_id ) + ) + ) { + // The user has access to the post and thus can see comments + } else { + return false; + } + if ( ! $approved_comments && ! $pending_comments ) { // No comments at all. printf( diff --git a/wp-admin/includes/dashboard.php b/wp-admin/includes/dashboard.php index 38a33e8557..a5a12dfca0 100644 --- a/wp-admin/includes/dashboard.php +++ b/wp-admin/includes/dashboard.php @@ -1052,7 +1052,17 @@ function wp_dashboard_recent_comments( $total_items = 5 ) { echo ''; diff --git a/wp-admin/includes/user.php b/wp-admin/includes/user.php index a08fe3f4b2..c8588210db 100644 --- a/wp-admin/includes/user.php +++ b/wp-admin/includes/user.php @@ -599,6 +599,8 @@ Please click the following link to activate your user account: * Checks if the Authorize Application Password request is valid. * * @since 5.6.0 + * @since 6.2.0 Allow insecure HTTP connections for the local environment. + * @since 6.3.2 Validates the success and reject URLs to prevent javascript pseudo protocol being executed. * * @param array $request { * The array of request data. All arguments are optional and may be empty. @@ -614,24 +616,22 @@ Please click the following link to activate your user account: function wp_is_authorize_application_password_request_valid( $request, $user ) { $error = new WP_Error(); - if ( ! empty( $request['success_url'] ) ) { - $scheme = wp_parse_url( $request['success_url'], PHP_URL_SCHEME ); - - if ( 'http' === $scheme ) { + if ( isset( $request['success_url'] ) ) { + $validated_success_url = wp_is_authorize_application_redirect_url_valid( $request['success_url'] ); + if ( is_wp_error( $validated_success_url ) ) { $error->add( - 'invalid_redirect_scheme', - __( 'The success url must be served over a secure connection.' ) + $validated_success_url->get_error_code(), + $validated_success_url->get_error_message() ); } } - if ( ! empty( $request['reject_url'] ) ) { - $scheme = wp_parse_url( $request['reject_url'], PHP_URL_SCHEME ); - - if ( 'http' === $scheme ) { + if ( isset( $request['reject_url'] ) ) { + $validated_reject_url = wp_is_authorize_application_redirect_url_valid( $request['reject_url'] ); + if ( is_wp_error( $validated_reject_url ) ) { $error->add( - 'invalid_redirect_scheme', - __( 'The rejection url must be served over a secure connection.' ) + $validated_reject_url->get_error_code(), + $validated_reject_url->get_error_message() ); } } @@ -660,3 +660,59 @@ function wp_is_authorize_application_password_request_valid( $request, $user ) { return true; } + +/** + * Validates the redirect URL protocol scheme. The protocol can be anything except http and javascript. + * + * @since 6.3.2 + * + * @param string $url - The redirect URL to be validated. + * + * @return true|WP_Error True if the redirect URL is valid, a WP_Error object otherwise. + */ +function wp_is_authorize_application_redirect_url_valid( $url ) { + $bad_protocols = array( 'javascript', 'data' ); + if ( empty( $url ) ) { + return true; + } + + // Based on https://www.rfc-editor.org/rfc/rfc2396#section-3.1 + $valid_scheme_regex = '/^[a-zA-Z][a-zA-Z0-9+.-]*:/'; + if ( ! preg_match( $valid_scheme_regex, $url ) ) { + return new WP_Error( + 'invalid_redirect_url_format', + __( 'Invalid URL format.' ) + ); + } + + /** + * Filters the list of invalid protocols used in applications redirect URLs. + * + * @since 6.3.2 + * + * @param string[] $bad_protocols Array of invalid protocols. + * @param string $url The redirect URL to be validated. + */ + $invalid_protocols = array_map( 'strtolower', apply_filters( 'wp_authorize_application_redirect_url_invalid_protocols', $bad_protocols, $url ) ); + + $scheme = wp_parse_url( $url, PHP_URL_SCHEME ); + $host = wp_parse_url( $url, PHP_URL_HOST ); + $is_local = 'local' === wp_get_environment_type(); + + // validates if the proper URI format is applied to the $url + if ( empty( $host ) || empty( $scheme ) || in_array( strtolower( $scheme ), $invalid_protocols, true ) ) { + return new WP_Error( + 'invalid_redirect_url_format', + __( 'Invalid URL format.' ) + ); + } + + if ( 'http' === $scheme && ! $is_local ) { + return new WP_Error( + 'invalid_redirect_scheme', + __( 'The URL must be served over a secure connection.' ) + ); + } + + return true; +} diff --git a/wp-includes/Requests/Hooks.php b/wp-includes/Requests/Hooks.php index 2e61c73406..c94ee9b5b9 100644 --- a/wp-includes/Requests/Hooks.php +++ b/wp-includes/Requests/Hooks.php @@ -65,4 +65,8 @@ class Requests_Hooks implements Requests_Hooker { return true; } -} \ No newline at end of file + + public function __wakeup() { + throw new \LogicException( __CLASS__ . ' should never be unserialized' ); + } +} diff --git a/wp-includes/Requests/IRI.php b/wp-includes/Requests/IRI.php index 8dc2fa2841..e262893b3a 100644 --- a/wp-includes/Requests/IRI.php +++ b/wp-includes/Requests/IRI.php @@ -703,6 +703,20 @@ class Requests_IRI { return true; } + public function __wakeup() { + $class_props = get_class_vars( __CLASS__ ); + $string_props = array( 'scheme', 'iuserinfo', 'ihost', 'port', 'ipath', 'iquery', 'ifragment' ); + $array_props = array( 'normalization' ); + foreach ( $class_props as $prop => $default_value ) { + if ( in_array( $prop, $string_props, true ) && ! is_string( $this->$prop ) ) { + throw new UnexpectedValueException(); + } elseif ( in_array( $prop, $array_props, true ) && ! is_array( $this->$prop ) ) { + throw new UnexpectedValueException(); + } + $this->$prop = null; + } + } + /** * Set the entire IRI. Returns true on success, false on failure (if there * are any invalid characters). diff --git a/wp-includes/Requests/Session.php b/wp-includes/Requests/Session.php index af14bbe416..6c3e0974be 100644 --- a/wp-includes/Requests/Session.php +++ b/wp-includes/Requests/Session.php @@ -227,6 +227,10 @@ class Requests_Session { return Requests::request_multiple($requests, $options); } + public function __wakeup() { + throw new \LogicException( __CLASS__ . ' should never be unserialized' ); + } + /** * Merge a request's data with the default data * diff --git a/wp-includes/class-wp-block-patterns-registry.php b/wp-includes/class-wp-block-patterns-registry.php index 95756de778..a6cad8f2db 100644 --- a/wp-includes/class-wp-block-patterns-registry.php +++ b/wp-includes/class-wp-block-patterns-registry.php @@ -124,6 +124,21 @@ final class WP_Block_Patterns_Registry { return isset( $this->registered_patterns[ $pattern_name ] ); } + public function __wakeup() { + if ( ! $this->registered_patterns ) { + return; + } + if ( ! is_array( $this->registered_patterns ) ) { + throw new UnexpectedValueException(); + } + foreach ( $this->registered_patterns as $value ) { + if ( ! is_array( $value ) ) { + throw new UnexpectedValueException(); + } + } + $this->registered_patterns_outside_init = array(); + } + /** * Utility method to retrieve the main instance of the class. * diff --git a/wp-includes/class-wp-block-type-registry.php b/wp-includes/class-wp-block-type-registry.php index e11cb9911a..a7f3f9fce0 100644 --- a/wp-includes/class-wp-block-type-registry.php +++ b/wp-includes/class-wp-block-type-registry.php @@ -155,6 +155,20 @@ final class WP_Block_Type_Registry { return isset( $this->registered_block_types[ $name ] ); } + public function __wakeup() { + if ( ! $this->registered_block_types ) { + return; + } + if ( ! is_array( $this->registered_block_types ) ) { + throw new UnexpectedValueException(); + } + foreach ( $this->registered_block_types as $value ) { + if ( ! $value instanceof WP_Block_Type ) { + throw new UnexpectedValueException(); + } + } + } + /** * Utility method to retrieve the main instance of the class. * diff --git a/wp-includes/class-wp-theme.php b/wp-includes/class-wp-theme.php index f75b957c71..44b6834409 100644 --- a/wp-includes/class-wp-theme.php +++ b/wp-includes/class-wp-theme.php @@ -675,6 +675,28 @@ final class WP_Theme implements ArrayAccess { return isset( $this->parent ) ? $this->parent : false; } + /** + * Perform reinitialization tasks. + * + * Prevents a callback from being injected during unserialization of an object. + * + * @return void + */ + public function __wakeup() { + if ( $this->parent && ! $this->parent instanceof self ) { + throw new UnexpectedValueException(); + } + if ( $this->headers && ! is_array( $this->headers ) ) { + throw new UnexpectedValueException(); + } + foreach ( $this->headers as $value ) { + if ( ! is_string( $value ) ) { + throw new UnexpectedValueException(); + } + } + $this->headers_sanitized = array(); + } + /** * Adds theme data to cache. * @@ -1659,4 +1681,16 @@ final class WP_Theme implements ArrayAccess { private static function _name_sort_i18n( $a, $b ) { return strnatcasecmp( $a->name_translated, $b->name_translated ); } + + private static function _check_headers_property_has_correct_type( $headers ) { + if ( ! is_array( $headers ) ) { + return false; + } + foreach ( $headers as $key => $value ) { + if ( ! is_string( $key ) || ! is_string( $value ) ) { + return false; + } + } + return true; + } } diff --git a/wp-includes/media.php b/wp-includes/media.php index f89af4e7da..e6ef8c1de6 100644 --- a/wp-includes/media.php +++ b/wp-includes/media.php @@ -2248,6 +2248,7 @@ function gallery_shortcode( $attr ) { $attachments[ $val->ID ] = $_attachments[ $key ]; } } elseif ( ! empty( $atts['exclude'] ) ) { + $post_parent_id = $id; $attachments = get_children( array( 'post_parent' => $id, @@ -2260,6 +2261,7 @@ function gallery_shortcode( $attr ) { ) ); } else { + $post_parent_id = $id; $attachments = get_children( array( 'post_parent' => $id, @@ -2272,6 +2274,17 @@ function gallery_shortcode( $attr ) { ); } + if ( ! empty( $post_parent_id ) ) { + $post_parent = get_post( $post_parent_id ); + + // terminate the shortcode execution if user cannot read the post or password-protected + if ( + ( ! is_post_publicly_viewable( $post_parent->ID ) && ! current_user_can( 'read_post', $post_parent->ID ) ) + || post_password_required( $post_parent ) ) { + return ''; + } + } + if ( empty( $attachments ) ) { return ''; } @@ -2598,6 +2611,15 @@ function wp_playlist_shortcode( $attr ) { $attachments = get_children( $args ); } + if ( ! empty( $args['post_parent'] ) ) { + $post_parent = get_post( $id ); + + // terminate the shortcode execution if user cannot read the post or password-protected + if ( ! current_user_can( 'read_post', $post_parent->ID ) || post_password_required( $post_parent ) ) { + return ''; + } + } + if ( empty( $attachments ) ) { return ''; } diff --git a/wp-includes/rest-api.php b/wp-includes/rest-api.php index 5a28f4f1d4..8f56a5487a 100644 --- a/wp-includes/rest-api.php +++ b/wp-includes/rest-api.php @@ -1012,6 +1012,7 @@ function rest_cookie_check_errors( $result ) { $result = wp_verify_nonce( $nonce, 'wp_rest' ); if ( ! $result ) { + add_filter( 'rest_send_nocache_headers', '__return_true', 20 ); return new WP_Error( 'rest_cookie_invalid_nonce', __( 'Cookie nonce is invalid' ), array( 'status' => 403 ) ); } diff --git a/wp-includes/rest-api/class-wp-rest-server.php b/wp-includes/rest-api/class-wp-rest-server.php index e0e8a3528e..6af326be5b 100644 --- a/wp-includes/rest-api/class-wp-rest-server.php +++ b/wp-includes/rest-api/class-wp-rest-server.php @@ -344,24 +344,6 @@ class WP_REST_Server { $this->send_header( 'Access-Control-Allow-Headers', implode( ', ', $allow_headers ) ); - /** - * Send nocache headers on authenticated requests. - * - * @since 4.4.0 - * - * @param bool $rest_send_nocache_headers Whether to send no-cache headers. - */ - $send_no_cache_headers = apply_filters( 'rest_send_nocache_headers', is_user_logged_in() ); - if ( $send_no_cache_headers ) { - foreach ( wp_get_nocache_headers() as $header => $header_value ) { - if ( empty( $header_value ) ) { - $this->remove_header( $header ); - } else { - $this->send_header( $header, $header_value ); - } - } - } - /** * Filters whether the REST API is enabled. * @@ -428,10 +410,12 @@ class WP_REST_Server { * $_GET['_method']. If that is not set, we check for the HTTP_X_HTTP_METHOD_OVERRIDE * header. */ + $method_overridden = false; if ( isset( $_GET['_method'] ) ) { $request->set_method( $_GET['_method'] ); } elseif ( isset( $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] ) ) { $request->set_method( $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] ); + $method_overridden = true; } $result = $this->check_authentication(); @@ -490,6 +474,28 @@ class WP_REST_Server { */ $served = apply_filters( 'rest_pre_serve_request', false, $result, $request, $this ); + /** + * Filters whether to send nocache headers on a REST API request. + * + * @since 4.4.0 + * @since 6.x.x Moved the block to catch the filter added on rest_cookie_check_errors() from rest-api.php + * + * @param bool $rest_send_nocache_headers Whether to send no-cache headers. + */ + $send_no_cache_headers = apply_filters( 'rest_send_nocache_headers', is_user_logged_in() ); + + // send no cache headers if the $send_no_cache_headers is true + // OR if the HTTP_X_HTTP_METHOD_OVERRIDE is used but resulted a 4xx response code. + if ( $send_no_cache_headers || ( true === $method_overridden && strpos( $code, '4' ) === 0 ) ) { + foreach ( wp_get_nocache_headers() as $header => $header_value ) { + if ( empty( $header_value ) ) { + $this->remove_header( $header ); + } else { + $this->send_header( $header, $header_value ); + } + } + } + if ( ! $served ) { if ( 'HEAD' === $request->get_method() ) { return null; diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php index cac342661b..aacee5f5e0 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php @@ -302,6 +302,9 @@ class WP_REST_Users_Controller extends WP_REST_Controller { } if ( ! empty( $prepared_args['search'] ) ) { + if ( ! current_user_can( 'list_users' ) ) { + $prepared_args['search_columns'] = array( 'ID', 'user_login', 'user_nicename', 'display_name' ); + } $prepared_args['search'] = '*' . $prepared_args['search'] . '*'; } /** diff --git a/wp-includes/shortcodes.php b/wp-includes/shortcodes.php index 90ca735dae..f29be82ed0 100644 --- a/wp-includes/shortcodes.php +++ b/wp-includes/shortcodes.php @@ -160,7 +160,45 @@ function has_shortcode( $content, $tag ) { } /** - * Search content for shortcodes and filter shortcodes through their hooks. + * Returns a list of registered shortcode names found in the given content. + * + * Example usage: + * + * get_shortcode_tags_in_content( '[audio src="file.mp3"][/audio] [foo] [gallery ids="1,2,3"]' ); + * // array( 'audio', 'gallery' ) + * + * @since 6.3.2 + * + * @param string $content The content to check. + * @return string[] An array of registered shortcode names found in the content. + */ +function get_shortcode_tags_in_content( $content ) { + if ( false === strpos( $content, '[' ) ) { + return array(); + } + + preg_match_all( '/' . get_shortcode_regex() . '/', $content, $matches, PREG_SET_ORDER ); + if ( empty( $matches ) ) { + return array(); + } + + $tags = array(); + foreach ( $matches as $shortcode ) { + $tags[] = $shortcode[2]; + + if ( ! empty( $shortcode[5] ) ) { + $deep_tags = get_shortcode_tags_in_content( $shortcode[5] ); + if ( ! empty( $deep_tags ) ) { + $tags = array_merge( $tags, $deep_tags ); + } + } + } + + return $tags; +} + +/** + * Searches content for shortcodes and filter shortcodes through their hooks. * * This function is an alias for do_shortcode(). *