Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/Masking/FieldMasker.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,15 @@ public function mask(array $data): array
string: $value,
);

$parts[1] = $this->star(
string: $parts[1],
);
if (count($parts) >= 2) {
for ($i = 1; $i < count($parts); $i++) {
$parts[$i] = $this->star(
string: $parts[$i]
);
}
} else {
$parts[0] = $this->star($parts[0]);
}

$value = implode(' ', $parts);
} else {
Expand Down
113 changes: 113 additions & 0 deletions tests/Masking/FieldMaskerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,116 @@
'foo' => 'bar',
]);
});

it('can handle a single Authorization entry', function () {
$masker = new FieldMasker(
fields: ['password', 'api_key', 'cc'],
);

expect($masker->mask(
data: [
'form' => [
'password' => 'password',
'api_key' => 'test',
],
'Authorization' => '123123123123123',
'X-API-KEY' => '1234-1234-4321',
'cc' => '1234-1234-1234-1234',
'foo' => 'bar',
],
))->toBeArray()->toEqual([
'form' => [
'password' => '********',
'api_key' => '****',
],
'Authorization' => '***************',
'X-API-KEY' => '**************',
'cc' => '*******************',
'foo' => 'bar',
]);
});

it('can handle a two Authorization entries', function () {
$masker = new FieldMasker(
fields: ['password', 'api_key', 'cc'],
);

expect($masker->mask(
data: [
'form' => [
'password' => 'password',
'api_key' => 'test',
],
'Authorization' => 'Bearer 123123123123123',
'X-API-KEY' => '1234-1234-4321',
'cc' => '1234-1234-1234-1234',
'foo' => 'bar',
],
))->toBeArray()->toEqual([
'form' => [
'password' => '********',
'api_key' => '****',
],
'Authorization' => 'Bearer ***************',
'X-API-KEY' => '**************',
'cc' => '*******************',
'foo' => 'bar',
]);
});


it('can handle a multiple Authorization entries', function () {
$masker = new FieldMasker(
fields: ['password', 'api_key', 'cc'],
);

expect($masker->mask(
data: [
'form' => [
'password' => 'password',
'api_key' => 'test',
],
'Authorization' => 'Bearer 123123123123123 123',
'X-API-KEY' => '1234-1234-4321',
'cc' => '1234-1234-1234-1234',
'foo' => 'bar',
],
))->toBeArray()->toEqual([
'form' => [
'password' => '********',
'api_key' => '****',
],
'Authorization' => 'Bearer *************** ***',
'X-API-KEY' => '**************',
'cc' => '*******************',
'foo' => 'bar',
]);
});

it('can handle a malformed Authorization entry', function () {
$masker = new FieldMasker(
fields: ['password', 'api_key', 'cc'],
);

expect($masker->mask(
data: [
'form' => [
'password' => 'password',
'api_key' => 'test',
],
'Authorization' => 'Alien',
'X-API-KEY' => '1234-1234-4321',
'cc' => '1234-1234-1234-1234',
'foo' => 'bar',
],
))->toBeArray()->toEqual([
'form' => [
'password' => '********',
'api_key' => '****',
],
'Authorization' => '*****',
'X-API-KEY' => '**************',
'cc' => '*******************',
'foo' => 'bar',
]);
});