diff --git a/src/Watchers/CacheWatcher.php b/src/Watchers/CacheWatcher.php index f5434fbd3..19f03014b 100644 --- a/src/Watchers/CacheWatcher.php +++ b/src/Watchers/CacheWatcher.php @@ -42,7 +42,7 @@ public function recordCacheHit(CacheHit $event) Telescope::recordCache(IncomingEntry::make([ 'type' => 'hit', 'key' => $event->key, - 'value' => $event->value, + 'value' => $this->formatValue($event), ])); } @@ -79,7 +79,7 @@ public function recordKeyWritten(KeyWritten $event) Telescope::recordCache(IncomingEntry::make([ 'type' => 'set', 'key' => $event->key, - 'value' => $event->value, + 'value' => $this->formatValue($event), 'expiration' => $this->formatExpiration($event), ])); } @@ -102,6 +102,33 @@ public function recordKeyForgotten(KeyForgotten $event) ])); } + /** + * Determine the value of an event. + * + * @param mixed $event + * @return mixed + */ + private function formatValue($event) + { + return (! $this->shouldHideValue($event)) + ? $event->value + : '********'; + } + + /** + * Determine if the event value should be ignored. + * + * @param mixed $event + * @return bool + */ + private function shouldHideValue($event) + { + return Str::is( + $this->options['hidden'] ?? [], + $event->key + ); + } + /** * @param \Illuminate\Cache\Events\KeyWritten $event * @return mixed diff --git a/tests/Watchers/CacheWatcherTest.php b/tests/Watchers/CacheWatcherTest.php index 18cf1ce25..2580ab633 100644 --- a/tests/Watchers/CacheWatcherTest.php +++ b/tests/Watchers/CacheWatcherTest.php @@ -15,7 +15,12 @@ protected function getEnvironmentSetUp($app) parent::getEnvironmentSetUp($app); $app->get('config')->set('telescope.watchers', [ - CacheWatcher::class => true, + CacheWatcher::class => [ + 'enabled' => true, + 'hidden' => [ + 'my-hidden-value-key', + ], + ], ]); } @@ -76,4 +81,34 @@ public function test_cache_watcher_registers_forget_entries() $this->assertSame('forget', $entry->content['type']); $this->assertSame('outdated', $entry->content['key']); } + + public function test_cache_watcher_hides_hidden_values_when_set() + { + $this->app->get(Repository::class)->put('my-hidden-value-key', 'laravel', 1); + + $entry = $this->loadTelescopeEntries()->first(); + + $this->assertSame(EntryType::CACHE, $entry->type); + $this->assertSame('set', $entry->content['type']); + $this->assertSame('my-hidden-value-key', $entry->content['key']); + $this->assertSame('********', $entry->content['value']); + } + + public function test_cache_watcher_hides_hidden_values_when_retrieved() + { + $repository = $this->app->get(Repository::class); + + Telescope::withoutRecording(function () use ($repository) { + $repository->put('my-hidden-value-key', 'laravel', 1); + }); + + $repository->get('my-hidden-value-key'); + + $entry = $this->loadTelescopeEntries()->first(); + + $this->assertSame(EntryType::CACHE, $entry->type); + $this->assertSame('hit', $entry->content['type']); + $this->assertSame('my-hidden-value-key', $entry->content['key']); + $this->assertSame('********', $entry->content['value']); + } }