Skip to content

Commit c43572b

Browse files
committed
Restrict SQLite deactivation
Require the SQLite management capability in addition to WordPress's normal plugin capability while the database drop-in is active. Allow deactivation when no SQLite drop-in exists.
1 parent 6fa62d6 commit c43572b

3 files changed

Lines changed: 141 additions & 22 deletions

File tree

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,44 @@
11
<?php
22

3+
/**
4+
* Require SQLite management permission to deactivate an active integration.
5+
*
6+
* WordPress checks this capability when one plugin is deactivated, including
7+
* each plugin in a site-level batch. It skips this per-plugin check for a
8+
* Network Admin batch, so that case is handled separately below.
9+
*
10+
* @access private
11+
*
12+
* @param string[] $caps Primitive capabilities required of the user.
13+
* @param string $cap Capability being checked.
14+
* @param int $user_id User ID.
15+
* @param mixed[] $args Additional capability arguments.
16+
* @return string[] Required primitive capabilities.
17+
*/
18+
function sqlite_plugin_map_deactivation_capability( $caps, $cap, $user_id, $args ) {
19+
if (
20+
'deactivate_plugin' === $cap
21+
&& isset( $args[0] )
22+
&& plugin_basename( SQLITE_MAIN_FILE ) === $args[0]
23+
&& sqlite_plugin_has_active_dropin()
24+
) {
25+
$caps[] = sqlite_plugin_get_manage_capability();
26+
}
27+
return $caps;
28+
}
29+
add_filter( 'map_meta_cap', 'sqlite_plugin_map_deactivation_capability', 10, 4 );
30+
31+
/**
32+
* Check whether the SQLite database drop-in is active.
33+
*
34+
* @access private
35+
*
36+
* @return bool Whether the SQLite database drop-in is active.
37+
*/
38+
function sqlite_plugin_has_active_dropin() {
39+
return defined( 'SQLITE_DB_DROPIN_VERSION' ) && file_exists( WP_CONTENT_DIR . '/db.php' );
40+
}
41+
342
/**
443
* Get the capability required to manage the SQLite integration.
544
*

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* @param bool $network_deactivating Whether the plugin is being deactivated network-wide.
1515
*/
1616
function sqlite_plugin_remove_db_file( $network_deactivating = false ) {
17-
if ( ! defined( 'SQLITE_DB_DROPIN_VERSION' ) || ! file_exists( WP_CONTENT_DIR . '/db.php' ) ) {
17+
if ( ! sqlite_plugin_has_active_dropin() ) {
1818
return;
1919
}
2020

‎tests/phpunit/WP_SQLite_Database_Integration_Authorization_Test.php‎

Lines changed: 101 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,67 @@ public function test_super_admin_with_valid_nonce_can_install() {
243243
$this->assert_valid_nonce_reaches_install_redirect();
244244
}
245245

246+
/**
247+
* @group ms-required
248+
*/
249+
public function test_super_admin_with_invalid_nonce_is_denied() {
250+
$this->set_multisite_user( true );
251+
252+
$this->assert_invalid_nonce_is_denied();
253+
}
254+
255+
/**
256+
* @group ms-required
257+
*/
258+
public function test_subsite_administrator_with_valid_nonce_cannot_install() {
259+
$this->set_multisite_user( false );
260+
261+
$this->assert_valid_nonce_install_is_denied();
262+
}
263+
264+
/**
265+
* @group ms-excluded
266+
*/
267+
public function test_single_site_administrator_can_deactivate_sqlite() {
268+
$this->set_single_site_user( 'administrator' );
269+
270+
$this->assertTrue( current_user_can( 'deactivate_plugin', plugin_basename( SQLITE_MAIN_FILE ) ) );
271+
$this->assert_deactivation_removes_dropin();
272+
}
273+
274+
/**
275+
* @group ms-excluded
276+
*/
277+
public function test_single_site_plugin_manager_can_only_deactivate_sqlite_without_dropin() {
278+
$this->set_single_site_user( 'administrator' );
279+
wp_get_current_user()->add_cap( 'manage_options', false );
280+
281+
$this->assertFalse( current_user_can( 'manage_options' ) );
282+
$this->assertTrue( current_user_can( 'deactivate_plugin', 'another-plugin/plugin.php' ) );
283+
$this->assertFalse( current_user_can( 'deactivate_plugin', plugin_basename( SQLITE_MAIN_FILE ) ) );
284+
285+
$this->run_without_dropin(
286+
function () {
287+
$this->assertTrue( current_user_can( 'deactivate_plugin', plugin_basename( SQLITE_MAIN_FILE ) ) );
288+
}
289+
);
290+
}
291+
292+
/**
293+
* @group ms-required
294+
*/
295+
public function test_super_admin_can_deactivate_sqlite() {
296+
$this->set_multisite_user( true );
297+
298+
$this->assertTrue( current_user_can( 'deactivate_plugin', plugin_basename( SQLITE_MAIN_FILE ) ) );
299+
$this->assert_deactivation_removes_dropin();
300+
}
301+
246302
/**
247303
* @group ms-required
248304
*/
249305
public function test_network_deactivation_context_is_forwarded_to_mysql_cleanup() {
306+
$this->set_multisite_user( true );
250307
$shutdown_hook_before = isset( $GLOBALS['wp_filter']['shutdown'] ) ? clone $GLOBALS['wp_filter']['shutdown'] : null;
251308

252309
try {
@@ -324,25 +381,58 @@ public function test_site_deactivation_updates_mysql_active_plugins() {
324381
/**
325382
* @group ms-required
326383
*/
327-
public function test_super_admin_with_invalid_nonce_is_denied() {
328-
$this->set_multisite_user( true );
384+
public function test_delegated_network_plugin_manager_can_only_deactivate_sqlite_without_dropin() {
385+
$this->run_as_delegated_network_plugin_manager(
386+
function () {
387+
$this->assertTrue( current_user_can( 'deactivate_plugin', 'another-plugin/plugin.php' ) );
388+
$this->assertFalse( current_user_can( 'deactivate_plugin', plugin_basename( SQLITE_MAIN_FILE ) ) );
389+
390+
$this->run_without_dropin(
391+
function () {
392+
$this->assertTrue( current_user_can( 'deactivate_plugin', plugin_basename( SQLITE_MAIN_FILE ) ) );
393+
}
394+
);
395+
}
396+
);
397+
}
329398

330-
$this->assert_invalid_nonce_is_denied();
399+
private function set_single_site_user( $role ) {
400+
$user_id = self::factory()->user->create( array( 'role' => $role ) );
401+
402+
wp_set_current_user( $user_id );
331403
}
332404

333-
/**
334-
* @group ms-required
335-
*/
336-
public function test_subsite_administrator_with_valid_nonce_cannot_install() {
405+
private function set_multisite_user( $is_super_admin ) {
406+
$user_id = $is_super_admin ? self::$super_admin_id : self::$subsite_admin_id;
407+
408+
switch_to_blog( self::$site_id );
409+
wp_set_current_user( $user_id );
410+
}
411+
412+
private function run_as_delegated_network_plugin_manager( $callback ) {
337413
$this->set_multisite_user( false );
338414

339-
$this->assert_valid_nonce_install_is_denied();
415+
$user = wp_get_current_user();
416+
$capabilities = array( 'activate_plugins', 'manage_network_plugins' );
417+
418+
foreach ( $capabilities as $capability ) {
419+
$user->add_cap( $capability );
420+
}
421+
422+
try {
423+
return call_user_func( $callback );
424+
} finally {
425+
foreach ( $capabilities as $capability ) {
426+
$user->remove_cap( $capability );
427+
}
428+
}
340429
}
341430

342-
private function set_single_site_user( $role ) {
343-
$user_id = self::factory()->user->create( array( 'role' => $role ) );
431+
private function assert_deactivation_removes_dropin() {
432+
$filesystem = $this->run_with_deactivation_filesystem( 'sqlite_plugin_remove_db_file' );
344433

345-
wp_set_current_user( $user_id );
434+
$this->assertSame( WP_CONTENT_DIR . '/db.php', $filesystem->deleted_path );
435+
$this->assertFileExists( WP_CONTENT_DIR . '/db.php' );
346436
}
347437

348438
private function run_with_deactivation_filesystem( $callback ) {
@@ -402,16 +492,6 @@ public function update( $table, $data, $where ) {
402492
};
403493
}
404494

405-
private function set_multisite_user( $is_super_admin ) {
406-
$user_id = $is_super_admin ? self::$super_admin_id : self::$subsite_admin_id;
407-
408-
switch_to_blog( self::$site_id );
409-
wp_set_current_user( $user_id );
410-
411-
$this->assertTrue( current_user_can( 'manage_options' ) );
412-
$this->assertSame( $is_super_admin, current_user_can( 'manage_network_options' ) );
413-
}
414-
415495
private function reset_admin_menu() {
416496
$global_names = array(
417497
'menu',

0 commit comments

Comments
 (0)