From 0378a40c741b15777f8eb1d468c9261117176dbd Mon Sep 17 00:00:00 2001 From: Alex Bouma Date: Thu, 18 Apr 2024 12:23:14 +0200 Subject: [PATCH] Fix username not being a string resulting in exception (#887) * Add tests * Cast username to string --- src/Sentry/Laravel/EventHandler.php | 4 +- test/Sentry/EventHandler/AuthEventsTest.php | 79 +++++++++++++++++++++ 2 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 test/Sentry/EventHandler/AuthEventsTest.php diff --git a/src/Sentry/Laravel/EventHandler.php b/src/Sentry/Laravel/EventHandler.php index 1596042f..3aeaa843 100644 --- a/src/Sentry/Laravel/EventHandler.php +++ b/src/Sentry/Laravel/EventHandler.php @@ -274,12 +274,14 @@ private function configureUserScopeFromModel($authUser): void // If the user is a Laravel Eloquent model we try to extract some common fields from it if ($authUser instanceof Model) { + $username = $authUser->getAttribute('username'); + $userData = [ 'id' => $authUser instanceof Authenticatable ? $authUser->getAuthIdentifier() : $authUser->getKey(), 'email' => $authUser->getAttribute('email') ?? $authUser->getAttribute('mail'), - 'username' => $authUser->getAttribute('username'), + 'username' => $username === null ? $username : (string)$username, ]; } diff --git a/test/Sentry/EventHandler/AuthEventsTest.php b/test/Sentry/EventHandler/AuthEventsTest.php new file mode 100644 index 00000000..1f5769d2 --- /dev/null +++ b/test/Sentry/EventHandler/AuthEventsTest.php @@ -0,0 +1,79 @@ + true, + ]; + + public function testAuthenticatedEventFillsUserOnScope(): void + { + $user = new AuthEventsTestUserModel(); + + $user->id = 123; + $user->username = 'username'; + $user->email = 'foo@example.com'; + + $scope = $this->getCurrentSentryScope(); + + $this->assertNull($scope->getUser()); + + $this->dispatchLaravelEvent(new Authenticated('test', $user)); + + $this->assertNotNull($scope->getUser()); + + $this->assertEquals($scope->getUser()->getId(), 123); + $this->assertEquals($scope->getUser()->getUsername(), 'username'); + $this->assertEquals($scope->getUser()->getEmail(), 'foo@example.com'); + } + + public function testAuthenticatedEventFillsUserOnScopeWhenUsernameIsNotAString(): void + { + $user = new AuthEventsTestUserModel(); + + $user->id = 123; + $user->username = 456; + + $scope = $this->getCurrentSentryScope(); + + $this->assertNull($scope->getUser()); + + $this->dispatchLaravelEvent(new Authenticated('test', $user)); + + $this->assertNotNull($scope->getUser()); + + $this->assertEquals($scope->getUser()->getId(), 123); + $this->assertEquals($scope->getUser()->getUsername(), '456'); + } + + public function testAuthenticatedEventDoesNotFillUserOnScopeWhenPIIShouldNotBeSent(): void + { + $this->resetApplicationWithConfig([ + 'sentry.send_default_pii' => false, + ]); + + $user = new AuthEventsTestUserModel(); + + $user->id = 123; + + $scope = $this->getCurrentSentryScope(); + + $this->assertNull($scope->getUser()); + + $this->dispatchLaravelEvent(new Authenticated('test', $user)); + + $this->assertNull($scope->getUser()); + } +} + +class AuthEventsTestUserModel extends Model implements Authenticatable +{ + use \Illuminate\Auth\Authenticatable; +}