A free open source IT asset/license management system
Extend middleware activation for API as well
snipeb3f12f974bb2
diff --git a/app/Http/Kernel.php b/app/Http/Kernel.phpindex 4d24d4a41f..9856d29f71 100644--- a/app/Http/Kernel.php+++ b/app/Http/Kernel.php@@ -83,6 +83,7 @@ class Kernel extends HttpKernel'api' => ['auth:api',+ CheckUserIsActivated::class,EnforceApiTwoFactorEnrollment::class,EnforceApiUserAgent::class,CheckLocale::class,diff --git a/app/Http/Middleware/CheckUserIsActivated.php b/app/Http/Middleware/CheckUserIsActivated.phpindex 945cd906aa..7fb79c14e8 100644--- a/app/Http/Middleware/CheckUserIsActivated.php+++ b/app/Http/Middleware/CheckUserIsActivated.php@@ -2,10 +2,12 @@namespace App\Http\Middleware;+use App\Helpers\Helper;use Closure;use Illuminate\Contracts\Auth\Guard;use Illuminate\Http\Request;use Illuminate\Support\Facades\Auth;+use Symfony\Component\HttpFoundation\Response;class CheckUserIsActivated{@@ -29,22 +31,48 @@ class CheckUserIsActivated/*** Handle an incoming request.*+ * Registered in both the `web` and `api` middleware groups. The web+ * group applied this from the start (the middleware was added to+ * terminate active sessions when a user's activated flag flips off).+ * The api group was missing it, so a deactivated user's already-issued+ * Passport token continued to authenticate and grant full access -+ * defeating "Activated" as an offboarding control and letting a+ * deactivated account with users.edit re-activate itself via the+ * API. This handler now returns the appropriate response for each+ * context: a JSON 401 for API/JSON clients, a session logout ++ * redirect-to-login for browser sessions.+ ** @param Request $request* @return mixed*/public function handle($request, Closure $next){-// If there is a user AND the user is NOT activated, send them to the login page// This prevents people who still have active sessions logged in and their status gets toggled// to inactive (aka unable to login)if (($request->user()) && (! $request->user()->isActivated())) {+ // API clients can't act on a redirect. Bearer tokens are+ // stateless so there's no session to Auth::logout(); this+ // request and every subsequent one from the same token fail+ // here until the account is re-activated. Generic+ // unauthorized message on purpose - do not confirm that the+ // token is otherwise valid and the account is specifically+ // deactivated, since that helps an attacker distinguish a+ // "known token, disabled account" case from an unknown+ // token. From the client's point of view this reads+ // identically to a rejected / expired token.+ if ($request->expectsJson()) {+ return response()->json(+ Helper::formatStandardApiResponse('error', null, trans('general.unauthorized')),+ Response::HTTP_UNAUTHORIZED,+ );+ }+Auth::logout();return redirect()->guest('login');}return $next($request);-}}diff --git a/tests/Feature/Authentication/CheckUserIsActivatedApiTest.php b/tests/Feature/Authentication/CheckUserIsActivatedApiTest.phpnew file mode 100644index 0000000000..28728bb9dd--- /dev/null+++ b/tests/Feature/Authentication/CheckUserIsActivatedApiTest.php@@ -0,0 +1,91 @@+<?php++namespace Tests\Feature\Authentication;++use App\Models\User;+use Laravel\Passport\Passport;+use Tests\TestCase;++/**+ * Confirms that Personal Access Tokens (or any Passport-authenticated API+ * request) can't be used to call the API when the token's owner has been+ * deactivated. The web session path has always been protected by+ * CheckUserIsActivated in the `web` middleware group; the `api` group+ * was missing it, so a deactivated user's already-issued token kept+ * full permission-level access to the API - defeating "Activated" as an+ * offboarding control. A deactivated account with users.edit could+ * additionally re-activate itself via PATCH /api/v1/users/{own_id}.+ * This file pins the fix (CheckUserIsActivated added to the api group).+ */+class CheckUserIsActivatedApiTest extends TestCase+{+ public function test_activated_user_can_use_api(): void+ {+ Passport::actingAs(User::factory()->create(['activated' => 1]));++ $this->getJson(route('api.users.me'))->assertOk();+ }++ public function test_deactivated_user_api_token_is_refused(): void+ {+ Passport::actingAs(User::factory()->create(['activated' => 0]));++ $this->getJson(route('api.users.me'))->assertUnauthorized();+ }++ public function test_deactivated_user_cannot_write_via_api(): void+ {+ Passport::actingAs(User::factory()->superuser()->create(['activated' => 0]));++ $this->postJson(route('api.users.store'), [+ 'first_name' => 'Should',+ 'last_name' => 'Not Persist',+ 'username' => 'shouldnotpersist',+ 'password' => 'This-Is-A-Long-Password-123!',+ 'password_confirmation' => 'This-Is-A-Long-Password-123!',+ ])->assertUnauthorized();++ $this->assertDatabaseMissing('users', ['username' => 'shouldnotpersist']);+ }++ /**+ * Escalation regression pin from the researcher's report: a+ * deactivated account with users.edit used to be able to+ * PATCH /api/v1/users/{own_id} {"activated": 1} and re-enable+ * itself, permanently defeating the deactivation. The middleware+ * now refuses the request before it reaches the controller.+ */+ public function test_deactivated_admin_cannot_self_reactivate_via_api(): void+ {+ $user = User::factory()->admin()->create(['activated' => 0]);++ Passport::actingAs($user);++ $this->patchJson(route('api.users.update', $user), [+ 'activated' => 1,+ ])->assertUnauthorized();++ $this->assertSame(0, (int) $user->fresh()->activated, 'Account must remain deactivated');+ }++ /**+ * Response body deliberately does NOT distinguish "known token,+ * deactivated account" from a generic 401. Locks in the generic+ * message so a later change doesn't leak the specific reason (which+ * would help an attacker validate that a token they hold is+ * otherwise correct and just points at a disabled account).+ */+ public function test_deactivated_response_is_generic_unauthorized(): void+ {+ Passport::actingAs(User::factory()->create(['activated' => 0]));++ $response = $this->getJson(route('api.users.me'))->assertUnauthorized();++ $body = $response->json();+ $this->assertSame('error', $body['status'] ?? null);+ // The generic 'general.unauthorized' translation; must NOT+ // mention "activated" / "disabled" / "inactive".+ $this->assertStringNotContainsStringIgnoringCase('activat', (string) ($body['messages'] ?? ''));+ $this->assertStringNotContainsStringIgnoringCase('disabled', (string) ($body['messages'] ?? ''));+ }+}