Skip to content

fix: require edit_post access on notification subscription AJAX handlers#931

Merged
mjangda merged 2 commits into
developfrom
copilot/add-post-edit-check
Apr 20, 2026
Merged

fix: require edit_post access on notification subscription AJAX handlers#931
mjangda merged 2 commits into
developfrom
copilot/add-post-edit-check

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 20, 2026

Both ajax_save_post_subscriptions and handle_user_post_subscription checked edit_post_subscriptions capability but not current_user_can( 'edit_post', $post_id ), allowing any user with that cap to read/write subscriptions on posts they have no editorial access to.

Changes

  • ajax_save_post_subscriptions — adds edit_post check to the existing guard condition alongside edit_post_subscriptions_cap, so only users who can edit the target post may update its subscriber list.
  • handle_user_post_subscription — adds the same edit_post check after the post is fetched and validated, mirroring the gate already enforced in filter_post_row_actions().
// Before – only checked custom cap, not post-level access
if ( ! isset( $_POST['ef_notifications_name'] ) || ! $valid_post
     || ! current_user_can( $this->edit_post_subscriptions_cap ) ) {

// After
if ( ! isset( $_POST['ef_notifications_name'] ) || ! $valid_post
     || ! current_user_can( $this->edit_post_subscriptions_cap )
     || ! current_user_can( 'edit_post', $post_id ) ) {

Tests

Added two integration tests in NotificationsAjaxTest using an author user (has edit_post_subscriptions but can only edit their own posts):

  • test_ajax_save_fails_when_user_cannot_edit_post — expects WPAjaxDieStopException when saving subscriptions on another user's post.
  • test_handle_user_post_subscription_fails_when_user_cannot_edit_post — expects a JSON error response when following another user's post.

Copilot AI and others added 2 commits April 20, 2026 17:53
…ndlers

Without this check, any user with the edit_post_subscriptions capability
could modify subscriptions (or follow/unfollow) for ANY post, including
posts they have no edit access to.

- ajax_save_post_subscriptions: add edit_post check alongside the existing
  edit_post_subscriptions_cap check so only users who can edit the target
  post may update its subscriber list.
- handle_user_post_subscription: add the same edit_post check after the
  post is fetched, mirroring the gate already applied in
  filter_post_row_actions().

Tests added for both handlers: an author (who has edit_post_subscriptions
but cannot edit another user's post) is now correctly rejected.

Agent-Logs-Url: https://github.com/Automattic/edit-flow/sessions/40903bf2-8e98-4206-a6ed-b8d9d3b67208

Co-authored-by: mjangda <86105+mjangda@users.noreply.github.com>
…ption

Defensive return ensures execution stops after the error response, even
if wp_die() is intercepted (e.g. in test environments).

Agent-Logs-Url: https://github.com/Automattic/edit-flow/sessions/40903bf2-8e98-4206-a6ed-b8d9d3b67208

Co-authored-by: mjangda <86105+mjangda@users.noreply.github.com>
@mjangda mjangda marked this pull request as ready for review April 20, 2026 18:53
@mjangda mjangda requested a review from a team as a code owner April 20, 2026 18:53
@mjangda mjangda merged commit 631e705 into develop Apr 20, 2026
10 checks passed
@mjangda mjangda deleted the copilot/add-post-edit-check branch April 20, 2026 20:01
@GaryJones GaryJones added this to the Next milestone Apr 23, 2026
@GaryJones GaryJones mentioned this pull request Apr 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants