Intermediate LevelPHP 8+
How does the PHP 8.2+ #[SensitiveParameter] attribute redact passwords and API keys from stack traces on fatal crashes?
❓ Interview Prompt: Explain PHP 8.2 #[SensitiveParameter] attribute, automatic parameter value redaction in stack traces, and credential leak prevention.
When a PHP script crashes and prints a stack trace to error logs, function parameter arguments (including raw passwords, secret keys, or credit card numbers) are included in cleartext in the stack trace log file!
The '#[SensitiveParameter]' Attribute (PHP 8.2+):
Mark sensitive function parameters with the '#[SensitiveParameter]' attribute. If an exception or crash occurs, PHP automatically redacts the argument value in stack traces, replacing it with a 'SensitiveParameterValue' object placeholder!
Code Implementation Example
php
<?php
declare(strict_types=1);
// PHP 8.2+ Sensitive Parameter Redaction
function authenticateUser(
string $username,
#[SensitiveParameter] string $password // Redacted from stack traces!
): bool {
throw new Exception("Authentication database unavailable!");
}
// Exception stack trace will display:
// authenticateUser('alice', Object(SensitiveParameterValue))💡Redacting sensitive password parameters from crash stack traces using PHP 8.2 #[SensitiveParameter].
🎯 Interview Key Takeaway
Mark sensitive parameters with #[SensitiveParameter] to redact passwords and secrets from stack traces during crashes.
#SensitiveParameter#PHP 8.2#Security#Attributes#Intermediate
Back to all PHP 8+ Questions→