Skip to content

Commit 068af9e

Browse files
committed
Protect network bulk deactivation
Remove SQLite from unauthorized Network Admin batches so other selected plugins can still be deactivated. Keep the drop-in intact for unauthorized logged-in callers if the request preflight is bypassed.
1 parent c43572b commit 068af9e

3 files changed

Lines changed: 252 additions & 0 deletions

File tree

‎packages/plugin-sqlite-database-integration/capabilities.php‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,35 @@ function sqlite_plugin_map_deactivation_capability( $caps, $cap, $user_id, $args
2828
}
2929
add_filter( 'map_meta_cap', 'sqlite_plugin_map_deactivation_capability', 10, 4 );
3030

31+
/**
32+
* WordPress does not check each plugin's permission when deactivating a Network
33+
* Admin batch. Remove SQLite from the batch when the user cannot manage the
34+
* network-wide drop-in, while allowing the other selected plugins to continue.
35+
*
36+
* @access private
37+
*/
38+
function sqlite_plugin_filter_network_bulk_deactivation() {
39+
$is_network_bulk_deactivation = is_network_admin()
40+
&& isset( $_REQUEST['action'] )
41+
&& is_string( $_REQUEST['action'] )
42+
&& 'deactivate-selected' === wp_unslash( $_REQUEST['action'] );
43+
44+
if (
45+
$is_network_bulk_deactivation
46+
&& sqlite_plugin_has_active_dropin()
47+
&& ! current_user_can( sqlite_plugin_get_manage_capability() )
48+
) {
49+
/*
50+
* Remove only SQLite from the submitted list. WordPress will still check
51+
* whether the user may run bulk deactivation and whether the request is
52+
* valid before it deactivates the plugins left in the list.
53+
*/
54+
$plugins = isset( $_POST['checked'] ) ? (array) $_POST['checked'] : array();
55+
$_POST['checked'] = array_values( array_diff( $plugins, array( plugin_basename( SQLITE_MAIN_FILE ) ) ) );
56+
}
57+
}
58+
add_action( 'load-plugins.php', 'sqlite_plugin_filter_network_bulk_deactivation' );
59+
3160
/**
3261
* Check whether the SQLite database drop-in is active.
3362
*

‎packages/plugin-sqlite-database-integration/deactivate.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ function sqlite_plugin_remove_db_file( $network_deactivating = false ) {
1818
return;
1919
}
2020

21+
// WP-CLI and other programmatic callers may deactivate without a current user.
22+
// In those cases, the caller is responsible for authorization.
23+
if ( is_user_logged_in() && ! current_user_can( sqlite_plugin_get_manage_capability() ) ) {
24+
return;
25+
}
26+
2127
global $wp_filesystem;
2228

2329
require_once ABSPATH . '/wp-admin/includes/file.php';

‎tests/phpunit/WP_SQLite_Database_Integration_Authorization_Test.php‎

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,183 @@ function () {
396396
);
397397
}
398398

399+
/**
400+
* @group ms-excluded
401+
*/
402+
public function test_single_site_bulk_deactivation_skips_sqlite() {
403+
$this->set_single_site_user( 'administrator' );
404+
wp_get_current_user()->add_cap( 'manage_options', false );
405+
$plugins = array( 'another-plugin/plugin.php', plugin_basename( SQLITE_MAIN_FILE ) );
406+
407+
foreach ( $plugins as $index => $plugin ) {
408+
if ( ! current_user_can( 'deactivate_plugin', $plugin ) ) {
409+
unset( $plugins[ $index ] );
410+
}
411+
}
412+
413+
$this->assertSame( array( 'another-plugin/plugin.php' ), array_values( $plugins ) );
414+
}
415+
416+
/**
417+
* @group ms-required
418+
*/
419+
public function test_network_bulk_deactivation_skips_sqlite_and_continues() {
420+
$sqlite_plugin = plugin_basename( SQLITE_MAIN_FILE );
421+
$other_plugin = 'another-plugin/plugin.php';
422+
$plugins = array( $other_plugin, $sqlite_plugin );
423+
424+
$this->run_as_delegated_network_plugin_manager(
425+
function () use ( $plugins, $sqlite_plugin, $other_plugin ) {
426+
$this->run_with_network_active_plugins(
427+
$plugins,
428+
function () use ( $plugins, $sqlite_plugin, $other_plugin ) {
429+
$dropin_before = file_get_contents( WP_CONTENT_DIR . '/db.php' );
430+
431+
$this->set_bulk_deactivation_request( $plugins );
432+
$this->run_bulk_deactivation_preflight( 'plugins-network' );
433+
434+
$this->assertSame( array( $other_plugin ), $_POST['checked'] );
435+
436+
deactivate_plugins( $_POST['checked'], false, true );
437+
438+
$this->assertTrue( is_plugin_active_for_network( $sqlite_plugin ) );
439+
$this->assertFalse( is_plugin_active_for_network( $other_plugin ) );
440+
$this->assertSame( $dropin_before, file_get_contents( WP_CONTENT_DIR . '/db.php' ) );
441+
}
442+
);
443+
}
444+
);
445+
}
446+
447+
/**
448+
* @group ms-required
449+
*/
450+
public function test_super_admin_can_bulk_deactivate_sqlite() {
451+
$this->set_multisite_user( true );
452+
$sqlite_plugin = plugin_basename( SQLITE_MAIN_FILE );
453+
454+
$this->run_with_network_active_plugins(
455+
array( $sqlite_plugin ),
456+
function () use ( $sqlite_plugin ) {
457+
$this->set_bulk_deactivation_request( array( $sqlite_plugin ) );
458+
$this->run_bulk_deactivation_preflight( 'plugins-network' );
459+
460+
$this->assertSame( array( $sqlite_plugin ), $_POST['checked'] );
461+
462+
$filesystem = $this->run_with_deactivation_filesystem(
463+
function () use ( $sqlite_plugin ) {
464+
deactivate_plugins( $sqlite_plugin, false, true );
465+
}
466+
);
467+
468+
$this->assertFalse( is_plugin_active_for_network( $sqlite_plugin ) );
469+
$this->assertSame( WP_CONTENT_DIR . '/db.php', $filesystem->deleted_path );
470+
}
471+
);
472+
}
473+
474+
/**
475+
* @group ms-required
476+
*/
477+
public function test_site_admin_bulk_preflight_does_not_modify_request() {
478+
$this->set_multisite_user( false );
479+
$sqlite_plugin = plugin_basename( SQLITE_MAIN_FILE );
480+
$plugins = array( 'another-plugin/plugin.php', $sqlite_plugin );
481+
482+
$this->set_bulk_deactivation_request( $plugins );
483+
$this->run_bulk_deactivation_preflight( 'plugins' );
484+
485+
$this->assertSame( $plugins, $_POST['checked'] );
486+
}
487+
488+
/**
489+
* @group ms-required
490+
*/
491+
public function test_network_bulk_preflight_preserves_slashed_plugin_names() {
492+
$sqlite_plugin = plugin_basename( SQLITE_MAIN_FILE );
493+
$other_plugin = "another-plugin/plugin's.php";
494+
495+
$this->run_as_delegated_network_plugin_manager(
496+
function () use ( $sqlite_plugin, $other_plugin ) {
497+
$this->set_bulk_deactivation_request( array( $other_plugin, $sqlite_plugin ) );
498+
$_POST['checked'] = wp_slash( $_POST['checked'] );
499+
500+
$this->run_bulk_deactivation_preflight( 'plugins-network' );
501+
502+
$this->assertSame( wp_slash( array( $other_plugin ) ), $_POST['checked'] );
503+
}
504+
);
505+
}
506+
507+
/**
508+
* @group ms-required
509+
*/
510+
public function test_network_bulk_preflight_ignores_non_string_action() {
511+
$this->set_multisite_user( false );
512+
$plugins = array( plugin_basename( SQLITE_MAIN_FILE ) );
513+
514+
$this->set_bulk_deactivation_request( $plugins );
515+
$_REQUEST['action'] = array( 'deactivate-selected' );
516+
517+
$this->run_bulk_deactivation_preflight( 'plugins-network' );
518+
519+
$this->assertSame( $plugins, $_POST['checked'] );
520+
}
521+
522+
/**
523+
* @group ms-required
524+
*/
525+
public function test_network_bulk_deactivation_allows_sqlite_without_dropin() {
526+
$sqlite_plugin = plugin_basename( SQLITE_MAIN_FILE );
527+
528+
$this->run_as_delegated_network_plugin_manager(
529+
function () use ( $sqlite_plugin ) {
530+
$this->run_without_dropin(
531+
function () use ( $sqlite_plugin ) {
532+
$this->set_bulk_deactivation_request( array( $sqlite_plugin ) );
533+
$this->run_bulk_deactivation_preflight( 'plugins-network' );
534+
535+
$this->assertSame( array( $sqlite_plugin ), $_POST['checked'] );
536+
}
537+
);
538+
}
539+
);
540+
}
541+
542+
/**
543+
* @group ms-required
544+
*/
545+
public function test_deactivation_hook_preserves_dropin_when_bulk_preflight_is_bypassed() {
546+
$sqlite_plugin = plugin_basename( SQLITE_MAIN_FILE );
547+
548+
$this->run_as_delegated_network_plugin_manager(
549+
function () use ( $sqlite_plugin ) {
550+
$this->run_with_network_active_plugins(
551+
array( $sqlite_plugin ),
552+
function () use ( $sqlite_plugin ) {
553+
$filesystem = $this->run_with_deactivation_filesystem(
554+
function () use ( $sqlite_plugin ) {
555+
deactivate_plugins( $sqlite_plugin, false, true );
556+
}
557+
);
558+
559+
$this->assertFalse( is_plugin_active_for_network( $sqlite_plugin ) );
560+
$this->assertNull( $filesystem->deleted_path );
561+
}
562+
);
563+
}
564+
);
565+
}
566+
567+
/**
568+
* @group ms-excluded
569+
*/
570+
public function test_deactivation_hook_allows_programmatic_call_without_user() {
571+
wp_set_current_user( 0 );
572+
573+
$this->assert_deactivation_removes_dropin();
574+
}
575+
399576
private function set_single_site_user( $role ) {
400577
$user_id = self::factory()->user->create( array( 'role' => $role ) );
401578

@@ -428,6 +605,46 @@ private function run_as_delegated_network_plugin_manager( $callback ) {
428605
}
429606
}
430607

608+
private function run_with_network_active_plugins( $plugins, $callback ) {
609+
$active_before = get_site_option( 'active_sitewide_plugins', array() );
610+
$active = $active_before;
611+
612+
foreach ( $plugins as $plugin ) {
613+
$active[ $plugin ] = time();
614+
}
615+
update_site_option( 'active_sitewide_plugins', $active );
616+
617+
try {
618+
return call_user_func( $callback );
619+
} finally {
620+
update_site_option( 'active_sitewide_plugins', $active_before );
621+
}
622+
}
623+
624+
private function set_bulk_deactivation_request( $plugins ) {
625+
$_POST = array( 'checked' => $plugins );
626+
$_REQUEST = array(
627+
'action' => 'deactivate-selected',
628+
);
629+
}
630+
631+
private function run_bulk_deactivation_preflight( $screen_id ) {
632+
$screen_was_set = array_key_exists( 'current_screen', $GLOBALS );
633+
$screen_before = $screen_was_set ? $GLOBALS['current_screen'] : null;
634+
635+
$GLOBALS['current_screen'] = WP_Screen::get( $screen_id );
636+
637+
try {
638+
sqlite_plugin_filter_network_bulk_deactivation();
639+
} finally {
640+
if ( $screen_was_set ) {
641+
$GLOBALS['current_screen'] = $screen_before;
642+
} else {
643+
unset( $GLOBALS['current_screen'] );
644+
}
645+
}
646+
}
647+
431648
private function assert_deactivation_removes_dropin() {
432649
$filesystem = $this->run_with_deactivation_filesystem( 'sqlite_plugin_remove_db_file' );
433650

0 commit comments

Comments
 (0)