-
Notifications
You must be signed in to change notification settings - Fork 67
Require super-admin for theme-modifying REST routes on multisite #850
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 11 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
0ef0894
Add can_modify_theme() permission helper with DISALLOW_FILE_* respect
mikachan dfa937b
Wire 9 mutating REST routes to can_modify_theme()
mikachan 8d4fa44
Add multisite permission tests (skip when not in multisite env)
mikachan 4f2c5cd
Keep DISALLOW_FILE_* authoritative over the cbt_file_mods_allowed filter
mikachan cc90c73
Assert /font-families returns 200 SUCCESS, not just not-403
mikachan 3a1fd65
Avoid constructor side effects in invoke_private test helper
mikachan 35aefde
Grant super-admin on multisite to isolate file-mod hardening assertion
mikachan a81cf45
Update docblock
mikachan 7849b2b
Restore docblock close marker dropped by Copilot suggestion
mikachan 0c6174a
Delegate file-mod check to WP Core's wp_is_file_mod_allowed
mikachan 769e25c
Add regression test for cbt filter not overriding core file_mod_allowed
mikachan 342f90f
Use edit_themes + wp_is_file_mod_allowed for permission check
mikachan f13e2e8
Document /reset-theme cap reuse for permission-surface consistency
mikachan 7642196
Hide UI when REST capability gate denies
mikachan b4f526e
Add multisite specific test runs
mikachan 755413e
Activate a block theme in UI-gate tests
mikachan 4c4c40c
Don't invoke sidebar enqueue under gate-open in test
mikachan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| <?php | ||
| /** | ||
| * @package Create_Block_Theme | ||
| */ | ||
| class Test_Create_Block_Theme_Api extends WP_UnitTestCase { | ||
|
|
||
| /** | ||
| * Helper: invoke a private method on a CBT_Theme_API instance. | ||
| */ | ||
| private function invoke_private( $method ) { | ||
| $ref_class = new ReflectionClass( CBT_Theme_API::class ); | ||
| $instance = $ref_class->newInstanceWithoutConstructor(); | ||
| $ref = new ReflectionMethod( $instance, $method ); | ||
| $ref->setAccessible( true ); | ||
| return $ref->invoke( $instance ); | ||
| } | ||
|
|
||
| public function test_admin_can_modify_theme_on_single_site() { | ||
| if ( is_multisite() ) { | ||
| $this->markTestSkipped( 'single-site only' ); | ||
| } | ||
| $admin = $this->factory->user->create( array( 'role' => 'administrator' ) ); | ||
| wp_set_current_user( $admin ); | ||
|
|
||
| $this->assertTrue( $this->invoke_private( 'can_modify_theme' ) ); | ||
| } | ||
|
|
||
| public function test_editor_cannot_modify_theme_on_single_site() { | ||
| if ( is_multisite() ) { | ||
| $this->markTestSkipped( 'single-site only' ); | ||
| } | ||
| $editor = $this->factory->user->create( array( 'role' => 'editor' ) ); | ||
| wp_set_current_user( $editor ); | ||
|
|
||
| $this->assertFalse( $this->invoke_private( 'can_modify_theme' ) ); | ||
| } | ||
|
|
||
| public function test_anonymous_cannot_modify_theme() { | ||
| wp_set_current_user( 0 ); | ||
| $this->assertFalse( $this->invoke_private( 'can_modify_theme' ) ); | ||
| } | ||
|
|
||
| public function test_admin_blocked_when_file_mods_disallowed() { | ||
| if ( is_multisite() ) { | ||
| $this->markTestSkipped( 'single-site only' ); | ||
| } | ||
| $admin = $this->factory->user->create( array( 'role' => 'administrator' ) ); | ||
| wp_set_current_user( $admin ); | ||
|
|
||
| add_filter( 'cbt_file_mods_allowed', '__return_false' ); | ||
| $result = $this->invoke_private( 'can_modify_theme' ); | ||
| remove_filter( 'cbt_file_mods_allowed', '__return_false' ); | ||
|
|
||
| $this->assertFalse( $result ); | ||
| } | ||
|
|
||
| public function test_file_mods_allowed_returns_true_by_default() { | ||
| $this->assertTrue( $this->invoke_private( 'file_mods_allowed' ) ); | ||
| } | ||
|
|
||
| public function test_file_mods_allowed_filter_can_disable() { | ||
| add_filter( 'cbt_file_mods_allowed', '__return_false' ); | ||
| $result = $this->invoke_private( 'file_mods_allowed' ); | ||
| remove_filter( 'cbt_file_mods_allowed', '__return_false' ); | ||
|
|
||
| $this->assertFalse( $result ); | ||
| } | ||
|
|
||
| public function test_file_mods_allowed_respects_core_file_mod_allowed_filter() { | ||
| // WordPress Core's canonical `file_mod_allowed` filter is the | ||
| // mechanism hosts and security plugins use to disable file mods | ||
| // globally. file_mods_allowed() must honour it. | ||
| add_filter( 'file_mod_allowed', '__return_false' ); | ||
| $result = $this->invoke_private( 'file_mods_allowed' ); | ||
| remove_filter( 'file_mod_allowed', '__return_false' ); | ||
|
|
||
| $this->assertFalse( $result ); | ||
| } | ||
|
Copilot marked this conversation as resolved.
Outdated
|
||
|
|
||
| public function test_file_mods_allowed_filter_cannot_reenable_core_disallow() { | ||
| add_filter( 'file_mod_allowed', '__return_false' ); | ||
| add_filter( 'cbt_file_mods_allowed', '__return_true' ); | ||
| $result = $this->invoke_private( 'file_mods_allowed' ); | ||
| remove_filter( 'cbt_file_mods_allowed', '__return_true' ); | ||
| remove_filter( 'file_mod_allowed', '__return_false' ); | ||
|
|
||
| $this->assertFalse( $result ); | ||
| } | ||
|
|
||
| public function test_save_endpoint_rejects_when_file_mods_disallowed() { | ||
| $admin = $this->factory->user->create( array( 'role' => 'administrator' ) ); | ||
| wp_set_current_user( $admin ); | ||
| if ( is_multisite() ) { | ||
| grant_super_admin( $admin ); | ||
| } | ||
|
|
||
| add_filter( 'cbt_file_mods_allowed', '__return_false' ); | ||
| $request = new WP_REST_Request( 'POST', '/create-block-theme/v1/save' ); | ||
| $response = rest_get_server()->dispatch( $request ); | ||
| remove_filter( 'cbt_file_mods_allowed', '__return_false' ); | ||
|
|
||
|
Copilot marked this conversation as resolved.
|
||
| if ( is_multisite() ) { | ||
| revoke_super_admin( $admin ); | ||
| } | ||
| $this->assertSame( 403, $response->get_status() ); | ||
| $this->assertSame( 'rest_forbidden', $response->get_data()['code'] ); | ||
| } | ||
|
|
||
| public function test_save_endpoint_rejects_anonymous() { | ||
| wp_set_current_user( 0 ); | ||
| $request = new WP_REST_Request( 'POST', '/create-block-theme/v1/save' ); | ||
| $response = rest_get_server()->dispatch( $request ); | ||
|
|
||
| $this->assertSame( 401, $response->get_status() ); | ||
| } | ||
|
|
||
| public function test_font_families_endpoint_still_accessible_to_admin() { | ||
| $admin = $this->factory->user->create( array( 'role' => 'administrator' ) ); | ||
| wp_set_current_user( $admin ); | ||
|
|
||
| // /font-families is a GET and is NOT gated by can_modify_theme(). | ||
| // Even with file mods disallowed, it should still respond (not 403). | ||
| add_filter( 'cbt_file_mods_allowed', '__return_false' ); | ||
| $request = new WP_REST_Request( 'GET', '/create-block-theme/v1/font-families' ); | ||
| $response = rest_get_server()->dispatch( $request ); | ||
| remove_filter( 'cbt_file_mods_allowed', '__return_false' ); | ||
|
|
||
| $this->assertSame( 200, $response->get_status() ); | ||
| $this->assertSame( 'SUCCESS', $response->get_data()['status'] ); | ||
| } | ||
|
|
||
| public function test_super_admin_can_modify_theme_on_multisite() { | ||
| if ( ! is_multisite() ) { | ||
| $this->markTestSkipped( 'requires multisite — set WP_TESTS_MULTISITE=1' ); | ||
| } | ||
| $super = $this->factory->user->create( array( 'role' => 'administrator' ) ); | ||
| grant_super_admin( $super ); | ||
| wp_set_current_user( $super ); | ||
|
|
||
| $this->assertTrue( $this->invoke_private( 'can_modify_theme' ) ); | ||
|
|
||
| revoke_super_admin( $super ); | ||
| } | ||
|
|
||
| public function test_subsite_admin_cannot_modify_theme_on_multisite() { | ||
| if ( ! is_multisite() ) { | ||
| $this->markTestSkipped( 'requires multisite — set WP_TESTS_MULTISITE=1' ); | ||
| } | ||
| $admin = $this->factory->user->create( array( 'role' => 'administrator' ) ); | ||
| // Explicitly NOT a super-admin. | ||
| wp_set_current_user( $admin ); | ||
|
|
||
| $this->assertFalse( $this->invoke_private( 'can_modify_theme' ) ); | ||
| } | ||
|
|
||
| public function test_save_endpoint_rejects_subsite_admin_on_multisite() { | ||
| if ( ! is_multisite() ) { | ||
| $this->markTestSkipped( 'requires multisite — set WP_TESTS_MULTISITE=1' ); | ||
| } | ||
| $admin = $this->factory->user->create( array( 'role' => 'administrator' ) ); | ||
| wp_set_current_user( $admin ); | ||
|
|
||
| $request = new WP_REST_Request( 'POST', '/create-block-theme/v1/save' ); | ||
| $response = rest_get_server()->dispatch( $request ); | ||
|
|
||
| $this->assertSame( 403, $response->get_status() ); | ||
| $this->assertSame( 'rest_forbidden', $response->get_data()['code'] ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.