Skip to content

Commit 6fa62d6

Browse files
committed
Restrict SQLite installation permissions
Require manage_options on single-site and manage_network_options on multisite across the management UI, activation redirect, and drop-in installation request.
1 parent 232894f commit 6fa62d6

6 files changed

Lines changed: 377 additions & 57 deletions

File tree

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

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,47 +14,63 @@
1414
* @param string $plugin The plugin basename.
1515
*/
1616
function sqlite_plugin_activation_redirect( $plugin ) {
17-
if ( plugin_basename( SQLITE_MAIN_FILE ) === $plugin ) {
18-
if ( wp_safe_redirect( sqlite_plugin_get_admin_page_url() ) ) {
19-
exit;
20-
}
17+
if (
18+
plugin_basename( SQLITE_MAIN_FILE ) !== $plugin
19+
|| ! current_user_can( sqlite_plugin_get_manage_capability() )
20+
) {
21+
return;
22+
}
23+
24+
if ( wp_safe_redirect( sqlite_plugin_get_admin_page_url() ) ) {
25+
exit;
2126
}
2227
}
2328
add_action( 'activated_plugin', 'sqlite_plugin_activation_redirect' );
2429

2530
/**
2631
* Check the URL to ensure we're on the plugin page,
2732
* the user has clicked the button to install SQLite,
28-
* and the nonce is valid.
33+
* the user has permission to manage the database drop-in, and the nonce is valid.
2934
* If the above conditions are met, run the sqlite_plugin_copy_db_file() function,
3035
* and redirect to the install screen.
3136
*
3237
* @since 1.0.0
3338
*/
3439
function sqlite_activation() {
35-
global $current_screen;
36-
if ( isset( $current_screen->base ) && 'settings_page_sqlite-integration' === $current_screen->base ) {
40+
if (
41+
! isset( $_GET['page'], $_GET['confirm-install'] )
42+
|| 'sqlite-integration' !== $_GET['page']
43+
) {
3744
return;
3845
}
39-
if ( isset( $_GET['confirm-install'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'sqlite-install' ) ) {
4046

41-
// Handle upgrading from the performance-lab plugin.
42-
if ( isset( $_GET['upgrade-from-pl'] ) ) {
43-
global $wp_filesystem;
44-
require_once ABSPATH . '/wp-admin/includes/file.php';
45-
// Delete the previous db.php file.
46-
$wp_filesystem->delete( WP_CONTENT_DIR . '/db.php' );
47-
// Deactivate the performance-lab SQLite module.
48-
$pl_option_name = defined( 'PERFLAB_MODULES_SETTING' ) ? PERFLAB_MODULES_SETTING : 'perflab_modules_settings';
49-
$pl_option = get_option( $pl_option_name, array() );
50-
unset( $pl_option['database/sqlite'] );
51-
update_option( $pl_option_name, $pl_option );
52-
}
53-
sqlite_plugin_copy_db_file();
54-
// WordPress will automatically redirect to the install screen here.
55-
wp_redirect( admin_url() );
56-
exit;
47+
if ( ! current_user_can( sqlite_plugin_get_manage_capability() ) ) {
48+
wp_die(
49+
esc_html__( 'Sorry, you are not allowed to install the SQLite database drop-in.', 'sqlite-database-integration' ),
50+
403
51+
);
52+
}
53+
54+
check_admin_referer( 'sqlite-install' );
55+
56+
// Handle upgrading from the performance-lab plugin.
57+
if ( isset( $_GET['upgrade-from-pl'] ) ) {
58+
global $wp_filesystem;
59+
require_once ABSPATH . '/wp-admin/includes/file.php';
60+
// Delete the previous db.php file.
61+
$wp_filesystem->delete( WP_CONTENT_DIR . '/db.php' );
62+
// Deactivate the performance-lab SQLite module.
63+
$pl_option_name = defined( 'PERFLAB_MODULES_SETTING' ) ? PERFLAB_MODULES_SETTING : 'perflab_modules_settings';
64+
$pl_option = get_option( $pl_option_name, array() );
65+
unset( $pl_option['database/sqlite'] );
66+
update_option( $pl_option_name, $pl_option );
5767
}
68+
69+
sqlite_plugin_copy_db_file();
70+
71+
// WordPress will automatically redirect to the install screen here.
72+
wp_redirect( admin_url() );
73+
exit;
5874
}
5975
add_action( 'admin_init', 'sqlite_activation' );
6076

‎packages/plugin-sqlite-database-integration/admin-notices.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
* When the plugin gets merged in wp-core, this is not to be ported.
1313
*/
1414
function sqlite_plugin_admin_notice() {
15+
if ( ! current_user_can( sqlite_plugin_get_manage_capability() ) ) {
16+
return;
17+
}
1518

1619
// Don't print notices in the plugin's admin screen.
1720
global $current_screen;

‎packages/plugin-sqlite-database-integration/admin-page.php‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@
1313
*/
1414
function sqlite_add_admin_menu() {
1515
$parent_slug = is_multisite() ? 'settings.php' : 'options-general.php';
16-
$capability = is_multisite() ? 'manage_network_options' : 'manage_options';
1716

1817
add_submenu_page(
1918
$parent_slug,
2019
__( 'SQLite integration', 'sqlite-database-integration' ),
2120
__( 'SQLite integration', 'sqlite-database-integration' ),
22-
$capability,
21+
sqlite_plugin_get_manage_capability(),
2322
'sqlite-integration',
2423
'sqlite_integration_admin_screen'
2524
);
@@ -30,6 +29,13 @@ function sqlite_add_admin_menu() {
3029
* The admin page contents.
3130
*/
3231
function sqlite_integration_admin_screen() {
32+
if ( ! current_user_can( sqlite_plugin_get_manage_capability() ) ) {
33+
wp_die(
34+
esc_html__( 'Sorry, you are not allowed to access the SQLite integration settings.', 'sqlite-database-integration' ),
35+
403
36+
);
37+
}
38+
3339
$db_dropin_path = WP_CONTENT_DIR . '/db.php';
3440

3541
/*
@@ -165,6 +171,10 @@ function sqlite_integration_admin_screen() {
165171
* @param WP_Admin_Bar $admin_bar The admin bar object.
166172
*/
167173
function sqlite_plugin_adminbar_item( $admin_bar ) {
174+
if ( ! current_user_can( sqlite_plugin_get_manage_capability() ) ) {
175+
return;
176+
}
177+
168178
global $wpdb;
169179

170180
if ( defined( 'SQLITE_DB_DROPIN_VERSION' ) && defined( 'DB_ENGINE' ) && 'sqlite' === DB_ENGINE ) {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
/**
4+
* Get the capability required to manage the SQLite integration.
5+
*
6+
* @access private
7+
*
8+
* @return string Required capability.
9+
*/
10+
function sqlite_plugin_get_manage_capability() {
11+
return is_multisite() ? 'manage_network_options' : 'manage_options';
12+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
define( 'SQLITE_MAIN_FILE', __FILE__ );
2323

24+
require_once __DIR__ . '/capabilities.php';
2425
require_once __DIR__ . '/admin-page.php';
2526
require_once __DIR__ . '/activate.php';
2627
require_once __DIR__ . '/deactivate.php';

0 commit comments

Comments
 (0)