| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Secrets API: Site Health integration 4 * 5 * @package WordPress 6 * @subpackage Secrets 7 * @since 7.2.0 8 */ 9 10 /** 11 * Registers the Secrets API's Site Health tests. 12 * 13 * There is no settings screen for secrets, so these tests are how an operator 14 * notices a broken key, a weak key source, or a pending rotation. 15 * 16 * @since 7.2.0 17 * 18 * @param array<string, array<string, array<string, mixed>>> $tests Existing Site Health tests. 19 * @return array<string, array<string, array<string, mixed>>> 20 */ 21 function wp_secrets_site_health_tests( $tests ) { 22 $tests['direct']['secrets_api_key_source'] = array( 23 'label' => __( 'Secrets API key source' ), 24 'test' => 'wp_secrets_site_health_test_key_source', 25 ); 26 27 $tests['direct']['secrets_api_undecryptable'] = array( 28 'label' => __( 'Secrets API: undecryptable secrets' ), 29 'test' => 'wp_secrets_site_health_test_undecryptable', 30 ); 31 32 $tests['direct']['secrets_api_needs_rotation'] = array( 33 'label' => __( 'Secrets API: credentials pending rotation' ), 34 'test' => 'wp_secrets_site_health_test_needs_rotation', 35 ); 36 37 return $tests; 38 } 39 add_filter( 'site_status_tests', 'wp_secrets_site_health_tests' ); 40 41 /** 42 * Builds the standard shape a Site Health test callback returns. 43 * 44 * @since 7.2.0 45 * 46 * @param string $test The test's own identifier, matching the key registered 47 * in wp_secrets_site_health_tests(). 48 * @param string $label Short label for the test result. 49 * @param string $status One of 'good', 'recommended', 'critical'. 50 * @param string $description HTML description, normally one or more <p> elements. 51 * @return array 52 * 53 * @phpstan-return array{test: string, label: string, status: 'good'|'recommended'|'critical', badge: array{label: string, color: string}, description: string} 54 * @phpstan-param 'good'|'recommended'|'critical' $status 55 */ 56 function wp_secrets_site_health_result( string $test, string $label, string $status, string $description ): array { 57 $colors = array( 58 'good' => 'green', 59 'recommended' => 'orange', 60 'critical' => 'red', 61 ); 62 63 return array( 64 'test' => $test, 65 'label' => $label, 66 'status' => $status, 67 'badge' => array( 68 'label' => __( 'Security' ), 69 'color' => $colors[ $status ] ?? 'blue', 70 ), 71 'description' => $description, 72 ); 73 } 74 75 /** 76 * Tests whether the site key comes from a dedicated constant or a weaker fallback. 77 * 78 * @since 7.2.0 79 * 80 * @return array 81 * 82 * @phpstan-return array{test: string, label: string, status: 'good'|'recommended'|'critical', badge: array{label: string, color: string}, description: string} 83 */ 84 function wp_secrets_site_health_test_key_source(): array { 85 $key_source = _wp_secrets_get_key_manager()->get_keyring()->get_key_source(); 86 87 if ( str_contains( $key_source, 'broken' ) ) { 88 return wp_secrets_site_health_result( 89 'secrets_api_key_source', 90 __( 'Secrets API key source is broken' ), 91 'critical', 92 sprintf( 93 '<p>%s</p>', 94 esc_html__( 'The active keyring did not load correctly. No secret can be encrypted or decrypted until this is fixed.' ) 95 ) 96 ); 97 } 98 99 if ( str_contains( $key_source, 'legacy interpretation' ) ) { 100 return wp_secrets_site_health_result( 101 'secrets_api_key_source', 102 __( 'Secrets API key should be regenerated' ), 103 'recommended', 104 sprintf( 105 '<p>%s</p>', 106 esc_html__( 'WP_SECRETS_KEY is defined, but not in the recommended base64-encoded 32-byte form. Regenerate it with `wp secret generate-key`.' ) 107 ) 108 ); 109 } 110 111 if ( str_contains( $key_source, 'LOGGED_IN_KEY' ) ) { 112 return wp_secrets_site_health_result( 113 'secrets_api_key_source', 114 __( 'Secrets API is using a fallback key' ), 115 'recommended', 116 sprintf( 117 '<p>%s</p>', 118 esc_html__( 'No dedicated WP_SECRETS_KEY constant is defined. Secrets are encrypted using a key derived from existing salts, which works but is not the recommended configuration. Define WP_SECRETS_KEY with a value from `wp secret generate-key`.' ) 119 ) 120 ); 121 } 122 123 return wp_secrets_site_health_result( 124 'secrets_api_key_source', 125 __( 'Secrets API is using a dedicated key' ), 126 'good', 127 sprintf( '<p>%s</p>', esc_html( $key_source ) ) 128 ); 129 } 130 131 /** 132 * Tests whether every stored secret still decrypts. 133 * 134 * Network secrets are included only for a super admin on a multisite install – 135 * never shown to a site administrator who is not one. 136 * 137 * @since 7.2.0 138 * 139 * @return array 140 * 141 * @phpstan-return array{test: string, label: string, status: 'good'|'recommended'|'critical', badge: array{label: string, color: string}, description: string} 142 */ 143 function wp_secrets_site_health_test_undecryptable(): array { 144 $broken = wp_secrets_site_health_find_undecryptable( false ); 145 146 if ( is_multisite() && is_super_admin() ) { 147 $broken = array_merge( $broken, wp_secrets_site_health_find_undecryptable( true ) ); 148 } 149 150 if ( empty( $broken ) ) { 151 return wp_secrets_site_health_result( 152 'secrets_api_undecryptable', 153 __( 'All secrets can be decrypted' ), 154 'good', 155 sprintf( '<p>%s</p>', esc_html__( 'Every stored secret decrypted successfully.' ) ) 156 ); 157 } 158 159 $items = ''; 160 161 foreach ( $broken as $entry ) { 162 $items .= sprintf( 163 '<li>%s (%s)</li>', 164 esc_html( $entry['name'] ), 165 sprintf( 166 /* translators: %s: A secret's last known fingerprint. */ 167 esc_html__( 'fingerprint: %s' ), 168 esc_html( $entry['fingerprint'] ) 169 ) 170 ); 171 } 172 173 return wp_secrets_site_health_result( 174 'secrets_api_undecryptable', 175 __( 'Some secrets cannot be decrypted' ), 176 'critical', 177 sprintf( 178 '<p>%s</p><ul>%s</ul><p>%s</p>', 179 esc_html__( 'The following secrets exist but could not be decrypted with the current key. There is no way to recover the original value. The credential must be re-entered.' ), 180 $items, 181 esc_html__( 'This can happen after losing WP_SECRETS_KEY, restoring a database backup without its matching key, or a failed key rotation.' ) 182 ) 183 ); 184 } 185 186 /** 187 * Finds secrets that fail to decrypt, for one scope. 188 * 189 * @since 7.2.0 190 * 191 * @param bool $network Whether to check network-scope secrets. 192 * @return array List of array( 'name' => ..., 'fingerprint' => ... ). 193 * 194 * @phpstan-return list<array{name: string, fingerprint: string}> 195 */ 196 function wp_secrets_site_health_find_undecryptable( bool $network ): array { 197 $entries = $network ? wp_list_network_secrets() : wp_list_secrets(); 198 199 if ( is_wp_error( $entries ) ) { 200 return array(); 201 } 202 203 $broken = array(); 204 205 foreach ( $entries as $entry ) { 206 $secret = $network ? wp_get_network_secret( $entry['name'] ) : wp_get_secret( $entry['name'] ); 207 208 if ( is_wp_error( $secret ) ) { 209 $broken[] = array( 210 'name' => $entry['name'], 211 'fingerprint' => $entry['fingerprint'], 212 ); 213 } 214 } 215 216 return $broken; 217 } 218 219 /** 220 * Tests whether any secrets are flagged as needing rotation. 221 * 222 * @since 7.2.0 223 * 224 * @return array 225 * 226 * @phpstan-return array{test: string, label: string, status: 'good'|'recommended'|'critical', badge: array{label: string, color: string}, description: string} 227 */ 228 function wp_secrets_site_health_test_needs_rotation(): array { 229 $count = wp_secrets_site_health_count_needing_rotation( false ); 230 231 if ( is_multisite() && is_super_admin() ) { 232 $count += wp_secrets_site_health_count_needing_rotation( true ); 233 } 234 235 if ( 0 === $count ) { 236 return wp_secrets_site_health_result( 237 'secrets_api_needs_rotation', 238 __( 'No secrets are pending rotation' ), 239 'good', 240 sprintf( '<p>%s</p>', esc_html__( 'No secret is flagged as needing rotation.' ) ) 241 ); 242 } 243 244 return wp_secrets_site_health_result( 245 'secrets_api_needs_rotation', 246 __( 'Some secrets are pending rotation' ), 247 'recommended', 248 sprintf( 249 '<p>%s</p>', 250 esc_html( 251 sprintf( 252 /* translators: %d: Number of secrets flagged for rotation. */ 253 _n( 254 '%d secret was imported from an existing option and is flagged for rotation. A credential that sat in a plain option has already been through whatever backups and replication paths that option went through; re-entering it with a new value is recommended.', 255 '%d secrets were imported from existing options and are flagged for rotation. A credential that sat in a plain option has already been through whatever backups and replication paths that option went through; re-entering each with a new value is recommended.', 256 $count 257 ), 258 $count 259 ) 260 ) 261 ) 262 ); 263 } 264 265 /** 266 * Counts secrets flagged needs_rotation, for one scope. 267 * 268 * @since 7.2.0 269 * 270 * @param bool $network Whether to check network-scope secrets. 271 * @return int 272 * 273 * @phpstan-return int<0, max> 274 */ 275 function wp_secrets_site_health_count_needing_rotation( bool $network ): int { 276 $entries = $network ? wp_list_network_secrets() : wp_list_secrets(); 277 278 if ( is_wp_error( $entries ) ) { 279 return 0; 280 } 281 282 $count = 0; 283 284 foreach ( $entries as $entry ) { 285 if ( ! empty( $entry['needs_rotation'] ) ) { 286 ++$count; 287 } 288 } 289 290 return $count; 291 } 292 293 /** 294 * Adds a Secrets API section to Site Health's debug information. 295 * 296 * Counts and class names only – no secret values, and no fingerprints. Network 297 * scope figures are included only for a super admin on a multisite install. 298 * 299 * @since 7.2.0 300 * 301 * @param array<string, array<string, mixed>> $info Existing debug information sections. 302 * @return array<string, array<string, mixed>> 303 */ 304 function wp_secrets_site_health_debug_info( $info ) { 305 $key_manager = _wp_secrets_get_key_manager(); 306 $provider = _wp_secrets_get_provider(); 307 308 $fields = array( 309 'dropin_active' => array( 310 'label' => __( 'Drop-in active' ), 311 'value' => wp_using_secrets_dropin() ? __( 'Yes' ) : __( 'No' ), 312 ), 313 'provider_class' => array( 314 'label' => __( 'Provider class' ), 315 'value' => get_class( $provider ), 316 ), 317 'protected_by' => array( 318 'label' => __( 'Secrets protected by' ), 319 'value' => $provider->get_label(), 320 ), 321 'protection_at' => array( 322 'label' => __( 'Encryption boundary' ), 323 'value' => WP_Secrets_Provider::BOUNDARY_WORDPRESS === $provider->get_protection_boundary() 324 ? __( 'WordPress' ) 325 : __( 'The provider (outside WordPress)' ), 326 ), 327 'writable' => array( 328 'label' => __( 'Accepts writes' ), 329 'value' => $provider->is_writable() ? __( 'Yes' ) : __( 'No' ), 330 ), 331 'keyring_class' => array( 332 'label' => __( 'Keyring class' ), 333 'value' => get_class( $key_manager->get_keyring() ), 334 ), 335 'key_source' => array( 336 'label' => __( 'Key source' ), 337 'value' => $key_manager->get_keyring()->get_key_source(), 338 ), 339 'record_version' => array( 340 'label' => __( 'Record format version' ), 341 'value' => (string) WP_SECRETS_RECORD_VERSION, 342 ), 343 ); 344 345 $site_secrets = wp_list_secrets(); 346 $fields['site_secret_count'] = array( 347 'label' => __( 'Site secrets' ), 348 'value' => is_wp_error( $site_secrets ) ? __( 'Unavailable' ) : (string) count( $site_secrets ), 349 ); 350 351 if ( is_multisite() && is_super_admin() ) { 352 $network_secrets = wp_list_network_secrets(); 353 $fields['network_secret_count'] = array( 354 'label' => __( 'Network secrets' ), 355 'value' => is_wp_error( $network_secrets ) ? __( 'Unavailable' ) : (string) count( $network_secrets ), 356 ); 357 } 358 359 $info['secrets-api'] = array( 360 'label' => __( 'Secrets API' ), 361 'fields' => $fields, 362 ); 363 364 return $info; 365 } 366 add_filter( 'debug_information', 'wp_secrets_site_health_debug_info' );
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Tue Nov 10 08:20:01 2026 | Cross-referenced by PHPXref |