Skip to content

Commit 9a23706

Browse files
committed
fix: reject oauth callbacks without provider emails
1 parent 13293e8 commit 9a23706

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

app/Http/Controllers/OauthController.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ public function callback(string $provider)
1919
{
2020
try {
2121
$oauthUser = get_socialite_provider($provider)->user();
22-
$email = strtolower(trim((string) $oauthUser->email));
22+
$email = trim((string) $oauthUser->email);
23+
if ($email === '') {
24+
abort(403, 'OAuth provider did not return an email address');
25+
}
26+
$email = strtolower($email);
2327
$user = User::whereEmail($email)->first();
2428
if (! $user) {
2529
$settings = instanceSettings();

tests/Feature/OauthControllerTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,33 @@
4747
$this->assertAuthenticatedAs($user);
4848
expect(User::count())->toBe(1);
4949
});
50+
51+
it('rejects oauth logins when the provider does not return an email address', function (?string $providerEmail) {
52+
config()->set('app.maintenance.driver', 'file');
53+
InstanceSettings::firstOrCreate([
54+
'id' => 0,
55+
], [
56+
'is_registration_enabled' => false,
57+
])->update([
58+
'is_registration_enabled' => true,
59+
]);
60+
61+
$provider = \Mockery::mock();
62+
$provider->shouldReceive('setConfig')->once()->andReturnSelf();
63+
$provider->shouldReceive('with')->once()->with(['hd' => 'example.com'])->andReturnSelf();
64+
$provider->shouldReceive('user')->once()->andReturn((object) [
65+
'email' => $providerEmail,
66+
'name' => 'Tristan Rhodes',
67+
'id' => 'google-user-id',
68+
]);
69+
70+
Socialite::shouldReceive('driver')->once()->with('google')->andReturn($provider);
71+
72+
$response = $this->from('/login')->get(route('auth.callback', 'google'));
73+
74+
$response->assertRedirect('/login');
75+
expect(User::count())->toBe(0);
76+
})->with([
77+
'null email' => [null],
78+
'blank email' => [' '],
79+
]);

0 commit comments

Comments
 (0)