Skip to content

Commit fb86a6d

Browse files
committed
Security: Backport the WordPress 7.1.1 security fixes to the 5.0 branch.
- Posts, Post Types: Reject a supplied post ID on the create path in `_wp_translate_postdata()`. - 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. - 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, r63665, r63669, r63672 to the 5.0 branch. Props xknown, westonruter, jorbin, vortfu, batmoo, davidbinda, jeremyfelt, johnbillion, peterwilsoncc, lancewillett, jonsurrell, dmsnell, villanovachile, whyisjake, buffer1024, joehoyle, rafiem. git-svn-id: https://develop.svn.wordpress.org/branches/5.0@63712 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 1e3437c commit fb86a6d

9 files changed

Lines changed: 271 additions & 12 deletions

File tree

‎src/wp-admin/custom-header.php‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ public function step_1() {
497497
if ( $custom_header->height )
498498
$header_image_style .= 'height:' . $custom_header->height . 'px;';
499499
?>
500-
<div id="headimg" style="<?php echo $header_image_style; ?>">
500+
<div id="headimg" style="<?php echo esc_attr( $header_image_style ); ?>">
501501
<?php
502502
if ( display_header_text() )
503503
$style = ' style="color:#' . get_header_textcolor() . ';"';
@@ -1020,8 +1020,8 @@ final public function set_header_image( $choice ) {
10201020
'attachment_id' => $choice['attachment_id'],
10211021
'url' => $choice['url'],
10221022
'thumbnail_url' => $choice['url'],
1023-
'height' => $choice['height'],
1024-
'width' => $choice['width'],
1023+
'height' => absint( $choice['height'] ),
1024+
'width' => absint( $choice['width'] ),
10251025
);
10261026

10271027
update_post_meta( $choice['attachment_id'], '_wp_attachment_is_custom_header', get_stylesheet() );
@@ -1048,7 +1048,13 @@ final public function set_header_image( $choice ) {
10481048
return;
10491049
}
10501050

1051-
set_theme_mod( 'header_image', esc_url_raw( $header_image_data['url'] ) );
1051+
$header_image_data['url'] = esc_url_raw( $header_image_data['url'] );
1052+
1053+
if ( isset( $header_image_data['thumbnail_url'] ) ) {
1054+
$header_image_data['thumbnail_url'] = esc_url_raw( $header_image_data['thumbnail_url'] );
1055+
}
1056+
1057+
set_theme_mod( 'header_image', $header_image_data['url'] );
10521058
set_theme_mod( 'header_image_data', $header_image_data );
10531059
}
10541060

@@ -1407,9 +1413,8 @@ public function get_uploaded_header_images() {
14071413
$alt_text_key = '_wp_attachment_image_alt';
14081414

14091415
foreach ( $header_images as &$header_image ) {
1410-
$header_meta = get_post_meta( $header_image['attachment_id'] );
1411-
$header_image['timestamp'] = isset( $header_meta[ $timestamp_key ] ) ? $header_meta[ $timestamp_key ] : '';
1412-
$header_image['alt_text'] = isset( $header_meta[ $alt_text_key ] ) ? $header_meta[ $alt_text_key ] : '';
1416+
$header_image['timestamp'] = get_post_meta( $header_image['attachment_id'], $timestamp_key, true );
1417+
$header_image['alt_text'] = get_post_meta( $header_image['attachment_id'], $alt_text_key, true );
14131418
}
14141419

14151420
return $header_images;

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,6 +1636,13 @@ function wp_ajax_menu_quick_search() {
16361636
function wp_ajax_get_permalink() {
16371637
check_ajax_referer( 'getpermalink', 'getpermalinknonce' );
16381638
$post_id = isset($_POST['post_id'])? intval($_POST['post_id']) : 0;
1639+
if ( ! $post_id ) {
1640+
// Bypass call to get_preview_post_link() for unspecified post ID.
1641+
wp_die( '' );
1642+
}
1643+
if ( ! current_user_can( 'edit_post', $post_id ) ) {
1644+
wp_die( -1 );
1645+
}
16391646
wp_die( get_preview_post_link( $post_id ) );
16401647
}
16411648

@@ -1647,6 +1654,13 @@ function wp_ajax_get_permalink() {
16471654
function wp_ajax_sample_permalink() {
16481655
check_ajax_referer( 'samplepermalink', 'samplepermalinknonce' );
16491656
$post_id = isset($_POST['post_id'])? intval($_POST['post_id']) : 0;
1657+
if ( ! $post_id ) {
1658+
// Bypass call to get_sample_permalink_html() for unspecified post ID.
1659+
wp_die( '' );
1660+
}
1661+
if ( ! current_user_can( 'edit_post', $post_id ) ) {
1662+
wp_die( -1 );
1663+
}
16501664
$title = isset($_POST['new_title'])? $_POST['new_title'] : '';
16511665
$slug = isset($_POST['new_slug'])? $_POST['new_slug'] : null;
16521666
wp_die( get_sample_permalink_html( $post_id, $title, $slug ) );

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,11 +505,17 @@ function is_plugin_active_for_network( $plugin ) {
505505
* Checks for "Site Wide Only: true" for backward compatibility.
506506
*
507507
* @since 3.0.0
508+
* @since 7.1.1 The `$plugin` path is normalized with `plugin_basename()` and `trim()`,
509+
* matching how `activate_plugin()` resolves it.
508510
*
509-
* @param string $plugin Path to the main plugin file from plugins directory.
511+
* @param string $plugin Path to the plugin file. Accepts a path relative to the plugins
512+
* directory, or an absolute path, with or without surrounding whitespace.
510513
* @return bool True if plugin is network only, false otherwise.
511514
*/
512515
function is_network_only_plugin( $plugin ) {
516+
// Normalize the path the same way activate_plugin() does, so both agree on the file.
517+
$plugin = plugin_basename( trim( $plugin ) );
518+
513519
$plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
514520
if ( $plugin_data )
515521
return $plugin_data['Network'];

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,20 @@ function _wp_translate_postdata( $update = false, $post_data = null ) {
2222
if ( empty($post_data) )
2323
$post_data = &$_POST;
2424

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

‎src/wp-admin/js/theme.js‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1910,6 +1910,17 @@ themes.InstallerRouter = Backbone.Router.extend({
19101910
});
19111911

19121912

1913+
/*
1914+
* jQuery.escapeSelector() (added in jQuery 3.0) is not available on this
1915+
* branch's vendored jQuery. Fall back to the native CSS.escape(), which is
1916+
* what jQuery.escapeSelector() delegates to and which jQuery UI polyfills
1917+
* it with. A browser with neither skips the preview route below rather
1918+
* than building a selector from an unescaped slug.
1919+
*/
1920+
var escapeSelector = $.escapeSelector || ( window.CSS && window.CSS.escape && function( sel ) {
1921+
return window.CSS.escape( sel + '' );
1922+
} );
1923+
19131924
themes.RunInstaller = {
19141925

19151926
init: function() {
@@ -1972,9 +1983,13 @@ themes.RunInstaller = {
19721983
self.view.collection.query( request );
19731984
self.view.collection.trigger( 'update' );
19741985

1975-
// Open the theme preview.
1986+
// Open the theme preview. The slug comes from the URL, so escape it.
19761987
self.view.collection.once( 'query:success', function() {
1977-
$( 'div[data-slug="' + slug + '"]' ).trigger( 'click' );
1988+
if ( ! escapeSelector ) {
1989+
return;
1990+
}
1991+
1992+
$( 'div.theme[data-slug="' + escapeSelector( slug ) + '"]' ).trigger( 'click' );
19781993
});
19791994

19801995
}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,6 +1376,33 @@ protected function _insert_post( $user, $content_struct ) {
13761376
if ( ! $post_type )
13771377
return new IXR_Error( 403, __( 'Invalid post type.' ) );
13781378

1379+
// Reject writes to internal-only builtin post types (e.g. customize_changeset)
1380+
// whose intended write path is a dedicated helper, not a generic post API.
1381+
$is_internal_only = (
1382+
empty( $post_type->public )
1383+
&& empty( $post_type->show_in_rest )
1384+
&& ! empty( $post_type->_builtin )
1385+
);
1386+
1387+
/**
1388+
* Filters whether a post type accepts writes via XML-RPC.
1389+
*
1390+
* Defaults to false for internal-only builtin post types (public=false,
1391+
* show_in_rest=false, _builtin=true), such as customize_changeset, whose
1392+
* writes are meant to flow through dedicated helpers. Return true to opt
1393+
* a post type back in.
1394+
*
1395+
* @since 7.1.1
1396+
*
1397+
* @param bool $allowed Whether the post type accepts XML-RPC writes.
1398+
* @param WP_Post_Type $post_type The post type object.
1399+
*/
1400+
$allowed = apply_filters( 'xmlrpc_allow_post_type_writes', ! $is_internal_only, $post_type );
1401+
1402+
if ( ! $allowed ) {
1403+
return new IXR_Error( 403, __( 'Sorry, this post type is not supported over XML-RPC.' ) );
1404+
}
1405+
13791406
$update = ! empty( $post_data['ID'] );
13801407

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

‎src/wp-includes/formatting.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ function wpautop( $pee, $br = true ) {
549549
$pee = preg_replace("|<p>(<li.+?)</p>|", "$1", $pee);
550550

551551
// If a <blockquote> is wrapped with a <p>, move it inside the <blockquote>.
552-
$pee = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $pee);
552+
$pee = preg_replace('!<p><blockquote((?:[^>"\']|"[^"]*"|\'[^\']*\')*)>!i', "<blockquote$1><p>", $pee);
553553
$pee = str_replace('</blockquote></p>', '</p></blockquote>', $pee);
554554

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

‎src/wp-includes/theme.php‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1230,6 +1230,7 @@ function get_uploaded_header_images() {
12301230
* Get the header image data.
12311231
*
12321232
* @since 3.4.0
1233+
* @since 7.1.1 The `width` and `height` are cast to non-negative integers.
12331234
*
12341235
* @global array $_wp_default_headers
12351236
*
@@ -1267,7 +1268,14 @@ function get_custom_header() {
12671268
'height' => get_theme_support( 'custom-header', 'height' ),
12681269
'video' => get_theme_support( 'custom-header', 'video' ),
12691270
);
1270-
return (object) wp_parse_args( $data, $default );
1271+
1272+
if ( ! is_array( $data ) && ! is_object( $data ) ) {
1273+
$data = array();
1274+
}
1275+
$header = (object) wp_parse_args( $data, $default );
1276+
$header->width = absint( $header->width );
1277+
$header->height = absint( $header->height );
1278+
return $header;
12711279
}
12721280

12731281
/**

0 commit comments

Comments
 (0)