Skip to content

Commit efe56a8

Browse files
Merge pull request #2 from Treblle/feature/manage-more-complex-auth
ehancing checks around auth headers
2 parents 4e78961 + 2d3e3b7 commit efe56a8

File tree

2 files changed

+122
-3
lines changed

2 files changed

+122
-3
lines changed

src/Masking/FieldMasker.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,15 @@ public function mask(array $data): array
5050
string: $value,
5151
);
5252

53-
$parts[1] = $this->star(
54-
string: $parts[1],
55-
);
53+
if (count($parts) >= 2) {
54+
for ($i = 1; $i < count($parts); $i++) {
55+
$parts[$i] = $this->star(
56+
string: $parts[$i]
57+
);
58+
}
59+
} else {
60+
$parts[0] = $this->star($parts[0]);
61+
}
5662

5763
$value = implode(' ', $parts);
5864
} else {

tests/Masking/FieldMaskerTest.php

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,116 @@
5555
'foo' => 'bar',
5656
]);
5757
});
58+
59+
it('can handle a single Authorization entry', function () {
60+
$masker = new FieldMasker(
61+
fields: ['password', 'api_key', 'cc'],
62+
);
63+
64+
expect($masker->mask(
65+
data: [
66+
'form' => [
67+
'password' => 'password',
68+
'api_key' => 'test',
69+
],
70+
'Authorization' => '123123123123123',
71+
'X-API-KEY' => '1234-1234-4321',
72+
'cc' => '1234-1234-1234-1234',
73+
'foo' => 'bar',
74+
],
75+
))->toBeArray()->toEqual([
76+
'form' => [
77+
'password' => '********',
78+
'api_key' => '****',
79+
],
80+
'Authorization' => '***************',
81+
'X-API-KEY' => '**************',
82+
'cc' => '*******************',
83+
'foo' => 'bar',
84+
]);
85+
});
86+
87+
it('can handle a two Authorization entries', function () {
88+
$masker = new FieldMasker(
89+
fields: ['password', 'api_key', 'cc'],
90+
);
91+
92+
expect($masker->mask(
93+
data: [
94+
'form' => [
95+
'password' => 'password',
96+
'api_key' => 'test',
97+
],
98+
'Authorization' => 'Bearer 123123123123123',
99+
'X-API-KEY' => '1234-1234-4321',
100+
'cc' => '1234-1234-1234-1234',
101+
'foo' => 'bar',
102+
],
103+
))->toBeArray()->toEqual([
104+
'form' => [
105+
'password' => '********',
106+
'api_key' => '****',
107+
],
108+
'Authorization' => 'Bearer ***************',
109+
'X-API-KEY' => '**************',
110+
'cc' => '*******************',
111+
'foo' => 'bar',
112+
]);
113+
});
114+
115+
116+
it('can handle a multiple Authorization entries', function () {
117+
$masker = new FieldMasker(
118+
fields: ['password', 'api_key', 'cc'],
119+
);
120+
121+
expect($masker->mask(
122+
data: [
123+
'form' => [
124+
'password' => 'password',
125+
'api_key' => 'test',
126+
],
127+
'Authorization' => 'Bearer 123123123123123 123',
128+
'X-API-KEY' => '1234-1234-4321',
129+
'cc' => '1234-1234-1234-1234',
130+
'foo' => 'bar',
131+
],
132+
))->toBeArray()->toEqual([
133+
'form' => [
134+
'password' => '********',
135+
'api_key' => '****',
136+
],
137+
'Authorization' => 'Bearer *************** ***',
138+
'X-API-KEY' => '**************',
139+
'cc' => '*******************',
140+
'foo' => 'bar',
141+
]);
142+
});
143+
144+
it('can handle a malformed Authorization entry', function () {
145+
$masker = new FieldMasker(
146+
fields: ['password', 'api_key', 'cc'],
147+
);
148+
149+
expect($masker->mask(
150+
data: [
151+
'form' => [
152+
'password' => 'password',
153+
'api_key' => 'test',
154+
],
155+
'Authorization' => 'Alien',
156+
'X-API-KEY' => '1234-1234-4321',
157+
'cc' => '1234-1234-1234-1234',
158+
'foo' => 'bar',
159+
],
160+
))->toBeArray()->toEqual([
161+
'form' => [
162+
'password' => '********',
163+
'api_key' => '****',
164+
],
165+
'Authorization' => '*****',
166+
'X-API-KEY' => '**************',
167+
'cc' => '*******************',
168+
'foo' => 'bar',
169+
]);
170+
});

0 commit comments

Comments
 (0)