diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 9443ef4..4b08362 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -76,9 +76,9 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout code - uses: actions/checkout@v4 + uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 - name: Setup Node.js - uses: actions/setup-node@v4 + uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4 with: node-version: '22' - name: Test exact option names and deletion confirmation diff --git a/README.md b/README.md index d6c7098..90b6e64 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ Every REST route requires `manage_options`. Option values can include API keys, ### Which actions are protected? -Essential WordPress options, including site URLs, active plugins, theme settings and the current site's roles option, cannot be deleted, created with a false value, or have their autoload state changed through this plugin. Single and bulk deletion require confirmation in the admin screen. Autoload changes preserve the stored value and use WordPress's cache-aware autoload API. +Essential WordPress options, including site URLs, active plugins, the active theme's settings, salts and keys stored in the database, and the current site's roles option, cannot be deleted, created with a false value, or have their autoload state changed through this plugin. Single and bulk deletion require confirmation in the admin screen. Autoload changes preserve the stored value and use WordPress's cache-aware autoload API. ### Why are unused-option recommendations paused? diff --git a/src/class-protected-options.php b/src/class-protected-options.php index 54087e0..25b100c 100644 --- a/src/class-protected-options.php +++ b/src/class-protected-options.php @@ -133,8 +133,7 @@ private static function core_options() { 'cron', 'fresh_site', - // Theme/customizer. - 'theme_mods', + // Theme/customizer. The active theme's mods are added in names(). 'theme_switched', 'widget_block', @@ -152,27 +151,62 @@ private static function core_options() { 'default_category', 'default_post_format', 'sidebars_widgets', + + // Salts and keys, stored here when not defined in wp-config.php. Deleting them logs everyone out. + 'auth_key', + 'auth_salt', + 'secure_auth_key', + 'secure_auth_salt', + 'logged_in_key', + 'logged_in_salt', + 'nonce_key', + 'nonce_salt', ]; } + /** + * Lowercased static names, built once per request. + * + * @var array|null + */ + private static $static_names = null; /** - * Check core, blog-specific roles, and this plugin's own settings. + * Lowercased protected names, keyed for lookups. * - * @param string $option_name Option name. - * @return bool + * Names that depend on the current site (roles prefix, active theme) are added per call so + * the map stays correct after switch_to_blog() or a theme switch within the request. + * + * @return array */ - public static function is_protected( $option_name ) { + private static function names() { global $wpdb; - $names = self::core_options(); - $names[] = $wpdb->prefix . 'user_roles'; - $names[] = 'alloptions'; - $names[] = 'notoptions'; - $names[] = 'option_optimizer'; - $names[] = 'aaa_option_optimizer_tracking_limited'; + if ( null === self::$static_names ) { + $names = self::core_options(); + $names[] = 'alloptions'; + $names[] = 'notoptions'; + $names[] = 'option_optimizer'; + $names[] = 'aaa_option_optimizer_tracking_limited'; + + self::$static_names = array_fill_keys( array_map( 'strtolower', $names ), true ); + } - return in_array( strtolower( $option_name ), array_map( 'strtolower', $names ), true ); + $names = self::$static_names; + $names[ strtolower( $wpdb->prefix . 'user_roles' ) ] = true; + $names[ strtolower( 'theme_mods_' . \get_stylesheet() ) ] = true; + return $names; + } + + + /** + * Check core, blog-specific roles, the active theme's mods, and this plugin's own settings. + * + * @param string $option_name Option name. + * @return bool + */ + public static function is_protected( $option_name ) { + return isset( self::names()[ strtolower( $option_name ) ] ); } /** @@ -182,13 +216,33 @@ public static function is_protected( $option_name ) { * @return bool */ public static function is_mutation_protected( $option_name ) { + return [] !== self::get_mutation_protected( [ $option_name ] ); + } + + /** + * Return the submitted names that are protected, directly or through a collation alias. + * + * Resolves every name with a single query, so bulk actions don't run one query per name. + * + * @param string[] $option_names Submitted option names. + * @return string[] Protected submitted names. + */ + public static function get_mutation_protected( array $option_names ) { global $wpdb; - if ( self::is_protected( $option_name ) ) { - return true; + $protected = array_values( array_filter( $option_names, [ self::class, 'is_protected' ] ) ); + if ( $protected ) { + return $protected; } - // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching - $stored_name = $wpdb->get_var( $wpdb->prepare( "SELECT option_name FROM {$wpdb->options} WHERE option_name = %s", $option_name ) ); - return null !== $stored_name && self::is_protected( $stored_name ); + if ( ! $option_names ) { + return []; + } + + $slots = implode( ',', array_fill( 0, count( $option_names ), '%s' ) ); + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- Placeholders are generated above. + $stored = $wpdb->get_col( $wpdb->prepare( "SELECT option_name FROM {$wpdb->options} WHERE option_name IN ($slots)", array_values( $option_names ) ) ); + + // The database matched these rows by collation, so the stored name stands in for the submitted one. + return array_values( array_filter( $stored, [ self::class, 'is_protected' ] ) ); } } diff --git a/src/class-rest.php b/src/class-rest.php index 87200e2..8076b58 100644 --- a/src/class-rest.php +++ b/src/class-rest.php @@ -735,10 +735,8 @@ public function delete_option( $request ) { public function delete_options( $request ) { $option_names = $request['option_names']; // Preflight the entire batch before deleting anything. - foreach ( $option_names as $option_name ) { - if ( Protected_Options::is_mutation_protected( $option_name ) ) { - return new \WP_Error( 'option_protected', 'Option is protected', [ 'status' => 403 ] ); - } + if ( Protected_Options::get_mutation_protected( $option_names ) ) { + return new \WP_Error( 'option_protected', 'Option is protected', [ 'status' => 403 ] ); } $failed = []; foreach ( array_unique( $option_names ) as $option_name ) { @@ -778,10 +776,8 @@ public function set_autoload_options( $request ) { * @return \WP_Error|\WP_REST_Response */ private function update_autoload( array $option_names, string $state ) { - foreach ( $option_names as $option_name ) { - if ( Protected_Options::is_mutation_protected( $option_name ) ) { - return new \WP_Error( 'option_protected', 'Option is protected', [ 'status' => 403 ] ); - } + if ( Protected_Options::get_mutation_protected( $option_names ) ) { + return new \WP_Error( 'option_protected', 'Option is protected', [ 'status' => 403 ] ); } $autoload = in_array( $state, \wp_autoload_values_to_autoload(), true ); @@ -903,7 +899,7 @@ public function get_sort_params(): array { } // phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- Type and allowlist checked before use. $direction = $_GET['order'][0]['dir'] ?? 'asc'; - return [ $column, 'desc' === $direction ? SORT_DESC : SORT_ASC ]; + return [ $column, is_string( $direction ) && 'desc' === strtolower( $direction ) ? SORT_DESC : SORT_ASC ]; } /** diff --git a/tests/hardening.php b/tests/hardening.php index d2bcf5d..520c156 100644 --- a/tests/hardening.php +++ b/tests/hardening.php @@ -16,6 +16,8 @@ require_once dirname( __DIR__ ) . '/aaa-option-optimizer.php'; Database::create_table(); +// Start from empty tracking data, so a previous failed run (e.g. a full table) can't skew results. +Database::clear_tracked_options(); wp_set_current_user( 1 ); rest_get_server(); @@ -93,7 +95,10 @@ static function () { ); // List responses must preserve the serialized bytes already stored in wp_options. Database::batch_insert( array_fill_keys( $names, 1 ) ); +// List routes paginate from $_GET (25 rows by default); fetch one full page so fixtures are present on busy sites. +$_GET = [ 'length' => 500 ]; $listed_values = array_column( request( 'used-not-autoloaded-options', [], 'GET' )->get_data()['data'], 'value', 'name' ); +$_GET = []; foreach ( $names as $name ) { $stored_value = $wpdb->get_var( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s", $name ) ); check( $stored_value === ( $listed_values[ $name ] ?? null ), 'List response must not serialize the raw value again: ' . $name ); @@ -182,7 +187,13 @@ static function () { ); } -foreach ( [ 'siteurl', 'SITEURL', 'sitéurl', 'active_plugins', $wpdb->prefix . 'user_roles', 'alloptions', 'notoptions' ] as $protected ) { +foreach ( [ 'siteurl', 'SITEURL', 'sitéurl', 'active_plugins', $wpdb->prefix . 'user_roles', 'alloptions', 'notoptions', 'theme_mods_' . get_stylesheet(), strtoupper( 'theme_mods_' . get_stylesheet() ), 'auth_salt', 'NONCE_KEY', 'logged_in_salt' ] as $protected ) { + // Alias folding depends on the database: MySQL matches 'sitéurl' to 'siteurl', SQLite doesn't. + // Where the engine doesn't fold it, the name is a different, unprotected option and is skipped. + $resolved = $wpdb->get_var( $wpdb->prepare( "SELECT option_name FROM $wpdb->options WHERE option_name = %s", $protected ) ); + if ( ! Protected_Options::is_protected( $protected ) && ( null === $resolved || ! Protected_Options::is_protected( $resolved ) ) ) { + continue; + } foreach ( [ 'delete-option', 'create-option-false', 'update-autoload' ] as $route ) { check( 403 === request( @@ -209,6 +220,20 @@ static function () { check( null !== $wpdb->get_var( "SELECT option_name FROM $wpdb->options WHERE option_name = 'aaaoo_false'" ), 'Protected batch deleted an earlier option.' ); } } +// Leftover mods of an inactive theme stay removable; only the active theme's are protected. +add_option( 'theme_mods_aaaoo_inactive_theme', [ 'header_textcolor' => 'fff' ], '', false ); +check( ! Protected_Options::is_protected( 'theme_mods' ), 'The bare theme_mods name is not a core option.' ); +check( 200 === request( 'delete-option', [ 'option_name' => 'theme_mods_aaaoo_inactive_theme' ] )->get_status(), 'Inactive theme mods are removable.' ); +check( + 200 === request( + 'set-autoload-options', + [ + 'option_names' => [ 'aaaoo_false' ], + 'autoload' => 'no', + ] + )->get_status(), + 'Unprotected batch still passes the batch preflight.' +); wp_set_current_user( 0 ); check( 401 === request( 'delete-option', [ 'option_name' => 'aaaoo_false' ] )->get_status(), 'Anonymous mutation denied.' ); @@ -229,6 +254,18 @@ static function () { 'columns' => [], ]; check( [ 'name', SORT_ASC ] === $rest->get_sort_params(), 'Array sort index must not throw.' ); +foreach ( [ 'DESC', 'Desc', 'desc' ] as $dir ) { + $_GET = [ + 'order' => [ + [ + 'column' => '0', + 'dir' => $dir, + ], + ], + 'columns' => [ [ 'data' => 'size' ] ], + ]; + check( [ 'size', SORT_DESC ] === $rest->get_sort_params(), 'Sort direction is case-insensitive: ' . $dir ); +} $_GET = [ 'order' => [ [