Skip to content

fix: check per-chart edit_post capability in chart_data REST field - #1348

Open
lucadobrescu wants to merge 2 commits into
developmentfrom
fix/605-chart-data-cap-check
Open

fix: check per-chart edit_post capability in chart_data REST field#1348
lucadobrescu wants to merge 2 commits into
developmentfrom
fix/605-chart-data-cap-check

Conversation

@lucadobrescu

@lucadobrescu lucadobrescu commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Any logged-in user with the edit_posts capability (Contributor and above) could read the full backend configuration of any chart on the site — data source query, series/settings, and JSON endpoint URLs with their authentication headers — via GET /wp-json/wp/v2/visualizer/{chart_id}, because the chart_data REST field callback only checked the generic edit_posts capability. Reported in Codeinwp/visualizer-pro#605.

What changed

  • chart_data REST field guardget_visualizer_data() now checks Visualizer_Module::can_edit_chart() against the requested chart instead of the generic current_user_can( 'edit_posts' ). Access is allowed for the chart's author and for users with edit_others_posts (Editor and above). The existing can_edit_chart() helper is used instead of a bare edit_post check because charts are force-published on save and Contributors lack edit_published_posts — a bare check would lock Contributor authors out of their own charts.

  • Regression testtests/test-chart-data-permissions.php asserts a Contributor gets false for another user's published chart, a Contributor author still receives their own chart's data, and an Editor can read any chart.

chart_data request flow

flowchart LR
    A[GET /wp/v2/visualizer/id] --> B[chart_data field callback]
    B --> C{Changed:<br/>can_edit_chart —<br/>author or Editor+?}:::changed
    C -- Yes --> D[Return chart config]
    C -- No --> E[Return false]

    classDef changed fill:#9a6700,color:#fff,stroke:#5c3d00,stroke-width:3px,stroke-dasharray:6 3
Loading

QA

  1. As an Administrator, create and publish a chart, and note its post ID.

  2. As a Contributor, request the chart over REST (e.g. from the browser console while logged in):

    wp.apiFetch( { path: '/wp/v2/visualizer/<chart_id>' } ).then( console.log );

    Expect: chart_data is false — no source query, settings, or endpoint headers in the response.

  3. As the chart's author (or an Editor/Administrator), repeat the same request.
    Expect: chart_data contains the chart configuration and the chart still loads normally in the Gutenberg block editor.

  4. As a Contributor, create a chart of your own, then repeat the request for that chart's ID.
    Expect: chart_data contains the chart configuration — Contributor authors keep access to their own charts.

🤖 Generated with Claude Code

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>
@lucadobrescu lucadobrescu self-assigned this Jul 27, 2026
@lucadobrescu lucadobrescu added the pr-checklist-skip Allow this Pull Request to skip checklist. label Jul 27, 2026
@pirate-bot pirate-bot added the pr-checklist-complete The Pull Request checklist is complete. (automatic label) label Jul 27, 2026
@pirate-bot

pirate-bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Plugin build for 6b05e98 is ready 🛎️!

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Secures the chart_data REST field with per-chart authorization.

Changes:

  • Replaces the generic capability check with object-specific authorization.
  • Adds regression tests for chart configuration access.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
classes/Visualizer/Gutenberg/Block.php Restricts REST chart data access.
tests/test-chart-data-permissions.php Tests owner and non-owner access.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/test-chart-data-permissions.php Outdated
Comment on lines +52 to +53
$author_id = self::factory()->user->create( array( 'role' => 'editor' ) );
$chart_id = $this->create_chart( $author_id );

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 6b05e98 — fixtures are now published, the positive case is a Contributor-owned chart, and added an Editor-reads-others'-chart case.

Comment thread classes/Visualizer/Gutenberg/Block.php Outdated
Charts are force-published on save, and Contributors lack
edit_published_posts, so a bare edit_post check locks chart authors out
of their own charts in the Gutenberg block. Use the existing
Visualizer_Module::can_edit_chart() helper, which allows the chart
owner with edit_posts while still rejecting other users' charts.

Covers Copilot review feedback on #1348.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-checklist-complete The Pull Request checklist is complete. (automatic label) pr-checklist-skip Allow this Pull Request to skip checklist.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants