Skip to content

Commit dafb6d9

Browse files
committed
fix: only allow admin and select
References: Codeinwp/visualizer-pro#433
1 parent 112c71c commit dafb6d9

File tree

4 files changed

+19
-4
lines changed

4 files changed

+19
-4
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ cypress.env.json
1111
.DS_Store
1212
/cypress/videos/
1313
/cypress/screenshots/
14-
artifacts
14+
artifacts
15+
.phpunit.result.cache

classes/Visualizer/Module/Chart.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1421,7 +1421,7 @@ private function _handleDataPage() {
14211421
public function getQueryData() {
14221422
check_ajax_referer( Visualizer_Plugin::ACTION_FETCH_DB_DATA . Visualizer_Plugin::VERSION, 'security' );
14231423

1424-
if ( ! current_user_can( 'edit_posts' ) ) {
1424+
if ( ! current_user_can( 'manage_options' ) ) {
14251425
wp_send_json_error( array( 'msg' => __( 'Action not allowed for this user.', 'visualizer' ) ) );
14261426
}
14271427

classes/Visualizer/Source/Query.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function fetch( $as_html = false, $results_as_numeric_array = false, $raw
8080
}
8181

8282
// only select queries allowed.
83-
if ( preg_match( '/\s*(\binsert\b|\bdelete\b|\bupdate\b|\breplace\b|\bcreate\b|\balter\b|\bdrop\b|\btruncate\b)\s/i', $this->_query ) ) {
83+
if ( ! preg_match( '/\s*(\bselect\b)\s/i', $this->_query ) ) {
8484
$this->_error = __( 'Only SELECT queries are allowed', 'visualizer' );
8585
return false;
8686
}

tests/test-ajax.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,17 @@ class Test_Visualizer_Ajax extends WP_Ajax_UnitTestCase {
2020
*/
2121
private $admin_user_id;
2222

23+
/**
24+
* Subscriber user ID.
25+
*
26+
* @var int
27+
*/
28+
private $subscriber_user_id;
29+
2330
/**
2431
* Set up.
2532
*/
26-
public function setUp() {
33+
public function setUp(): void {
2734
parent::setUp();
2835
$this->admin_user_id = $this->factory->user->create(
2936
array(
@@ -32,6 +39,12 @@ public function setUp() {
3239
);
3340
wp_set_current_user( $this->admin_user_id );
3441

42+
$this->subscriber_user_id = $this->factory->user->create(
43+
array(
44+
'role' => 'subscriber',
45+
)
46+
);
47+
3548
}
3649

3750
/**
@@ -117,6 +130,7 @@ public function test_ajax_response_get_query_data_valid_query_with_filtered_colu
117130
* Test the AJAX response for fetching the database data with user capability.
118131
*/
119132
public function test_ajax_response_get_query_data_subcriber_dissallow() {
133+
wp_set_current_user( $this->subscriber_user_id );
120134
$this->_setRole( 'subscriber' );
121135

122136
$_GET['security'] = wp_create_nonce( Visualizer_Plugin::ACTION_FETCH_DB_DATA . Visualizer_Plugin::VERSION );

0 commit comments

Comments
 (0)