Subchapter 4.2
references/capability-gate-tracing.mdMarkdown8 KBView on GitHub
How to resolve the actual capability (or capabilities) a plugin’s REST
controllers gate on. The audit’s capability_gate field and each ability’s
permission.resolves_to field need to reflect reality, not what the
controller docblock says.
Two common mechanisms cover most plugins. Document both explicitly so the auditor doesn’t hard-code one plugin family’s assumptions.
check_permission() returning a single cap)The base REST controller declares a check_permission() (or
permissions_check()) method that calls current_user_can('<some_cap>')
once. Every route in the controller uses that method as
permission_callback.
public function check_permission() {
return current_user_can( 'manage_options' );
}grep -n 'current_user_can' <base-controller>.php yields one hit.# Locate the base controller (usually the parent of every REST controller).
grep -rn 'extends .*REST_Controller' includes/ | head
# Read its permission_callback implementation.
grep -n 'check_permission\|permissions_check' <base-controller>.phpTrace once: the single current_user_can() call is the plugin’s gate.
capability_gate: manage_options # confirmed at includes/admin/class-<plugin>-rest-controller.php line 64Plugin-specific capabilities (e.g. WooCommerce’s manage_woocommerce for
shop-aware contexts, Jetpack Forms’ edit_pages) substitute for
manage_options cleanly — the shape stays the same.
The controller extends a WordPress core post-type-backed class that
dispatches to the post-type capability map. There is no local
check_permission() — the permission callback resolves dynamically at
request time based on the request context (read vs write) and the post
type’s cap object.
WP_REST_Posts_Controller — the core post-type REST base.check_permission() — permission callbacks are inherited.capability_type => '<cpt_or_shadow>',
and the cap map is resolved by core’s map_meta_cap().# Find the post-type registration.
grep -rn "register_post_type\s*(\s*['\"]<cpt_name>['\"]" .
# Read the registration block. The relevant fields are:
# - capability_type: the type whose cap map this post type uses.
# A custom post type can either declare its own caps or shadow another
# type's (e.g. capability_type => 'page' to reuse Pages' caps).
# - capabilities: optional explicit cap-string overrides.
# - map_meta_cap: whether meta caps (read_post, edit_post) get mapped to
# primitive caps (read_private_<type>s, edit_others_<type>s).Dynamic resolution typically lands at:
current_user_can('read_private_<type>s') or current_user_can('read_<type>', $id).current_user_can('edit_<type>s'), current_user_can('edit_others_<type>s'), or current_user_can('delete_<type>s', $id).The two often differ — post-type-backed plugins routinely have distinct read and write caps.
Use the structured {read, write} form from audit-schema.md:
capability_gate:
read: read_private_pages
write: edit_others_pages
confirmed: true
verified_at: "custom_post_type capability_type='page' → core map_meta_cap (wp-includes/post.php) → primitive page caps"In each ability’s permission block, spell out both calls:
permission:
callback: get_items_permissions_check
resolves_to: "WP_REST_Posts_Controller::get_items_permissions_check (inherited) → current_user_can('read_private_pages')"
confirmed: trueExample A — generic plugin shadowing core Pages caps. A custom post type
registered with capability_type='page' inherits the Pages cap map, so
reads gate on read_private_pages and writes gate on edit_others_pages.
Example B — WooCommerce-style sidebar. WooCommerce’s shop_subscription is
registered with capability_type='shop_order', so reads gate on
read_private_shop_orders and writes gate on edit_shop_orders.
Mechanically identical to Example A; the cap names are project-specific.
WooCommerce also exposes a helper wc_rest_check_post_permissions() that
wraps the same core machinery — the helper is convenience; the underlying
mechanism is core’s map_meta_cap().
Some earlier audits encoded compound gates as a single string with a /
separator:
capability_gate: read_private_pages / edit_others_pagesThis is accepted for backwards compatibility, but:
/.{read, write} object form is machine-parseable and carries
confirmed and verified_at in-band.Prefer the structured form for any new audit.
The ability’s permission should match the plugin’s intended gate for the
proposed behavior, not necessarily the REST route. Often the REST
controller’s permission_callback is the right source of truth, but in
some plugins the canonical permission lives elsewhere — an admin-action
handler with its own check_admin_referer + current_user_can block, a
service / helper method that performs the check before doing the work, a
domain-policy / authorization layer, or a post-type cap shadow resolved
through core’s map_meta_cap. The audit should preserve where the
permission canonically lives so the implementer doesn’t silently drift
to whichever source the REST layer happens to expose.
For each proposed ability, walk the chain once:
permission_callback first; if the REST callback is '__return_true',
delegates entirely, or doesn’t match the behavior’s intended gate,
look for the canonical source in an admin handler, a shared service
method, a domain-policy class, or a post-type cap map.permission.source field
per audit-schema.md: one of rest_controller, admin_action,
service, domain_policy, post_type_map, none. Default
rest_controller; pick another value when the canonical source is
elsewhere.current_user_can() call(s). For Mechanism B,
resolve BOTH read and write if the ability crosses contexts.permission.resolves_to field verbatim — the
string should read as an actual trace, not a best-guess summary.permission_callback
must consult the canonical source (or replicate its check), not
copy the REST callback by reflex.{read, write} pair) at the same source, hoist it into the top-level
capability_gate. If any behavior diverges in cap OR in source,
record the divergence in “Notes and Surprises”.Zero-arg public endpoints sometimes declare permission_callback => '__return_true' at the REST layer (e.g. status lookups, enumerated lists
that are safe to expose). The audit still needs a gate:
resolves_to: "__return_true (public)") so the auditor isn’t hiding reality.'__return_true' — the ability’s own permission_callback must match
the plugin’s intended user gate (e.g. manage_options, edit_pages, or
whatever your plugin uses). The ability layer is the agent-facing surface
and needs that gate even when the underlying REST route is public.