Skip to content

Commit ebaac57

Browse files
committed
REST API: Lockdown post parameter of the terms endpoint.
Props johnbillion, tykoted, timothyblynjacobs, peterwilsoncc, martinkrcho, ehtis. git-svn-id: https://develop.svn.wordpress.org/trunk@54528 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 89c8f79 commit ebaac57

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,35 @@ public function register_routes() {
144144
);
145145
}
146146

147+
/**
148+
* Checks if the terms for a post can be read.
149+
*
150+
* @since 6.0.3
151+
*
152+
* @param WP_Post $post Post object.
153+
* @param WP_REST_Request $request Full details about the request.
154+
* @return bool Whether the terms for the post can be read.
155+
*/
156+
public function check_read_terms_permission_for_post( $post, $request ) {
157+
// If the requested post isn't associated with this taxonomy, deny access.
158+
if ( ! is_object_in_taxonomy( $post->post_type, $this->taxonomy ) ) {
159+
return false;
160+
}
161+
162+
// Grant access if the post is publicly viewable.
163+
if ( is_post_publicly_viewable( $post ) ) {
164+
return true;
165+
}
166+
167+
// Otherwise grant access if the post is readable by the logged in user.
168+
if ( current_user_can( 'read_post', $post->ID ) ) {
169+
return true;
170+
}
171+
172+
// Otherwise, deny access.
173+
return false;
174+
}
175+
147176
/**
148177
* Checks if a request has access to read terms in the specified taxonomy.
149178
*
@@ -167,6 +196,30 @@ public function get_items_permissions_check( $request ) {
167196
);
168197
}
169198

199+
if ( ! empty( $request['post'] ) ) {
200+
$post = get_post( $request['post'] );
201+
202+
if ( ! $post ) {
203+
return new WP_Error(
204+
'rest_post_invalid_id',
205+
__( 'Invalid post ID.' ),
206+
array(
207+
'status' => 400,
208+
)
209+
);
210+
}
211+
212+
if ( ! $this->check_read_terms_permission_for_post( $post, $request ) ) {
213+
return new WP_Error(
214+
'rest_forbidden_context',
215+
__( 'Sorry, you are not allowed to view terms for this post.' ),
216+
array(
217+
'status' => rest_authorization_required_code(),
218+
)
219+
);
220+
}
221+
}
222+
170223
return true;
171224
}
172225

0 commit comments

Comments
 (0)