PHP 8.2: the SensitiveParameter attribute

spO0q 🐒🎃 - Jul 11 '23 - - Dev Community

Functions, objects, or some configs can contain credentials and sensitive data. There could be multiple occurrences in your code.

Using the SensitiveParameter attribute will prevent any unwanted disclosure in stack traces (e.g., debug_print_backtrace), error logs, and, more generally, in fatal errors.

Basic syntax

function hashData(#[\SensitiveParameter] string $password) {}
Enter fullscreen mode Exit fullscreen mode

Instead of the actual value, people will get a SensitiveParameterValue in debugs and other var_dump. Behind the scene, it encapsulates the real value in a private value.

The SensitiveParameterValue class is final and implements a magic method called __debugInfo to ensure nothing is returned (empty array).

Source: The SensitiveParameter class

Kill a classic vector

Logs and stack traces are classic point of entries for attackers, as it usually bypasses authentication and authorization.

Using this attribute will not make your app bulletproof, but it does add an interesting layer.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .