| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Secrets API 4 * 5 * @package WordPress 6 * @subpackage Secrets 7 * @since 7.2.0 8 */ 9 10 /** 11 * The store or key backend is misbehaving. 12 * 13 * The secret exists and was decrypted, but the backend it came from failed in a way 14 * none of the more specific codes below describe. 15 * 16 * @since 7.2.0 17 */ 18 define( 'WP_SECRETS_ERROR_DECRYPTION_FAILED', 'secret_decryption_failed' ); 19 20 /** 21 * The key needed to encrypt or decrypt a secret could not be obtained. 22 * 23 * The keyring is unreachable, misconfigured, or refusing to answer. 24 * 25 * @since 7.2.0 26 */ 27 define( 'WP_SECRETS_ERROR_KEY_UNAVAILABLE', 'secret_key_unavailable' ); 28 29 /** 30 * The storage back end could not be reached at all. 31 * 32 * @since 7.2.0 33 */ 34 define( 'WP_SECRETS_ERROR_STORE_UNAVAILABLE', 'secret_store_unavailable' ); 35 36 /** 37 * A secret name failed wp_secrets_validate_name(). 38 * 39 * @since 7.2.0 40 */ 41 define( 'WP_SECRETS_ERROR_INVALID_NAME', 'secret_invalid_name' ); 42 43 /** 44 * A secret value was not a string. 45 * 46 * @since 7.2.0 47 */ 48 define( 'WP_SECRETS_ERROR_INVALID_VALUE', 'secret_invalid_value' ); 49 50 /** 51 * A caller passed an argument this API cannot act on. 52 * 53 * An unrecognized version constant, or an invalid scope, slot, or site ID. 54 * 55 * Distinct from the codes above in cause rather than in severity: those describe 56 * a runtime condition a correct caller can still hit (a name that failed 57 * validation, an unavailable key), while this one only ever means the calling 58 * code is wrong. Every site that returns it also calls _doing_it_wrong(), so the 59 * mistake is visible in development rather than only in a return value the caller 60 * may not be checking. 61 * 62 * @since 7.2.0 63 */ 64 define( 'WP_SECRETS_ERROR_INVALID_ARGUMENT', 'secret_invalid_argument' ); 65 66 /** 67 * The cipher this API depends on is unavailable. 68 * 69 * Neither libsodium nor its sodium_compat fallback is present in this environment. 70 * 71 * @since 7.2.0 72 */ 73 define( 'WP_SECRETS_ERROR_CRYPTO_UNAVAILABLE', 'secret_crypto_unavailable' ); 74 75 /** 76 * A stored record could not be parsed as a secret record at all. 77 * 78 * Distinct from decrypting incorrectly, which is WP_SECRETS_ERROR_DECRYPTION_FAILED. 79 * 80 * @since 7.2.0 81 */ 82 define( 'WP_SECRETS_ERROR_RECORD_MALFORMED', 'secret_record_malformed' ); 83 84 /** 85 * A stored record parses, but its format version is not one this API understands. 86 * 87 * Distinct from WP_SECRETS_ERROR_RECORD_MALFORMED so that an operator, or Site 88 * Health, can tell a corrupt record apart from one written by a newer version of 89 * WordPress than is currently running. The upgrade path for a future record format 90 * is not designed, so an unrecognized version is rejected outright rather than 91 * guessed at. 92 * 93 * @since 7.2.0 94 */ 95 define( 'WP_SECRETS_ERROR_RECORD_UNSUPPORTED_VERSION', 'secret_record_unsupported_version' ); 96 97 /** 98 * The active provider does not accept writes. 99 * 100 * Its credentials are managed by host tooling, a control panel, or a key policy 101 * outside WordPress. Distinct from WP_SECRETS_ERROR_STORE_UNAVAILABLE, which means 102 * the write could have succeeded and did not. This one means it was never possible. 103 * 104 * @since 7.2.0 105 */ 106 define( 'WP_SECRETS_ERROR_PROVIDER_READ_ONLY', 'secret_provider_read_only' ); 107 108 /** 109 * The current record format version. 110 * 111 * Bumped only if the record shape in WP_Secrets_Cipher's slot arrays ever changes 112 * incompatibly. 113 * 114 * @since 7.2.0 115 */ 116 define( 'WP_SECRETS_RECORD_VERSION', 1 ); 117 118 /** 119 * The longest a secret name may be. 120 * 121 * The wp_options.option_name column allows 191 characters. The longest prefix this 122 * API stores under is '_wp_network_secret_' (19 characters), so 191 - 19 = 172 123 * is the longest name any store built on core's options tables can accept 124 * without truncation, and every store is held to the same limit for 125 * consistency regardless of what a specific backend could technically fit. 126 * 127 * @since 7.2.0 128 */ 129 define( 'WP_SECRETS_MAX_NAME_LENGTH', 172 ); 130 131 /** 132 * Capability required to manage site-scope secrets at the operator boundary. 133 * 134 * Checked by WP-CLI and by a future admin screen, never by the function-level API 135 * itself. See wp_set_secret() for why. 136 * 137 * @since 7.2.0 138 */ 139 define( 'WP_SECRETS_CAP_MANAGE', 'manage_secrets' ); 140 141 /** 142 * Capability required at the operator boundary to manage network-scope secrets. 143 * 144 * @since 7.2.0 145 */ 146 define( 'WP_SECRETS_CAP_MANAGE_NETWORK', 'manage_network_secrets' ); 147 148 /** 149 * Best-effort clearing of a plaintext value from memory. 150 * 151 * This is hygiene, not a guarantee. PHP strings are reference-counted and often 152 * shared by copy-on-write; sodium_memzero() can only scrub the bytes of a string it 153 * exclusively owns; if another reference to the same value exists elsewhere, 154 * PHP forces a private copy before zeroing rather than corrupting the shared buffer, 155 * and that other reference is left completely untouched. Call this on every plaintext 156 * local as soon as it is no longer needed, and do not keep incidental copies around. 157 * 158 * Under sodium_compat, core's documented fallback when the libsodium extension is 159 * unavailable, this scrubs nothing. A userland polyfill cannot reach a PHP string's 160 * underlying memory at all. Overwriting the local binding is the only thing available 161 * in that case, and it removes the live reference from this scope without touching 162 * the memory the string used to occupy. 163 * 164 * @since 7.2.0 165 * 166 * @param string $value The value to clear, by reference. 167 */ 168 function wp_secrets_memzero( &$value ): void { 169 if ( ! is_string( $value ) ) { 170 return; 171 } 172 173 if ( function_exists( 'sodium_memzero' ) ) { 174 try { 175 /* 176 * sodium_memzero() is called through a true reference alias, not 177 * directly on $value, so that a static analyzer's stub for 178 * sodium_memzero() (whose own by-ref parameter is documented as 179 * becoming null) is evaluated against a local variable rather than 180 * against this function's own by-ref parameter. The alias is not a 181 * copy: `=&` shares the identical underlying storage, so this still 182 * zeroes the real buffer the caller holds. 183 */ 184 $alias =& $value; 185 sodium_memzero( $alias ); 186 } catch ( \Throwable $e ) { 187 /* 188 * A value this function doesn't exclusively own (a shared, 189 * copy-on-write buffer) can legitimately refuse. The unconditional 190 * reassignment below still runs regardless of what happened here. 191 */ 192 unset( $e ); 193 } 194 } 195 196 /* 197 * Reached on every path, including sodium_memzero() success (it leaves the 198 * variable null, not an empty string) so callers see one consistent contract. 199 */ 200 $value = ''; 201 } 202 203 /** 204 * Validates a secret's name. 205 * 206 * Names take the form 'plugin-slug/secret-name': lowercase alphanumerics, 207 * hyphens, and underscores in each segment, exactly one '/' separating them, 208 * and no segment starting or ending with a hyphen or underscore. 209 * 210 * Namespacing is a matter of organization, not security. It groups secrets by owner 211 * so that listings and admin screens can be sensible. It confers no isolation: any 212 * plugin that can run PHP can read any secret, namespaced or not. 213 * 214 * An unnamespaced name ('secret-name', no '/') is accepted, but reports through 215 * _doing_it_wrong(). It exists for one reason: existing code may already use a flat 216 * keyspace, and refusing those names outright would mean every such call site has 217 * to be rewritten before it can be ported at all. 218 * Accepting them keeps that migration incremental. Nothing else should use one. 219 * Two plugins that both choose 'api-key' collide silently, which is a real problem 220 * even though it is not a security one. 221 * 222 * @since 7.2.0 223 * 224 * @param string $name Candidate secret name. 225 * @return true|WP_Error True if $name is usable, including the unnamespaced form. 226 * Otherwise WP_Error with code WP_SECRETS_ERROR_INVALID_NAME. 227 */ 228 function wp_secrets_validate_name( string $name ) { 229 if ( '' === $name ) { 230 return new WP_Error( 231 WP_SECRETS_ERROR_INVALID_NAME, 232 __( 'Secret names must not be empty.' ) 233 ); 234 } 235 236 if ( strlen( $name ) > WP_SECRETS_MAX_NAME_LENGTH ) { 237 return new WP_Error( 238 WP_SECRETS_ERROR_INVALID_NAME, 239 sprintf( 240 /* translators: %d: Maximum allowed length, in characters. */ 241 __( 'Secret names must be %d characters or fewer.' ), 242 WP_SECRETS_MAX_NAME_LENGTH 243 ) 244 ); 245 } 246 247 $slashes = substr_count( $name, '/' ); 248 249 if ( $slashes > 1 ) { 250 return new WP_Error( 251 WP_SECRETS_ERROR_INVALID_NAME, 252 __( 'Secret names may contain at most one "/", separating a namespace from a key.' ) 253 ); 254 } 255 256 $segment_pattern = '/^[a-z0-9]([a-z0-9_-]*[a-z0-9])?\z/'; 257 258 if ( 0 === $slashes ) { 259 if ( ! preg_match( $segment_pattern, $name ) ) { 260 return new WP_Error( 261 WP_SECRETS_ERROR_INVALID_NAME, 262 __( 'Secret names may contain only lowercase letters, numbers, hyphens, and underscores, and must not start or end with a hyphen or underscore.' ) 263 ); 264 } 265 266 /* 267 * Reported here rather than at each public entry point: this is the one 268 * place that decides what a name is, so a caller cannot reach the store 269 * with an unnamespaced name without passing through it. 270 */ 271 _doing_it_wrong( 272 __FUNCTION__, 273 sprintf( 274 /* translators: %s: The unnamespaced secret name. */ 275 __( 'The secret name "%s" has no namespace. Namespaced names ("plugin-slug/secret-name") group secrets by owner so that listings and admin screens can be organized; unnamespaced names are supported only so that existing code using flat names can be ported incrementally.' ), 276 $name 277 ), 278 '7.2.0' 279 ); 280 281 return true; 282 } 283 284 $segments = explode( '/', $name ); 285 $namespace = $segments[0]; 286 $key = $segments[1]; 287 288 if ( '' === $namespace || '' === $key ) { 289 return new WP_Error( 290 WP_SECRETS_ERROR_INVALID_NAME, 291 __( 'Both segments of a secret name must be non-empty.' ) 292 ); 293 } 294 295 if ( ! preg_match( $segment_pattern, $namespace ) || ! preg_match( $segment_pattern, $key ) ) { 296 return new WP_Error( 297 WP_SECRETS_ERROR_INVALID_NAME, 298 __( 'Secret name segments may contain only lowercase letters, numbers, hyphens, and underscores, and must not start or end with a hyphen or underscore.' ) 299 ); 300 } 301 302 return true; 303 } 304 305 /** 306 * Returns the active secret store. 307 * 308 * No filter is applied here, or anywhere on the retrieval path. A secrets.php 309 * drop-in overrides the store by setting $GLOBALS['wp_secrets_store'] to an 310 * instance before this is first called, and that global is checked here directly, 311 * not through a hook. 312 * 313 * If the global was set but is not a valid WP_Secrets_Store, or if the drop-in 314 * itself failed to load cleanly, this returns WP_Secrets_Broken_Store rather than 315 * silently using the default: the drop-in's presence signals the operator wants 316 * storage other than local options, and falling back to local options anyway would 317 * be the exact silent downgrade this API refuses to make. 318 * 319 * @since 7.2.0 320 * @access private 321 * 322 * @global bool $wp_secrets_dropin_broken Whether the secrets.php drop-in failed to load cleanly. 323 * @global WP_Secrets_Store $wp_secrets_store Optional. Store override set by the drop-in. 324 * 325 * @return WP_Secrets_Store 326 */ 327 function _wp_secrets_get_store(): WP_Secrets_Store { 328 static $store = null; 329 330 if ( $store instanceof WP_Secrets_Store ) { 331 return $store; 332 } 333 334 if ( ! empty( $GLOBALS['wp_secrets_dropin_broken'] ) ) { 335 $store = new WP_Secrets_Broken_Store(); 336 337 return $store; 338 } 339 340 if ( isset( $GLOBALS['wp_secrets_store'] ) && $GLOBALS['wp_secrets_store'] instanceof WP_Secrets_Store ) { 341 $store = $GLOBALS['wp_secrets_store']; 342 343 return $store; 344 } 345 346 $store = new WP_Secrets_Option_Store(); 347 348 return $store; 349 } 350 351 /** 352 * Returns the active key manager. 353 * 354 * A secrets.php drop-in overrides the keyring by setting 355 * $GLOBALS['wp_secrets_keyring'] to an instance before this is first called. See 356 * _wp_secrets_get_store() for why an invalid override or a failed drop-in load 357 * fails closed (WP_Secrets_Broken_Keyring) rather than falling back to the default. 358 * 359 * @since 7.2.0 360 * @access private 361 * 362 * @global bool $wp_secrets_dropin_broken Whether the secrets.php drop-in failed to load cleanly. 363 * @global WP_Secrets_Keyring $wp_secrets_keyring Optional. Keyring override set by the drop-in. 364 * 365 * @return WP_Secrets_Key_Manager 366 */ 367 function _wp_secrets_get_key_manager(): WP_Secrets_Key_Manager { 368 static $key_manager = null; 369 370 if ( $key_manager instanceof WP_Secrets_Key_Manager ) { 371 return $key_manager; 372 } 373 374 if ( ! empty( $GLOBALS['wp_secrets_dropin_broken'] ) ) { 375 $key_manager = new WP_Secrets_Key_Manager( new WP_Secrets_Broken_Keyring() ); 376 377 return $key_manager; 378 } 379 380 $keyring = null; 381 382 if ( isset( $GLOBALS['wp_secrets_keyring'] ) && $GLOBALS['wp_secrets_keyring'] instanceof WP_Secrets_Keyring ) { 383 $keyring = $GLOBALS['wp_secrets_keyring']; 384 } 385 386 $key_manager = new WP_Secrets_Key_Manager( $keyring ); 387 388 return $key_manager; 389 } 390 391 /** 392 * Whether a secrets.php drop-in is present and was loaded. 393 * 394 * True whether or not the drop-in successfully provided a store or keyring 395 * override. It reports presence rather than health; Site Health reports separately 396 * on whether a loaded drop-in is actually working. 397 * 398 * @since 7.2.0 399 * 400 * @global bool $wp_secrets_dropin_loaded Whether a secrets.php drop-in file was found. 401 * 402 * @return bool 403 */ 404 function wp_using_secrets_dropin(): bool { 405 return ! empty( $GLOBALS['wp_secrets_dropin_loaded'] ); 406 } 407 408 /** 409 * Encrypts and stores a secret. 410 * 411 * Encryption is unconditional: there is no plaintext mode and no constant to 412 * disable it. No capability check is applied here, because this must be callable from 413 * cron, REST, and front-end requests where no user is logged in. Enforce 414 * capabilities at the operator boundary (CLI, an admin screen) instead. 415 * 416 * @since 7.2.0 417 * 418 * @param string $name The secret's namespaced name ('plugin-slug/secret-name'). 419 * @param string $value The plaintext value to store. 420 * @return true|WP_Error 421 */ 422 function wp_set_secret( 423 string $name, 424 #[\SensitiveParameter] 425 $value 426 ) { 427 return _wp_secrets_set( $name, $value, false ); 428 } 429 430 /** 431 * Imports an existing option's value as a secret. 432 * 433 * For an explicit migration on the caller's own schedule, because core cannot 434 * reliably tell which options are credentials, and guessing would break sites. The 435 * source option is left untouched: this reads it, it does not move or delete it. 436 * 437 * The imported secret is flagged needs_rotation, unconditionally. A credential that 438 * sat in a plain option has already been through however many backups and 439 * replication paths that option went through; re-encrypting it here does not undo 440 * that, and the flag exists so an operator (or a future admin screen) knows to 441 * actually rotate the value rather than considering the migration finished. 442 * 443 * @since 7.2.0 444 * 445 * @param string $option The existing option's name. 446 * @param string $name The secret's namespaced name to store it under. 447 * @return true|WP_Error 448 */ 449 function wp_import_option_as_secret( string $option, string $name ) { 450 if ( '' === $option ) { 451 return new WP_Error( 452 WP_SECRETS_ERROR_INVALID_VALUE, 453 __( 'The option name must not be empty.' ) 454 ); 455 } 456 457 $value = get_option( $option, null ); 458 459 if ( null === $value ) { 460 return new WP_Error( 461 WP_SECRETS_ERROR_INVALID_VALUE, 462 __( 'The option does not exist.' ) 463 ); 464 } 465 466 if ( ! is_string( $value ) ) { 467 return new WP_Error( 468 WP_SECRETS_ERROR_INVALID_VALUE, 469 __( 'The option value is not a string and cannot be imported as a secret.' ) 470 ); 471 } 472 473 return _wp_secrets_set( $name, $value, false, true, 'imported' ); 474 } 475 476 /** 477 * Retrieves a secret. 478 * 479 * Three states, never collapsed: a WP_Secret if it exists and decrypts, null if it 480 * does not exist, WP_Error if it exists but could not be retrieved. No capability 481 * check is applied here. See wp_set_secret(). 482 * 483 * @since 7.2.0 484 * 485 * @param string $name The secret's namespaced name. 486 * @param string $version A WP_Secret_Version constant. Default WP_Secret_Version::CURRENT. 487 * @return WP_Secret|null|WP_Error 488 * 489 * @phpstan-param WP_Secret_Version::CURRENT|WP_Secret_Version::PREVIOUS $version 490 */ 491 function wp_get_secret( string $name, string $version = WP_Secret_Version::CURRENT ) { 492 return _wp_secrets_get( $name, $version, false ); 493 } 494 495 /** 496 * Deletes a secret. 497 * 498 * @since 7.2.0 499 * 500 * @param string $name The secret's namespaced name. 501 * @return true|WP_Error 502 */ 503 function wp_delete_secret( string $name ) { 504 return _wp_secrets_delete( $name, false ); 505 } 506 507 /** 508 * Clears a secret's previous version, retiring it for good. 509 * 510 * An explicit operator action, with no timers and no cron. Calling this on a secret 511 * that has never been rotated, or that does not exist, is a successful no-op: 512 * the previous slot is already absent either way. 513 * 514 * @since 7.2.0 515 * 516 * @param string $name The secret's namespaced name. 517 * @return true|WP_Error 518 */ 519 function wp_retire_secret_version( string $name ) { 520 return _wp_secrets_retire( $name, false ); 521 } 522 523 /** 524 * Lists secrets by name and metadata. Never a value. 525 * 526 * @since 7.2.0 527 * 528 * @param string $namespace Only secrets whose name starts with "{$namespace}/" are 529 * returned. Default '' returns every secret. 530 * @return array|WP_Error Array of associative arrays, each with keys 'name', 531 * 'fingerprint', 'created', 'has_previous', and 532 * 'needs_rotation'. 533 * 534 * @phpstan-return list<array{name: string, fingerprint: string, created: int, has_previous: bool, needs_rotation: bool}>|WP_Error 535 */ 536 function wp_list_secrets( string $namespace = '' ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.namespaceFound -- $namespace is the public parameter name; renaming it would break named-argument callers. 537 return _wp_secrets_list( $namespace, false ); 538 } 539 540 /** 541 * Encrypts and stores a network-scope secret. 542 * 543 * Site secrets and network secrets are separate functions with separate 544 * capabilities and separate storage prefixes, with no implicit fallback from one 545 * scope to the other. 546 * 547 * @since 7.2.0 548 * 549 * @param string $name The secret's namespaced name. 550 * @param string $value The plaintext value to store. 551 * @return true|WP_Error 552 */ 553 function wp_set_network_secret( 554 string $name, 555 #[\SensitiveParameter] 556 $value 557 ) { 558 return _wp_secrets_set( $name, $value, true ); 559 } 560 561 /** 562 * Retrieves a network-scope secret. 563 * 564 * @since 7.2.0 565 * 566 * @param string $name The secret's namespaced name. 567 * @param string $version A WP_Secret_Version constant. Default WP_Secret_Version::CURRENT. 568 * @return WP_Secret|null|WP_Error 569 * 570 * @phpstan-param WP_Secret_Version::CURRENT|WP_Secret_Version::PREVIOUS $version 571 */ 572 function wp_get_network_secret( string $name, string $version = WP_Secret_Version::CURRENT ) { 573 return _wp_secrets_get( $name, $version, true ); 574 } 575 576 /** 577 * Deletes a network-scope secret. 578 * 579 * @since 7.2.0 580 * 581 * @param string $name The secret's namespaced name. 582 * @return true|WP_Error 583 */ 584 function wp_delete_network_secret( string $name ) { 585 return _wp_secrets_delete( $name, true ); 586 } 587 588 /** 589 * Clears a network-scope secret's previous version. 590 * 591 * @since 7.2.0 592 * 593 * @param string $name The secret's namespaced name. 594 * @return true|WP_Error 595 */ 596 function wp_retire_network_secret_version( string $name ) { 597 return _wp_secrets_retire( $name, true ); 598 } 599 600 /** 601 * Lists network-scope secrets by name and metadata. Never a value. 602 * 603 * @since 7.2.0 604 * 605 * @param string $namespace Only secrets whose name starts with "{$namespace}/" are 606 * returned. Default '' returns every network-scope secret. 607 * @return array|WP_Error 608 * 609 * @phpstan-return list<array{name: string, fingerprint: string, created: int, has_previous: bool, needs_rotation: bool}>|WP_Error 610 */ 611 function wp_list_network_secrets( string $namespace = '' ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.namespaceFound -- matches wp_list_secrets()'s own signature. 612 return _wp_secrets_list( $namespace, true ); 613 } 614 615 /** 616 * Returns the active provider. 617 * 618 * The provider is whatever is responsible for holding, protecting, and answering for 619 * this site's secrets. 620 * 621 * A secrets.php drop-in overrides it by setting $GLOBALS['wp_secrets_provider'] to 622 * an instance before this is first called. Absent that, the provider WordPress 623 * ships is assembled from the active store and keyring, which a drop-in can also 624 * replace individually, so wanting host-managed key custody with default storage needs 625 * no provider at all. 626 * 627 * No filter is applied here, or anywhere on the retrieval path. Substitution is by 628 * replacement, never by interception: a filter that can observe which provider 629 * answers is one step from a filter that can answer instead. 630 * 631 * Cached for the request. A failed drop-in fails closed, returning WP_Error from 632 * every operation, rather than silently reverting to the default, because a 633 * misconfigured credential backend must never look like a working one that happens 634 * to be empty. 635 * 636 * @since 7.2.0 637 * @access private 638 * 639 * @global bool $wp_secrets_dropin_broken Whether the secrets.php drop-in failed to load cleanly. 640 * @global WP_Secrets_Provider $wp_secrets_provider Optional. Provider override set by the drop-in. 641 * 642 * @return WP_Secrets_Provider 643 */ 644 function _wp_secrets_get_provider(): WP_Secrets_Provider { 645 static $provider = null; 646 647 if ( $provider instanceof WP_Secrets_Provider ) { 648 return $provider; 649 } 650 651 if ( ! empty( $GLOBALS['wp_secrets_dropin_broken'] ) ) { 652 $provider = new WP_Secrets_Broken_Provider(); 653 654 return $provider; 655 } 656 657 if ( isset( $GLOBALS['wp_secrets_provider'] ) ) { 658 /* 659 * Set, but not a provider. Failing closed here rather than falling through to 660 * the default is the whole point: a platform drop-in that names its class 661 * wrongly would otherwise have every secret served from wp_options, where none 662 * of them exist, and a site whose credentials are held elsewhere would report 663 * them absent rather than unreachable. 664 */ 665 if ( ! ( $GLOBALS['wp_secrets_provider'] instanceof WP_Secrets_Provider ) ) { 666 $provider = new WP_Secrets_Broken_Provider(); 667 668 return $provider; 669 } 670 671 $provider = $GLOBALS['wp_secrets_provider']; 672 673 return $provider; 674 } 675 676 $provider = new WP_Secrets_Libsodium_Provider( 677 _wp_secrets_get_store(), 678 _wp_secrets_get_key_manager() 679 ); 680 681 return $provider; 682 } 683 684 /** 685 * Whether the active provider accepts writes. 686 * 687 * Exists so a settings screen can disable its own save control before an operator 688 * types a credential into a field that will only reject it. A provider whose 689 * credentials are managed by host tooling or a control panel reports false. 690 * 691 * @since 7.2.0 692 * 693 * @return bool 694 */ 695 function wp_secrets_provider_is_writable(): bool { 696 return _wp_secrets_get_provider()->is_writable(); 697 } 698 699 /** 700 * Whether the Secrets API can store and read secrets on this site. 701 * 702 * A plugin can check this before it moves a credential into the Secrets API, and 703 * keep its existing storage while it returns false: 704 * 705 * if ( wp_is_secrets_api_available() ) { 706 * // Move the credential, then use wp_set_secret() and wp_get_secret(). 707 * } else { 708 * // Keep using the existing storage. 709 * } 710 * 711 * Returns false when the secrets.php drop-in failed to load, when the default 712 * keyring has no usable WP_SECRETS_KEY, or when WP_SECRETS_KEY cannot unwrap the 713 * stored root key. The last happens when a site that already stored secrets adds 714 * or changes the key without re-wrapping the root key. 715 * 716 * A provider or keyring from a working drop-in manages its own keys, so it is 717 * reported as available. Its backend can still be unreachable on a given request, 718 * which each call reports as a WP_Error. 719 * 720 * Whether the provider accepts writes is a separate question. See 721 * wp_secrets_provider_is_writable(). 722 * 723 * @since 7.2.0 724 * 725 * @global WP_Secrets_Provider $wp_secrets_provider Optional. Provider override set by the drop-in. 726 * 727 * @return bool 728 */ 729 function wp_is_secrets_api_available(): bool { 730 if ( _wp_secrets_get_provider() instanceof WP_Secrets_Broken_Provider ) { 731 return false; 732 } 733 734 if ( isset( $GLOBALS['wp_secrets_provider'] ) ) { 735 return true; 736 } 737 738 $keyring = _wp_secrets_get_key_manager()->get_keyring(); 739 740 if ( $keyring instanceof WP_Secrets_Broken_Keyring ) { 741 return false; 742 } 743 744 if ( $keyring instanceof WP_Secrets_Config_Key_Provider ) { 745 $is_usable = in_array( 746 $keyring->get_key_status(), 747 array( WP_Secrets_Config_Key_Provider::KEY_STATUS_BASE64, WP_Secrets_Config_Key_Provider::KEY_STATUS_LEGACY ), 748 true 749 ); 750 751 if ( ! $is_usable ) { 752 return false; 753 } 754 755 $root_key = _wp_secrets_get_key_manager()->get_root_key( false ); 756 757 if ( ! is_wp_error( $root_key ) ) { 758 wp_secrets_memzero( $root_key ); 759 760 return true; 761 } 762 763 // With no root key stored yet, the first write creates one under this key. 764 return false === get_site_option( WP_Secrets_Key_Manager::ROOT_KEY_OPTION ); 765 } 766 767 return true; 768 } 769 770 /** 771 * A human-readable description of what is protecting this site's secrets. 772 * 773 * For Site Health and a future admin screen. Never key material, never a value. 774 * 775 * @since 7.2.0 776 * 777 * @return string 778 */ 779 function wp_secrets_provider_label(): string { 780 return _wp_secrets_get_provider()->get_label(); 781 } 782 783 /** 784 * Shared implementation behind wp_set_secret() and wp_set_network_secret(). 785 * 786 * @since 7.2.0 787 * @access private 788 * 789 * @param string $name The secret's name. 790 * @param string $value The plaintext value. 791 * @param bool $network Whether this is a network-scope secret. 792 * @param bool $needs_rotation Flag the stored secret as needing rotation. 793 * @param string|null $action_override Overrides the $action reported to 794 * wp_secret_changed. 795 * @return true|WP_Error 796 */ 797 function _wp_secrets_set( 798 string $name, 799 #[\SensitiveParameter] 800 $value, 801 bool $network, 802 bool $needs_rotation = false, 803 ?string $action_override = null 804 ) { 805 return _wp_secrets_get_provider()->set( $name, $value, $network, $needs_rotation, $action_override ); 806 } 807 808 /** 809 * Shared implementation behind wp_get_secret() and wp_get_network_secret(). 810 * 811 * @since 7.2.0 812 * @access private 813 * 814 * @param string $name The secret's name. 815 * @param string $version A WP_Secret_Version constant. 816 * @param bool $network Whether this is a network-scope secret. 817 * @return WP_Secret|null|WP_Error 818 * 819 * @phpstan-param WP_Secret_Version::CURRENT|WP_Secret_Version::PREVIOUS $version 820 */ 821 function _wp_secrets_get( string $name, string $version, bool $network ) { 822 /* 823 * Checked here rather than inside the provider: this is a contract between 824 * the API and its caller, and asking every provider to re-validate a core 825 * constant would be duplicated work that a third-party provider could 826 * silently skip. 827 */ 828 if ( ! in_array( $version, array( WP_Secret_Version::CURRENT, WP_Secret_Version::PREVIOUS ), true ) ) { 829 _doing_it_wrong( 830 __FUNCTION__, 831 __( 'The version must be WP_Secret_Version::CURRENT or WP_Secret_Version::PREVIOUS.' ), 832 '7.2.0' 833 ); 834 835 return new WP_Error( 836 WP_SECRETS_ERROR_INVALID_ARGUMENT, 837 __( 'The version must be WP_Secret_Version::CURRENT or WP_Secret_Version::PREVIOUS.' ) 838 ); 839 } 840 841 return _wp_secrets_get_provider()->get( $name, $version, $network ); 842 } 843 844 /** 845 * Shared implementation behind wp_delete_secret() and wp_delete_network_secret(). 846 * 847 * @since 7.2.0 848 * @access private 849 * 850 * @param string $name The secret's name. 851 * @param bool $network Whether this is a network-scope secret. 852 * @return true|WP_Error 853 */ 854 function _wp_secrets_delete( string $name, bool $network ) { 855 return _wp_secrets_get_provider()->delete( $name, $network ); 856 } 857 858 /** 859 * Shared implementation behind wp_retire_secret_version() and its network twin. 860 * 861 * @since 7.2.0 862 * @access private 863 * 864 * @param string $name The secret's name. 865 * @param bool $network Whether this is a network-scope secret. 866 * @return true|WP_Error 867 */ 868 function _wp_secrets_retire( string $name, bool $network ) { 869 return _wp_secrets_get_provider()->retire_previous( $name, $network ); 870 } 871 872 /** 873 * Shared implementation behind wp_list_secrets() and wp_list_network_secrets(). 874 * 875 * @since 7.2.0 876 * @access private 877 * 878 * @param string $name_prefix Restrict to names beginning with this prefix. 879 * @param bool $network Whether to list network-scope secrets. 880 * @return array|WP_Error 881 * 882 * @phpstan-return list<array{name: string, fingerprint: string, created: int, has_previous: bool, needs_rotation: bool}>|WP_Error 883 */ 884 function _wp_secrets_list( string $name_prefix, bool $network ) { 885 return _wp_secrets_get_provider()->list_secrets( $name_prefix, $network ); 886 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Mon Nov 16 08:20:01 2026 | Cross-referenced by PHPXref |