Skip to main content
Blue Moose logo Blue Moose Field notes
← All articles
php

PHP #[\SensitiveParameter]: keys out of traces

Code on a dark screen

A while ago I was debugging a failed call from a Drupal site to a third-party API. The exception landed in the logs, and when I scrolled down the stack trace there it was: the full API token, in plain text, as an argument to sign(). Anyone with access to watchdog, a log shipper, or an error-tracking dashboard could read it. PHP 8.2 has a one-line fix for this, and I wasn't using it.

What the attribute does

Per the PHP manual, #[\SensitiveParameter] (since 8.2.0) can be put on a function or method parameter. If that function appears in a stack trace, the argument's value is redacted. Instead of the string, the trace shows Object(SensitiveParameterValue). Other arguments in the same call are still shown normally, so the trace stays useful for debugging.

The wrapper is a real class, SensitiveParameterValue: a final class with a private readonly value, a getValue() method, and a __debugInfo() that hides the contents. That means I can also use it deliberately, to wrap a secret so that a stray var_dump() doesn't print it.

A signing service that doesn't leak its key

Here is the shape I now use for a small service that signs outgoing requests. The key comes from settings, and the parameters that carry it are marked.

<?php

namespace Drupal\my_module;

use Drupal\Core\Site\Settings;

final class RequestSigner {

  public function __construct(
    private readonly Settings $settings,
  ) {}

  public function sign(
    string $payload,
    #[\SensitiveParameter] string $key,
  ): string {
    if ($key === '') {
      throw new \InvalidArgumentException('Signing key is empty.');
    }
    return hash_hmac('sha256', $payload, $key);
  }

  public function signWithSiteKey(string $payload): string {
    $key = $this->settings->get('my_module_api_secret', '');
    return $this->sign($payload, $key);
  }

}

If hash_hmac() or my own guard throws, the trace for sign() shows the payload and an opaque object where the key used to be. The settings value comes from settings.php (or an environment variable read there), so it never lives in config export either.

Wrapping a value yourself

The attribute only protects stack traces. If a secret is held on an object, it can still show up in a dump. For that I wrap it explicitly:

<?php

final class ApiCredentials {

  private readonly \SensitiveParameterValue $token;

  public function __construct(
    #[\SensitiveParameter] string $token,
  ) {
    $this->token = new \SensitiveParameterValue($token);
  }

  public function authorizationHeader(): string {
    return 'Bearer ' . $this->token->getValue();
  }

}

Now var_dump($credentials) shows the wrapper rather than the token, because of __debugInfo(). I traced this from the class synopsis rather than running it against every dumping tool, so check how your own debugging stack (Xdebug, Devel's dpm(), Symfony's VarDumper) renders it before you rely on it.

Gotchas

  • Put it on the implementation. A reader note on the manual page reports the attribute has no effect when only the interface declaration carries it, so mark the parameter on the implementing method too. That note isn't official documentation, so test it if it matters to you.
  • It only covers traces. The value is still a normal string inside the function. Don't log it, concatenate it into an exception message, or pass it to \Drupal::logger() as a placeholder.
  • Constructor promotion works too, but I prefer to keep promoted properties for non-secret data so I don't forget which fields are sensitive.

I'd add this to every method that takes a password, API key, or token as an argument, since it costs nothing at runtime and it's easy to review. It isn't a substitute for keeping secrets out of logs in the first place, but it closes the one leak I hadn't thought about: the error you never expected to read.

BM
Blue Moose
The moose behind Blue Moose. Full-stack PHP developer — Drupal by day, Symfony by night, tests always.