Skip to content

Fix fatal in role_can_view() when cap check fires before init - #1963

Open
andreilupu wants to merge 1 commit into
xwp:developfrom
andreilupu:fix/early-view-cap-check-fatal
Open

Fix fatal in role_can_view() when cap check fires before init#1963
andreilupu wants to merge 1 commit into
xwp:developfrom
andreilupu:fix/early-view-cap-check-fatal

Conversation

@andreilupu

Copy link
Copy Markdown

Fixes #1962.

The user_has_cap/role_has_cap filters are registered in the Admin constructor, but the Settings object they dereference is only constructed in Plugin::init() on init priority 9. Any capability check for view_stream between plugins_loaded and init 9 (e.g. a security plugin evaluating firewall rules on plugins_loaded) reaches role_can_view() with a null options chain, and in_array() with a null haystack fatals on PHP 8+ — taking down wp-admin and admin-ajax.php for every logged-in user.

This PR null-coalesces the options access in role_can_view() and denies access during the pre-init window instead of fataling. Post-init behavior is unchanged. Includes regression tests for both paths (early check denied without fatal, allowed role still granted the cap).

Checklist

  • Project documentation has been updated to reflect the changes in this pull request, if applicable.
  • I have tested the changes in the local development environment (see contributing.md).
  • I have added phpunit tests.

Release Changelog

  • Fix: Fatal TypeError in role_can_view() when the view_stream capability is checked before init priority 9 (e.g. by security/firewall plugins on plugins_loaded) on PHP 8+.

The user_has_cap/role_has_cap filters are registered in the Admin
constructor, but the Settings object they dereference is only
constructed in Plugin::init() on init priority 9. Any capability check
for view_stream between plugins_loaded and init 9 — security/firewall
plugins evaluating rules on plugins_loaded do exactly this — reaches
role_can_view() with a null options chain, and in_array() with a null
haystack is an uncaught TypeError on PHP 8+, fataling wp-admin and
admin-ajax for every logged-in user.

Null-coalesce the options access and deny access during the pre-init
window instead of fataling. Adds regression tests for both the early
(denied, no fatal) and normal (granted) paths.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.

Fatal TypeError in role_can_view() when a capability check fires before init priority 9 (PHP 8)

1 participant