You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 342bbf8
Browse filesBrowse the repository at this point in the historyBrowse files
## Sensitive Parameter Value Masking (PHP 8.2+ `#[SensitiveParameter]`)
41
41
42
-
PHP 8.2 introduced the native `#[SensitiveParameter]` attribute to protect sensitive values (such as passwords, API keys, secret PINs, and bearer tokens) from leaking into stack traces and error monitoring logs (e.g. Sentry, Datadog, log aggregators).
42
+
PHP 8.2 introduced the native `#[SensitiveParameter]` attribute to protect sensitive values (such as passwords, API keys, secret PINs, and bearer tokens) from leaking into stack traces and error monitoring logs (e.g. Sentry, Datadog, Bugsnag, CloudWatch).
43
43
44
44
TypePHP natively detects `#[SensitiveParameter]` on functions, class methods, constructors, promoted properties, and inherited interface contracts.
45
45
46
46
### Why Value Masking Matters
47
47
48
-
For standard parameters, TypePHP provides rich value inspection (such as `negative int (-5) given` or `empty string ('') given`). However, for sensitive parameters, printing the raw value would risk exposing credentials in exception messages and log files:
48
+
For standard parameters, TypePHP provides rich, descriptive error messages to make debugging effortless (such as `negative int (-5) given` or `empty string ('') given`). However, for sensitive parameters, printing the raw value risks exposing confidential credentials in exception messages and log files:
49
49
50
50
```php
51
51
use SensitiveParameter;
@@ -91,6 +91,85 @@ TypePHP enforces sensitive value masking across all declaration styles:
91
91
92
92
---
93
93
94
+
## Global Value Redaction (`redact_values => true`)
95
+
96
+
While `#[SensitiveParameter]` is ideal for targeted parameters, relying **solely** on attributes has three major limitations in enterprise and production codebases:
97
+
98
+
1.**Human Error & Coverage Gaps:** Developers often forget to annotate every sensitive parameter across hundreds of controllers, commands, and DTOs.
99
+
2.**Unsupported Targets:**`#[SensitiveParameter]` only applies to method/function parameters. It **cannot** protect return values (`@return`), class property assignments (`$model->apiKey = ...`), or local inline variables (`/** @var ... */ $token = ...`).
100
+
3.**PHP Version Restrictions:**`#[SensitiveParameter]` is only available on PHP 8.2+. Applications running on PHP 8.1 cannot use it.
101
+
102
+
To solve this, TypePHP provides the **Global Value Redaction** toggle (`'redact_values' => true`).
103
+
104
+
### Why Global Redaction is Useful
105
+
106
+
When enabled, TypePHP treats **every single parameter, return value, class property, and local variable** across your entire application as sensitive:
107
+
108
+
```
109
+
# Without Redaction (Development Mode)
110
+
TypeError: OrderService::checkout(): Argument $creditCard must be 'valid', string '4111-2222-3333-4444' given
111
+
TypeError: Property User::$secretToken must be positive-int, negative int (-42) given
112
+
TypeError: Return value must be of type positive-int, int (-999) returned
113
+
114
+
# With Global Redaction (Production / Staging / Audit Mode)
115
+
TypeError: OrderService::checkout(): Argument $creditCard must be 'valid', string given
116
+
TypeError: Property User::$secretToken must be positive-int, int given
117
+
TypeError: Return value must be of type positive-int, int returned
118
+
```
119
+
120
+
#### Key Benefits:
121
+
***Zero Code Modifications:** Protects legacy codebases, monolithic apps, and thousands of existing classes without requiring you to manually touch a single file or add attributes.
122
+
***PHP 8.1+ Compatibility:** Provides enterprise-grade credential masking even if your project has not upgraded to PHP 8.2 yet.
123
+
***Full-Spectrum Protection:** Redacts values across **all four** boundaries:
124
+
- Function & method arguments (`@param`).
125
+
- Function & method returns (`@return`).
126
+
- Class property assignments (both instance and static `$this->prop` / `self::$prop`).
127
+
- Inline `@var` variable reassignments and destructuring (`$var = ...`).
128
+
***Compliance Ready:** Essential for meeting strict data privacy standards (**HIPAA, GDPR, PCI-DSS, SOC 2**) by ensuring raw PII, payment details, or credentials are never written to error log aggregators (Sentry, Datadog) or TypePHP JSON audit reports.
129
+
130
+
### How to Enable Global Redaction
131
+
132
+
You can activate global redaction via configuration file, `composer.json`, or environment variable:
133
+
134
+
#### 1. In `typephp.php`:
135
+
```php
136
+
return [
137
+
'redact_values' => true,
138
+
];
139
+
```
140
+
141
+
#### 2. In `composer.json`:
142
+
```json
143
+
{
144
+
"extra": {
145
+
"typephp": {
146
+
"redact-values": true
147
+
}
148
+
}
149
+
}
150
+
```
151
+
152
+
#### 3. Via Environment Variable (Recommended for Production & CI):
153
+
```bash
154
+
export TYPEPHP_REDACT_VALUES=true
155
+
```
156
+
157
+
---
158
+
159
+
## Comparison: `#[SensitiveParameter]` vs. `'redact_values'`
|**Ideal Environment**| Local development (detailed errors elsewhere, masked on secrets) | Staging, production, CI/CD audit runs, compliance-restricted systems |
168
+
169
+
> **Best Practice:** Use `#[SensitiveParameter]` in your source code for explicit self-documentation. In your **production** and **staging** environments, set `TYPEPHP_REDACT_VALUES=true` as a fail-safe catch-all to guarantee that no secret or PII ever leaks into your centralized logs.
170
+
171
+
---
172
+
94
173
## Native Parameter Nullability & DocBlock Refinement (`?Type` and `Type|null`)
95
174
96
175
In modern PHP, parameters frequently declare native nullable typehints:
0 commit comments