Skip to content

Commit 89d1d76

Browse files
committed
chore: use word boundries for regex
1 parent adb68d4 commit 89d1d76

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

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*(insert|delete|update|replace|create|alter|drop|truncate)\s/i', $this->_query ) ) {
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 ) ) {
8484
$this->_error = __( 'Only SELECT queries are allowed', 'visualizer' );
8585
return false;
8686
}

tests/test-ajax.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,32 @@ public function test_ajax_response_get_query_data_invalid_query() {
8787
$this->assertFalse( $response->success );
8888
}
8989

90+
/**
91+
* Test the AJAX response for fetching the database data with a valid query that uses columns that might get filtered.
92+
*/
93+
public function test_ajax_response_get_query_data_valid_query_with_filtered_columns() {
94+
$this->_setRole( 'administrator' );
95+
96+
$_GET['security'] = wp_create_nonce( Visualizer_Plugin::ACTION_FETCH_DB_DATA . Visualizer_Plugin::VERSION );
97+
98+
$_POST['params'] = array(
99+
'query' => 'select date_create from wp_insert;',
100+
'chart_id' => 1,
101+
);
102+
try {
103+
// Trigger the AJAX action
104+
$this->_handleAjax( Visualizer_Plugin::ACTION_FETCH_DB_DATA );
105+
} catch ( WPAjaxDieContinueException $e ) {
106+
// We expected this, do nothing.
107+
}
108+
109+
$response = json_decode( $this->_last_response );
110+
$this->assertIsObject( $response );
111+
$this->assertObjectHasAttribute( 'success', $response );
112+
$this->assertObjectHasAttribute( 'data', $response );
113+
$this->assertTrue( $response->success );
114+
}
115+
90116
/**
91117
* Test the AJAX response for fetching the database data with user capability.
92118
*/

0 commit comments

Comments
 (0)