Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

fix: normalize oauth emails before matching users

Tristan Rhodes519a186e841d
diff --git a/app/Http/Controllers/OauthController.php b/app/Http/Controllers/OauthController.php
index 3a3f18c9c4..4038fe63e2 100644
--- a/app/Http/Controllers/OauthController.php
+++ b/app/Http/Controllers/OauthController.php
@@ -19,7 +19,12 @@ class OauthController extends Controller
{
try {
$oauthUser = get_socialite_provider($provider)->user();
- $user = User::whereEmail($oauthUser->email)->first();
+ $email = trim((string) $oauthUser->email);
+ if ($email === '') {
+ abort(403, 'OAuth provider did not return an email address');
+ }
+ $email = strtolower($email);
+ $user = User::whereEmail($email)->first();
if (! $user) {
$settings = instanceSettings();
if (! $settings->is_registration_enabled) {
@@ -28,7 +33,7 @@ class OauthController extends Controller
$user = User::create([
'name' => $oauthUser->name,
- 'email' => $oauthUser->email,
+ 'email' => $email,
]);
}
Auth::login($user);
diff --git a/tests/Feature/OauthControllerTest.php b/tests/Feature/OauthControllerTest.php
new file mode 100644
index 0000000000..af5fb0658f
--- /dev/null
+++ b/tests/Feature/OauthControllerTest.php
@@ -0,0 +1,79 @@
+<?php
+
+use App\Models\InstanceSettings;
+use App\Models\OauthSetting;
+use App\Models\User;
+use Illuminate\Foundation\Testing\RefreshDatabase;
+use Laravel\Socialite\Facades\Socialite;
+
+uses(RefreshDatabase::class);
+
+beforeEach(function () {
+ InstanceSettings::create([
+ 'id' => 0,
+ 'is_registration_enabled' => false,
+ ]);
+
+ OauthSetting::create([
+ 'provider' => 'google',
+ 'client_id' => 'client-id',
+ 'client_secret' => 'client-secret',
+ 'redirect_uri' => 'https://coolify.example.com/auth/google/callback',
+ 'tenant' => 'example.com',
+ ]);
+});
+
+it('logs in an existing user when the oauth provider returns a mixed-case email', function () {
+ config()->set('app.maintenance.driver', 'file');
+
+ $user = User::factory()->create([
+ 'email' => '[email protected]',
+ ]);
+
+ $provider = \Mockery::mock();
+ $provider->shouldReceive('setConfig')->once()->andReturnSelf();
+ $provider->shouldReceive('with')->once()->with(['hd' => 'example.com'])->andReturnSelf();
+ $provider->shouldReceive('user')->once()->andReturn((object) [
+ 'email' => '[email protected]',
+ 'name' => 'Example User',
+ 'id' => 'google-user-id',
+ ]);
+
+ Socialite::shouldReceive('driver')->once()->with('google')->andReturn($provider);
+
+ $response = $this->get(route('auth.callback', 'google'));
+
+ $response->assertRedirect('/');
+ $this->assertAuthenticatedAs($user);
+ expect(User::count())->toBe(1);
+});
+
+it('rejects oauth logins when the provider does not return an email address', function (?string $providerEmail) {
+ config()->set('app.maintenance.driver', 'file');
+ InstanceSettings::firstOrCreate([
+ 'id' => 0,
+ ], [
+ 'is_registration_enabled' => false,
+ ])->update([
+ 'is_registration_enabled' => true,
+ ]);
+
+ $provider = \Mockery::mock();
+ $provider->shouldReceive('setConfig')->once()->andReturnSelf();
+ $provider->shouldReceive('with')->once()->with(['hd' => 'example.com'])->andReturnSelf();
+ $provider->shouldReceive('user')->once()->andReturn((object) [
+ 'email' => $providerEmail,
+ 'name' => 'Example User',
+ 'id' => 'google-user-id',
+ ]);
+
+ Socialite::shouldReceive('driver')->once()->with('google')->andReturn($provider);
+
+ $response = $this->from('/login')->get(route('auth.callback', 'google'));
+
+ $response->assertRedirect('/login');
+ expect(User::count())->toBe(0);
+})->with([
+ 'null email' => [null],
+ 'blank email' => [' '],
+]);