From 9fe694fb841085709b1aafa79219ea96cef3ea2b Mon Sep 17 00:00:00 2001 From: Nol de Roos <108540791+nolderoos@users.noreply.github.com> Date: Fri, 22 May 2026 02:28:21 +0200 Subject: [PATCH] Salt notice: detection-only, dismissible, links to docs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Remove all salt generation and the in-plugin fix wizard. The plugin no longer generates salts, reads or scans wp-config.php, or offers a "Fix it for me" path — WordPress already ships a generator, and rewriting or parsing the site's master config is too high blast-radius for a login plugin. Keep weak-salt detection (runtime constants are authoritative) and rework the admin notice: link to the docs guide (DOCS_SALTS_URL) rather than api.wordpress.org directly, drop the "Fix it for me" button, and make it site-wide dismissible. One admin dismissing hides it for all, persisted in the magicauth_salt_notice_dismissed site option and re-armed by check_salts() if salts ever regress to weak. Removed: - Installer: generate_salt_value / salt_values / generate_salt_block, recheck_salts_from_file, config_has_weak_salts / define_pattern, locate_wp_config / filesystem - Settings: ajax_fix_salts + its wp_ajax registration, and the "Fix WordPress salts" diagnostics card - admin JS: initSaltFix() wizard, plus orphaned wizard CSS - tests: SaltFixTest (exercised the now-removed methods) Token and login-code generation are untouched; the informed-consent gate on the branded-login toggle is unchanged. Version stays 1.0.0. --- assets/css/magicauth-admin.css | 29 ---- assets/js/magicauth-admin.js | 113 -------------- includes/Admin/Settings.php | 52 +------ includes/Installer.php | 232 +++++++++------------------- includes/Plugin.php | 7 +- tests/phpunit/Model/SaltFixTest.php | 110 ------------- 6 files changed, 76 insertions(+), 467 deletions(-) delete mode 100644 tests/phpunit/Model/SaltFixTest.php diff --git a/assets/css/magicauth-admin.css b/assets/css/magicauth-admin.css index a41ddc1..62df64f 100644 --- a/assets/css/magicauth-admin.css +++ b/assets/css/magicauth-admin.css @@ -841,14 +841,6 @@ body.admin-bar .magicauth-admin .magicauth-topbar__bar { top: 32px; } .magicauth-admin .magicauth-row + .magicauth-action-row, .magicauth-admin .magicauth-action-row + .magicauth-row { border-top: 1px solid var(--ettic-border); } -.magicauth-admin .magicauth-action-row--alert { - background: #FEF2F2; - border-radius: 8px; - padding-left: 14px; - padding-right: 14px; - box-shadow: inset 3px 0 0 #DC2626; -} - .magicauth-admin .magicauth-action-row__main h3 { margin: 0; font-size: 13.5px; @@ -1095,27 +1087,6 @@ body > .magicauth-modal-backdrop.is-leaving { animation: magicauth-backdrop-out font-family: 'JetBrains Mono', ui-monospace, SFMono-Regular, monospace; } -.magicauth-modal-backdrop .magicauth-modal-list { - margin: 10px 0 0; - padding-left: 18px; -} -.magicauth-modal-backdrop .magicauth-modal-list li { margin: 0 0 4px; } - -.magicauth-modal-backdrop .magicauth-salt-block { - width: 100%; - margin: 10px 0; - padding: 10px 12px; - border: 1px solid #E2E5E9; - border-radius: 6px; - background: #F5F7FA; - font-family: 'JetBrains Mono', ui-monospace, SFMono-Regular, monospace; - font-size: 11.5px; - line-height: 1.5; - color: #1F2733; - resize: vertical; - white-space: pre; -} - .magicauth-modal-backdrop .magicauth-modal__foot { padding: 14px 18px; background: #F5F7FA; diff --git a/assets/js/magicauth-admin.js b/assets/js/magicauth-admin.js index 4681f4b..2eeea3f 100644 --- a/assets/js/magicauth-admin.js +++ b/assets/js/magicauth-admin.js @@ -22,7 +22,6 @@ initToastFromUrl(); initDiscardFlash(); initRecoveryActions(); - initSaltFix(); initUserProfile(); } @@ -686,118 +685,6 @@ } ); } - // Salt-fix wizard — "Fix WordPress salts" - function initSaltFix() { - var root = document.querySelector( '.magicauth-recovery' ); - if ( ! root ) { - return; - } - var btn = root.querySelector( '[data-magicauth-salt-fix]' ); - if ( ! btn ) { - return; - } - - var ajaxurl = root.getAttribute( 'data-ajaxurl' ) || ( window.ajaxurl || '' ); - var nonce = root.getAttribute( 'data-nonce' ) || ''; - - function post( mode ) { - return fetch( ajaxurl, { - method: 'POST', - credentials: 'same-origin', - headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, - body: new URLSearchParams( { action: 'magicauth_admin_fix_salts', mode: mode, _ajax_nonce: nonce } ) - } ).then( function ( res ) { return res.json(); } ); - } - - function escapeHtml( str ) { - return String( str ).replace( /[&<>"']/g, function ( c ) { - return { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[ c ]; - } ); - } - - var effects = - '

Fresh, random values will be generated for all eight WordPress security keys.

' + - ''; - - // Open the salt-fix wizard: hand the admin fresh salts to paste in. - function openManual( block ) { - var body = - effects + - '

Copy these freshly generated salts and replace the matching define() lines in wp-config.php, then save the file. Need a hand? Read the step-by-step guide.

' + - '' + - ''; - - showConfirm( { - title: 'Fix WordPress salts', - lede: 'Copy these fresh salts into wp-config.php.', - body: body, - confirmText: 'I have updated wp-config.php', - cancelText: 'Close', - onBody: function ( bodyEl ) { - var copyBtn = bodyEl.querySelector( '[data-salt-copy]' ); - var area = bodyEl.querySelector( '.magicauth-salt-block' ); - if ( ! copyBtn || ! area ) { - return; - } - copyBtn.addEventListener( 'click', function () { - area.focus(); - area.select(); - var done = function () { copyBtn.textContent = 'Copied'; setTimeout( function () { copyBtn.textContent = 'Copy to clipboard'; }, 1500 ); }; - if ( navigator.clipboard && navigator.clipboard.writeText ) { - navigator.clipboard.writeText( area.value ).then( done, function () {} ); - } else { - try { document.execCommand( 'copy' ); done(); } catch ( e ) {} - } - } ); - }, - onConfirm: function () { - return post( 'recheck' ).then( function ( payload ) { - if ( ! payload || ! payload.success ) { - showToast( { type: 'error', message: ( payload && payload.data && payload.data.message ) || 'Still detecting weak salts.' } ); - return false; // keep the modal open so they can fix and retry - } - showToast( { type: 'success', message: payload.data.message || 'Salts updated.' } ); - setTimeout( function () { window.location.reload(); }, 1500 ); - } ).catch( function () { - showToast( { type: 'error', message: 'Network error. Please try again.' } ); - return false; - } ); - } - } ); - } - - function openWizard() { - setLoading( btn, true, 'Checking…' ); - post( 'preview' ).then( function ( payload ) { - setLoading( btn, false ); - if ( ! payload || ! payload.success ) { - showToast( { type: 'error', message: ( payload && payload.data && payload.data.message ) || 'Could not start the wizard.' } ); - return; - } - openManual( payload.data.block || '' ); - } ).catch( function () { - setLoading( btn, false ); - showToast( { type: 'error', message: 'Network error. Please try again.' } ); - } ); - } - - btn.addEventListener( 'click', function ( ev ) { - ev.preventDefault(); - openWizard(); - } ); - - // Auto-open when arriving from the admin notice's "Fix it for me" button. - try { - if ( new URLSearchParams( window.location.search ).get( 'magicauth-fix-salts' ) === '1' ) { - openWizard(); - } - } catch ( e ) {} - } - // User-profile actions — surfaced through toasts function initUserProfile() { var root = document.querySelector( '.magicauth-user-fields' ); diff --git a/includes/Admin/Settings.php b/includes/Admin/Settings.php index 75a500f..6e05051 100644 --- a/includes/Admin/Settings.php +++ b/includes/Admin/Settings.php @@ -43,7 +43,6 @@ public static function setup(): void { add_action( 'admin_post_magicauth_send_test_email', [ self::class, 'handle_test_send' ] ); add_action( 'wp_ajax_magicauth_admin_revoke_all_tokens', [ self::class, 'ajax_revoke_all_tokens' ] ); add_action( 'wp_ajax_magicauth_admin_reset_throttle', [ self::class, 'ajax_reset_throttle' ] ); - add_action( 'wp_ajax_magicauth_admin_fix_salts', [ self::class, 'ajax_fix_salts' ] ); add_action( 'current_screen', [ self::class, 'suppress_foreign_notices' ] ); add_filter( 'plugin_action_links_' . plugin_basename( MAGICAUTH_FILE ), [ self::class, 'plugin_action_links' ] ); } @@ -250,20 +249,7 @@ private static function render_section_diagnostics(): void {
- - -
-
-

-

-
- -
- + data-nonce="">
@@ -823,40 +809,4 @@ public static function ajax_reset_throttle(): void { ] ); } - - /** - * AJAX: salt-fix helper ("Fix WordPress salts"). Two modes: - * - preview: return a ready-to-paste block of freshly generated salts. - * - recheck: re-evaluate the salts and clear the notice once they are strong - * (used after the admin pastes the salts into wp-config.php). - * MagicAuth never writes wp-config.php itself, and never auto-enables the - * branded login replacement. - */ - public static function ajax_fix_salts(): void { - check_ajax_referer( 'magicauth-admin-recovery' ); - if ( ! current_user_can( 'manage_options' ) ) { - wp_send_json_error( [ 'message' => __( 'You are not allowed to do this.', 'magicauth' ) ], 403 ); - } - - $mode = 'preview'; - if ( isset( $_POST['mode'] ) && is_string( $_POST['mode'] ) ) { - $mode = sanitize_key( wp_unslash( $_POST['mode'] ) ); - } - - if ( 'recheck' === $mode ) { - $result = Installer::recheck_salts_from_file(); - if ( ! $result['ok'] ) { - wp_send_json_error( [ 'message' => $result['message'] ] ); - } - wp_send_json_success( [ 'message' => $result['message'] ] ); - } - - // Default: preview. MagicAuth never edits wp-config.php; it generates a - // fresh block of salts for the admin to paste in themselves. - wp_send_json_success( - [ - 'block' => Installer::generate_salt_block(), - ] - ); - } } diff --git a/includes/Installer.php b/includes/Installer.php index 8d3796c..acfc748 100644 --- a/includes/Installer.php +++ b/includes/Installer.php @@ -13,6 +13,17 @@ final class Installer { private const SALT_NOTICE_KEY = 'magicauth_salt_notice'; + /** + * Site option set when an admin dismisses the weak-salt notice. Site-wide + * (a single option, not per-user meta), so one admin dismissing hides the + * notice for every admin. check_salts() clears it if salts ever return to + * strong, so the notice re-arms on a future regression. + */ + private const SALT_NOTICE_DISMISSED_OPTION = 'magicauth_salt_notice_dismissed'; + + /** Nonce action backing the AJAX dismissal of the weak-salt notice. */ + private const SALT_NOTICE_DISMISS_NONCE = 'magicauth_dismiss_salt_notice'; + /** The eight key/salt constants WordPress derives wp_salt() from. */ private const SALT_CONSTANTS = [ 'AUTH_KEY', @@ -169,8 +180,10 @@ private static function runtime_salts_weak(): bool { /** * Keep the weak-salt admin-notice transient in sync with runtime reality. * Runs on activation and on every admin_init, so a site whose salts are - * provided by the environment, or fixed outside the wizard, clears the notice - * on its own without a false "still weak" nag. Writes only on a state change. + * provided by the environment, or fixed by hand, clears the notice on its + * own without a false "still weak" nag. Writes only on a state change. When + * salts test strong again it also drops the site-wide dismissal so a later + * regression re-arms the notice. */ public static function check_salts(): void { $weak = self::runtime_salts_weak(); @@ -180,6 +193,12 @@ public static function check_salts(): void { } elseif ( ! $weak && $flagged ) { delete_transient( self::SALT_NOTICE_KEY ); } + + // Re-arm the dismissible notice if salts return to strong. Guarded so we + // only write on an actual state change. + if ( ! $weak && false !== get_option( self::SALT_NOTICE_DISMISSED_OPTION, false ) ) { + delete_option( self::SALT_NOTICE_DISMISSED_OPTION ); + } } /** Whether the weak-salt notice is raised. Drives the notice and the toggle warning. */ @@ -215,187 +234,74 @@ public static function render_fpm_notice(): void { 'magicauth', - 'magicauth-fix-salts' => '1', - ], - admin_url( 'options-general.php' ) - ); + $nonce = wp_create_nonce( self::SALT_NOTICE_DISMISS_NONCE ); ?> -
+

- - step-by-step guide, or generate fresh salts at api.wordpress.org/secret-key and paste them into wp-config.php.', 'magicauth' ), - esc_url( self::DOCS_SALTS_URL ), - 'https://api.wordpress.org/secret-key/1.1/salt/' - ), - [ - 'a' => [ - 'href' => true, - 'target' => true, - 'rel' => true, - ], - 'code' => [], - ] - ); - ?> +

+ - */ - private static function salt_values(): array { - $values = []; - foreach ( self::SALT_CONSTANTS as $name ) { - $values[ $name ] = self::generate_salt_value(); - } - return $values; - } - /** - * Build a ready-to-paste block of all eight define() lines with fresh salts. - * Used for the manual copy-and-paste path when wp-config.php is not writable. + * AJAX: persist a site-wide dismissal of the weak-salt notice. One admin + * dismissing hides it for every admin (a single site option, not per-user + * meta). check_salts() clears the option if salts ever return to strong, so + * the notice re-arms on a future regression. */ - public static function generate_salt_block(): string { - $lines = []; - foreach ( self::salt_values() as $name => $value ) { - $lines[] = sprintf( "define( '%s', '%s' );", $name, $value ); + public static function ajax_dismiss_salt_notice(): void { + check_ajax_referer( self::SALT_NOTICE_DISMISS_NONCE ); + if ( ! current_user_can( 'manage_options' ) ) { + wp_send_json_error( [ 'message' => __( 'You are not allowed to do this.', 'magicauth' ) ], 403 ); } - return implode( "\n", $lines ); - } - - /** - * Regex capturing a single define() with its quote style and value (group 3). - * Anchored to a line start (multiline) so commented-out lines such as - * `// define( 'AUTH_KEY', ... );` are skipped — PHP honours the first active - * define, and matching a comment instead would corrupt detection/rewrite. - */ - private static function define_pattern( string $name ): string { - return '/^[ \t]*define\(\s*([\'"])' . preg_quote( $name, '/' ) . '\1\s*,\s*([\'"])(.*?)\2\s*\)\s*;/m'; - } - - /** - * File-based weak-salt detection (operates on wp-config text, not runtime - * constants). A salt is weak if missing, empty, or placeholder. - */ - public static function config_has_weak_salts( string $contents ): bool { - foreach ( self::SALT_CONSTANTS as $name ) { - if ( ! preg_match( self::define_pattern( $name ), $contents, $matches ) ) { - return true; - } - $value = $matches[3]; - if ( '' === trim( $value ) ) { - return true; - } - foreach ( self::PLACEHOLDER_MARKERS as $marker ) { - if ( false !== stripos( $value, $marker ) ) { - return true; - } - } - } - return false; - } - - /** Resolve wp-config.php the same way wp-load.php does (ABSPATH or one level up). */ - public static function locate_wp_config(): ?string { - $candidate = ABSPATH . 'wp-config.php'; - if ( file_exists( $candidate ) ) { - return $candidate; - } - $parent = dirname( ABSPATH ) . '/wp-config.php'; - if ( file_exists( $parent ) && ! file_exists( dirname( ABSPATH ) . '/wp-settings.php' ) ) { - return $parent; - } - return null; - } - - /** WP_Filesystem in the credential-free 'direct' mode, or null if unavailable. */ - private static function filesystem(): ?\WP_Filesystem_Base { - if ( ! function_exists( 'WP_Filesystem' ) ) { - require_once ABSPATH . 'wp-admin/includes/file.php'; - } - if ( 'direct' !== get_filesystem_method() ) { - return null; - } - if ( ! WP_Filesystem() ) { - return null; - } - global $wp_filesystem; - return $wp_filesystem instanceof \WP_Filesystem_Base ? $wp_filesystem : null; - } - - /** - * Verify salts after a manual edit and clear the notice if they are now strong. - * The running AJAX request has already reloaded wp-config.php, so the runtime - * constants are authoritative and cover environment- or include-provided salts - * that never appear as a literal define(). The located wp-config.php text is a - * secondary signal only. - * - * @return array{ok:bool,message:string} - */ - public static function recheck_salts_from_file(): array { - $ok_message = __( 'Salts look good now. You can enable the branded login replacement whenever you like.', 'magicauth' ); - - // Authoritative: runtime constants reflect the current file plus any - // environment/included salts. If they are strong, the salts are fixed. - if ( ! self::runtime_salts_weak() ) { - delete_transient( self::SALT_NOTICE_KEY ); - return [ - 'ok' => true, - 'message' => $ok_message, - ]; - } - - // Secondary: scan the located wp-config.php directly, in case this request - // did not pick up the edit. - $path = self::locate_wp_config(); - if ( null !== $path ) { - $filesystem = self::filesystem(); - $contents = null !== $filesystem ? $filesystem->get_contents( $path ) : null; - if ( is_string( $contents ) && '' !== $contents && ! self::config_has_weak_salts( $contents ) ) { - delete_transient( self::SALT_NOTICE_KEY ); - return [ - 'ok' => true, - 'message' => $ok_message, - ]; - } - } - - return [ - 'ok' => false, - 'message' => __( 'Still detecting placeholder or empty salts. Make sure you replaced all eight lines and saved wp-config.php.', 'magicauth' ), - ]; + update_option( self::SALT_NOTICE_DISMISSED_OPTION, 1 ); + wp_send_json_success(); } } diff --git a/includes/Plugin.php b/includes/Plugin.php index 23d6134..92581a3 100644 --- a/includes/Plugin.php +++ b/includes/Plugin.php @@ -45,8 +45,13 @@ public function boot(): void { add_action( 'admin_notices', [ Installer::class, 'render_salt_notice' ] ); add_action( 'admin_notices', [ Installer::class, 'render_fpm_notice' ] ); + // Site-wide dismissal of the weak-salt notice. Registered here (not inside + // the is_admin() block below) so it wires for admin-ajax requests, which the + // notice's self-contained dismiss script POSTs to from any admin screen. + add_action( 'wp_ajax_magicauth_dismiss_salt_notice', [ Installer::class, 'ajax_dismiss_salt_notice' ] ); + // Re-evaluate salt strength on each admin load so the notice tracks reality - // (e.g. salts provided by the environment, or fixed by hand outside the wizard). + // (e.g. salts provided by the environment, or fixed by hand). add_action( 'admin_init', [ Installer::class, 'check_salts' ] ); Auth\Controller::setup(); diff --git a/tests/phpunit/Model/SaltFixTest.php b/tests/phpunit/Model/SaltFixTest.php deleted file mode 100644 index a508fd5..0000000 --- a/tests/phpunit/Model/SaltFixTest.php +++ /dev/null @@ -1,110 +0,0 @@ -assertSame( 64, strlen( $value ) ); - // Must be embeddable inside a single-quoted PHP literal. - $this->assertStringNotContainsString( "'", $value ); - $this->assertStringNotContainsString( '\\', $value ); - $this->assertNotSame( $value, Installer::generate_salt_value(), 'Values must be random per call.' ); - } - - public function test_salt_block_defines_all_eight_constants(): void { - $block = Installer::generate_salt_block(); - foreach ( self::CONSTANTS as $name ) { - $this->assertStringContainsString( "define( '" . $name . "',", $block ); - } - $this->assertSame( 8, substr_count( $block, 'define(' ) ); - $this->assertFalse( Installer::config_has_weak_salts( $block ), 'A freshly generated block is not weak.' ); - } - - public function test_generated_block_is_locally_random(): void { - // Local generation only: two blocks must differ (no fixed or remote source). - $this->assertNotSame( Installer::generate_salt_block(), Installer::generate_salt_block() ); - } - - public function test_placeholder_config_reads_as_weak(): void { - $this->assertTrue( Installer::config_has_weak_salts( $this->placeholder_config() ) ); - } - - public function test_empty_value_reads_as_weak(): void { - $config = "assertTrue( Installer::config_has_weak_salts( $config ) ); - } - - public function test_missing_define_reads_as_weak(): void { - // AUTH_KEY present and strong, the other seven absent. - $config = "assertTrue( Installer::config_has_weak_salts( $config ) ); - } - - public function test_strong_config_reads_as_clean(): void { - $lines = [ 'assertFalse( Installer::config_has_weak_salts( implode( "\n", $lines ) ) ); - } - - public function test_detection_ignores_commented_define_lines(): void { - // A commented, strong-looking define sits above the active placeholder - // defines. Detection must read the active defines (weak), not the comment. - $lines = [ 'assertTrue( Installer::config_has_weak_salts( implode( "\n", $lines ) ) ); - } - - public function test_detection_handles_double_quoted_defines(): void { - $weak = [ 'assertTrue( Installer::config_has_weak_salts( implode( "\n", $weak ) ) ); - $this->assertFalse( Installer::config_has_weak_salts( implode( "\n", $strong ) ) ); - } -}