Skip to content

Commit 43597e6

Browse files
committed
Security: Backport the WordPress 7.1.1 security fixes to the 6.1 branch.
- Posts, Post Types: Reject a supplied post ID on the create path in `_wp_translate_postdata()`. - Editor: Constrain block template file resolution to the template directory. - Comments: Enforce target post permissions when updating notes via REST. - XML-RPC: Reject writes to internal-only builtin post types. - Administration: Add authorization check to `wp_ajax_sample_permalink()`. - Customize: Improve `header_image_data` theme mod sanitization. - Media: Check `read_post` on the attachment's parent post. - Themes: Escape the theme installer preview route value. - Plugins: Require network plugin authority to Ajax-activate a network-only plugin. - Formatting: Prevent `wpautop()` moving a paragraph into an attribute of a blockquote. Merges r63657, r63658, r63659, r63660, r63663, r63665, r63669, r63670, r63672, r63675 to the 6.1 branch. Props xknown, westonruter, jorbin, vortfu, batmoo, davidbinda, jeremyfelt, ramonopoly, jonsurrell, adamsilverstein, villanovachile, johnbillion, peterwilsoncc, lancewillett, dmsnell, whyisjake, buffer1024, joehoyle, rafiem. git-svn-id: https://develop.svn.wordpress.org/branches/6.1@63700 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 650edec commit 43597e6

12 files changed

Lines changed: 344 additions & 18 deletions

‎src/js/_enqueues/wp/theme.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2026,9 +2026,9 @@ themes.RunInstaller = {
20262026
self.view.collection.query( request );
20272027
self.view.collection.trigger( 'update' );
20282028

2029-
// Open the theme preview.
2029+
// Open the theme preview. The slug comes from the URL, so escape it.
20302030
self.view.collection.once( 'query:success', function() {
2031-
$( 'div[data-slug="' + slug + '"]' ).trigger( 'click' );
2031+
$( 'div.theme[data-slug="' + $.escapeSelector( slug ) + '"]' ).trigger( 'click' );
20322032
});
20332033

20342034
}

‎src/wp-admin/includes/ajax-actions.php‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2002,6 +2002,13 @@ function wp_ajax_menu_quick_search() {
20022002
function wp_ajax_get_permalink() {
20032003
check_ajax_referer( 'getpermalink', 'getpermalinknonce' );
20042004
$post_id = isset( $_POST['post_id'] ) ? (int) $_POST['post_id'] : 0;
2005+
if ( ! $post_id ) {
2006+
// Bypass call to get_preview_post_link() for unspecified post ID.
2007+
wp_die( '' );
2008+
}
2009+
if ( ! current_user_can( 'edit_post', $post_id ) ) {
2010+
wp_die( -1 );
2011+
}
20052012
wp_die( get_preview_post_link( $post_id ) );
20062013
}
20072014

@@ -2013,8 +2020,15 @@ function wp_ajax_get_permalink() {
20132020
function wp_ajax_sample_permalink() {
20142021
check_ajax_referer( 'samplepermalink', 'samplepermalinknonce' );
20152022
$post_id = isset( $_POST['post_id'] ) ? (int) $_POST['post_id'] : 0;
2016-
$title = isset( $_POST['new_title'] ) ? $_POST['new_title'] : '';
2017-
$slug = isset( $_POST['new_slug'] ) ? $_POST['new_slug'] : null;
2023+
if ( ! $post_id ) {
2024+
// Bypass call to get_sample_permalink_html() for unspecified post ID.
2025+
wp_die( '' );
2026+
}
2027+
if ( ! current_user_can( 'edit_post', $post_id ) ) {
2028+
wp_die( -1 );
2029+
}
2030+
$title = isset( $_POST['new_title'] ) ? $_POST['new_title'] : '';
2031+
$slug = isset( $_POST['new_slug'] ) ? $_POST['new_slug'] : null;
20182032
wp_die( get_sample_permalink_html( $post_id, $title, $slug ) );
20192033
}
20202034

‎src/wp-admin/includes/class-custom-image-header.php‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ public function step_1() {
557557
$header_image_style .= 'height:' . $custom_header->height . 'px;';
558558
}
559559
?>
560-
<div id="headimg" style="<?php echo $header_image_style; ?>">
560+
<div id="headimg" style="<?php echo esc_attr( $header_image_style ); ?>">
561561
<?php
562562
if ( display_header_text() ) {
563563
$style = ' style="color:#' . get_header_textcolor() . ';"';
@@ -1166,8 +1166,8 @@ final public function set_header_image( $choice ) {
11661166
'attachment_id' => $choice['attachment_id'],
11671167
'url' => $choice['url'],
11681168
'thumbnail_url' => $choice['url'],
1169-
'height' => $choice['height'],
1170-
'width' => $choice['width'],
1169+
'height' => absint( $choice['height'] ),
1170+
'width' => absint( $choice['width'] ),
11711171
);
11721172

11731173
update_post_meta( $choice['attachment_id'], '_wp_attachment_is_custom_header', get_stylesheet() );
@@ -1198,7 +1198,13 @@ final public function set_header_image( $choice ) {
11981198
}
11991199
}
12001200

1201-
set_theme_mod( 'header_image', sanitize_url( $header_image_data['url'] ) );
1201+
$header_image_data['url'] = sanitize_url( $header_image_data['url'] );
1202+
1203+
if ( isset( $header_image_data['thumbnail_url'] ) ) {
1204+
$header_image_data['thumbnail_url'] = sanitize_url( $header_image_data['thumbnail_url'] );
1205+
}
1206+
1207+
set_theme_mod( 'header_image', $header_image_data['url'] );
12021208
set_theme_mod( 'header_image_data', $header_image_data );
12031209
}
12041210

@@ -1563,9 +1569,8 @@ public function get_uploaded_header_images() {
15631569
$alt_text_key = '_wp_attachment_image_alt';
15641570

15651571
foreach ( $header_images as &$header_image ) {
1566-
$header_meta = get_post_meta( $header_image['attachment_id'] );
1567-
$header_image['timestamp'] = isset( $header_meta[ $timestamp_key ] ) ? $header_meta[ $timestamp_key ] : '';
1568-
$header_image['alt_text'] = isset( $header_meta[ $alt_text_key ] ) ? $header_meta[ $alt_text_key ] : '';
1572+
$header_image['timestamp'] = get_post_meta( $header_image['attachment_id'], $timestamp_key, true );
1573+
$header_image['alt_text'] = get_post_meta( $header_image['attachment_id'], $alt_text_key, true );
15691574
}
15701575

15711576
return $header_images;

‎src/wp-admin/includes/media.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3309,7 +3309,7 @@ function attachment_submitbox_metadata() {
33093309
<?php
33103310
if ( $post->post_parent ) {
33113311
$post_parent = get_post( $post->post_parent );
3312-
if ( $post_parent ) {
3312+
if ( $post_parent && current_user_can( 'read_post', $post_parent->ID ) ) {
33133313
$uploaded_to_title = $post_parent->post_title ? $post_parent->post_title : __( '(no title)' );
33143314
$uploaded_to_link = get_edit_post_link( $post->post_parent, 'raw' );
33153315
?>

‎src/wp-admin/includes/plugin.php‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,11 +586,17 @@ function is_plugin_active_for_network( $plugin ) {
586586
* Checks for "Site Wide Only: true" for backward compatibility.
587587
*
588588
* @since 3.0.0
589+
* @since 7.1.1 The `$plugin` path is normalized with `plugin_basename()` and `trim()`,
590+
* matching how `activate_plugin()` resolves it.
589591
*
590-
* @param string $plugin Path to the plugin file relative to the plugins directory.
592+
* @param string $plugin Path to the plugin file. Accepts a path relative to the plugins
593+
* directory, or an absolute path, with or without surrounding whitespace.
591594
* @return bool True if plugin is network only, false otherwise.
592595
*/
593596
function is_network_only_plugin( $plugin ) {
597+
// Normalize the path the same way activate_plugin() does, so both agree on the file.
598+
$plugin = plugin_basename( trim( $plugin ) );
599+
594600
$plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
595601
if ( $plugin_data ) {
596602
return $plugin_data['Network'];

‎src/wp-admin/includes/post.php‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@ function _wp_translate_postdata( $update = false, $post_data = null ) {
2424
$post_data = &$_POST;
2525
}
2626

27+
/*
28+
* A raw `ID` on the create path (no `post_ID`) is an attempt to overwrite an
29+
* existing post while bypassing the per-post capability checks below, which only
30+
* run on the update path. Reject it outright: legitimate post creation never
31+
* carries an `ID`.
32+
*/
33+
if ( ! $update && ! empty( $post_data['ID'] ) ) {
34+
if ( 'page' === $post_data['post_type'] ) {
35+
return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to edit pages as this user.' ) );
36+
} else {
37+
return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to edit posts as this user.' ) );
38+
}
39+
}
40+
2741
if ( $update ) {
2842
$post_data['ID'] = (int) $post_data['post_ID'];
2943
}

‎src/wp-includes/block-template-utils.php‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,11 +262,22 @@ function _get_block_template_file( $template_type, $slug ) {
262262
);
263263
foreach ( $themes as $theme_slug => $theme_dir ) {
264264
$template_base_paths = get_block_theme_folders( $theme_slug );
265-
$file_path = $theme_dir . '/' . $template_base_paths[ $template_type ] . '/' . $slug . '.html';
266-
if ( file_exists( $file_path ) ) {
265+
$template_dir = $theme_dir . '/' . $template_base_paths[ $template_type ];
266+
$file_path = $template_dir . '/' . $slug . '.html';
267+
$template_file = realpath( $file_path );
268+
$template_root = realpath( $template_dir );
269+
270+
if (
271+
false !== $template_file &&
272+
false !== $template_root &&
273+
str_starts_with(
274+
wp_normalize_path( $template_file ),
275+
trailingslashit( wp_normalize_path( $template_root ) )
276+
)
277+
) {
267278
$new_template_item = array(
268279
'slug' => $slug,
269-
'path' => $file_path,
280+
'path' => $template_file,
270281
'theme' => $theme_slug,
271282
'type' => $template_type,
272283
);

‎src/wp-includes/class-wp-xmlrpc-server.php‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,6 +1440,33 @@ protected function _insert_post( $user, $content_struct ) {
14401440
return new IXR_Error( 403, __( 'Invalid post type.' ) );
14411441
}
14421442

1443+
// Reject writes to internal-only builtin post types (e.g. customize_changeset)
1444+
// whose intended write path is a dedicated helper, not a generic post API.
1445+
$is_internal_only = (
1446+
empty( $post_type->public )
1447+
&& empty( $post_type->show_in_rest )
1448+
&& ! empty( $post_type->_builtin )
1449+
);
1450+
1451+
/**
1452+
* Filters whether a post type accepts writes via XML-RPC.
1453+
*
1454+
* Defaults to false for internal-only builtin post types (public=false,
1455+
* show_in_rest=false, _builtin=true), such as customize_changeset, whose
1456+
* writes are meant to flow through dedicated helpers. Return true to opt
1457+
* a post type back in.
1458+
*
1459+
* @since 7.1.1
1460+
*
1461+
* @param bool $allowed Whether the post type accepts XML-RPC writes.
1462+
* @param WP_Post_Type $post_type The post type object.
1463+
*/
1464+
$allowed = apply_filters( 'xmlrpc_allow_post_type_writes', ! $is_internal_only, $post_type );
1465+
1466+
if ( ! $allowed ) {
1467+
return new IXR_Error( 403, __( 'Sorry, this post type is not supported over XML-RPC.' ) );
1468+
}
1469+
14431470
$update = ! empty( $post_data['ID'] );
14441471

14451472
if ( $update ) {

‎src/wp-includes/customize/class-wp-customize-header-image-setting.php‎

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@
1515
* @since 3.4.0
1616
*
1717
* @see WP_Customize_Setting
18+
*
19+
* @phpstan-type Header_Image_Data array{
20+
* attachment_id?: int,
21+
* url?: string,
22+
* thumbnail_url?: string,
23+
* timestamp?: int,
24+
* width?: int,
25+
* height?: int,
26+
* alt_text?: string,
27+
* attachment_parent?: int,
28+
* }
1829
*/
1930
final class WP_Customize_Header_Image_Setting extends WP_Customize_Setting {
2031

@@ -57,4 +68,163 @@ public function update( $value ) {
5768
$custom_image_header->set_header_image( $value );
5869
}
5970
}
71+
72+
/**
73+
* Sanitizes a header value.
74+
*
75+
* The value is expected to be one of the following:
76+
*
77+
* - An array of header image data, with the keys `attachment_id`, `url`, `thumbnail_url`, `timestamp`, `width`,
78+
* `height`, `alt_text`, and `attachment_parent`, as supplied by {@see get_uploaded_header_images()}. Any other
79+
* key is discarded.
80+
* - An array with a `choice` key, being the legacy format in which any of the other accepted values is nested.
81+
* - The string `remove-header`, `random-default-image`, or `random-uploaded-image`.
82+
* - A string corresponding to one of the keys for the array returned by {@see get_uploaded_header_images()}, or
83+
* one of the keys for the array passed into {@see register_default_headers()}.
84+
*
85+
* @since 7.1.1
86+
*
87+
* @see WP_Customize_Header_Image_Setting::update()
88+
* @see Custom_Image_Header::set_header_image()
89+
*
90+
* @param mixed $value Value to sanitize.
91+
* @return array|string|WP_Error|null Sanitized value, or `null`/`WP_Error` if invalid. The array holds
92+
* the header image data, or that data nested under a `choice` key,
93+
* before the `customize_sanitize_header_image_data` filter, which
94+
* may return anything, is applied to it.
95+
*
96+
* @phpstan-return array<mixed, mixed>|string|WP_Error|null
97+
*/
98+
public function sanitize( $value ) {
99+
/*
100+
* The update() method unwraps the legacy `choice` format before handing the value off to
101+
* Custom_Image_Header::set_header_image(), so the nested value is what must be sanitized.
102+
*/
103+
if ( is_array( $value ) && isset( $value['choice'] ) ) {
104+
$choice = $this->sanitize_choice( $value['choice'] );
105+
if ( is_null( $choice ) || is_wp_error( $choice ) ) {
106+
return $choice;
107+
}
108+
$value = array( 'choice' => $choice );
109+
} else {
110+
$value = $this->sanitize_choice( $value );
111+
if ( is_null( $value ) || is_wp_error( $value ) ) {
112+
return $value;
113+
}
114+
}
115+
116+
return parent::sanitize( $value );
117+
}
118+
119+
/**
120+
* Sanitizes a header image choice.
121+
*
122+
* This is the value which is ultimately passed to {@see Custom_Image_Header::set_header_image()}, whether
123+
* supplied at the top level of the setting value or nested under its legacy `choice` key.
124+
*
125+
* @since 7.1.1
126+
*
127+
* @param mixed $value Value to sanitize.
128+
* @return array|string|WP_Error|null Sanitized value, or `null`/`WP_Error` if invalid.
129+
*
130+
* @phpstan-return Header_Image_Data|string|WP_Error|null
131+
*/
132+
private function sanitize_choice( $value ) {
133+
// Custom_Image_Header::set_header_image() accepts an object in place of an array.
134+
if ( is_object( $value ) ) {
135+
$value = (array) $value;
136+
}
137+
138+
if ( is_string( $value ) ) {
139+
return sanitize_text_field( $value );
140+
}
141+
142+
if ( ! is_array( $value ) ) {
143+
return null;
144+
}
145+
146+
/*
147+
* The sanitized value is assembled member by member rather than filtered down from the
148+
* supplied one, so that nothing but the members below can end up in it.
149+
*/
150+
$sanitized = array();
151+
152+
if ( isset( $value['attachment_id'] ) ) {
153+
if ( ! is_scalar( $value['attachment_id'] ) ) {
154+
return null;
155+
}
156+
$attachment_id = absint( $value['attachment_id'] );
157+
158+
/*
159+
* A supplied attachment must be an existing image, since its ID is written to postmeta and its
160+
* data displayed. Note that an ID of zero must be skipped rather than looked up, as
161+
* get_post_mime_type() falls back to the global post when passed an empty value.
162+
*/
163+
if ( $attachment_id > 0 ) {
164+
$mime_type = get_post_mime_type( $attachment_id );
165+
if ( ! is_string( $mime_type ) || ! str_starts_with( $mime_type, 'image/' ) ) {
166+
return null;
167+
}
168+
}
169+
170+
$sanitized['attachment_id'] = $attachment_id;
171+
}
172+
173+
if ( isset( $value['url'] ) ) {
174+
if ( ! is_string( $value['url'] ) ) {
175+
return null;
176+
}
177+
$sanitized['url'] = sanitize_url( $value['url'] );
178+
if ( '' === $sanitized['url'] ) {
179+
return new WP_Error( 'invalid_url', __( 'Invalid URL.' ) );
180+
}
181+
}
182+
183+
if ( isset( $value['thumbnail_url'] ) ) {
184+
if ( ! is_string( $value['thumbnail_url'] ) ) {
185+
return null;
186+
}
187+
$sanitized['thumbnail_url'] = sanitize_url( $value['thumbnail_url'] );
188+
if ( '' === $sanitized['thumbnail_url'] ) {
189+
return new WP_Error( 'invalid_url', __( 'Invalid URL.' ) );
190+
}
191+
}
192+
193+
if ( isset( $value['timestamp'] ) ) {
194+
if ( ! is_scalar( $value['timestamp'] ) ) {
195+
return null;
196+
}
197+
$sanitized['timestamp'] = absint( $value['timestamp'] );
198+
}
199+
200+
if ( isset( $value['width'] ) ) {
201+
if ( ! is_scalar( $value['width'] ) ) {
202+
return null;
203+
}
204+
$sanitized['width'] = absint( $value['width'] );
205+
}
206+
207+
if ( isset( $value['height'] ) ) {
208+
if ( ! is_scalar( $value['height'] ) ) {
209+
return null;
210+
}
211+
$sanitized['height'] = absint( $value['height'] );
212+
}
213+
214+
if ( isset( $value['alt_text'] ) ) {
215+
if ( ! is_string( $value['alt_text'] ) ) {
216+
return null;
217+
}
218+
$sanitized['alt_text'] = sanitize_text_field( $value['alt_text'] );
219+
}
220+
221+
if ( isset( $value['attachment_parent'] ) ) {
222+
if ( ! is_scalar( $value['attachment_parent'] ) ) {
223+
return null;
224+
}
225+
$sanitized['attachment_parent'] = absint( $value['attachment_parent'] );
226+
}
227+
228+
return $sanitized;
229+
}
60230
}

‎src/wp-includes/formatting.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ function wpautop( $text, $br = true ) {
556556
$text = preg_replace( '|<p>(<li.+?)</p>|', '$1', $text );
557557

558558
// If a <blockquote> is wrapped with a <p>, move it inside the <blockquote>.
559-
$text = preg_replace( '|<p><blockquote([^>]*)>|i', '<blockquote$1><p>', $text );
559+
$text = preg_replace( '!<p><blockquote((?:[^>"\']|"[^"]*"|\'[^\']*\')*)>!i', '<blockquote$1><p>', $text );
560560
$text = str_replace( '</blockquote></p>', '</p></blockquote>', $text );
561561

562562
// If an opening or closing block element tag is preceded by an opening <p> tag, remove it.

0 commit comments

Comments
 (0)