Skip to content

Commit 4d11191

Browse files
Improve nonce handling and permissions in crop settings API (#2624)
* Conditionally include crop settings nonce * Improve nonce validation and permission checks in crop settings API * Sanitize nonce input in crop settings API for improved security
1 parent 3ca90e1 commit 4d11191

File tree

2 files changed

+38
-38
lines changed

2 files changed

+38
-38
lines changed

includes/admin/class-coblocks-crop-settings.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -86,26 +86,22 @@ public function hide_cropped_from_library( $query ) {
8686
* Retrieve the original image.
8787
*/
8888
public function get_original_image() {
89-
$nonce = filter_input( INPUT_POST, 'nonce' );
90-
91-
if ( ! $nonce ) {
92-
93-
wp_send_json_error( 'No nonce value present.' );
94-
89+
if ( ! wp_verify_nonce( sanitize_text_field( filter_input( INPUT_POST, 'nonce' ) ), 'cropSettingsOriginalImageNonce' ) ) {
90+
wp_send_json_error( 'Invalid nonce value.', 403 );
9591
}
9692

97-
if ( ! wp_verify_nonce( htmlspecialchars( $nonce ), 'cropSettingsOriginalImageNonce' ) ) {
98-
99-
wp_send_json_error( 'Invalid nonce value.' );
100-
93+
if ( ! current_user_can( 'upload_files' ) ) {
94+
wp_send_json_error( 'You do not have permission.', 403 );
10195
}
10296

10397
$id = filter_input( INPUT_POST, 'id', FILTER_SANITIZE_NUMBER_INT );
10498

10599
if ( ! $id ) {
106-
107100
wp_send_json_error( 'Missing id value.' );
101+
}
108102

103+
if ( ! current_user_can( 'edit_post', $id ) ) {
104+
wp_send_json_error( 'You do not have permission to edit this attachment.', 403 );
109105
}
110106

111107
$attachment_meta = wp_get_attachment_metadata( $id );
@@ -127,18 +123,22 @@ public function get_original_image() {
127123
* Cropping.
128124
*/
129125
public function api_crop() {
130-
$nonce = filter_input( INPUT_POST, 'nonce' );
131-
132-
if ( ! $nonce ) {
133-
134-
wp_send_json_error( 'No nonce value present.' );
126+
if ( ! wp_verify_nonce( sanitize_text_field( filter_input( INPUT_POST, 'nonce' ) ), 'cropSettingsNonce' ) ) {
127+
wp_send_json_error( 'Invalid nonce value.', 403 );
128+
}
135129

130+
if ( ! current_user_can( 'upload_files' ) ) {
131+
wp_send_json_error( 'You do not have permission.', 403 );
136132
}
137133

138-
if ( ! wp_verify_nonce( htmlspecialchars( $nonce ), 'cropSettingsNonce' ) ) {
134+
$id = filter_input( INPUT_POST, 'id', FILTER_SANITIZE_NUMBER_INT );
139135

140-
wp_send_json_error( 'Invalid nonce value.' );
136+
if ( ! $id ) {
137+
wp_send_json_error( 'Missing id value.' );
138+
}
141139

140+
if ( ! current_user_can( 'edit_post', $id ) ) {
141+
wp_send_json_error( 'You do not have permission to edit this attachment.', 403 );
142142
}
143143

144144
if (

includes/class-coblocks-block-assets.php

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -288,28 +288,28 @@ public function editor_assets() {
288288
$form_subject = $form->default_subject();
289289
$success_text = $form->default_success_text();
290290

291-
wp_localize_script(
292-
'coblocks-editor',
293-
'coblocksBlockData',
294-
array(
295-
'form' => array(
296-
'adminEmail' => $email_to,
297-
'emailSubject' => $form_subject,
298-
'successText' => $success_text,
299-
),
300-
'cropSettingsOriginalImageNonce' => wp_create_nonce( 'cropSettingsOriginalImageNonce' ),
301-
'cropSettingsNonce' => wp_create_nonce( 'cropSettingsNonce' ),
302-
'labsSiteDesignNonce' => wp_create_nonce( 'labsSiteDesignNonce' ),
303-
'bundledIconsEnabled' => $bundled_icons_enabled,
304-
'customIcons' => $this->get_custom_icons(),
305-
'customIconConfigExists' => file_exists( get_stylesheet_directory() . '/coblocks/icons/config.json' ),
306-
'typographyControlsEnabled' => $typography_controls_enabled,
307-
'animationControlsEnabled' => $animation_controls_enabled,
308-
'localeCode' => get_locale(),
309-
'baseApiNamespace' => COBLOCKS_API_NAMESPACE,
310-
)
291+
$localize_data = array(
292+
'form' => array(
293+
'adminEmail' => $email_to,
294+
'emailSubject' => $form_subject,
295+
'successText' => $success_text,
296+
),
297+
'labsSiteDesignNonce' => wp_create_nonce( 'labsSiteDesignNonce' ),
298+
'bundledIconsEnabled' => $bundled_icons_enabled,
299+
'customIcons' => $this->get_custom_icons(),
300+
'customIconConfigExists' => file_exists( get_stylesheet_directory() . '/coblocks/icons/config.json' ),
301+
'typographyControlsEnabled' => $typography_controls_enabled,
302+
'animationControlsEnabled' => $animation_controls_enabled,
303+
'localeCode' => get_locale(),
304+
'baseApiNamespace' => COBLOCKS_API_NAMESPACE,
311305
);
312306

307+
if ( current_user_can( 'upload_files' ) ) {
308+
$localize_data['cropSettingsOriginalImageNonce'] = wp_create_nonce( 'cropSettingsOriginalImageNonce' );
309+
$localize_data['cropSettingsNonce'] = wp_create_nonce( 'cropSettingsNonce' );
310+
}
311+
312+
wp_localize_script( 'coblocks-editor', 'coblocksBlockData', $localize_data );
313313
}
314314

315315
/**

0 commit comments

Comments
 (0)