[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Secrets API: WP_Secret class
   4   *
   5   * @package WordPress
   6   * @subpackage Secrets
   7   * @since 7.2.0
   8   */
   9  
  10  /**
  11   * A decrypted secret value, returned by wp_get_secret().
  12   *
  13   * Masking is total and unconditional. Every representation short of an explicit call
  14   * to reveal() yields the placeholder '[secret:{name}]' – printing, logging, JSON
  15   * encoding, or dumping an instance never exposes the plaintext.
  16   *
  17   * The plaintext is never a declared property of this class. It lives in a private
  18   * static table keyed by spl_object_id(), which is what makes the masking unconditional
  19   * rather than something every magic method has to individually get right: var_export()
  20   * in particular ignores __debugInfo() and __toString() and serializes declared
  21   * properties directly, so if the plaintext lived in one there would be no way to mask
  22   * it there at all.
  23   *
  24   * @since 7.2.0
  25   */
  26  final class WP_Secret implements JsonSerializable {
  27  
  28      /**
  29       * Plaintext values, keyed by the owning instance's spl_object_id().
  30       *
  31       * Deliberately not an SplObjectStorage keyed by the instance itself: that holds a
  32       * strong reference to its keys, which would keep every WP_Secret ever constructed
  33       * alive – and its plaintext un-zeroed – for the life of the request. Keying by
  34       * the integer ID instead leaves an instance's own reference count to govern its
  35       * lifetime normally, so __destruct() fires and zeroes the value the moment nothing
  36       * else references the instance.
  37       *
  38       * @since 7.2.0
  39       * @var array<int, string>
  40       */
  41      private static $vault = array();
  42  
  43      /**
  44       * The secret's namespaced name.
  45       *
  46       * @since 7.2.0
  47       * @var string
  48       */
  49      private $name;
  50  
  51      /**
  52       * Keyed fingerprint of the plaintext.
  53       *
  54       * Not sensitive. It is a one-way, per-site, per-key digest rather than the value
  55       * itself.
  56       *
  57       * @since 7.2.0
  58       * @var string
  59       */
  60      private $fingerprint;
  61  
  62      /**
  63       * Why this secret's value is unavailable to PHP, or null when it is available.
  64       *
  65       * Set only for a secret a provider can prove exists but will not hand over –
  66       * an HSM that will sign with a key but never export it, a broker that releases
  67       * a credential to a subsystem rather than to WordPress. Such a secret is still
  68       * a real secret with a real name and fingerprint; what it is not is readable,
  69       * and reveal() says so rather than returning something misleading.
  70       *
  71       * @since 7.2.0
  72       * @var WP_Error|null
  73       */
  74      private $withheld_reason = null;
  75  
  76      /**
  77       * Constructor.
  78       *
  79       * Throws, where the rest of this API returns WP_Error for a caller mistake.
  80       * That is not an inconsistency to be tidied up later: a constructor has no
  81       * return channel, so the only alternatives are throwing or building a
  82       * half-valid WP_Secret and letting it fail somewhere less obvious. Every
  83       * function that *can* return WP_Error does – see
  84       * this file's own reasoning below. The same rule covers the serialization
  85       * and clone refusals below, which are magic methods with the same problem
  86       * and an additional one: silently permitting them would leak a plaintext.
  87       *
  88       * Nothing outside this API constructs a WP_Secret in normal use; the values
  89       * passed here come from _wp_secrets_get() having just decrypted them.
  90       *
  91       * @since 7.2.0
  92       *
  93       * @param string $name        The secret's namespaced name.
  94       * @param string $value       The decrypted plaintext.
  95       * @param string $fingerprint Keyed fingerprint of $value.
  96       *
  97       * @throws InvalidArgumentException If $name or $fingerprint is empty, or if $value
  98       *                                  is not a string. $value may be empty.
  99       */
 100  	public function __construct(
 101          string $name,
 102          #[\SensitiveParameter]
 103          $value,
 104          string $fingerprint
 105      ) {
 106          if ( '' === $name ) {
 107              throw new InvalidArgumentException( 'WP_Secret requires a non-empty name.' );
 108          }
 109  
 110          if ( ! is_string( $value ) ) {
 111              throw new InvalidArgumentException( 'WP_Secret values must be strings.' );
 112          }
 113  
 114          if ( '' === $fingerprint ) {
 115              throw new InvalidArgumentException( 'WP_Secret requires a non-empty fingerprint.' );
 116          }
 117  
 118          $this->name        = $name;
 119          $this->fingerprint = $fingerprint;
 120  
 121          self::$vault[ spl_object_id( $this ) ] = $value;
 122      }
 123  
 124      /**
 125       * Returns the decrypted plaintext.
 126       *
 127       * This is the only path to the stored plaintext anywhere in the API.
 128       *
 129       * Returns WP_Error for a secret whose provider will not release the value to
 130       * PHP at all – see withheld(). That case does not arise for the provider
 131       * WordPress ships, which decrypts eagerly and therefore always has a value in
 132       * hand, so most callers will never see it. It is in the signature because a
 133       * return type cannot be widened after adoption: leaving reveal() as
 134       * `string` would permanently foreclose brokered and use-only credentials,
 135       * which is a decision better made deliberately than by omission.
 136       *
 137       * @since 7.2.0
 138       *
 139       * @return string|WP_Error The plaintext, or WP_Error if this provider does not
 140       *                         release values to PHP.
 141       */
 142  	public function reveal() {
 143          if ( null !== $this->withheld_reason ) {
 144              return $this->withheld_reason;
 145          }
 146  
 147          return self::$vault[ spl_object_id( $this ) ];
 148      }
 149  
 150      /**
 151       * Builds a secret whose value exists but is not available to PHP.
 152       *
 153       * For a provider that can prove a credential exists, and can name and
 154       * fingerprint it, but will not release it – typically because releasing it
 155       * would defeat the point of where it is held. Everything except reveal()
 156       * behaves normally, so such a secret still lists, still reports a stable
 157       * fingerprint, and still masks itself in every output path.
 158       *
 159       * @since 7.2.0
 160       *
 161       * @param string   $name        The secret's name.
 162       * @param string   $fingerprint A stable per-site digest, from the provider.
 163       * @param WP_Error $reason      Why the value cannot be produced. Returned
 164       *                              verbatim by reveal().
 165       *
 166       * @throws InvalidArgumentException If $name or $fingerprint fail the constructor's
 167       *                                  checks.
 168       *
 169       * @return WP_Secret
 170       */
 171  	public static function withheld( string $name, string $fingerprint, WP_Error $reason ): WP_Secret {
 172          $secret = new self( $name, '', $fingerprint );
 173  
 174          $secret->withheld_reason = $reason;
 175  
 176          return $secret;
 177      }
 178  
 179      /**
 180       * Returns the keyed fingerprint of the plaintext.
 181       *
 182       * Stable for a given value on a given site; not a value-recovery oracle.
 183       *
 184       * @since 7.2.0
 185       *
 186       * @return string
 187       */
 188  	public function fingerprint(): string {
 189          return $this->fingerprint;
 190      }
 191  
 192      /**
 193       * Returns the secret's namespaced name.
 194       *
 195       * @since 7.2.0
 196       *
 197       * @return string
 198       */
 199  	public function get_name(): string {
 200          return $this->name;
 201      }
 202  
 203      /**
 204       * Builds the masked placeholder used everywhere a plaintext would otherwise appear.
 205       *
 206       * @since 7.2.0
 207       *
 208       * @return string
 209       *
 210       * @phpstan-return non-falsy-string
 211       */
 212  	private function mask(): string {
 213          return '[secret:' . $this->name . ']';
 214      }
 215  
 216      /**
 217       * Masks the instance for string conversion.
 218       *
 219       * Covers error_log( $secret ) and any other implicit string coercion.
 220       *
 221       * @since 7.2.0
 222       *
 223       * @return string
 224       *
 225       * @phpstan-return non-falsy-string
 226       */
 227  	public function __toString(): string {
 228          return $this->mask();
 229      }
 230  
 231      /**
 232       * Masks the instance for var_dump() and print_r().
 233       *
 234       * Note that var_export() does not use this hook. It does not expose plaintext
 235       * from the vault, but it enumerates all instance properties, including the
 236       * unmasked WP_Error reason on a withheld secret. Use a masked output path
 237       * instead when the reason must remain private.
 238       *
 239       * @since 7.2.0
 240       *
 241       * @return array<string, string>
 242       */
 243  	public function __debugInfo(): array {
 244          return array( 'value' => $this->mask() );
 245      }
 246  
 247      /**
 248       * Masks the instance for json_encode().
 249       *
 250       * @since 7.2.0
 251       *
 252       * @return string
 253       *
 254       * @phpstan-return non-falsy-string
 255       */
 256  	public function jsonSerialize(): string {
 257          return $this->mask();
 258      }
 259  
 260      /**
 261       * Refuses serialization outright.
 262       *
 263       * @since 7.2.0
 264       *
 265       * @throws LogicException Always.
 266       *
 267       * @return void
 268       */
 269  	public function __sleep() {
 270          throw new LogicException( 'WP_Secret cannot be serialized.' );
 271      }
 272  
 273      /**
 274       * Refuses unserialization outright.
 275       *
 276       * @since 7.2.0
 277       *
 278       * @throws LogicException Always.
 279       *
 280       * @return void
 281       */
 282  	public function __wakeup() {
 283          throw new LogicException( 'WP_Secret cannot be unserialized.' );
 284      }
 285  
 286      /**
 287       * Refuses serialization outright.
 288       *
 289       * PHP calls this in preference to __sleep() when both are defined, so both must
 290       * refuse independently.
 291       *
 292       * @since 7.2.0
 293       *
 294       * @throws LogicException Always.
 295       *
 296       * @return void
 297       */
 298  	public function __serialize() {
 299          throw new LogicException( 'WP_Secret cannot be serialized.' );
 300      }
 301  
 302      /**
 303       * Refuses unserialization outright.
 304       *
 305       * @since 7.2.0
 306       *
 307       * @param array<mixed> $data Ignored.
 308       *
 309       * @throws LogicException Always.
 310       *
 311       * @return void
 312       */
 313  	public function __unserialize( array $data ): void {
 314          throw new LogicException( 'WP_Secret cannot be unserialized.' );
 315      }
 316  
 317      /**
 318       * Refuses cloning outright.
 319       *
 320       * A clone would be a second, unaudited reference to the same plaintext with an
 321       * independent lifetime – and, since the vault is keyed by object ID rather than
 322       * copied, a bare `clone` would otherwise leave the clone's reveal() silently
 323       * reading nothing.
 324       *
 325       * @since 7.2.0
 326       *
 327       * @throws LogicException Always.
 328       */
 329  	public function __clone() {
 330          throw new LogicException( 'WP_Secret cannot be cloned.' );
 331      }
 332  
 333      /**
 334       * Zeroes the plaintext and removes it from the vault.
 335       *
 336       * @since 7.2.0
 337       */
 338  	public function __destruct() {
 339          $id = spl_object_id( $this );
 340  
 341          if ( isset( self::$vault[ $id ] ) ) {
 342              wp_secrets_memzero( self::$vault[ $id ] );
 343              unset( self::$vault[ $id ] );
 344          }
 345      }
 346  }


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