Skip to content

Require super-admin for theme-modifying REST routes on multisite#850

Open
mikachan wants to merge 13 commits into
trunkfrom
fix/multisite-permission-checks
Open

Require super-admin for theme-modifying REST routes on multisite#850
mikachan wants to merge 13 commits into
trunkfrom
fix/multisite-permission-checks

Conversation

@mikachan

Copy link
Copy Markdown
Member

On a multisite, themes are network-shared. This PR tightens the permission check on the 9 filesystem-mutating REST routes in CBT_Theme_API so that only super-admins can call them when the site is multisite. The read-only /font-families route is unchanged.

Also restores DISALLOW_FILE_EDIT / DISALLOW_FILE_MODS honouring that the plugin had prior to v2.1.2. Both constants now gate theme-file modifications, matching the behaviour of wp-admin/theme-editor.php.

To test, ensure the following tests pass:

  npm run test:unit:php -- --filter Test_Create_Block_Theme_Api

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

This PR tightens permissions for theme-modifying REST API routes in CBT_Theme_API, requiring super-admin on multisite (where themes are network-shared) and restoring gating by DISALLOW_FILE_EDIT / DISALLOW_FILE_MODS. It also adds PHPUnit coverage around the new permission behavior.

Changes:

  • Updated 9 filesystem-mutating REST routes to use a centralized can_modify_theme() permission check (super-admin on multisite, edit_theme_options otherwise).
  • Added file_mods_allowed() to block modifications when DISALLOW_FILE_EDIT / DISALLOW_FILE_MODS are set (with a filter seam for tests).
  • Introduced a new PHPUnit test class covering can_modify_theme(), file_mods_allowed(), and select route permission outcomes.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
includes/class-create-block-theme-api.php Centralizes and tightens permission checks for mutating REST routes; adds file-mod hardening gate.
tests/test-class-create-block-theme-api.php Adds unit tests for new permission logic and REST route permission enforcement.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread includes/class-create-block-theme-api.php Outdated
Comment thread tests/test-class-create-block-theme-api.php Outdated
Comment thread tests/test-class-create-block-theme-api.php
Comment thread tests/test-class-create-block-theme-api.php
mikachan and others added 4 commits June 16, 2026 13:24
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@mikachan

Copy link
Copy Markdown
Member Author

Looks like these were all good suggestions - I've applied them all.

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 1 comment.

Comment thread includes/class-create-block-theme-api.php Outdated
mikachan and others added 2 commits June 16, 2026 15:25
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Comment thread includes/class-create-block-theme-api.php Outdated
scruffian flagged that the previous implementation bypassed WordPress
Core's canonical `file_mod_allowed` filter — the mechanism hosts and
security plugins use to disable file modifications globally.

Now delegates the DISALLOW_FILE_MODS / file_mod_allowed branch to
wp_is_file_mod_allowed( 'create_block_theme_modify_theme' ). The
explicit DISALLOW_FILE_EDIT check is kept on top because Core's
helper does NOT cover that constant (it's specifically for the theme
file editor UI).

The cbt_file_mods_allowed filter remains as a test seam — it can only
further restrict, never re-enable, the policy decided by core.

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 1 comment.

Comment thread tests/test-class-create-block-theme-api.php Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Comment thread includes/class-create-block-theme-api.php
Comment thread includes/class-create-block-theme-api.php Outdated
mikachan added 2 commits June 17, 2026 16:03
Refactors can_modify_theme() to compose two WordPress Core primitives
instead of reimplementing the same checks:

  current_user_can( 'edit_themes' )
    && wp_is_file_mod_allowed( 'create_block_theme_modify_theme' )

The edit_themes capability already encodes the matrix this plugin
needs: held by Administrators on single-site, super-admins on multisite
(NOT sub-site admins), and automatically denied by Core when
DISALLOW_FILE_EDIT is defined. wp_is_file_mod_allowed handles
DISALLOW_FILE_MODS and the canonical file_mod_allowed filter.

That removes:
  - the explicit is_multisite() / is_super_admin() branch
  - the explicit DISALLOW_FILE_EDIT constant check
  - the file_mods_allowed() helper
  - the cbt_file_mods_allowed filter (was a test seam; tests now use
    the canonical file_mod_allowed filter instead)

Functional matrix is unchanged.
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