← All changes
|
json-endpoints/class.wpcom-json-api-update-media-endpoint.php
+51
-2
13.5.2
→
16.3-a.1
View file →
| @@ -4,8 +4,12 @@ | ||
| 4 | 4 | * |
| 5 | 5 | * Endpoint: /sites/%s/media/%d |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 9 | + exit( 0 ); | |
| 10 | +} | |
| 11 | + | |
| 8 | 12 | new WPCOM_JSON_API_Update_Media_Endpoint( |
| 9 | 13 | array( |
| 10 | 14 | 'description' => 'Edit basic information about a media item.', |
| 11 | 15 | 'group' => 'media', |
| @@ -49,11 +53,56 @@ | ||
| 49 | 53 | ); |
| 50 | 54 | |
| 51 | 55 | /** |
| 52 | 56 | * Update media item info class. |
| 57 | + * | |
| 58 | + * @phan-constructor-used-for-side-effects | |
| 53 | 59 | */ |
| 54 | 60 | class WPCOM_JSON_API_Update_Media_Endpoint extends WPCOM_JSON_API_Endpoint { |
| 55 | 61 | /** |
| 62 | + * Whether the current user may edit the given media item. | |
| 63 | + * | |
| 64 | + * `upload_files` is a primitive capability and ignores any object passed to it, | |
| 65 | + * so it only tells us the caller may upload something, never that they may edit | |
| 66 | + * this particular item. A missing item is passed through so the caller receives | |
| 67 | + * the endpoint's own 404 rather than a 403. A userless request gets no exemption: | |
| 68 | + * `edit_post` fails closed for user 0 like any other caller. | |
| 69 | + * | |
| 70 | + * Non-attachments are refused outright. `get_post()` resolves any post type, so | |
| 71 | + * without this test a media endpoint edits ordinary posts, pages and revisions. | |
| 72 | + * The post-type test must stay below the missing-post passthrough: that branch | |
| 73 | + * returns true, so testing there would skip `edit_post` for ordinary posts. | |
| 74 | + * | |
| 75 | + * A non-attachment yields 403, not the 404 a missing item gets. This is a boolean | |
| 76 | + * gate, and `get_media_item*()` resolves any post type, so a passthrough would | |
| 77 | + * return 200 rather than 404. Revisit if clients conflate it with an auth failure. | |
| 78 | + * | |
| 79 | + * Do not move this into a trait: this file instantiates the endpoint above the | |
| 80 | + * class declaration, and `use Trait;` disables PHP early binding, which makes the | |
| 81 | + * file fatal with "Class not found". | |
| 82 | + * | |
| 83 | + * @param int $media_id Media post ID. | |
| 84 | + * @return bool | |
| 85 | + */ | |
| 86 | + protected function current_user_can_edit_media_item( $media_id ) { | |
| 87 | + if ( ! current_user_can( 'upload_files' ) ) { | |
| 88 | + return false; | |
| 89 | + } | |
| 90 | + | |
| 91 | + $post = get_post( $media_id ); | |
| 92 | + | |
| 93 | + if ( ! $post ) { | |
| 94 | + return true; | |
| 95 | + } | |
| 96 | + | |
| 97 | + if ( 'attachment' !== $post->post_type ) { | |
| 98 | + return false; | |
| 99 | + } | |
| 100 | + | |
| 101 | + return current_user_can( 'edit_post', $media_id ); | |
| 102 | + } | |
| 103 | + | |
| 104 | + /** | |
| 56 | 105 | * Update media item info API callback. |
| 57 | 106 | * |
| 58 | 107 | * @param string $path API path. |
| 59 | 108 | * @param int $blog_id Blog ID. |
| @@ -66,10 +115,10 @@ | ||
| 66 | 115 | if ( is_wp_error( $blog_id ) ) { |
| 67 | 116 | return $blog_id; |
| 68 | 117 | } |
| 69 | 118 | |
| 70 | - if ( ! current_user_can( 'upload_files', $media_id ) ) { | |
| 71 | - return new WP_Error( 'unauthorized', 'User cannot view media', 403 ); | |
| 119 | + if ( ! $this->current_user_can_edit_media_item( $media_id ) ) { | |
| 120 | + return new WP_Error( 'unauthorized', 'User cannot edit media', 403 ); | |
| 72 | 121 | } |
| 73 | 122 | |
| 74 | 123 | $item = $this->get_media_item( $media_id ); |
| 75 | 124 | |