Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions classes/Visualizer/Module/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public function __construct( Visualizer_Plugin $plugin ) {
$this->_addAction( 'init', 'maybe_reschedule_refresh_db' );
$this->_addAction( 'action_scheduler_ensure_recurring_actions', 'ensure_refresh_db_action' );
$this->_addFilter( 'visualizer_schedule_refresh_chart', 'refresh_db_for_chart', 10, 3 );
$this->_addFilter( 'is_protected_meta', 'protect_chart_meta', 10, 3 );

$this->_addAction( 'admin_init', 'adminInit' );
$this->_addAction( 'init', 'setupCustomPostTypes' );
Expand Down Expand Up @@ -311,6 +312,22 @@ public function adminInit() {
}


/**
* Marks the chart meta as protected.
*
* Chart meta selects the data source and holds its query, URL and credentials. A protected key
* cannot be written through the Custom Fields panel, XML-RPC or the REST API, so only the plugin sets it.
*
* @param bool $is_protected Whether the key is protected.
* @param string $meta_key Meta key.
* @param string $meta_type Type of the object the meta belongs to.
*
* @return bool
*/
public function protect_chart_meta( $is_protected, $meta_key, $meta_type ) {
return $is_protected || ( 'post' === $meta_type && 0 === strpos( (string) $meta_key, 'visualizer-' ) );
}

/**
* Refresh the specific chart from the db.
*
Expand Down
4 changes: 4 additions & 0 deletions classes/Visualizer/Source/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ public function fetch( $as_html = false, $results_as_numeric_array = false, $raw
'SAVEPOINT',
'TRANSACTION',
'SET',

'INTO\s+OUTFILE',
'INTO\s+DUMPFILE',
'LOAD_FILE',
);
$disallow_regex = implode(
'|',
Expand Down
109 changes: 109 additions & 0 deletions tests/test-chart-meta-protection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php
/**
* Chart meta protection tests.
*
* @package Visualizer
*/
class Test_Visualizer_Chart_Meta_Protection extends WP_XMLRPC_UnitTestCase {

/**
* Chart meta keys that select a data source or name a resource.
*
* @return array<string, array{string}>
*/
public function chart_meta_keys() {
return array(
'source' => array( Visualizer_Plugin::CF_SOURCE ),
'db query' => array( Visualizer_Plugin::CF_DB_QUERY ),
'chart image' => array( Visualizer_Plugin::CF_CHART_IMAGE ),
'remote db' => array( Visualizer_Plugin::CF_REMOTE_DB_PARAMS ),
'report user' => array( Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER ),
'd3 code' => array( Visualizer_Module_AIBuilder::CF_D3_CODE ),
);
}

/**
* The author of a chart cannot write chart meta through the WordPress meta capabilities.
*
* @dataProvider chart_meta_keys
*
* @param string $meta_key Meta key.
*/
public function test_chart_meta_is_not_writable_through_meta_capabilities( $meta_key ) {
$user_id = self::factory()->user->create( array( 'role' => 'contributor' ) );
$chart_id = self::factory()->post->create(
array(
'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
'post_author' => $user_id,
'post_status' => 'draft',
)
);

$this->assertTrue( user_can( $user_id, 'edit_post', $chart_id ) );
foreach ( array( 'add_post_meta', 'edit_post_meta', 'delete_post_meta' ) as $capability ) {
$this->assertFalse( user_can( $user_id, $capability, $chart_id, $meta_key ), $capability );
}
$this->assertTrue( user_can( $user_id, 'add_post_meta', $chart_id, 'my-note' ) );
}

/**
* A chart created through XML-RPC does not get the data source that its custom fields ask for.
*/
public function test_xmlrpc_cannot_set_the_data_source_of_a_chart() {
$this->make_user_by_role( 'contributor' );

$chart_id = $this->myxmlrpcserver->wp_newPost(
array(
1,
'contributor',
'contributor',
array(
'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
'post_status' => 'draft',
'custom_fields' => array(
array(
'key' => Visualizer_Plugin::CF_SOURCE,
'value' => 'Visualizer_Source_Query',
),
array(
'key' => Visualizer_Plugin::CF_DB_QUERY,
'value' => 'SELECT user_login FROM wp_users',
),
),
),
)
);

$this->assertNotIXRError( $chart_id );
$this->assertSame( '', get_post_meta( (int) $chart_id, Visualizer_Plugin::CF_SOURCE, true ) );
$this->assertSame( '', get_post_meta( (int) $chart_id, Visualizer_Plugin::CF_DB_QUERY, true ) );
}

/**
* Queries that read or write a file on the database server.
*
* @return array<string, array{string}>
*/
public function file_queries() {
return array(
'LOAD_FILE' => array( "SELECT LOAD_FILE('/etc/passwd') AS a" ),
'INTO OUTFILE' => array( "SELECT 'x' AS a INTO OUTFILE '/tmp/visualizer-test.txt'" ),
'INTO DUMPFILE' => array( "SELECT 'x' AS a INTO DUMPFILE '/tmp/visualizer-test.txt'" ),
);
}

/**
* The database source refuses a query that reads or writes a file.
*
* @dataProvider file_queries
*
* @param string $query SQL query.
*/
public function test_database_source_refuses_file_access( $query ) {
$source = new Visualizer_Source_Query( $query );

$this->assertFalse( $source->fetch( false ) );
$this->assertSame( 'Only SELECT queries are allowed', $source->get_error() );
}

}
Loading