Skip to content

Commit

Permalink
Fix username not being a string resulting in exception (#887)
Browse files Browse the repository at this point in the history
* Add tests

* Cast username to string
  • Loading branch information
stayallive committed Apr 18, 2024
1 parent fefacb9 commit 0378a40
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Sentry/Laravel/EventHandler.php
Expand Up @@ -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,
];
}

Expand Down
79 changes: 79 additions & 0 deletions test/Sentry/EventHandler/AuthEventsTest.php
@@ -0,0 +1,79 @@
<?php

namespace Sentry\EventHandler;

use Illuminate\Auth\Events\Authenticated;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Sentry\Laravel\Tests\TestCase;

class AuthEventsTest extends TestCase
{
protected $setupConfig = [
'sentry.send_default_pii' => 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;
}

0 comments on commit 0378a40

Please sign in to comment.