From 6b3a5ef98803c91390137f16cb21932e95b46d75 Mon Sep 17 00:00:00 2001 From: roble Date: Sat, 7 Nov 2026 12:55:10 +0000 Subject: [PATCH 1/2] fix: never link a social sign-in to an existing unverified account (sc-1130) Co-Authored-By: Claude Opus 5.5 --- lang/en/socialite.php | 1 + .../saucebase-auth-development/SKILL.md | 1 + src/Exceptions/SocialiteException.php | 5 +++++ src/Services/SocialiteService.php | 4 ++++ tests/Feature/SocialiteCallbackTest.php | 19 +++++++++++++++++++ 5 files changed, 30 insertions(+) diff --git a/lang/en/socialite.php b/lang/en/socialite.php index 094b865..c575baa 100644 --- a/lang/en/socialite.php +++ b/lang/en/socialite.php @@ -25,4 +25,5 @@ 'account_already_linked' => 'This :provider account is already linked to another user', 'unsupported_provider' => 'The social provider :provider is not supported', 'registration_disabled' => 'New account registration is currently disabled', + 'unverified_account' => 'An account with this email already exists. Sign in the way you did before, then connect this provider from your settings.', ]; diff --git a/resources/boost/skills/saucebase-auth-development/SKILL.md b/resources/boost/skills/saucebase-auth-development/SKILL.md index 5dbb428..23d173b 100644 --- a/resources/boost/skills/saucebase-auth-development/SKILL.md +++ b/resources/boost/skills/saucebase-auth-development/SKILL.md @@ -237,6 +237,7 @@ npx playwright test --project="@auth*" # E2E - `LoginRequest::validateCredentials()` validates without logging in — the controller handles `Auth::login()` separately - Social users get a random password and are **not** marked verified — signing in with a provider is not proof of the address, and nobody has confirmed it (sc-713) +- A guest's social sign-in never links to an existing **unverified** account with the same email (`SocialiteException::unverifiedAccount()`): anyone can register with someone else's address, and linking would hand that account, and whoever knows its password, to the provider's owner. They sign in as before and connect the provider from Settings (sc-1130) - Socialite redirect/callback routes are outside both guest and auth middleware groups - `RegisterRequest::passedValidation()` hashes the password before the controller sees it - Filament UserResource enforces single role (maxItems: 1) despite multi-select UI diff --git a/src/Exceptions/SocialiteException.php b/src/Exceptions/SocialiteException.php index 8cbb87b..ecdf7b0 100644 --- a/src/Exceptions/SocialiteException.php +++ b/src/Exceptions/SocialiteException.php @@ -31,6 +31,11 @@ public static function registrationDisabled(): self return new self(trans('auth::socialite.registration_disabled')); } + public static function unverifiedAccount(): self + { + return new self(trans('auth::socialite.unverified_account')); + } + public static function unsupportedProvider(string $provider): self { return new self(trans('auth::socialite.unsupported_provider', ['provider' => ucfirst($provider)])); diff --git a/src/Services/SocialiteService.php b/src/Services/SocialiteService.php index 0beb8d6..2e29f89 100644 --- a/src/Services/SocialiteService.php +++ b/src/Services/SocialiteService.php @@ -100,6 +100,10 @@ public function handleCallback(string $provider): User } $user = $this->createNewUser($socialiteUser, $avatarUrl); + } elseif (! $user->hasVerifiedEmail()) { + // Anyone can sign up with someone else's address; linking would hand this + // account to the provider's owner while its password may be another's. + throw SocialiteException::unverifiedAccount(); } else { $this->updateUserAvatar($user, $avatarUrl); } diff --git a/tests/Feature/SocialiteCallbackTest.php b/tests/Feature/SocialiteCallbackTest.php index 05b84c3..f600c00 100644 --- a/tests/Feature/SocialiteCallbackTest.php +++ b/tests/Feature/SocialiteCallbackTest.php @@ -104,6 +104,25 @@ public function test_social_callback_still_logs_in_existing_user_when_registrati $this->assertAuthenticatedAs($user); } + /** + * An unconfirmed address proves nothing about who made the account: anyone can sign up + * with someone else's email. Linking would hand the provider's owner an account whose + * password someone else may know. + */ + public function test_social_callback_does_not_link_to_an_unverified_account(): void + { + $this->enableGithub(); + + $user = User::factory()->unverified()->create(); + $this->mockSocialiteDriver($this->makeSocialiteUser(email: $user->email)); + + $this->get(route('auth.socialite.callback', ['provider' => 'github'])) + ->assertRedirect(route('login')); + + $this->assertGuest(); + $this->assertDatabaseMissing('social_accounts', ['user_id' => $user->id]); + } + public function test_callback_sets_last_social_provider_cookie(): void { $this->enableGithub(); From f2084de7051f4d195a9d64927a0f7bb6dc00aba3 Mon Sep 17 00:00:00 2001 From: roble Date: Sat, 7 Nov 2026 15:13:32 +0000 Subject: [PATCH 2/2] fix: give social sign-ups no password and encrypt provider tokens (sc-1132) A random password defeated disconnectProvider()'s only-sign-in guard, so a social-only user could remove their provider and lock themselves out. Provider tokens are now cast encrypted and hidden; the upgrade migration encrypts stored rows, turns empty values into null and skips rows already encrypted, so a retry is safe. Co-Authored-By: Claude Opus 5.5 --- ...0_000007_encrypt_social_account_tokens.php | 74 +++++++++++++++++++ .../saucebase-auth-development/SKILL.md | 3 +- src/Models/SocialAccount.php | 12 ++- src/Services/SocialiteService.php | 6 +- ...ncryptSocialAccountTokensMigrationTest.php | 66 +++++++++++++++++ tests/Feature/SocialiteCallbackTest.php | 46 +++++++++++- tests/Unit/SocialiteServiceTest.php | 4 +- 7 files changed, 201 insertions(+), 10 deletions(-) create mode 100644 database/migrations/1000_00_00_000007_encrypt_social_account_tokens.php create mode 100644 tests/Feature/EncryptSocialAccountTokensMigrationTest.php diff --git a/database/migrations/1000_00_00_000007_encrypt_social_account_tokens.php b/database/migrations/1000_00_00_000007_encrypt_social_account_tokens.php new file mode 100644 index 0000000..34fd9ce --- /dev/null +++ b/database/migrations/1000_00_00_000007_encrypt_social_account_tokens.php @@ -0,0 +1,74 @@ +text('provider_refresh_token')->nullable()->change(); + }); + + // MySQL runs this outside a transaction, so a retry after a crash part-way finds + // some rows already encrypted: those are left alone rather than encrypted twice. + $this->rewrite(fn (string $value): string => $this->isEncrypted($value) ? $value : Crypt::encryptString($value)); + } + + public function down(): void + { + $this->rewrite(fn (string $value): string => $this->isEncrypted($value) ? Crypt::decryptString($value) : $value); + + Schema::table('social_accounts', function (Blueprint $table) { + $table->string('provider_refresh_token')->nullable()->change(); + }); + } + + private function isEncrypted(string $value): bool + { + try { + Crypt::decryptString($value); + + return true; + } catch (DecryptException) { + return false; + } + } + + /** @param Closure(string): string $convert */ + private function rewrite(Closure $convert): void + { + DB::table('social_accounts')->orderBy('id')->chunkById(500, function ($rows) use ($convert): void { + foreach ($rows as $row) { + $changes = []; + + foreach (self::COLUMNS as $column) { + $value = $row->{$column}; + + // Empty becomes null: the cast skips decrypting null and nothing else. + if ($value === '') { + $changes[$column] = null; + } elseif ($value !== null) { + $changes[$column] = $convert($value); + } + } + + if ($changes !== []) { + DB::table('social_accounts')->where('id', $row->id)->update($changes); + } + } + }); + } +}; diff --git a/resources/boost/skills/saucebase-auth-development/SKILL.md b/resources/boost/skills/saucebase-auth-development/SKILL.md index 5dbb428..5b4b8ca 100644 --- a/resources/boost/skills/saucebase-auth-development/SKILL.md +++ b/resources/boost/skills/saucebase-auth-development/SKILL.md @@ -236,7 +236,8 @@ npx playwright test --project="@auth*" # E2E ## Gotchas - `LoginRequest::validateCredentials()` validates without logging in — the controller handles `Auth::login()` separately -- Social users get a random password and are **not** marked verified — signing in with a provider is not proof of the address, and nobody has confirmed it (sc-713) +- Social sign-ups get **no** password (`users.password` is nullable), so `disconnectProvider()` refuses to remove their only provider until they add one; a made-up password let them lock themselves out (sc-1132). They are **not** marked verified: signing in with a provider is not proof of the address, and nobody has confirmed it (sc-713) +- `SocialAccount::provider_token` and `provider_refresh_token` are cast `encrypted` and `$hidden`: they let the app act on the provider as the user. `1000_00_00_000007` encrypts rows stored before the cast (sc-1132) - Socialite redirect/callback routes are outside both guest and auth middleware groups - `RegisterRequest::passedValidation()` hashes the password before the controller sees it - Filament UserResource enforces single role (maxItems: 1) despite multi-select UI diff --git a/src/Models/SocialAccount.php b/src/Models/SocialAccount.php index 2fbad24..4400060 100644 --- a/src/Models/SocialAccount.php +++ b/src/Models/SocialAccount.php @@ -13,8 +13,8 @@ * @property int $user_id * @property string $provider * @property string $provider_id - * @property string $provider_token - * @property string $provider_refresh_token + * @property string|null $provider_token + * @property string|null $provider_refresh_token * @property Carbon|null $provider_token_expires_at * @property string|null $provider_avatar_url * @property Carbon|null $last_login_at @@ -36,9 +36,17 @@ class SocialAccount extends Model 'last_login_at', ]; + /** They let the app act on the provider as the user. */ + protected $hidden = [ + 'provider_token', + 'provider_refresh_token', + ]; + protected function casts(): array { return [ + 'provider_token' => 'encrypted', + 'provider_refresh_token' => 'encrypted', 'provider_token_expires_at' => 'datetime', 'last_login_at' => 'datetime', ]; diff --git a/src/Services/SocialiteService.php b/src/Services/SocialiteService.php index 0beb8d6..ba59bf6 100644 --- a/src/Services/SocialiteService.php +++ b/src/Services/SocialiteService.php @@ -5,8 +5,6 @@ use App\Models\User; use Illuminate\Auth\Events\Registered; use Illuminate\Support\Facades\DB; -use Illuminate\Support\Facades\Hash; -use Illuminate\Support\Str; use Laravel\Socialite\Facades\Socialite; use Laravel\Socialite\Two\User as SocialiteUser; use Modules\Auth\Exceptions\SocialiteException; @@ -198,7 +196,9 @@ private function createNewUser(SocialiteUser $socialiteUser, ?string $avatarUrl) $user = User::create([ 'name' => $socialiteUser->getName() ?: $socialiteUser->getNickname(), 'email' => $socialiteUser->getEmail(), - 'password' => Hash::make(Str::random(32)), // Random password + // None: the provider is their way in until they add one, which is what stops + // them disconnecting it (disconnectProvider) and locking themselves out. + 'password' => null, 'avatar' => $avatarUrl, ]); diff --git a/tests/Feature/EncryptSocialAccountTokensMigrationTest.php b/tests/Feature/EncryptSocialAccountTokensMigrationTest.php new file mode 100644 index 0000000..ede53a3 --- /dev/null +++ b/tests/Feature/EncryptSocialAccountTokensMigrationTest.php @@ -0,0 +1,66 @@ +createUser(); + $this->migration()->down(); + + $id = DB::table('social_accounts')->insertGetId([ + 'user_id' => $user->id, + 'provider' => 'github', + 'provider_id' => 'legacy-1', + 'provider_token' => 'plain-access', + 'provider_refresh_token' => 'plain-refresh', + 'created_at' => now(), + 'updated_at' => now(), + ]); + DB::table('social_accounts')->insert([ + 'user_id' => $user->id, + 'provider' => 'google', + 'provider_id' => 'legacy-2', + 'provider_token' => null, + 'provider_refresh_token' => '', + 'created_at' => now(), + 'updated_at' => now(), + ]); + + $this->migration()->up(); + + $this->assertNotSame('plain-access', DB::table('social_accounts')->where('id', $id)->value('provider_token')); + $account = SocialAccount::findOrFail($id); + $this->assertSame('plain-access', $account->provider_token); + $this->assertSame('plain-refresh', $account->provider_refresh_token); + $this->assertNull(SocialAccount::where('provider_id', 'legacy-2')->sole()->provider_token); + // Empty is stored as null: the cast only skips decrypting null. + $this->assertNull(SocialAccount::where('provider_id', 'legacy-2')->sole()->provider_refresh_token); + } + + /** MySQL runs it outside a transaction: a retry after a crash part-way must not encrypt twice. */ + public function test_running_it_again_leaves_encrypted_tokens_alone(): void + { + $account = SocialAccount::factory()->create(['provider_token' => 'kept', 'provider_refresh_token' => 'kept-too']); + + $this->migration()->up(); + + $account->refresh(); + $this->assertSame('kept', $account->provider_token); + $this->assertSame('kept-too', $account->provider_refresh_token); + } +} diff --git a/tests/Feature/SocialiteCallbackTest.php b/tests/Feature/SocialiteCallbackTest.php index 05b84c3..a8ba7b2 100644 --- a/tests/Feature/SocialiteCallbackTest.php +++ b/tests/Feature/SocialiteCallbackTest.php @@ -6,12 +6,15 @@ use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\Crypt; +use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Exceptions; use Illuminate\Support\Facades\Notification; use Laravel\Socialite\Facades\Socialite; use Laravel\Socialite\Two\AbstractProvider; use Laravel\Socialite\Two\User as SocialiteUser; use Mockery\MockInterface; +use Modules\Auth\Models\SocialAccount; use Modules\Auth\Notifications\LoginNotification; use Modules\Auth\Settings\AuthSettings; use Saucebase\Core\Facades\Home; @@ -206,8 +209,8 @@ public function test_a_signed_in_user_links_the_provider_account(): void 'user_id' => $user->id, 'provider' => 'github', 'provider_id' => 'gh-42', - 'provider_refresh_token' => 'refresh-token', ]); + $this->assertSame('refresh-token', SocialAccount::where('provider_id', 'gh-42')->sole()->provider_refresh_token); $this->assertSame(trans('auth::socialite.account_connected', ['provider' => 'Github']), session('toast')['message']); } @@ -282,6 +285,45 @@ public function test_a_new_social_user_stores_the_refresh_token(): void $this->get(route('auth.socialite.callback', ['provider' => 'github'])); - $this->assertDatabaseHas('social_accounts', ['provider_id' => 'gh-7', 'provider_refresh_token' => 'refresh-token']); + $account = SocialAccount::where('provider_id', 'gh-7')->sole(); + $this->assertSame('refresh-token', $account->provider_refresh_token); + $this->assertSame('access-token', $account->provider_token); + } + + /** They let the app act on the provider as the user: never stored readable, never serialised. */ + public function test_provider_tokens_are_encrypted_at_rest_and_hidden(): void + { + $this->enableGithub(); + $this->mockSocialiteDriver($this->makeSocialiteUser(id: 'gh-8')); + + $this->get(route('auth.socialite.callback', ['provider' => 'github'])); + + $row = DB::table('social_accounts')->where('provider_id', 'gh-8')->sole(); + $this->assertNotSame('access-token', $row->provider_token); + $this->assertNotSame('refresh-token', $row->provider_refresh_token); + $this->assertSame('access-token', Crypt::decryptString($row->provider_token)); + + $serialised = SocialAccount::where('provider_id', 'gh-8')->sole()->toArray(); + $this->assertArrayNotHasKey('provider_token', $serialised); + $this->assertArrayNotHasKey('provider_refresh_token', $serialised); + } + + /** + * A provider sign-up sets no password, so the provider stays its only way in until the + * user adds one: a made-up password would let them disconnect it and lock themselves out. + */ + public function test_a_social_sign_up_has_no_password_and_cannot_drop_its_only_sign_in(): void + { + $this->enableGithub(); + $this->mockSocialiteDriver($this->makeSocialiteUser(id: 'gh-9', email: 'only@example.com')); + + $this->get(route('auth.socialite.callback', ['provider' => 'github'])); + + $user = User::where('email', 'only@example.com')->sole(); + $this->assertNull($user->password); + + $this->actingAs($user)->delete(route('auth.socialite.disconnect', ['provider' => 'github'])); + + $this->assertDatabaseHas('social_accounts', ['user_id' => $user->id, 'provider' => 'github']); } } diff --git a/tests/Unit/SocialiteServiceTest.php b/tests/Unit/SocialiteServiceTest.php index 9d9c21b..a0974bb 100644 --- a/tests/Unit/SocialiteServiceTest.php +++ b/tests/Unit/SocialiteServiceTest.php @@ -133,8 +133,8 @@ public function test_link_account_updates_tokens_when_same_user_re_links(): void 'user_id' => $user->id, 'provider' => 'google', 'provider_id' => 'provider-123', - 'provider_token' => 'new-token', ]); + $this->assertSame('new-token', SocialAccount::where('provider_id', 'provider-123')->sole()->provider_token); $this->assertDatabaseCount('social_accounts', 1); } @@ -261,8 +261,8 @@ public function test_handle_callback_returns_existing_user_by_provider_id(): voi $this->assertEquals($existingUser->id, $user->id); $this->assertDatabaseHas('social_accounts', [ 'provider_id' => 'existing-provider-id', - 'provider_token' => 'updated-token', ]); + $this->assertSame('updated-token', SocialAccount::where('provider_id', 'existing-provider-id')->sole()->provider_token); Event::assertNotDispatched(Registered::class); }