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.

refactor(auth): upgrade email verification hash to sha256

Andras Bacsai49b5472961dc
diff --git a/app/Http/Controllers/Controller.php b/app/Http/Controllers/Controller.php
index a6b7f6440b..6ce6b6d571 100644
--- a/app/Http/Controllers/Controller.php
+++ b/app/Http/Controllers/Controller.php
@@ -6,8 +6,8 @@ use App\Events\TestEvent;
use App\Models\TeamInvitation;
use App\Models\User;
use App\Providers\RouteServiceProvider;
+use Illuminate\Auth\Events\Verified;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
-use Illuminate\Foundation\Auth\EmailVerificationRequest;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
@@ -39,9 +39,29 @@ class Controller extends BaseController
return view('auth.verify-email');
}
- public function email_verify(EmailVerificationRequest $request)
+ public function email_verify(Request $request)
{
- $request->fulfill();
+ if (! $request->hasValidSignature()) {
+ abort(403);
+ }
+
+ $user = auth()->user();
+ if (! $user) {
+ abort(403);
+ }
+
+ if (! hash_equals((string) $request->route('id'), (string) $user->getKey())) {
+ abort(403);
+ }
+
+ if (! hash_equals((string) $request->route('hash'), hash('sha256', $user->getEmailForVerification()))) {
+ abort(403);
+ }
+
+ if (! $user->hasVerifiedEmail()) {
+ $user->markEmailAsVerified();
+ event(new Verified($user));
+ }
return redirect(RouteServiceProvider::HOME);
}
diff --git a/app/Models/User.php b/app/Models/User.php
index 3199d20244..237f3836f3 100644
--- a/app/Models/User.php
+++ b/app/Models/User.php
@@ -257,7 +257,7 @@ class User extends Authenticatable implements SendsEmail
Carbon::now()->addMinutes(Config::get('auth.verification.expire', 60)),
[
'id' => $this->getKey(),
- 'hash' => sha1($this->getEmailForVerification()),
+ 'hash' => hash('sha256', $this->getEmailForVerification()),
]
);
$mail->view('emails.email-verification', [
diff --git a/tests/Feature/EmailVerificationHashTest.php b/tests/Feature/EmailVerificationHashTest.php
new file mode 100644
index 0000000000..5d42c4e44c
--- /dev/null
+++ b/tests/Feature/EmailVerificationHashTest.php
@@ -0,0 +1,73 @@
+<?php
+
+use App\Http\Middleware\CheckForcePasswordReset;
+use App\Http\Middleware\DecideWhatToDoWithUser;
+use App\Models\InstanceSettings;
+use App\Models\User;
+use Illuminate\Foundation\Testing\RefreshDatabase;
+use Illuminate\Support\Facades\URL;
+use Illuminate\Support\Once;
+
+uses(RefreshDatabase::class);
+
+beforeEach(function () {
+ $this->withoutMiddleware([DecideWhatToDoWithUser::class, CheckForcePasswordReset::class]);
+ Once::flush();
+ if (! InstanceSettings::find(0)) {
+ $settings = new InstanceSettings;
+ $settings->id = 0;
+ $settings->saveQuietly();
+ }
+});
+
+describe('email verification hash', function () {
+ test('sha256 hash is accepted and marks the user verified', function () {
+ $user = User::factory()->create([
+ 'email' => '[email protected]',
+ 'email_verified_at' => null,
+ ]);
+
+ $url = URL::temporarySignedRoute('verify.verify', now()->addHour(), [
+ 'id' => $user->getKey(),
+ 'hash' => hash('sha256', $user->getEmailForVerification()),
+ ]);
+
+ $this->actingAs($user)->get($url)->assertRedirect();
+
+ $user->refresh();
+ expect($user->email_verified_at)->not->toBeNull();
+ });
+
+ test('legacy sha1 hash is rejected', function () {
+ $user = User::factory()->create([
+ 'email' => '[email protected]',
+ 'email_verified_at' => null,
+ ]);
+
+ $url = URL::temporarySignedRoute('verify.verify', now()->addHour(), [
+ 'id' => $user->getKey(),
+ 'hash' => sha1($user->getEmailForVerification()),
+ ]);
+
+ $this->actingAs($user)->get($url)->assertStatus(403);
+
+ $user->refresh();
+ expect($user->email_verified_at)->toBeNull();
+ });
+
+ test('tampered signature is rejected', function () {
+ $user = User::factory()->create([
+ 'email' => '[email protected]',
+ 'email_verified_at' => null,
+ ]);
+
+ $url = URL::temporarySignedRoute('verify.verify', now()->addHour(), [
+ 'id' => $user->getKey(),
+ 'hash' => hash('sha256', $user->getEmailForVerification()),
+ ]);
+
+ $tampered = $url.'x';
+
+ $this->actingAs($user)->get($tampered)->assertStatus(403);
+ });
+});