Skip to content

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

Merged
kushh23 merged 2 commits into
developmentfrom
fix/605-chart-data-cap-check
Jul 30, 2026
Merged

fix: check per-chart edit_post capability in chart_data REST field#1348
kushh23 merged 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 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.

@kushh23
kushh23 merged commit 4094672 into development Jul 30, 2026
16 checks passed
@kushh23
kushh23 deleted the fix/605-chart-data-cap-check branch July 30, 2026 05:43
@pirate-bot

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 4.0.7 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

@pirate-bot pirate-bot added the released Indicate that an issue has been resolved and released in a particular version of the product. label Jul 30, 2026
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. released Indicate that an issue has been resolved and released in a particular version of the product.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants