Repository navigation
Expand file tree
/
Copy pathSensitiveDataRedactorTest.php
More file actions
211 lines (193 loc) · 7.78 KB
/
Copy pathSensitiveDataRedactorTest.php
File metadata and controls
211 lines (193 loc) · 7.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
declare(strict_types=1);
namespace PHPForge\Debug\Tests\Helper;
use InvalidArgumentException;
use PHPForge\Debug\Helper\SensitiveDataRedactor;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;
/**
* Unit tests for {@see SensitiveDataRedactor} covering exact, case-insensitive, and nested key replacement.
*/
#[Group('helpers')]
final class SensitiveDataRedactorTest extends TestCase
{
public function testConfigurationRejectsEmptyPrefixesAndInvalidPatterns(): void
{
try {
SensitiveDataRedactor::redact(['value' => 'secret'], [], ['']);
self::fail('An empty sensitive-key prefix would match every key and must be rejected.');
} catch (InvalidArgumentException $exception) {
self::assertStringContainsString(
'must not be empty',
$exception->getMessage(),
'The empty-prefix error must explain the unsafe configuration.',
);
}
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('not a valid PCRE pattern');
SensitiveDataRedactor::isSensitiveKey('value', [], [], ['~[~']);
}
public function testDefaultKeysCoverCommonEnvironmentCredentialNames(): void
{
self::assertSame(
[
'DB_PASSWORD' => SensitiveDataRedactor::PLACEHOLDER,
'AWS_SECRET_ACCESS_KEY' => SensitiveDataRedactor::PLACEHOLDER,
'DATABASE_URL' => SensitiveDataRedactor::PLACEHOLDER,
'MY_APP_PASSWORD' => SensitiveDataRedactor::PLACEHOLDER,
'REDIS_PASSWORD' => SensitiveDataRedactor::PLACEHOLDER,
'SMTP_API_TOKEN' => SensitiveDataRedactor::PLACEHOLDER,
'DATABASE_HOST' => 'database.test',
'tokenizer' => 'safe-service',
'passwordless' => true,
],
SensitiveDataRedactor::redact(
[
'DB_PASSWORD' => 'database-secret',
'AWS_SECRET_ACCESS_KEY' => 'cloud-secret',
'DATABASE_URL' => 'mysql://user:secret@database.test/app',
'MY_APP_PASSWORD' => 'application-secret',
'REDIS_PASSWORD' => 'redis-secret',
'SMTP_API_TOKEN' => 'smtp-secret',
'DATABASE_HOST' => 'database.test',
'tokenizer' => 'safe-service',
'passwordless' => true,
],
),
'High-risk environment credential names must be redacted without hiding safe connection metadata.',
);
}
public function testDefaultPolicyBoundsDepthAndNodeTraversal(): void
{
$deep = ['password' => 'secret'];
for ($depth = 0; $depth < 11; $depth++) {
$deep = ['nested' => $deep];
}
$wide = SensitiveDataRedactor::redact(range(1, 10_050));
self::assertStringContainsString(
SensitiveDataRedactor::TRUNCATED,
json_encode(SensitiveDataRedactor::redact($deep), JSON_THROW_ON_ERROR),
'Redaction must stop traversing beyond the depth budget.',
);
self::assertCount(
10_001,
$wide,
'Redaction must retain one explicit marker at the node boundary.',
);
self::assertSame(
SensitiveDataRedactor::TRUNCATED,
$wide[10_000] ?? null,
'Redaction must stop traversing values beyond the node budget.',
);
}
public function testDefaultPolicyPreservesTheExactDepthBoundaryAndLaterSiblings(): void
{
$value = [
'tooDeep' => ['password' => 'secret'],
'after' => 'preserved',
];
$expected = [
'tooDeep' => SensitiveDataRedactor::TRUNCATED,
'after' => 'preserved',
];
for ($depth = 0; $depth < 10; $depth++) {
$value = ['nested' => $value];
$expected = ['nested' => $expected];
}
self::assertSame(
$expected,
SensitiveDataRedactor::redact($value),
'Only arrays beyond depth ten must truncate, without dropping later siblings.',
);
}
public function testLiteralPrefixesAndPatternsAreAdditiveAndConfigurable(): void
{
$value = [
'PRIVATE_ONE' => 'one',
'privateTwo' => 'two',
'credential_123' => 'three',
'public' => 'visible',
];
self::assertSame(
[
'PRIVATE_ONE' => SensitiveDataRedactor::PLACEHOLDER,
'privateTwo' => SensitiveDataRedactor::PLACEHOLDER,
'credential_123' => SensitiveDataRedactor::PLACEHOLDER,
'public' => 'visible',
],
SensitiveDataRedactor::redact(
$value,
[],
['private'],
['~^credential_\d+$~i'],
),
'Literal prefixes and PCRE patterns must augment exact-key matching throughout a value tree.',
);
self::assertTrue(
SensitiveDataRedactor::isSensitiveKey('PrivateValue', [], ['PRIVATE']),
'Literal prefixes must ignore case.',
);
self::assertFalse(
SensitiveDataRedactor::isSensitiveKey('not_credential_123', [], [], ['~^credential_\d+$~i']),
'Configured patterns must retain their caller-defined anchoring semantics.',
);
self::assertFalse(
SensitiveDataRedactor::isSensitiveKey('MY_APP_PASSWORD', SensitiveDataRedactor::DEFAULT_KEYS, [], []),
'An explicit empty pattern list must opt out while retaining exact default keys.',
);
}
public function testRedactNormalizesNestedPublicObjectsBeforeTraversal(): void
{
self::assertSame(
['payload' => ['token' => SensitiveDataRedactor::PLACEHOLDER]],
SensitiveDataRedactor::redact(['payload' => (object) ['token' => 'secret']]),
'Nested public object properties must not bypass key redaction.',
);
}
public function testRedactPreservesNumericKeysAndNonSensitiveValues(): void
{
self::assertSame(
[0 => 'first', 'user' => ['name' => 'Ada']],
SensitiveDataRedactor::redact([0 => 'first', 'user' => ['name' => 'Ada']], ['password']),
'Unmatched values and numeric keys must remain unchanged.',
);
}
public function testRedactReplacesConfiguredKeysCaseInsensitivelyAtEveryDepth(): void
{
self::assertSame(
[
'Password' => SensitiveDataRedactor::PLACEHOLDER,
'nested' => [
'accessToken' => SensitiveDataRedactor::PLACEHOLDER,
'tokenSuffix' => 'visible',
],
],
SensitiveDataRedactor::redact(
[
'Password' => 'secret',
'nested' => [
'accessToken' => 'token',
'tokenSuffix' => 'visible',
],
],
['password', 'ACCESSTOKEN'],
),
'Configured keys must match exactly and without case sensitivity throughout nested arrays.',
);
}
public function testSensitiveKeyMatchingUsesDefaultsOrAnExplicitList(): void
{
self::assertTrue(
SensitiveDataRedactor::isSensitiveKey('AUTHORIZATION'),
'Default key matching must ignore case.',
);
self::assertTrue(
SensitiveDataRedactor::isSensitiveKey('private', ['PRIVATE']),
'Explicit key matching must ignore case.',
);
self::assertFalse(
SensitiveDataRedactor::isSensitiveKey('privateSuffix', ['private']),
'Key matching must remain exact.',
);
}
}