Make WordPress Core

Changeset 56846


Ignore:
Timestamp:
10/12/2023 01:27:29 PM (3 years ago)
Author:
audrasjb
Message:

Shortcodes: Restrict ajax handler for media shortcode.

Props tykoted, xknown, peterwilsoncc, antpb, jorbin.
Merges [56838] to the 6.3 branch.

Location:
branches/6.3
Files:
4 edited
1 copied

Legend:

Unmodified
Added
Removed
  • branches/6.3

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

    r56245 r56846  
    38833883        $shortcode = wp_unslash( $_POST['shortcode'] );
    38843884
     3885        // Only process previews for media related shortcodes:
     3886        $found_shortcodes = get_shortcode_tags_in_content( $shortcode );
     3887        $media_shortcodes = array(
     3888                'audio',
     3889                'embed',
     3890                'playlist',
     3891                'video',
     3892                'gallery',
     3893        );
     3894
     3895        $other_shortcodes = array_diff( $found_shortcodes, $media_shortcodes );
     3896
     3897        if ( ! empty( $other_shortcodes ) ) {
     3898                wp_send_json_error();
     3899        }
     3900
    38853901        if ( ! empty( $_POST['post_ID'] ) ) {
    38863902                $post = get_post( (int) $_POST['post_ID'] );
     
    38893905        // The embed shortcode requires a post.
    38903906        if ( ! $post || ! current_user_can( 'edit_post', $post->ID ) ) {
    3891                 if ( 'embed' === $shortcode ) {
     3907                if ( in_array( 'embed', $found_shortcodes, true ) ) {
    38923908                        wp_send_json_error();
    38933909                }
  • branches/6.3/src/wp-includes/media.php

    r56429 r56846  
    26062606                }
    26072607        } elseif ( ! empty( $atts['exclude'] ) ) {
     2608                $post_parent_id = $id;
    26082609                $attachments = get_children(
    26092610                        array(
     
    26182619                );
    26192620        } else {
     2621                $post_parent_id = $id;
    26202622                $attachments = get_children(
    26212623                        array(
     
    26282630                        )
    26292631                );
     2632        }
     2633
     2634        if ( ! empty( $post_parent_id ) ) {
     2635                $post_parent = get_post( $post_parent_id );
     2636
     2637                // terminate the shortcode execution if user cannot read the post or password-protected
     2638                if (
     2639                ( ! is_post_publicly_viewable( $post_parent->ID ) && ! current_user_can( 'read_post', $post_parent->ID ) )
     2640                || post_password_required( $post_parent ) ) {
     2641                        return '';
     2642                }
    26302643        }
    26312644
     
    29622975        }
    29632976
     2977        if ( ! empty( $args['post_parent'] ) ) {
     2978                $post_parent = get_post( $id );
     2979
     2980                // terminate the shortcode execution if user cannot read the post or password-protected
     2981                if ( ! current_user_can( 'read_post', $post_parent->ID ) || post_password_required( $post_parent ) ) {
     2982                        return '';
     2983                }
     2984        }
     2985
    29642986        if ( empty( $attachments ) ) {
    29652987                return '';
  • branches/6.3/src/wp-includes/shortcodes.php

    r56214 r56846  
    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.