Skip to content

Commit e860e00

Browse files
committed
Set permission_callback for all Rest API endpoints
1 parent cb76098 commit e860e00

7 files changed

Lines changed: 39 additions & 20 deletions

File tree

php/class-admin.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,10 @@ public function __construct( Plugin $plugin ) {
108108
public function rest_endpoints( $endpoints ) {
109109

110110
$endpoints['dismiss_notice'] = array(
111-
'method' => WP_REST_Server::CREATABLE,
112-
'callback' => array( $this, 'rest_dismiss_notice' ),
113-
'args' => array(),
111+
'method' => WP_REST_Server::CREATABLE,
112+
'callback' => array( $this, 'rest_dismiss_notice' ),
113+
'args' => array(),
114+
'permission_callback' => array( 'Cloudinary\REST_API', 'validate_request' ),
114115
);
115116

116117
$endpoints['save_settings'] = array(

php/class-cache.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,9 +352,11 @@ public function rest_endpoints( $endpoints ) {
352352
'args' => array(),
353353
);
354354
$endpoints['upload_cache'] = array(
355-
'method' => \WP_REST_Server::CREATABLE,
356-
'callback' => array( $this, 'rest_upload_cache' ),
357-
'args' => array(),
355+
'method' => \WP_REST_Server::CREATABLE,
356+
'callback' => array( $this, 'rest_upload_cache' ),
357+
'permission_callback' => array( 'Cloudinary\REST_API', 'validate_request' ),
358+
'args' => array(),
359+
358360
);
359361

360362
return $endpoints;

php/class-connect.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,13 @@ public function rest_endpoints( $endpoints ) {
129129
'method' => WP_REST_Server::CREATABLE,
130130
'callback' => array( $this, 'rest_save_wizard' ),
131131
'args' => array(),
132-
'permission_callback' => array( 'Cloudinary\REST_API', 'rest_can_connect' ),
132+
'permission_callback' => array( 'Cloudinary\REST_API', 'validate_request' ),
133133
);
134134
$endpoints['test_rest_api'] = array(
135-
'method' => WP_REST_Server::READABLE,
136-
'callback' => array( $this, 'rest_test_rest_api_connectivity' ),
137-
'args' => array(),
135+
'method' => WP_REST_Server::READABLE,
136+
'callback' => array( $this, 'rest_test_rest_api_connectivity' ),
137+
'args' => array(),
138+
'permission_callback' => array( 'Cloudinary\REST_API', 'allow_public_health_check' ),
138139
);
139140

140141
return $endpoints;

php/class-cron.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,12 @@ public function rest_endpoints( $endpoints ) {
187187
$endpoints['cron_watch'] = array(
188188
'method' => \WP_REST_Server::READABLE,
189189
'callback' => array( $this, 'daemon_watcher' ),
190-
'permission_callback' => '__return_true',
190+
'permission_callback' => array( 'Cloudinary\REST_API', 'rest_can_connect' ),
191191
);
192192
$endpoints['cron_process'] = array(
193193
'method' => \WP_REST_Server::READABLE,
194194
'callback' => array( $this, 'run_queue' ),
195-
'permission_callback' => '__return_true',
195+
'permission_callback' => array( 'Cloudinary\REST_API', 'rest_can_connect' ),
196196
);
197197

198198
return $endpoints;

php/class-rest-api.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,14 @@ public function rest_api_init() {
5151
'method' => \WP_REST_Server::READABLE,
5252
'callback' => __return_empty_array(),
5353
'args' => array(),
54-
'permission_callback' => '__return_true',
54+
'permission_callback' => array( __CLASS__, 'validate_request' ),
5555
);
5656

5757
$this->endpoints = apply_filters( 'cloudinary_api_rest_endpoints', array() );
5858

5959
foreach ( $this->endpoints as $route => $endpoint ) {
6060
$endpoint = wp_parse_args( $endpoint, $defaults );
61+
6162
register_rest_route(
6263
static::BASE,
6364
$route,
@@ -138,4 +139,16 @@ public function background_request( $endpoint, $params = array(), $method = 'POS
138139
public static function validate_request( $request ) {
139140
return wp_verify_nonce( $request->get_header( 'x_wp_nonce' ), self::NONCE_KEY );
140141
}
142+
143+
/**
144+
* Permission callback for public health check endpoints.
145+
*
146+
* Intentionally allows unauthenticated access for REST API connectivity testing.
147+
* This endpoint is read-only and returns no sensitive data.
148+
*
149+
* @return bool Always returns true to allow public access.
150+
*/
151+
public static function allow_public_health_check() {
152+
return true;
153+
}
141154
}

php/sync/class-push-sync.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,16 @@ public function rest_endpoints( $endpoints ) {
124124
);
125125

126126
$endpoints['queue'] = array(
127-
'method' => \WP_REST_Server::CREATABLE,
128-
'callback' => array( $this, 'process_queue' ),
129-
'args' => array(),
127+
'method' => \WP_REST_Server::CREATABLE,
128+
'callback' => array( $this, 'process_queue' ),
129+
'args' => array(),
130+
'permission_callback' => array( 'Cloudinary\REST_API', 'validate_request' ),
130131
);
131132
$endpoints['stats'] = array(
132-
'method' => \WP_REST_Server::READABLE,
133-
'callback' => array( $this->queue, 'get_total_synced_media' ),
134-
'args' => array(),
133+
'method' => \WP_REST_Server::READABLE,
134+
'callback' => array( $this->queue, 'get_total_synced_media' ),
135+
'args' => array(),
136+
'permission_callback' => array( 'Cloudinary\REST_API', 'validate_request' ),
135137
);
136138

137139
return $endpoints;

php/ui/component/class-notice.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public function render( $echo = false ) { // phpcs:ignore Universal.NamingConven
117117
if ( $this->setting->get_option_parent()->has_param( 'dismissible_notice' ) && ! $this->setting->get_option_parent()->has_param( 'notice_scripts' ) ) {
118118
$args = array(
119119
'url' => Utils::rest_url( REST_API::BASE . '/dismiss_notice' ),
120-
'nonce' => wp_create_nonce( 'wp_rest' ),
120+
'nonce' => wp_create_nonce( REST_API::NONCE_KEY ),
121121
);
122122
wp_add_inline_script( 'cloudinary', 'var CLDIS = ' . wp_json_encode( $args ), 'before' );
123123
$this->setting->get_option_parent()->set_param( 'notice_scripts', true ); // Prevent repeated rendering.

0 commit comments

Comments
 (0)