Skip to content

Honor WPGraphQL permissions to post content - #122

Merged
alecgeatches merged 4 commits into
trunkfrom
fix/wpgraphql-access
Oct 16, 2026
Merged

alecgeatches merged 4 commits into
trunkfrom
fix/wpgraphql-access

Conversation

@alecgeatches

@alecgeatches alecgeatches commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

When WPGraphQL is active, a request for a password-protected published post or page could read content. #118 fixed the same class of problem on the REST route but did not touch the GraphQL resolvers.

The REST approach to rely directly on controller permissions in #118 doesn't transfer to WPGraphQL directly. That fix uses post type's REST controller, but post types can be exposed to GraphQL without show_in_rest, and the REST permission check still permits password-protected posts because core only blanks their content in the response. For GraphQL, the controller is WPGraphQL's own permissions, which decides access before any resolver runs.

Both resolvers (blockData, blockDataV2) now share a get_readable_post() helper that returns nullwhen the model is restricted, when the model has no post ID, or when the post is protected and the current user cannot edit it. The last check is a bit redundant and mirrors the REST rule and core's own logic, and acts as defense-in-depth should the resolver ever be reached with an unrestricted model for a protected post. Private and draft posts were already correctly handled by WPGraphQL returning no model, and that behavior is unchanged.

I have verified this works manually (with the WPGraphQL plugin activated) and a query on a password-protected post:

{ post(id: <password protected post ID>, idType: DATABASE_ID) { databaseId hasPassword isRestricted content blocksDataV2 { blocks { name attributes { name value } } } blocksData { blocks { name attributes { name value } } } } }

On trunk:

{
  "data": {
    "post": {
      "databaseId": 6,
      "content": null,
      "blocksDataV2": {
        "blocks": [
          {
            "name": "core/paragraph",
            "attributes": [
              {
                "name": "content",
                "value": "secret content"
              },
              {
                "name": "dropCap",
                "value": "false"
              }
            ]
          }
        ]
      },
      "blocksData": {
        "blocks": [
          {
            "name": "core/paragraph",
            "attributes": [
              {
                "name": "content",
                "value": "secret content"
              },
              {
                "name": "dropCap",
                "value": ""
              }
            ]
          }
        ]
      }
    }
  }
}

On fix/wpgraphql-access:

{
  "data": {
    "post": {
      "databaseId": 6,
      "content": null,
      "blocksDataV2": null,
      "blocksData": null
    }
  }
}

@alecgeatches alecgeatches self-assigned this Oct 16, 2026
@alecgeatches
alecgeatches requested a review from a team as a code owner October 16, 2026 03:40
// WPGraphQL withholds the content field for password-protected posts. Since this
// resolver parses raw post_content, require edit access before returning that content.
// This matches the REST endpoint's password-protected post handling.
if ( ! empty( $post->post_password ) && ! current_user_can( 'edit_post', $post->ID ) ) {

@alecgeatches alecgeatches Oct 16, 2026 •

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.

This check (and the one in v2 below) isn't necessary for this fix, because isRestricted from WPGraphQL earlier in the function should return false for this case already. This is just an extra sanity check.

@alecgeatches
alecgeatches merged commit 9258c66 into trunk Oct 16, 2026
20 checks passed
@alecgeatches
alecgeatches deleted the fix/wpgraphql-access branch October 16, 2026 05:01
@alecgeatches alecgeatches mentioned this pull request Oct 16, 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.

2 participants