Skip to content

Commit 2bcb43e

Browse files
lucadobrescuclaude
andcommitted
fix: check per-post edit_post capability in chart_data REST field
The chart_data REST field callback only checked the generic edit_posts capability, so any contributor could read the full backend configuration of any chart (data source query, settings, JSON endpoint auth headers) via GET /wp-json/wp/v2/visualizer/{id}. Check edit_post on the requested chart instead, so access follows the standard post capability map (author or edit_others_posts). Fixes Codeinwp/visualizer-pro#605 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent cea4ea6 commit 2bcb43e

2 files changed

Lines changed: 61 additions & 1 deletion

File tree

classes/Visualizer/Gutenberg/Block.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ public function register_rest_endpoints() {
276276
* Get Post Meta Fields
277277
*/
278278
public function get_visualizer_data( $post ) {
279-
if ( ! current_user_can( 'edit_posts' ) ) {
279+
if ( ! current_user_can( 'edit_post', $post['id'] ) ) {
280280
return false;
281281
}
282282

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* WordPress unit test plugin.
4+
*
5+
* @package visualizer
6+
* @subpackage Tests
7+
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
8+
*/
9+
10+
/**
11+
* Test the capability check on the chart_data REST field callback.
12+
*/
13+
class Test_Visualizer_Chart_Data_Permissions extends WP_UnitTestCase {
14+
15+
/**
16+
* Create a chart owned by the given user.
17+
*
18+
* @param int $author_id The chart author.
19+
* @return int The chart id.
20+
*/
21+
private function create_chart( $author_id ) {
22+
$chart_id = self::factory()->post->create(
23+
array(
24+
'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
25+
'post_author' => $author_id,
26+
'post_content' => wp_slash( serialize( array( array( 'Label' ), array( 'Value' ) ) ) ),
27+
)
28+
);
29+
update_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, 'line' );
30+
update_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, array() );
31+
update_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, array( array( 'label' => 'Label', 'type' => 'string' ) ) );
32+
return $chart_id;
33+
}
34+
35+
/**
36+
* A contributor must not read another user's chart configuration.
37+
*/
38+
public function test_contributor_cannot_read_others_chart_data() {
39+
$author_id = self::factory()->user->create( array( 'role' => 'editor' ) );
40+
$chart_id = $this->create_chart( $author_id );
41+
42+
wp_set_current_user( self::factory()->user->create( array( 'role' => 'contributor' ) ) );
43+
44+
$result = Visualizer_Gutenberg_Block::get_instance()->get_visualizer_data( array( 'id' => $chart_id ) );
45+
$this->assertFalse( $result );
46+
}
47+
48+
/**
49+
* The chart author can still read their own chart configuration.
50+
*/
51+
public function test_author_can_read_own_chart_data() {
52+
$author_id = self::factory()->user->create( array( 'role' => 'editor' ) );
53+
$chart_id = $this->create_chart( $author_id );
54+
55+
wp_set_current_user( $author_id );
56+
57+
$result = Visualizer_Gutenberg_Block::get_instance()->get_visualizer_data( array( 'id' => $chart_id ) );
58+
$this->assertIsArray( $result );
59+
}
60+
}

0 commit comments

Comments
 (0)