Skip to content

Commit 41403fd

Browse files
arraypressclaude
andcommitted
Derive the key the way core derives its salts when the constants are absent
A wp-config.php without salt constants is one core copes with: wp_salt() generates salts once and keeps them in the database. Every WordPress test suite runs that way, and so do more live sites than anyone would like. Treating the absent constants as no key made encryption unavailable there, and a field marked encrypted then stored nothing -- which is how a Stripe key saved as an empty string. The constants are still preferred, so a value encrypted under them stays readable; only when there are none does wp_salt() stand in. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015Kwx2STJ4EaVaJofEJJ9Zo
1 parent 576f19d commit 41403fd

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

‎src/Context/EncryptedContext.php‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,15 @@ private static function key(): string {
313313
);
314314
}
315315

316+
// No constants at all is how core itself runs when wp-config.php
317+
// leaves them out, and how every test suite runs: wp_salt() then
318+
// generates salts once and keeps them in the database. Used only
319+
// when the constants are absent, so a value encrypted under them
320+
// stays readable on a site that has them.
321+
if ( '' === $salt && function_exists( 'wp_salt' ) ) {
322+
$salt = wp_salt( 'auth' ) . wp_salt( 'secure_auth' ) . wp_salt( 'logged_in' );
323+
}
324+
316325
return '' === $salt ? '' : hash( 'sha256', $salt, true );
317326
}
318327

‎tests/stubs.php‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,17 @@ function register_rest_route( $namespace, $route, $args = [], $override = false
476476
}
477477
}
478478

479+
if ( ! function_exists( 'wp_salt' ) ) {
480+
/*
481+
* Deterministic within a run, like core's, so the encryption tests can
482+
* round-trip without salt constants in the bootstrap -- which is also
483+
* how the WordPress test suite itself runs.
484+
*/
485+
function wp_salt( $scheme = 'auth' ) {
486+
return 'fk-test-salt-' . $scheme;
487+
}
488+
}
489+
479490
if ( ! function_exists( 'is_user_logged_in' ) ) {
480491
function is_user_logged_in() {
481492
return true;

0 commit comments

Comments
 (0)