PHP 8.6.0 Beta 1 is available for testing

El atributo SensitiveParameter

(PHP 8 >= 8.2.0)

Introducción

Este atributo se utiliza para marcar un parámetro que es sensible y cuya valor debe ser censurado si está presente en un rastro de pila.

Sinopsis de la clase

#[\Attribute]
final class SensitiveParameter {
/* Métodos */
public function __construct ()
}

Ejemplos

<?php
function defaultBehavior(
 string $secret,
 string $normal
) {
 throw new Exception('Error!');
}
function sensitiveParametersWithAttribute(
 #[\SensitiveParameter]
 string $secret,
 string $normal
) {
 throw new Exception('Error!');
}
try {
 defaultBehavior('password', 'normal');
} catch (Exception $e) {
 echo $e, PHP_EOL, PHP_EOL;
}
try {
 sensitiveParametersWithAttribute('password', 'normal');
} catch (Exception $e) {
 echo $e, PHP_EOL, PHP_EOL;
}
?>

Resultado del ejemplo anterior en PHP 8.2 es similar a:

Exception: Error! in example.php:7
Stack trace:
#0 example.php(19): defaultBehavior('password', 'normal')
#1 {main}
Exception: Error! in example.php:15
Stack trace:
#0 example.php(25): sensitiveParametersWithAttribute(Object(SensitiveParameterValue), 'normal')
#1 {main}

Tabla de contenidos

Found A Problem?

Learn How To Improve This PageSubmit a Pull RequestReport a Bug
+add a note

User Contributed Notes 1 note

up
21
miqrogroove at gmail dot com
1 year ago
Beware this attribute does nothing on object interfaces and will permit password exposure when used incorrectly.
<?php
interface Server
{
 public function connect(
 #[\SensitiveParameter]
 string $password,
 );
}
class TestServer implements Server
{
 public function connect(
 string $password,
 ) {
 throw new Exception('Guess what?');
 }
}
($var = new TestServer())->connect('wrl!L3=6O57T9?r');
+add a note

AltStyle によって変換されたページ (->オリジナル) /