Skip to content

Commit 562881a

Browse files
committed
Restrict SQLite installation permissions
Require network option management for the shared database drop-in on multisite, while retaining manage_options on single-site installations. Enforce the capability before rendering the install UI or accepting its nonce.
1 parent 96db8f0 commit 562881a

2 files changed

Lines changed: 67 additions & 23 deletions

File tree

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

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,23 @@
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( admin_url( 'options-general.php?page=sqlite-integration' ) ) ) {
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( admin_url( 'options-general.php?page=sqlite-integration' ) ) ) {
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 shared 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
*
@@ -36,25 +41,29 @@ function sqlite_activation() {
3641
if ( isset( $current_screen->base ) && 'settings_page_sqlite-integration' === $current_screen->base ) {
3742
return;
3843
}
39-
if ( isset( $_GET['confirm-install'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'sqlite-install' ) ) {
44+
if ( ! isset( $_GET['confirm-install'] ) ) {
45+
return;
46+
}
4047

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;
48+
sqlite_plugin_check_manage_capability();
49+
check_admin_referer( 'sqlite-install' );
50+
51+
// Handle upgrading from the performance-lab plugin.
52+
if ( isset( $_GET['upgrade-from-pl'] ) ) {
53+
global $wp_filesystem;
54+
require_once ABSPATH . '/wp-admin/includes/file.php';
55+
// Delete the previous db.php file.
56+
$wp_filesystem->delete( WP_CONTENT_DIR . '/db.php' );
57+
// Deactivate the performance-lab SQLite module.
58+
$pl_option_name = defined( 'PERFLAB_MODULES_SETTING' ) ? PERFLAB_MODULES_SETTING : 'perflab_modules_settings';
59+
$pl_option = get_option( $pl_option_name, array() );
60+
unset( $pl_option['database/sqlite'] );
61+
update_option( $pl_option_name, $pl_option );
5762
}
63+
sqlite_plugin_copy_db_file();
64+
// WordPress will automatically redirect to the install screen here.
65+
wp_redirect( admin_url() );
66+
exit;
5867
}
5968
add_action( 'admin_init', 'sqlite_activation' );
6069

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function sqlite_add_admin_menu() {
1515
add_options_page(
1616
__( 'SQLite integration', 'sqlite-database-integration' ),
1717
__( 'SQLite integration', 'sqlite-database-integration' ),
18-
'manage_options',
18+
sqlite_plugin_get_manage_capability(),
1919
'sqlite-integration',
2020
'sqlite_integration_admin_screen'
2121
);
@@ -26,6 +26,8 @@ function sqlite_add_admin_menu() {
2626
* The admin page contents.
2727
*/
2828
function sqlite_integration_admin_screen() {
29+
sqlite_plugin_check_manage_capability();
30+
2931
$db_dropin_path = WP_CONTENT_DIR . '/db.php';
3032

3133
/*
@@ -154,6 +156,10 @@ function sqlite_integration_admin_screen() {
154156
function sqlite_plugin_adminbar_item( $admin_bar ) {
155157
global $wpdb;
156158

159+
if ( is_multisite() && ! current_user_can( sqlite_plugin_get_manage_capability() ) ) {
160+
return;
161+
}
162+
157163
if ( defined( 'SQLITE_DB_DROPIN_VERSION' ) && defined( 'DB_ENGINE' ) && 'sqlite' === DB_ENGINE ) {
158164
$title = '<span style="color:#46B450;">' . __( 'Database: SQLite', 'sqlite-database-integration' ) . '</span>';
159165
} elseif ( stripos( $wpdb->db_server_info(), 'maria' ) !== false ) {
@@ -172,3 +178,32 @@ function sqlite_plugin_adminbar_item( $admin_bar ) {
172178
$admin_bar->add_node( $args );
173179
}
174180
add_action( 'admin_bar_menu', 'sqlite_plugin_adminbar_item', 999 );
181+
182+
/**
183+
* Stop users who cannot manage the SQLite integration.
184+
*
185+
* @since n.e.x.t
186+
*/
187+
function sqlite_plugin_check_manage_capability() {
188+
if ( ! current_user_can( sqlite_plugin_get_manage_capability() ) ) {
189+
wp_die(
190+
esc_html__( 'Sorry, you are not allowed to manage the SQLite integration.', 'sqlite-database-integration' ),
191+
'',
192+
403
193+
);
194+
}
195+
}
196+
197+
/**
198+
* Get the capability required to manage the SQLite integration.
199+
*
200+
* The database drop-in is shared by every site in a multisite network, so only
201+
* users who can manage network options may change it.
202+
*
203+
* @since n.e.x.t
204+
*
205+
* @return string Required capability.
206+
*/
207+
function sqlite_plugin_get_manage_capability() {
208+
return is_multisite() ? 'manage_network_options' : 'manage_options';
209+
}

0 commit comments

Comments
 (0)