Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 4 additions & 31 deletions assets/js/magicauth-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -720,37 +720,14 @@
'<ul class="magicauth-modal-list">' +
'<li><strong>Everyone is signed out.</strong> All sessions (including yours) end — you will sign in again right after.</li>' +
'<li><strong>Pending sign-in links and codes stop working.</strong> They were signed with the old keys, so anyone mid-sign-in requests a fresh email.</li>' +
'<li><strong>The branded login screen stays off.</strong> This only clears the block; you turn the replacement on yourself when ready.</li>' +
'<li><strong>The branded login screen stays off.</strong> Fixing salts never flips that toggle — you turn the replacement on yourself when ready.</li>' +
'</ul>';

// Open the wizard for the writable (auto-write) case.
function openAutoWrite() {
showConfirm( {
title: 'Fix WordPress salts',
lede: 'wp-config.php is writable, so MagicAuth can update it for you.',
body: effects + '<p>A backup is not left in the web root (it would expose your database credentials); the file is replaced atomically instead.</p>',
confirmText: 'Generate & write salts',
cancelText: 'Cancel',
onConfirm: function () {
return post( 'apply' ).then( function ( payload ) {
if ( ! payload || ! payload.success ) {
showToast( { type: 'error', message: ( payload && payload.data && payload.data.message ) || 'Could not write wp-config.php.' } );
return;
}
showToast( { type: 'success', message: payload.data.message || 'Salts updated. Signing you out…' } );
setTimeout( function () { window.location.reload(); }, 1800 );
} ).catch( function () {
showToast( { type: 'error', message: 'Network error. Please try again.' } );
} );
}
} );
}

// Open the wizard for the manual (copy-and-paste) case.
// Open the salt-fix wizard: hand the admin fresh salts to paste in.
function openManual( block ) {
var body =
effects +
'<p>wp-config.php is not writable from PHP on this server, so copy these lines and replace the matching salt lines in <code>wp-config.php</code>, then save it:</p>' +
'<p>Copy these freshly generated salts and replace the matching <code>define()</code> lines in <code>wp-config.php</code>, then save the file. Need a hand? <a href="' + ( root.getAttribute( 'data-docs' ) || 'https://docs.ettic.nl/docs/magicauth/weak-salts' ) + '" target="_blank" rel="noopener">Read the step-by-step guide</a>.</p>' +
'<textarea class="magicauth-salt-block" readonly rows="8">' + escapeHtml( block ) + '</textarea>' +
'<button type="button" class="magicauth-btn magicauth-btn--ghost magicauth-btn--sm" data-salt-copy>Copy to clipboard</button>';

Expand Down Expand Up @@ -801,11 +778,7 @@
showToast( { type: 'error', message: ( payload && payload.data && payload.data.message ) || 'Could not start the wizard.' } );
return;
}
if ( payload.data.writable ) {
openAutoWrite();
} else {
openManual( payload.data.block || '' );
}
openManual( payload.data.block || '' );
} ).catch( function () {
setLoading( btn, false );
showToast( { type: 'error', message: 'Network error. Please try again.' } );
Expand Down
55 changes: 23 additions & 32 deletions includes/Admin/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,13 +250,14 @@ private static function render_section_diagnostics(): void {
</header>
<div class="magicauth-card magicauth-recovery"
data-ajaxurl="<?php echo esc_url( $ajaxurl ); ?>"
data-nonce="<?php echo esc_attr( $recovery_nonce ); ?>">
data-nonce="<?php echo esc_attr( $recovery_nonce ); ?>"
data-docs="<?php echo esc_url( Installer::DOCS_SALTS_URL ); ?>">

<?php if ( Installer::has_weak_salts() ) : ?>
<div class="magicauth-action-row magicauth-action-row--alert">
<div class="magicauth-action-row__main">
<h3><?php esc_html_e( 'Fix WordPress salts', 'magicauth' ); ?></h3>
<p><?php esc_html_e( 'Your security keys hold placeholder or empty values. Generate fresh ones and write them to wp-config.php. This does not turn on the branded login screen — you stay in control of that.', 'magicauth' ); ?></p>
<p><?php esc_html_e( 'Your security keys hold placeholder or empty values. Generate fresh ones and paste them into wp-config.php yourself. This does not turn on the branded login screen — you stay in control of that.', 'magicauth' ); ?></p>
</div>
<button type="button" class="magicauth-btn magicauth-btn--primary magicauth-btn--sm" data-magicauth-salt-fix>
<?php esc_html_e( 'Fix salts…', 'magicauth' ); ?>
Expand Down Expand Up @@ -338,18 +339,20 @@ public static function sanitize( $input ): array {
$out['throttle'] = $throttle;
}

// replace_default gated by salt strength.
$wants_replace = ! empty( $input['replace_default'] );
// Branded login replacement: allowed even with weak salts (informed consent).
// Weak salts are a site-wide WordPress hardening issue, not a property of the
// branded screen, and the toggle is never re-checked against salts at runtime,
// so blocking it would not make the site safer — it only locked correctly
// configured sites out of the feature. Save the choice; warn, do not block.
$wants_replace = ! empty( $input['replace_default'] );
$out['replace_default'] = $wants_replace;
if ( $wants_replace && Installer::has_weak_salts() ) {
add_settings_error(
self::OPTION_NAME,
'magicauth_weak_salts',
__( 'Replace-default sign-in is unavailable while wp-config.php contains placeholder salts. Generate fresh salts and re-save.', 'magicauth' ),
'error'
__( 'Branded login is enabled, but your WordPress salts are still weak. Magic links each carry their own random secret so they stay safe; your session cookies and the 6-character codes are better protected once you fix the salts. See the weak-salt notice for how.', 'magicauth' ),
'warning'
);
$out['replace_default'] = false;
} else {
$out['replace_default'] = (bool) $wants_replace;
}

if ( isset( $input['company_name'] ) ) {
Expand Down Expand Up @@ -545,15 +548,15 @@ public static function field_replace_default( bool $weak_salts = false ): void {
<span class="magicauth-row__label">
<?php esc_html_e( 'Replace WordPress login screen', 'magicauth' ); ?>
<?php if ( $weak_salts ) : ?>
<button class="magicauth-tooltip-trigger" type="button" aria-label="<?php esc_attr_e( 'Why is this disabled?', 'magicauth' ); ?>">?<span class="magicauth-tooltip" role="tooltip"><strong><?php esc_html_e( 'Disabled: weak salts', 'magicauth' ); ?></strong><?php esc_html_e( 'Replace-default needs strong values in', 'magicauth' ); ?> <code style="background:rgba(255,255,255,0.06);color:#FFB070;padding:1px 5px;border-radius:3px;font-family:var(--ff-mono);font-size:11.5px;">wp-config.php</code>. <a href="https://api.wordpress.org/secret-key/1.1/salt/" target="_blank" rel="noopener"><?php esc_html_e( 'Generate fresh salts →', 'magicauth' ); ?></a></span></button>
<button class="magicauth-tooltip-trigger" type="button" aria-label="<?php esc_attr_e( 'Why is this flagged?', 'magicauth' ); ?>">!<span class="magicauth-tooltip" role="tooltip"><strong><?php esc_html_e( 'Heads up: weak salts', 'magicauth' ); ?></strong><?php esc_html_e( 'You can turn this on now, but the salts in', 'magicauth' ); ?> <code style="background:rgba(255,255,255,0.06);color:#FFB070;padding:1px 5px;border-radius:3px;font-family:var(--ff-mono);font-size:11.5px;">wp-config.php</code> <?php esc_html_e( 'are weak — fixing them first is recommended.', 'magicauth' ); ?> <a href="<?php echo esc_url( Installer::DOCS_SALTS_URL ); ?>" target="_blank" rel="noopener"><?php esc_html_e( 'How to fix →', 'magicauth' ); ?></a></span></button>
<?php endif; ?>
</span>
<p class="magicauth-row__help"><?php esc_html_e( 'Intercepts wp-login.php and serves the branded MagicAuth screen. The native form stays available at ?magicauth=off as a recovery path.', 'magicauth' ); ?></p>
</div>
<div class="magicauth-row__control">
<input type="hidden" name="<?php echo esc_attr( $name ); ?>" value="0">
<label class="magicauth-toggle">
<input class="magicauth-toggle__input" type="checkbox" name="<?php echo esc_attr( $name ); ?>" value="1" <?php checked( $value ); ?> <?php disabled( $weak_salts ); ?>>
<input class="magicauth-toggle__input" type="checkbox" name="<?php echo esc_attr( $name ); ?>" value="1" <?php checked( $value ); ?>>
<span class="magicauth-toggle__thumb"></span>
</label>
</div>
Expand Down Expand Up @@ -822,13 +825,12 @@ public static function ajax_reset_throttle(): void {
}

/**
* AJAX: salt-fix wizard ("Fix WordPress salts"). Three modes:
* - preview: report whether wp-config.php is auto-writable; if not, return a
* ready-to-paste block of fresh salts for the manual path.
* - apply: rewrite wp-config.php in place with fresh salts.
* - recheck: re-read wp-config.php and clear the notice if it is now clean
* (used after the admin pastes salts manually).
* Never auto-enables the branded login replacement — only clears the block.
* 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' );
Expand All @@ -841,15 +843,6 @@ public static function ajax_fix_salts(): void {
$mode = sanitize_key( wp_unslash( $_POST['mode'] ) );
}

if ( 'apply' === $mode ) {
$result = Installer::apply_salt_fix();
if ( ! $result['ok'] ) {
wp_send_json_error( [ 'message' => $result['message'] ] );
}
magicauth_debug_log( sprintf( 'admin recovery: fix_salts apply by user_id=%d', (int) get_current_user_id() ) );
wp_send_json_success( [ 'message' => $result['message'] ] );
}

if ( 'recheck' === $mode ) {
$result = Installer::recheck_salts_from_file();
if ( ! $result['ok'] ) {
Expand All @@ -858,13 +851,11 @@ public static function ajax_fix_salts(): void {
wp_send_json_success( [ 'message' => $result['message'] ] );
}

// Default: preview.
$writable = Installer::salt_autofix_available();
// 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(
[
'writable' => $writable,
// Only hand the browser fresh salts when it actually needs them to paste.
'block' => $writable ? '' : Installer::generate_salt_block(),
'block' => Installer::generate_salt_block(),
]
);
}
Expand Down
Loading
Loading