Skip to content

Commit 25b5bd2

Browse files
Merge pull request #41 from utopia-php/feat-mask-fields
feat: mask fields
2 parents 7e8ff51 + 6c7449d commit 25b5bd2

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

src/Logger/Log.php

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ class Log
8282
*/
8383
protected array $breadcrumbs = [];
8484

85+
/**
86+
* @var array<string> (optional)
87+
*/
88+
protected array $masked = [];
89+
8590
/**
8691
* Log constructor.
8792
*/
@@ -299,7 +304,7 @@ public function addTag(string $key, string $value): void
299304
*/
300305
public function getTags(): array
301306
{
302-
return $this->tags;
307+
return $this->mask($this->tags);
303308
}
304309

305310
/**
@@ -321,7 +326,7 @@ public function addExtra(string $key, mixed $value): void
321326
*/
322327
public function getExtra(): array
323328
{
324-
return $this->extra;
329+
return $this->mask($this->extra);
325330
}
326331

327332
/**
@@ -365,4 +370,39 @@ public function getBreadcrumbs(): array
365370
{
366371
return $this->breadcrumbs;
367372
}
373+
374+
/**
375+
* Set masked fields, which will be replaced by asterisks
376+
*
377+
* @param array<string> $masked
378+
* @return void
379+
*/
380+
public function setMasked(array $masked): void
381+
{
382+
$this->masked = $masked;
383+
}
384+
385+
/**
386+
* @template T
387+
*
388+
* @param array<string, T> $data
389+
* @return array<string, T|string>
390+
*/
391+
private function mask(array $data): array
392+
{
393+
$masked = [];
394+
395+
foreach ($data as $key => $value) {
396+
if (is_string($value) && in_array($key, $this->masked, true)) {
397+
$masked[$key] = str_repeat('*', strlen($value));
398+
} elseif (is_array($value)) {
399+
$maskedValue = $this->mask($value); /** @var T $maskedValue */
400+
$masked[$key] = $maskedValue;
401+
} else {
402+
$masked[$key] = $value;
403+
}
404+
}
405+
406+
return $masked;
407+
}
368408
}

tests/unit/LogTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,31 @@ public function testLog(): void
7272
self::assertEquals('DELETE /api/v1/database/abcd1234/efgh5678', $log->getBreadcrumbs()[0]->getMessage());
7373
self::assertEquals($timestamp, $log->getBreadcrumbs()[0]->getTimestamp());
7474
}
75+
76+
public function testLogMasked(): void
77+
{
78+
$log = new Log();
79+
80+
$log->addTag('password', '123456');
81+
$log->addExtra('name', 'John Doe');
82+
83+
self::assertEquals(['password' => '123456'], $log->getTags());
84+
self::assertEquals(['name' => 'John Doe'], $log->getExtra());
85+
86+
$log->setMasked(['password', 'name']);
87+
88+
self::assertEquals(['password' => '******'], $log->getTags());
89+
self::assertEquals(['name' => '********'], $log->getExtra());
90+
91+
// test nested array
92+
$log->addExtra('user', ['password' => 'abc']);
93+
94+
self::assertEquals(['password' => '***'], $log->getExtra()['user']);
95+
96+
// test remove mask
97+
$log->setMasked([]);
98+
99+
self::assertEquals(['password' => '123456'], $log->getTags());
100+
self::assertEquals(['name' => 'John Doe', 'user' => ['password' => 'abc']], $log->getExtra());
101+
}
75102
}

0 commit comments

Comments
 (0)