Skip to content

Commit b987487

Browse files
committed
Comments: ensure unauthenticated users cannot access the single comment endpoint for notes.
Fix an issue where notes could be accessed by unauthenticated users by using the single comment REST API endpoint and passing the comment ID (`/wp/v2/comments/<ID>`). This fix only affects the `note` type. Reviewed by peterwilsoncc. Merges [61276] to the 6.9 branch. Props adamsilverstein, peterwilsoncc, westonruter, tharsheblows. See #44157. Built from https://develop.svn.wordpress.org/branches/6.9@61297 git-svn-id: http://core.svn.wordpress.org/branches/6.9@60609 1a063a9b-81f0-0310-95a4-ce76da25c4cd
1 parent 91e4f33 commit b987487

2 files changed

Lines changed: 36 additions & 15 deletions

File tree

wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -123,21 +123,15 @@ public function register_routes() {
123123
* @return true|WP_Error True if the request has read access, error object otherwise.
124124
*/
125125
public function get_items_permissions_check( $request ) {
126-
$is_note = 'note' === $request['type'];
127-
$is_edit_context = 'edit' === $request['context'];
126+
$is_note = 'note' === $request['type'];
127+
$is_edit_context = 'edit' === $request['context'];
128+
$protected_params = array( 'author', 'author_exclude', 'author_email', 'type', 'status' );
129+
$forbidden_params = array();
128130

129131
if ( ! empty( $request['post'] ) ) {
130132
foreach ( (array) $request['post'] as $post_id ) {
131133
$post = get_post( $post_id );
132134

133-
if ( $post && $is_note && ! $this->check_post_type_supports_notes( $post->post_type ) ) {
134-
return new WP_Error(
135-
'rest_comment_not_supported_post_type',
136-
__( 'Sorry, this post type does not support notes.' ),
137-
array( 'status' => 403 )
138-
);
139-
}
140-
141135
if ( ! empty( $post_id ) && $post && ! $this->check_read_post_permission( $post, $request ) ) {
142136
return new WP_Error(
143137
'rest_cannot_read_post',
@@ -151,6 +145,36 @@ public function get_items_permissions_check( $request ) {
151145
array( 'status' => rest_authorization_required_code() )
152146
);
153147
}
148+
149+
if ( $post && $is_note && ! $this->check_post_type_supports_notes( $post->post_type ) ) {
150+
if ( current_user_can( 'edit_post', $post->ID ) ) {
151+
return new WP_Error(
152+
'rest_comment_not_supported_post_type',
153+
__( 'Sorry, this post type does not support notes.' ),
154+
array( 'status' => 403 )
155+
);
156+
}
157+
158+
foreach ( $protected_params as $param ) {
159+
if ( 'status' === $param ) {
160+
if ( 'approve' !== $request[ $param ] ) {
161+
$forbidden_params[] = $param;
162+
}
163+
} elseif ( 'type' === $param ) {
164+
if ( 'comment' !== $request[ $param ] ) {
165+
$forbidden_params[] = $param;
166+
}
167+
} elseif ( ! empty( $request[ $param ] ) ) {
168+
$forbidden_params[] = $param;
169+
}
170+
}
171+
return new WP_Error(
172+
'rest_forbidden_param',
173+
/* translators: %s: List of forbidden parameters. */
174+
sprintf( __( 'Query parameter not permitted: %s' ), implode( ', ', $forbidden_params ) ),
175+
array( 'status' => rest_authorization_required_code() )
176+
);
177+
}
154178
}
155179
}
156180

@@ -174,9 +198,6 @@ public function get_items_permissions_check( $request ) {
174198
}
175199

176200
if ( ! current_user_can( 'edit_posts' ) ) {
177-
$protected_params = array( 'author', 'author_exclude', 'author_email', 'type', 'status' );
178-
$forbidden_params = array();
179-
180201
foreach ( $protected_params as $param ) {
181202
if ( 'status' === $param ) {
182203
if ( 'approve' !== $request[ $param ] ) {
@@ -1890,7 +1911,7 @@ protected function check_read_post_permission( $post, $request ) {
18901911
* @return bool Whether the comment can be read.
18911912
*/
18921913
protected function check_read_permission( $comment, $request ) {
1893-
if ( ! empty( $comment->comment_post_ID ) ) {
1914+
if ( 'note' !== $comment->comment_type && ! empty( $comment->comment_post_ID ) ) {
18941915
$post = get_post( $comment->comment_post_ID );
18951916
if ( $post ) {
18961917
if ( $this->check_read_post_permission( $post, $request ) && 1 === (int) $comment->comment_approved ) {

wp-includes/version.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
* @global string $wp_version
1818
*/
19-
$wp_version = '6.9-RC2-61296';
19+
$wp_version = '6.9-RC2-61297';
2020

2121
/**
2222
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.

0 commit comments

Comments
 (0)