diff --git a/classes/Visualizer/Module/Setup.php b/classes/Visualizer/Module/Setup.php index 3f530b35..cea683fd 100644 --- a/classes/Visualizer/Module/Setup.php +++ b/classes/Visualizer/Module/Setup.php @@ -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' ); @@ -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. * diff --git a/classes/Visualizer/Source/Query.php b/classes/Visualizer/Source/Query.php index b72a142a..c1296c71 100644 --- a/classes/Visualizer/Source/Query.php +++ b/classes/Visualizer/Source/Query.php @@ -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( '|', diff --git a/tests/test-chart-meta-protection.php b/tests/test-chart-meta-protection.php new file mode 100644 index 00000000..789481e5 --- /dev/null +++ b/tests/test-chart-meta-protection.php @@ -0,0 +1,109 @@ + + */ + 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 + */ + 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() ); + } + +}