diff --git a/includes/classes/wp-maintenance-mode-admin.php b/includes/classes/wp-maintenance-mode-admin.php index aa9ccc91..817add06 100644 --- a/includes/classes/wp-maintenance-mode-admin.php +++ b/includes/classes/wp-maintenance-mode-admin.php @@ -928,7 +928,14 @@ public function change_template_category() { * @return void */ public function toggle_gutenberg() { - if ( empty( $_POST['source'] ) ) { + // check capabilities + if ( ! current_user_can( wpmm_get_capability( 'settings' ) ) ) { + wp_send_json_error( __( 'You do not have access to this resource.', 'wp-maintenance-mode' ) ); + } + + // only the migration notices may toggle; other notice nonces reach every role. + $source = isset( $_POST['source'] ) && in_array( $_POST['source'], array( 'migration', 'rollback' ), true ) ? $_POST['source'] : ''; + if ( empty( $source ) ) { die( esc_html__( 'The source filed must not be empty.', 'wp-maintenance-mode' ) ); } @@ -938,7 +945,7 @@ public function toggle_gutenberg() { } // check nonce validation - if ( ! wp_verify_nonce( $_POST['_wpnonce'], 'notice_nonce_' . $_POST['source'] ) ) { + if ( ! wp_verify_nonce( $_POST['_wpnonce'], 'notice_nonce_' . $source ) ) { die( esc_html__( 'Security check.', 'wp-maintenance-mode' ) ); } diff --git a/tests/ajax-api-test.php b/tests/ajax-api-test.php index 22e56a4f..2c5148fd 100644 --- a/tests/ajax-api-test.php +++ b/tests/ajax-api-test.php @@ -358,8 +358,8 @@ public function test_toggle_gutenberg_flips_the_new_look_option() { delete_option( 'wpmm_new_look' ); delete_option( 'wpmm_migration_time' ); - $_POST['source'] = 'dashboard'; - $_POST['_wpnonce'] = wp_create_nonce( 'notice_nonce_dashboard' ); + $_POST['source'] = 'migration'; + $_POST['_wpnonce'] = wp_create_nonce( 'notice_nonce_migration' ); $response = $this->handle( 'wpmm_toggle_gutenberg' ); @@ -368,6 +368,21 @@ public function test_toggle_gutenberg_flips_the_new_look_option() { $this->assertNotEmpty( get_option( 'wpmm_migration_time' ) ); } + public function test_toggle_gutenberg_requires_capability_even_with_a_notice_nonce() { + $this->_setRole( 'subscriber' ); + update_option( 'wpmm_new_look', '1' ); + + // Every role sees the rebrand notice, so a subscriber holds its nonce. + $_POST['source'] = 'rebrand'; + $_POST['_wpnonce'] = wp_create_nonce( 'notice_nonce_rebrand' ); + + $response = $this->handle( 'wpmm_toggle_gutenberg' ); + + $this->assertFalse( $response['success'] ); + $this->assertSame( 'You do not have access to this resource.', $response['data'] ); + $this->assertSame( '1', get_option( 'wpmm_new_look' ) ); + } + /* * wpmm_subscribe (plugin newsletter, external HTTP) */