# SensitiveDataRedactor

> Replaces values whose array keys match configured exact names, prefixes, or PCRE patterns.

[API reference](/docs/api/index.md) › [Debug Core](/docs/extensions/debug-core/reference/index.md) › [Helper](/docs/extensions/debug-core/reference/helper/index.md) › SensitiveDataRedactor

| final class | `PHPForge\Debug\Helper\SensitiveDataRedactor` |
| --- | --- |
| Source | [`src/Helper/SensitiveDataRedactor.php`](https://github.com/php-forge/debug-core/blob/main/src/Helper/SensitiveDataRedactor.php#L24) |

Replaces values whose array keys match configured exact names, prefixes, or PCRE patterns.

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`isSensitiveKey()`](#method-isSensitiveKey) static | Returns whether a key matches a configured exact name, literal prefix, or PCRE pattern. | `SensitiveDataRedactor` |
| [`patterns()`](#method-patterns) static | Resolves the pattern list a redaction rule set applies, preserving exact-list override behavior. | `SensitiveDataRedactor` |
| [`redact()`](#method-redact) static | Redacts configured keys case-insensitively throughout a bounded nested array. | `SensitiveDataRedactor` |

## Constants

| Constant | Value | Description | Defined by |
| --- | --- | --- | --- |
| <span id="const-DEFAULT_KEYS"></span>`DEFAULT_KEYS` | `['access_token', 'accessToken', 'api_key', 'apiKey', 'authorization', 'auth_key', 'authKey', 'aws_secret_access_key', 'client_secret', 'clientSecret', 'cookie', 'csrf_token', 'csrfToken', 'database_url', 'db_password', 'http_authorization', 'http_cookie', 'password', 'password_hash', 'password_reset_token', 'passwd', 'php_auth_pw', 'proxy-authorization', 'redirect_http_authorization', 'refresh_token', 'refreshToken', 'secret', 'session_id', 'set-cookie', 'token', 'verification_token', 'x-api-key', 'x-auth-token', 'x-csrf-token']` | Common credential and session fields that must never be persisted by default. | `SensitiveDataRedactor` |
| <span id="const-DEFAULT_PATTERNS"></span>`DEFAULT_PATTERNS` | `['~(?:^\|[_\-.])(?:password\|passwd\|secret\|token\|api[_-]?key\|private[_-]?key\|credential)(?:$\|[_\-.])~i']` | Segment-aware defaults for environment-style credential keys. | `SensitiveDataRedactor` |
| <span id="const-PLACEHOLDER"></span>`PLACEHOLDER` | `'[redacted]'` | Replacement written in place of a sensitive value. | `SensitiveDataRedactor` |
| <span id="const-TRUNCATED"></span>`TRUNCATED` | `'[truncated]'` | Replacement written where the depth or node budget stops traversal. | `SensitiveDataRedactor` |

## Method details

### isSensitiveKey() <span id="method-isSensitiveKey"></span>

```php
public static function isSensitiveKey(string $key, list<string> $sensitiveKeys = \self::DEFAULT_KEYS, list<string> $sensitiveKeyPrefixes = [], list<string>|null $sensitiveKeyPatterns = null): bool
```

Returns whether a key matches a configured exact name, literal prefix, or PCRE pattern.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$key` | `string` | Original key to inspect. Exact names and literal prefixes ignore case; PCRE patterns retain their own case-sensitivity modifiers. |
| `$sensitiveKeys` | `list<string>` | Exact key names to inspect. |
| `$sensitiveKeyPrefixes` | `list<string>` | Literal key prefixes to inspect case-insensitively. |
| `$sensitiveKeyPatterns` | `list<string>\|null` | PCRE patterns applied to the complete original key. `null` uses defaults only with the default exact-key list; `[]` explicitly disables patterns. |

**Returns**

`bool`: `true` when the key matches any configured rule; `false` otherwise.

### patterns() <span id="method-patterns"></span>

```php
public static function patterns(list<string> $keys, list<string>|null $patterns): list<string>
```

Resolves the pattern list a redaction rule set applies, preserving exact-list override behavior.

An explicit list wins. Otherwise [`DEFAULT_PATTERNS`](/docs/extensions/debug-core/reference/helper/sensitivedataredactor/index.md#const-DEFAULT_PATTERNS) applies only alongside the unmodified
[`DEFAULT_KEYS`](/docs/extensions/debug-core/reference/helper/sensitivedataredactor/index.md#const-DEFAULT_KEYS) list, so a custom exact-key list never inherits the segment-aware defaults.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$keys` | `list<string>` | Configured exact key names. |
| `$patterns` | `list<string>\|null` | Configured PCRE patterns, or `null` to resolve the defaults. |

**Returns**

`list<string>`: Patterns applied to complete original keys.

### redact() <span id="method-redact"></span>

```php
public static function redact(array<PHPForge\Debug\Helper\TKey, mixed> $value, list<string> $sensitiveKeys = \self::DEFAULT_KEYS, list<string> $sensitiveKeyPrefixes = [], list<string>|null $sensitiveKeyPatterns = null): array<PHPForge\Debug\Helper\TKey, mixed>
```

Redacts configured keys case-insensitively throughout a bounded nested array.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `array<PHPForge\Debug\Helper\TKey, mixed>` | Value tree to sanitize. |
| `$sensitiveKeys` | `list<string>` | Exact key names to redact. |
| `$sensitiveKeyPrefixes` | `list<string>` | Literal key prefixes to redact case-insensitively. |
| `$sensitiveKeyPatterns` | `list<string>\|null` | PCRE patterns applied to complete original keys. `null` uses defaults only with the default exact-key list; `[]` explicitly disables patterns. |

**Returns**

`array<PHPForge\Debug\Helper\TKey, mixed>`: Sanitized tree with keys and non-sensitive values preserved.