[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/secrets/ -> class-wp-secrets-libsodium-provider.php (source)

   1  <?php
   2  /**
   3   * Secrets API: WP_Secrets_Libsodium_Provider class
   4   *
   5   * @package WordPress
   6   * @subpackage Secrets
   7   * @since 7.2.0
   8   */
   9  
  10  /**
  11   * The provider WordPress ships.
  12   *
  13   * Uses libsodium envelope encryption and stores ciphertext in the options tables.
  14   *
  15   * This is the default rather than a privileged case. Everything it does is expressed
  16   * through WP_Secrets_Provider, the same class a platform extends, so a KMS-
  17   * or HSM-backed provider is a peer rather than an exception carved into the side of
  18   * this one.
  19   *
  20   * It is assembled from two smaller pieces, and that is deliberate: a host who wants
  21   * their own key custody but is happy with WordPress's storage swaps only the
  22   * keyring and keeps everything else, and the inverse works too. Coupling them would
  23   * force an all-or-nothing decision most sites cannot make.
  24   *
  25   * - WP_Secrets_Store   – where the ciphertext records live.
  26   * - WP_Secrets_Keyring – what wraps the root key everything else derives from.
  27   *
  28   * Neither is handed a plaintext, because for this provider the encryption boundary
  29   * genuinely is inside WordPress. That is a property of *this* implementation rather
  30   * than a rule imposed on every provider; see WP_Secrets_Provider for why the
  31   * distinction matters.
  32   *
  33   * @since 7.2.0
  34   */
  35  final class WP_Secrets_Libsodium_Provider extends WP_Secrets_Provider {
  36  
  37      /**
  38       * Where ciphertext records live.
  39       *
  40       * @since 7.2.0
  41       * @var WP_Secrets_Store
  42       */
  43      private $store;
  44  
  45      /**
  46       * Derives the per-scope master keys this provider encrypts under.
  47       *
  48       * @since 7.2.0
  49       * @var WP_Secrets_Key_Manager
  50       */
  51      private $key_manager;
  52  
  53      /**
  54       * Composes the provider from its two replaceable halves.
  55       *
  56       * @since 7.2.0
  57       *
  58       * @param WP_Secrets_Store       $store       Where ciphertext records live.
  59       * @param WP_Secrets_Key_Manager $key_manager Derives per-scope master keys.
  60       */
  61  	public function __construct( WP_Secrets_Store $store, WP_Secrets_Key_Manager $key_manager ) {
  62          $this->store       = $store;
  63          $this->key_manager = $key_manager;
  64      }
  65  
  66      /**
  67       * A description of what is protecting these secrets, for Site Health.
  68       *
  69       * Reports the keyring's own description rather than a fixed string: "libsodium"
  70       * is only half the answer, and which key source is in use is the half an
  71       * operator cannot otherwise see.
  72       *
  73       * @since 7.2.0
  74       *
  75       * @return string
  76       */
  77  	public function get_label(): string {
  78          $keyring = $this->key_manager->get_keyring();
  79  
  80          return sprintf(
  81              /* translators: %s: Human-readable description of the key source. */
  82              __( 'WordPress (libsodium), key source: %s' ),
  83              $keyring->get_key_source()
  84          );
  85      }
  86  
  87      /**
  88       * Protection happens inside WordPress for this provider.
  89       *
  90       * @since 7.2.0
  91       *
  92       * @return string
  93       *
  94       * @phpstan-return self::BOUNDARY_*
  95       */
  96  	public function get_protection_boundary(): string {
  97          return self::BOUNDARY_WORDPRESS;
  98      }
  99  
 100      /**
 101       * Always true.
 102       *
 103       * A store that cannot accept a write reports that from set() itself. There is no
 104       * separate capability flag to consult, and the shipped store accepts writes
 105       * unconditionally.
 106       *
 107       * @since 7.2.0
 108       *
 109       * @return bool
 110       */
 111  	public function is_writable(): bool {
 112          return true;
 113      }
 114      /**
 115       * Validates the shape of a record read back from a store.
 116       *
 117       * Runs before any decryption is attempted.
 118       *
 119       * @since 7.2.0
 120       *
 121       * @param mixed $record Candidate record.
 122       * @return true|WP_Error
 123       */
 124  	private function validate_record_shape( $record ) {
 125          if ( ! is_array( $record ) || ! array_key_exists( 'v', $record ) || ! array_key_exists( 'current', $record ) ) {
 126              return new WP_Error(
 127                  WP_SECRETS_ERROR_RECORD_MALFORMED,
 128                  __( 'The stored secret record is missing required fields.' )
 129              );
 130          }
 131  
 132          if ( WP_SECRETS_RECORD_VERSION !== $record['v'] ) {
 133              return new WP_Error(
 134                  WP_SECRETS_ERROR_RECORD_UNSUPPORTED_VERSION,
 135                  sprintf(
 136                      /* translators: %s: Unrecognized record format version. */
 137                      __( 'Secret record format version "%s" is not supported.' ),
 138                      is_scalar( $record['v'] ) ? $record['v'] : gettype( $record['v'] )
 139                  )
 140              );
 141          }
 142  
 143          if ( ! is_array( $record['current'] ) ) {
 144              return new WP_Error(
 145                  WP_SECRETS_ERROR_RECORD_MALFORMED,
 146                  __( 'The stored secret record is missing required fields.' )
 147              );
 148          }
 149  
 150          return true;
 151      }
 152  
 153      /**
 154       * Reads the record a write or delete is about to replace, tolerating corruption.
 155       *
 156       * A write path needs the prior record only to report the old fingerprint on the
 157       * change hook. That is not worth refusing the operation over: if the stored record
 158       * is unreadable, overwriting or deleting it is the only repair an operator has
 159       * through this API, and returning WP_Error here would leave a corrupted secret
 160       * permanently stuck – neither readable, nor fixable, nor removable without editing
 161       * the database by hand.
 162       *
 163       * An unreachable *store* is different, and still aborts: that is an infrastructure
 164       * failure, not a data problem, and continuing would mean writing blind.
 165       *
 166       * @since 7.2.0
 167       *
 168       * @param WP_Secrets_Store $store   The active store.
 169       * @param string           $name    The secret's namespaced name.
 170       * @param bool             $network Whether this is a network-scope secret.
 171       * @return array<mixed>|null|WP_Error The prior record, null if absent or unreadable,
 172       *                                    or WP_Error only if the store itself is
 173       *                                    unavailable.
 174       */
 175  	private function read_prior_record( WP_Secrets_Store $store, string $name, bool $network ) {
 176          $existing = $store->get( $name, $network );
 177  
 178          if ( is_wp_error( $existing ) ) {
 179              if ( WP_SECRETS_ERROR_STORE_UNAVAILABLE === $existing->get_error_code() ) {
 180                  return $existing;
 181              }
 182  
 183              return null;
 184          }
 185  
 186          return $existing;
 187      }
 188  
 189      /**
 190       * Extracts the current slot's stored fingerprint from a record, if it has one.
 191       *
 192       * @since 7.2.0
 193       *
 194       * @param mixed $record A record previously read from the store, or null.
 195       * @return string The fingerprint, or '' if there is not a usable one.
 196       */
 197  	private function stored_fingerprint( $record ): string {
 198          if ( ! is_array( $record ) || ! isset( $record['current'] ) || ! is_array( $record['current'] ) ) {
 199              return '';
 200          }
 201  
 202          return $this->slot_fingerprint( $record['current'] );
 203      }
 204  
 205      /**
 206       * Extracts a single slot's stored fingerprint, if it has one.
 207       *
 208       * @since 7.2.0
 209       *
 210       * @param mixed $slot A slot previously read from the store.
 211       * @return string The fingerprint, or '' if there is not a usable one.
 212       */
 213  	private function slot_fingerprint( $slot ): string {
 214          if ( ! is_array( $slot ) || ! isset( $slot['fingerprint'] ) || ! is_string( $slot['fingerprint'] ) ) {
 215              return '';
 216          }
 217  
 218          return $slot['fingerprint'];
 219      }
 220  
 221      /**
 222       * Re-encrypts an outgoing current slot so it can be stored as the previous slot.
 223       *
 224       * A slot's ciphertext is bound, via AAD, to the exact position it was written to.
 225       * Copying its stored bytes as-is into the other slot would leave it permanently
 226       * undecryptable there: the authentication tag was computed for the slot it came
 227       * from, and every future read asks for it under the other one. Demoting a slot
 228       * means decrypting it under its old binding and re-encrypting the same plaintext
 229       * under the new one.
 230       *
 231       * @since 7.2.0
 232       *
 233       * @param WP_Secrets_Cipher $cipher       A cipher instance.
 234       * @param string            $master_key   32-byte master key.
 235       * @param string            $scope        'site' or 'network'.
 236       * @param int               $site_id      Blog ID for site scope, 0 for network scope.
 237       * @param string            $name         The secret's namespaced name.
 238       * @param array<mixed>      $current_slot The outgoing current slot, as stored.
 239       * @return array|WP_Error The same value, re-encrypted and bound to
 240       *                        WP_Secret_Version::PREVIOUS. 'created' and
 241       *                        'needs_rotation' carry over unchanged; 'fingerprint' is
 242       *                        recomputed but identical, since the plaintext and master
 243       *                        key are unchanged.
 244       *
 245       * @phpstan-return array{dk: string, dk_nonce: string, ct: string, nonce: string, fingerprint: string, created: int, needs_rotation: bool}|WP_Error
 246       * @phpstan-param 'site'|'network' $scope
 247       */
 248  	private function demote_slot(
 249          WP_Secrets_Cipher $cipher,
 250          #[\SensitiveParameter]
 251          string $master_key,
 252          string $scope,
 253          int $site_id,
 254          string $name,
 255          array $current_slot
 256      ) {
 257          $plaintext = $cipher->decrypt_value( $master_key, $scope, $site_id, $name, WP_Secret_Version::CURRENT, $current_slot );
 258  
 259          if ( is_wp_error( $plaintext ) ) {
 260              return $plaintext;
 261          }
 262  
 263          $demoted = $cipher->encrypt_value( $master_key, $scope, $site_id, $name, WP_Secret_Version::PREVIOUS, $plaintext );
 264  
 265          wp_secrets_memzero( $plaintext );
 266  
 267          if ( is_wp_error( $demoted ) ) {
 268              return $demoted;
 269          }
 270  
 271          $demoted['created']        = isset( $current_slot['created'] ) && is_int( $current_slot['created'] ) ? $current_slot['created'] : time();
 272          $demoted['needs_rotation'] = ! empty( $current_slot['needs_rotation'] );
 273  
 274          return $demoted;
 275      }
 276  
 277      /**
 278       * Shared implementation behind the three secret-writing functions.
 279       *
 280       * Backs wp_set_secret(), wp_set_network_secret(), and wp_import_option_as_secret(),
 281       * which select their behavior through the last two parameters.
 282       *
 283       * @since 7.2.0
 284       *
 285       * @param string      $name           The secret's namespaced name.
 286       * @param string      $value          The plaintext value.
 287       * @param bool        $network        Whether this is a network-scope secret.
 288       * @param bool        $needs_rotation Value for the new current slot's
 289       *                                    'needs_rotation' flag. False for an ordinary
 290       *                                    write; wp_import_option_as_secret() passes true,
 291       *                                    since a credential that sat in an option is
 292       *                                    already in backups and re-encrypting does not
 293       *                                    fix that.
 294       * @param string|null $action          When given, used as the $action reported to
 295       *                                     the wp_secret_changed hook instead of the
 296       *                                     usual 'created'/'updated' detection –
 297       *                                     wp_import_option_as_secret() passes 'imported'.
 298       * @return true|WP_Error
 299       */
 300  	public function set(
 301          string $name,
 302          #[\SensitiveParameter]
 303          $value,
 304          bool $network = false,
 305          bool $needs_rotation = false,
 306          ?string $action = null
 307      ) {
 308          $name_check = wp_secrets_validate_name( $name );
 309  
 310          if ( is_wp_error( $name_check ) ) {
 311              return $name_check;
 312          }
 313  
 314          if ( ! is_string( $value ) ) {
 315              return new WP_Error(
 316                  WP_SECRETS_ERROR_INVALID_VALUE,
 317                  __( 'Secret values must be strings.' )
 318              );
 319          }
 320  
 321          $store = $this->store;
 322  
 323          $scope   = $network ? 'network' : 'site';
 324          $site_id = $network ? 0 : get_current_blog_id();
 325  
 326          $existing = $this->read_prior_record( $store, $name, $network );
 327  
 328          if ( is_wp_error( $existing ) ) {
 329              return $existing;
 330          }
 331  
 332          $master_key = $this->key_manager->get_master_key( $scope, $network ? null : $site_id );
 333  
 334          if ( is_wp_error( $master_key ) ) {
 335              return $master_key;
 336          }
 337  
 338          $cipher       = new WP_Secrets_Cipher();
 339          $has_current  = is_array( $existing ) && isset( $existing['current'] ) && is_array( $existing['current'] );
 340          $is_unchanged = false;
 341  
 342          if ( $has_current ) {
 343              $current_value = $cipher->decrypt_value( $master_key, $scope, $site_id, $name, WP_Secret_Version::CURRENT, $existing['current'] );
 344              $is_unchanged  = is_string( $current_value ) && hash_equals( $current_value, $value );
 345  
 346              if ( is_string( $current_value ) ) {
 347                  wp_secrets_memzero( $current_value );
 348              }
 349          }
 350  
 351          /*
 352           * A write that would change nothing stored is skipped, and reports nothing
 353           * to wp_secret_changed: a settings form that saves an unchanged value on
 354           * every submit has not changed the secret. Rewriting the record would also
 355           * reset the current slot's 'created' time and clear a 'needs_rotation' flag
 356           * on a value that was never rotated.
 357           *
 358           * The exception is a write that raises 'needs_rotation', such as importing
 359           * an option that holds the value already stored. That value has still sat
 360           * in plaintext, so the flag is recorded, and handled as a rewrite below.
 361           */
 362          if ( $is_unchanged && ( ! $needs_rotation || ! empty( $existing['current']['needs_rotation'] ) ) ) {
 363              wp_secrets_memzero( $master_key );
 364  
 365              return true;
 366          }
 367  
 368          $new_slot = $cipher->encrypt_value( $master_key, $scope, $site_id, $name, WP_Secret_Version::CURRENT, $value );
 369  
 370          if ( is_wp_error( $new_slot ) ) {
 371              wp_secrets_memzero( $master_key );
 372  
 373              return $new_slot;
 374          }
 375  
 376          $new_slot['created']        = time();
 377          $new_slot['needs_rotation'] = $needs_rotation;
 378  
 379          $record = array(
 380              'v'       => WP_SECRETS_RECORD_VERSION,
 381              'current' => $new_slot,
 382          );
 383  
 384          /*
 385           * Demotion: the outgoing current slot becomes the new previous slot. Only
 386           * one previous slot is ever kept – whatever was already in
 387           * $existing['previous'] is never carried forward, which is what makes a
 388           * third write discard the oldest value rather than accumulating history.
 389           *
 390           * This cannot be a plain array copy. A slot's AAD binds its ciphertext to
 391           * the exact position it occupies – current or previous – so a slot moved
 392           * verbatim would still carry an authentication tag computed for 'current'
 393           * while every future read asks for it under 'previous', and would fail to
 394           * decrypt forever. Demoting requires decrypting under the outgoing binding
 395           * and re-encrypting under the incoming one.
 396           *
 397           * If the outgoing current slot cannot be decrypted at all – already
 398           * corrupted, or orphaned by a botched key rotation – it is dropped rather
 399           * than failing this write. The value was already unreadable before this
 400           * call; refusing to let an operator set a new one over it would repeat the
 401           * corrupted-record-blocks-everything failure this API is built to avoid.
 402           *
 403           * Saving the current value again, as a settings form does on every save, is
 404           * not a rotation: demoting it would replace the previous value that a
 405           * rotation in progress still depends on. The existing previous slot is kept
 406           * instead, and can be carried over as stored because it is already bound to
 407           * WP_Secret_Version::PREVIOUS.
 408           */
 409          if ( $has_current ) {
 410              if ( $is_unchanged ) {
 411                  if ( isset( $existing['previous'] ) && is_array( $existing['previous'] ) ) {
 412                      $record['previous'] = $existing['previous'];
 413                  }
 414              } else {
 415                  $demoted = $this->demote_slot( $cipher, $master_key, $scope, $site_id, $name, $existing['current'] );
 416  
 417                  if ( ! is_wp_error( $demoted ) ) {
 418                      $record['previous'] = $demoted;
 419                  }
 420              }
 421          }
 422  
 423          wp_secrets_memzero( $master_key );
 424  
 425          $result = $store->set( $name, $record, $network );
 426  
 427          if ( is_wp_error( $result ) ) {
 428              return $result;
 429          }
 430  
 431          $is_update       = is_array( $existing );
 432          $old_fingerprint = $this->stored_fingerprint( $existing );
 433  
 434          /**
 435           * Fires whenever a secret is created, updated, deleted, imported, or retired.
 436           *
 437           * This hook does not fire on the retrieval path. Fingerprints are passed;
 438           * values never are.
 439           *
 440           * @since 7.2.0
 441           *
 442           * @param string $name            The secret's namespaced name.
 443           * @param string $action          One of 'created', 'updated', 'deleted',
 444           *                                'imported', 'retired'.
 445           * @param int    $actor_id        The current user ID, or 0.
 446           * @param int    $timestamp       Unix timestamp of the change.
 447           * @param string $old_fingerprint The previous fingerprint, or '' if none.
 448           * @param string $new_fingerprint The new fingerprint, or '' if the secret was deleted.
 449           * @param bool   $network         Whether the secret is network-scope. A site-scope
 450           *                                secret belongs to the current site.
 451           */
 452          do_action(
 453              'wp_secret_changed',
 454              $name,
 455              $action ?? ( $is_update ? 'updated' : 'created' ),
 456              get_current_user_id(),
 457              $new_slot['created'],
 458              $old_fingerprint,
 459              $new_slot['fingerprint'],
 460              $network
 461          );
 462  
 463          return true;
 464      }
 465  
 466      /**
 467       * Shared implementation behind wp_get_secret() and wp_get_network_secret().
 468       *
 469       * @since 7.2.0
 470       *
 471       * @param string $name    The secret's namespaced name.
 472       * @param string $version A WP_Secret_Version constant.
 473       * @param bool   $network Whether this is a network-scope secret.
 474       * @return WP_Secret|null|WP_Error
 475       *
 476       * @phpstan-param WP_Secret_Version::CURRENT|WP_Secret_Version::PREVIOUS $version
 477       */
 478  	public function get( string $name, string $version, bool $network = false ) {
 479  
 480          $name_check = wp_secrets_validate_name( $name );
 481  
 482          if ( is_wp_error( $name_check ) ) {
 483              return $name_check;
 484          }
 485  
 486          $record = $this->store->get( $name, $network );
 487  
 488          if ( is_wp_error( $record ) ) {
 489              return $record;
 490          }
 491  
 492          if ( null === $record ) {
 493              return null;
 494          }
 495  
 496          $shape_check = $this->validate_record_shape( $record );
 497  
 498          if ( is_wp_error( $shape_check ) ) {
 499              return $shape_check;
 500          }
 501  
 502          if ( ! isset( $record[ $version ] ) || ! is_array( $record[ $version ] ) ) {
 503              /*
 504               * The secret exists, but this slot does not. Absence, not an error –
 505               * this matters most for PREVIOUS on a secret that has never been rotated.
 506               */
 507              return null;
 508          }
 509  
 510          $scope   = $network ? 'network' : 'site';
 511          $site_id = $network ? 0 : get_current_blog_id();
 512  
 513          // Reading never creates a root key: without the stored one, nothing can be decrypted.
 514          $master_key = $this->key_manager->get_master_key( $scope, $network ? null : $site_id, false );
 515  
 516          if ( is_wp_error( $master_key ) ) {
 517              return $master_key;
 518          }
 519  
 520          $cipher    = new WP_Secrets_Cipher();
 521          $plaintext = $cipher->decrypt_value( $master_key, $scope, $site_id, $name, $version, $record[ $version ] );
 522  
 523          if ( is_wp_error( $plaintext ) ) {
 524              wp_secrets_memzero( $master_key );
 525  
 526              return $plaintext;
 527          }
 528  
 529          /*
 530           * Recomputed from the plaintext just decrypted, never read back from the record.
 531           * The stored 'fingerprint' field sits outside the AAD and so is not
 532           * authenticated: anyone who can write to the store can set it to anything
 533           * without disturbing the ciphertext. Trusting it would make
 534           * WP_Secret::fingerprint() attacker-controlled – which matters most where a
 535           * fingerprint comparison gates something irreversible, such as a migration
 536           * verifying a value before deleting its source. The stored copy still exists,
 537           * but only so wp_list_secrets() has something to show without decrypting.
 538           */
 539          $fingerprint = $cipher->fingerprint( $master_key, $plaintext );
 540  
 541          wp_secrets_memzero( $master_key );
 542  
 543          if ( is_wp_error( $fingerprint ) ) {
 544              wp_secrets_memzero( $plaintext );
 545  
 546              return $fingerprint;
 547          }
 548  
 549          $secret = new WP_Secret( $name, $plaintext, $fingerprint );
 550  
 551          wp_secrets_memzero( $plaintext );
 552  
 553          return $secret;
 554      }
 555  
 556      /**
 557       * Shared implementation behind wp_delete_secret() and wp_delete_network_secret().
 558       *
 559       * @since 7.2.0
 560       *
 561       * @param string $name    The secret's namespaced name.
 562       * @param bool   $network Whether this is a network-scope secret.
 563       * @return true|WP_Error
 564       */
 565  	public function delete( string $name, bool $network = false ) {
 566          $name_check = wp_secrets_validate_name( $name );
 567  
 568          if ( is_wp_error( $name_check ) ) {
 569              return $name_check;
 570          }
 571  
 572          $store = $this->store;
 573  
 574          $existing = $this->read_prior_record( $store, $name, $network );
 575  
 576          if ( is_wp_error( $existing ) ) {
 577              return $existing;
 578          }
 579  
 580          $result = $store->delete( $name, $network );
 581  
 582          if ( is_wp_error( $result ) ) {
 583              return $result;
 584          }
 585  
 586          if ( is_array( $existing ) ) {
 587              /** This action is documented in wp-includes/secrets/class-wp-secrets-libsodium-provider.php */
 588              do_action(
 589                  'wp_secret_changed',
 590                  $name,
 591                  'deleted',
 592                  get_current_user_id(),
 593                  time(),
 594                  $this->stored_fingerprint( $existing ),
 595                  '',
 596                  $network
 597              );
 598          }
 599  
 600          return true;
 601      }
 602  
 603      /**
 604       * Shared implementation behind the two version-retirement functions.
 605       *
 606       * Backs wp_retire_secret_version() and wp_retire_network_secret_version().
 607       *
 608       * @since 7.2.0
 609       *
 610       * @param string $name    The secret's namespaced name.
 611       * @param bool   $network Whether this is a network-scope secret.
 612       * @return true|WP_Error
 613       */
 614  	public function retire_previous( string $name, bool $network = false ) {
 615          $name_check = wp_secrets_validate_name( $name );
 616  
 617          if ( is_wp_error( $name_check ) ) {
 618              return $name_check;
 619          }
 620  
 621          $store  = $this->store;
 622          $record = $store->get( $name, $network );
 623  
 624          if ( is_wp_error( $record ) ) {
 625              return $record;
 626          }
 627  
 628          if ( null === $record || ! isset( $record['previous'] ) ) {
 629              /*
 630               * The goal of retirement – "there is no previous slot" – is already
 631               * true, whether because the secret doesn't exist or because it was
 632               * never rotated. A successful no-op, not an error, and deliberately
 633               * checked before the write-support check below: a store that can't
 634               * accept writes can still report a correct "nothing to do" without
 635               * that being mistaken for the store refusing the operation.
 636               */
 637              return true;
 638          }
 639  
 640          $retired_fingerprint = $this->slot_fingerprint( $record['previous'] );
 641  
 642          unset( $record['previous'] );
 643  
 644          $result = $store->set( $name, $record, $network );
 645  
 646          if ( is_wp_error( $result ) ) {
 647              return $result;
 648          }
 649  
 650          /** This action is documented in wp-includes/secrets/class-wp-secrets-libsodium-provider.php */
 651          do_action(
 652              'wp_secret_changed',
 653              $name,
 654              'retired',
 655              get_current_user_id(),
 656              time(),
 657              $retired_fingerprint,
 658              '',
 659              $network
 660          );
 661  
 662          return true;
 663      }
 664  
 665      /**
 666       * Shared implementation behind wp_list_secrets() and wp_list_network_secrets().
 667       *
 668       * Fingerprints returned here come directly from the stored record field, not
 669       * recomputed by decrypting each secret. That is a deliberate difference from
 670       * WP_Secret::fingerprint(), which always recomputes – recomputing here would mean
 671       * decrypting every matching secret just to list them, defeating the point of a
 672       * lightweight listing call. This is safe specifically because a list entry is
 673       * documented as informational only and
 674       * is never used to gate anything; nothing in this codebase performs a security
 675       * decision based on a fingerprint returned from this function.
 676       *
 677       * @since 7.2.0
 678       *
 679       * @param string $name_prefix Only secrets whose name starts with "{$name_prefix}/"
 680       *                            are returned. Default '' returns every secret in this
 681       *                            scope. Named to match the public function's own
 682       *                            $namespace parameter, without using the reserved word
 683       *                            'namespace' in an internal signature.
 684       * @param bool   $network     Whether to list network-scope secrets.
 685       * @return array|WP_Error Array of associative arrays, each with keys 'name',
 686       *                        'fingerprint', 'created', 'has_previous', and
 687       *                        'needs_rotation'. Never a value. WP_Error on failure.
 688       *
 689       * @phpstan-return list<array{name: string, fingerprint: string, created: int, has_previous: bool, needs_rotation: bool}>|WP_Error
 690       */
 691  	public function list_secrets( string $name_prefix = '', bool $network = false ) {
 692  
 693          $store = $this->store;
 694  
 695          $names = $store->list_names( $network );
 696  
 697          if ( is_wp_error( $names ) ) {
 698              return $names;
 699          }
 700  
 701          $entries = array();
 702  
 703          foreach ( $names as $name ) {
 704              if ( '' !== $name_prefix && ! str_starts_with( $name, $name_prefix . '/' ) ) {
 705                  continue;
 706              }
 707  
 708              $record = $store->get( $name, $network );
 709  
 710              if ( ! is_wp_error( $record ) && null !== $record && true === $this->validate_record_shape( $record ) && is_array( $record['current'] ) ) {
 711                  $entries[] = array(
 712                      'name'           => $name,
 713                      'fingerprint'    => $this->stored_fingerprint( $record ),
 714                      'created'        => isset( $record['current']['created'] ) && is_int( $record['current']['created'] ) ? $record['current']['created'] : 0,
 715                      'has_previous'   => isset( $record['previous'] ) && is_array( $record['previous'] ),
 716                      'needs_rotation' => ! empty( $record['current']['needs_rotation'] ),
 717                  );
 718  
 719                  continue;
 720              }
 721  
 722              /*
 723               * A corrupted or unreadable record is still listed, with whatever
 724               * metadata could not be salvaged left blank, rather than silently
 725               * omitted. Site Health's "undecryptable secrets" check depends on
 726               * exactly this: a secret that has gone bad must remain visible.
 727               */
 728              $entries[] = array(
 729                  'name'           => $name,
 730                  'fingerprint'    => '',
 731                  'created'        => 0,
 732                  'has_previous'   => false,
 733                  'needs_rotation' => false,
 734              );
 735          }
 736  
 737          return $entries;
 738      }
 739  }


Generated : Mon Nov 16 08:20:01 2026 Cross-referenced by PHPXref