Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validate postId in the site-editor.php #7188

Open
wants to merge 13 commits into
base: trunk
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 68 additions & 2 deletions src/wp-admin/site-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
);
}

$is_template_part = isset( $_GET['postType'] ) && 'wp_template_part' === sanitize_key( $_GET['postType'] );
$post_type_param = isset( $_GET['postType'] ) ? sanitize_key( wp_unslash( $_GET['postType'] ) ) : '';
$is_template_part = $post_type_param && 'wp_template_part' === $post_type_param;
$is_template_part_path = isset( $_GET['path'] ) && 'wp_template_partall' === sanitize_key( $_GET['path'] );
$is_template_part_editor = $is_template_part || $is_template_part_path;
$is_patterns = isset( $_GET['postType'] ) && 'wp_block' === sanitize_key( $_GET['postType'] );
$is_patterns = $post_type_param && 'wp_block' === $post_type_param;
$is_patterns_path = isset( $_GET['path'] ) && 'patterns' === sanitize_key( $_GET['path'] );
$is_patterns_editor = $is_patterns || $is_patterns_path;

Expand All @@ -34,6 +35,71 @@
}
}

// Validate postId and postType.
if ( isset( $_GET['postId'] ) && $post_type_param ) {
switch ( $post_type_param ) {
case 'page':
$post = get_post( (int) $_GET['postId'] );

if ( null === $post || 'page' !== get_post_type( $post ) ) {
wp_die(
__( 'Invalid page ID.' ),
'',
array(
'link_url' => admin_url( 'site-editor.php' ),
'link_text' => __( 'Return to site editor' ),
)
);
}
break;

case 'wp_block':
$post = get_post( (int) $_GET['postId'] );

if ( null === $post || 'wp_block' !== get_post_type( $post ) ) {
wp_die(
__( 'Invalid pattern ID.' ),
'',
array(
'link_url' => add_query_arg( array( 'postType' => 'wp_block' ), admin_url( 'site-editor.php' ) ),
'link_text' => __( 'Return to patterns page' ),
)
);
}
break;

case 'wp_template':
$block_template = get_block_template( $_GET['postId'] );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noting that $_GET['postId'] isn't sanitized here or in the next case.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been fixed


if ( null === $block_template ) {
wp_die(
__( 'Invalid template ID.' ),
'',
array(
'link_url' => add_query_arg( array( 'postType' => 'wp_template' ), admin_url( 'site-editor.php' ) ),
'link_text' => __( 'Return to templates page' ),
)
);
}
break;

case 'wp_template_part':
$block_template = get_block_template( $_GET['postId'], 'wp_template_part' );

if ( null === $block_template ) {
wp_die(
__( 'Invalid template part ID.' ),
'',
array(
'link_url' => add_query_arg( array( 'postType' => 'wp_template_part' ), admin_url( 'site-editor.php' ) ),
'link_text' => __( 'Return to template parts page' ),
)
);
}
break;
}
}

// Used in the HTML title tag.
$title = _x( 'Editor', 'site editor title tag' );
$parent_file = 'themes.php';
Expand Down
Loading