From cc5d9ec217de7c5647480dffd84805e42228f904 Mon Sep 17 00:00:00 2001 From: Nol de Roos <108540791+nolderoos@users.noreply.github.com> Date: Fri, 22 May 2026 00:42:36 +0200 Subject: [PATCH] fix: write salts in place when wp-config dir is not writable (1.0.1) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The salt-fix auto-write created a sibling temp file in the wp-config.php directory and move()'d it over the original. That requires the *directory* to be writable, but the precheck only verified the *file* was writable — so on the common hardening where wp-config.php is writable yet its directory is locked, the temp creation failed with "Could not write the new configuration. No changes were made." Keep the atomic temp-swap as the preferred path, but fall back to an in-place put_contents() overwrite of wp-config.php when the temp can't be created. Overwriting an existing writable file needs no directory write access, and the content was already validated by the safety gate. Original file mode is preserved on both paths. Bumps version to 1.0.1. --- includes/Installer.php | 47 ++++++++++++++++++++++++++---------------- magicauth.php | 4 ++-- readme.txt | 2 +- 3 files changed, 32 insertions(+), 21 deletions(-) diff --git a/includes/Installer.php b/includes/Installer.php index 4c508bb..5bfe22e 100644 --- a/includes/Installer.php +++ b/includes/Installer.php @@ -486,28 +486,39 @@ public static function apply_salt_fix(): array { 'message' => __( 'The generated configuration failed a safety check, so nothing was written.', 'magicauth' ), ]; } - // Preserve the original file mode across the swap: move() uses rename(), - // so the destination would otherwise inherit the temp file's perms and - // silently widen a hardened wp-config.php (e.g. 0600 -> 0644). Applying - // the original mode to the temp before the move also keeps the brief - // temp-file window no more readable than the file it replaces. - $mode = $filesystem->getchmod( $path ); - - $temp = dirname( $path ) . '/.magicauth-wpconfig-' . bin2hex( random_bytes( 8 ) ) . '.tmp'; - if ( ! $filesystem->put_contents( $temp, $updated, FS_CHMOD_FILE ) ) { - return [ - 'ok' => false, - 'message' => __( 'Could not write the new configuration. No changes were made.', 'magicauth' ), - ]; + // Target mode: preserve the original file's permissions. move() uses + // rename() and put_contents() chmods to its $mode argument, either of + // which would otherwise widen a hardened wp-config.php (e.g. 0600 -> 0644). + // Fall back to the WordPress default when the mode can't be read. + $mode_str = $filesystem->getchmod( $path ); + $chmod = '' !== $mode_str ? (int) octdec( $mode_str ) : FS_CHMOD_FILE; + + // Preferred path: atomic temp-file swap. Requires a writable *directory* + // (to create the sibling temp) and also keeps the brief temp window no + // more readable than the file it replaces. + $temp = dirname( $path ) . '/.magicauth-wpconfig-' . bin2hex( random_bytes( 8 ) ) . '.tmp'; + $written = false; + if ( $filesystem->put_contents( $temp, $updated, $chmod ) ) { + if ( $filesystem->move( $temp, $path, true ) ) { + $written = true; + } else { + $filesystem->delete( $temp ); + } } - if ( '' !== $mode ) { - $filesystem->chmod( $temp, (int) octdec( $mode ) ); + + // Fallback: overwrite wp-config.php in place. Covers the common hardening + // where wp-config.php itself is writable but its directory is not, so a + // sibling temp file can't be created. Overwriting an existing writable + // file needs no directory write access; the content was already validated + // by the safety gate above, so the (non-atomic) in-place write is sound. + if ( ! $written ) { + $written = $filesystem->put_contents( $path, $updated, $chmod ); } - if ( ! $filesystem->move( $temp, $path, true ) ) { - $filesystem->delete( $temp ); + + if ( ! $written ) { return [ 'ok' => false, - 'message' => __( 'Could not replace wp-config.php. No changes were made.', 'magicauth' ), + 'message' => __( 'Could not write to wp-config.php. Use the manual copy-and-paste option instead.', 'magicauth' ), ]; } delete_transient( self::SALT_NOTICE_KEY ); diff --git a/magicauth.php b/magicauth.php index 21db5cb..e1dd000 100644 --- a/magicauth.php +++ b/magicauth.php @@ -3,7 +3,7 @@ * Plugin Name: MagicAuth * Plugin URI: https://github.com/EtticDevelopment/magicauth * Description: Passwordless WordPress sign-in via email magic link or 6-character code. - * Version: 1.0.0 + * Version: 1.0.1 * Requires at least: 6.4 * Requires PHP: 8.0 * Author: Ettic @@ -26,7 +26,7 @@ return; } -define( 'MAGICAUTH_VERSION', '1.0.0' ); +define( 'MAGICAUTH_VERSION', '1.0.1' ); define( 'MAGICAUTH_DB_VERSION', 1 ); define( 'MAGICAUTH_FILE', __FILE__ ); define( 'MAGICAUTH_DIR', plugin_dir_path( __FILE__ ) ); diff --git a/readme.txt b/readme.txt index 905f51e..109cc71 100644 --- a/readme.txt +++ b/readme.txt @@ -4,7 +4,7 @@ Tags: login, passwordless, magic link, authentication, security Requires at least: 6.4 Tested up to: 7.0 Requires PHP: 8.0 -Stable tag: 1.0.0 +Stable tag: 1.0.1 License: GPL-2.0-or-later License URI: https://www.gnu.org/licenses/gpl-2.0.html