A free open source IT asset/license management system
Fixed FD-55230 / hr9m-63rg - reject HTTP_ headers in REMOTE_LOGIN
snipea5927ca21e97
diff --git a/app/Http/Controllers/Auth/LoginController.php b/app/Http/Controllers/Auth/LoginController.phpindex d19cd16b9d..5e3e6fd45d 100644--- a/app/Http/Controllers/Auth/LoginController.php+++ b/app/Http/Controllers/Auth/LoginController.php@@ -230,6 +230,28 @@ class LoginController extends Controllerprivate function loginViaRemoteUser(Request $request){$header_name = Setting::getSettings()->login_remote_user_header_name ?: 'REMOTE_USER';++ // Defensive runtime guard against HTTP_-prefixed server variable+ // names, which PHP populates directly from inbound request headers.+ // Honoring one would let any unauthenticated caller spoof the+ // header and get logged in as any active user (pre-auth takeover).+ // Settings-side validation blocks this at save time now, but a+ // pre-fix install may already have an HTTP_-prefixed value+ // persisted, so refuse to use it at runtime regardless. Reported+ // by Brayden Arnold. See advisory for details.+ if (preg_match('/^HTTP_/i', $header_name)) {+ Log::warning(sprintf(+ 'Refusing to honor HTTP_-prefixed login_remote_user_header_name "%s". '.+ 'PHP populates $_SERVER[HTTP_*] from inbound request headers, so this value '.+ 'would allow any client to forge the auth header. Change the setting to '.+ 'REMOTE_USER (default) or to a server variable your upstream sets from a '.+ 'vetted source.',+ $header_name,+ ));++ return;+ }+$remote_user = $request->server($header_name);if (! isset($remote_user)) {$remote_user = $request->server('REDIRECT_'.$header_name);@@ -459,6 +481,7 @@ class LoginController extends Controller// too, with enrolled still at 0.if (strlen((string) $user->two_factor_secret) < 16) {\Log::debug('two_factor_secret is too short to be valid, redirecting to enrollment page');+return redirect()->route('two-factor-enroll');}diff --git a/app/Http/Requests/StoreSecuritySettings.php b/app/Http/Requests/StoreSecuritySettings.phpindex 31e237e658..1ed85757c5 100644--- a/app/Http/Requests/StoreSecuritySettings.php+++ b/app/Http/Requests/StoreSecuritySettings.php@@ -30,7 +30,25 @@ class StoreSecuritySettings extends FormRequest'login_remote_user_enabled' => 'numeric|nullable','login_common_disabled' => 'numeric|nullable','login_remote_user_custom_logout_url' => 'string|nullable',- 'login_remote_user_header_name' => 'string|nullable',+ // Reject HTTP_-prefixed server variable names. PHP populates+ // $_SERVER[HTTP_*] directly from inbound request headers, so+ // configuring a HTTP_-prefixed name here lets any unauthenticated+ // caller spoof the header and get logged in as any active user,+ // including superuser (pre-auth takeover). Legitimate values are+ // REMOTE_USER (default, populated by Apache mod_auth_*), or a+ // name that the operator's upstream proxy / FastCGI explicitly+ // sets from a vetted source. Reported by Brayden Arnold.+ 'login_remote_user_header_name' => 'string|nullable|not_regex:/^HTTP_/i',+ ];+ }++ /**+ * @return array<string, string>+ */+ public function messages(): array+ {+ return [+ 'login_remote_user_header_name.not_regex' => trans('admin/settings/general.login_remote_user_header_http_prefix_rejected'),];}}diff --git a/resources/lang/en-US/admin/settings/general.php b/resources/lang/en-US/admin/settings/general.phpindex 3b36c442b5..beb4fffc46 100644--- a/resources/lang/en-US/admin/settings/general.php+++ b/resources/lang/en-US/admin/settings/general.php@@ -571,7 +571,8 @@ return ['too_many_users_to_show' => 'The number of users (:count) is larger than the unpaginated record limit (:max). Use the bulk user edit tool to manage group memberships.','update_existing_dates' => 'Also update all of the existing "next audit dates" for assets with an upcoming audit with the Audit Interval above. This is most commonly used when the internal policy for audit interval has changed.','remote_user_legend_warning' => 'Use with caution! Incorrectly configuring this section could lock you and your users out of the system or worse yet automatically log users in as a specific user.',- 'login_remote_user_header_name_help' => 'If your web server is configured to send a custom header with the username, you can specify that header here. For example, if your web server sends a header called <code>X-Remote-User</code> with the username, you would enter <code>HTTP_X_REMOTE_USER</code> here. If this field is left blank, the system will use the default <code>REMOTE_USER</code> header. <strong>Misconfiguring this could lock you and all other users out of the system, or could auto-login EVERYONE into your system as a superadmin user!</strong>',+ 'login_remote_user_header_name_help' => 'The server variable your upstream (Apache <code>mod_auth_*</code>, Nginx auth_request, Shibboleth, Google IAP, etc.) sets with the authenticated username. Leave blank to use the default <code>REMOTE_USER</code>. <strong>Do NOT use an <code>HTTP_</code>-prefixed value such as <code>HTTP_X_REMOTE_USER</code>.</strong> PHP populates <code>$_SERVER[HTTP_*]</code> directly from inbound request headers, so a value like that lets any unauthenticated client forge the header and get logged in as any active user, including a superuser. If you need to key off a custom header, configure your upstream proxy to copy that header into a non-<code>HTTP_</code> server variable (e.g. with Apache <code>SetEnvIfExpr</code> or Nginx <code>fastcgi_param</code>) and reference the server-side variable here.',+ 'login_remote_user_header_http_prefix_rejected' => 'The remote-user header name cannot start with HTTP_. PHP populates $_SERVER[HTTP_*] from inbound HTTP headers, so an HTTP_-prefixed value would let any unauthenticated client forge the header and log in as any active user. Use REMOTE_USER (the default) or a server variable your upstream sets from a vetted source.','username_formats' => ['username_format' => 'Username Format',diff --git a/resources/views/settings/security.blade.php b/resources/views/settings/security.blade.phpindex 47684a9dc3..ea485269d5 100644--- a/resources/views/settings/security.blade.php+++ b/resources/views/settings/security.blade.php@@ -57,7 +57,7 @@</x-slot:input></x-form.row>- <x-form.legend>+ <x-form.legend help_text="{{ trans('admin/settings/general.pwd_secure_complexity_help') }}">{{ trans('admin/settings/general.legends.passwords') }}</x-form.legend>@@ -83,7 +83,6 @@<x-form.rowname="pwd_secure_complexity":label="trans('admin/settings/general.pwd_secure_complexity')"- :help_text="trans('admin/settings/general.pwd_secure_complexity_help')"><x-slot:input><label class="form-control">diff --git a/tests/Feature/Authentication/RemoteUserHeaderSpoofingTest.php b/tests/Feature/Authentication/RemoteUserHeaderSpoofingTest.phpnew file mode 100644index 0000000000..f13bace8ea--- /dev/null+++ b/tests/Feature/Authentication/RemoteUserHeaderSpoofingTest.php@@ -0,0 +1,127 @@+<?php++namespace Tests\Feature\Authentication;++use App\Models\Setting;+use App\Models\User;+use Tests\TestCase;++/**+ * Regression coverage for the REMOTE_USER-header spoofing vulnerability+ * reported by Brayden Arnold. `LoginController::loginViaRemoteUser` read+ * `$request->server($header_name)` where `$header_name` was any string+ * an admin configured. PHP populates `$_SERVER[HTTP_*]` directly from+ * inbound HTTP headers, so configuring an HTTP_-prefixed value let any+ * unauthenticated client send the matching request header and get+ * logged in as any active user, including superuser (pre-auth+ * takeover).+ *+ * Fix is two layers:+ *+ * 1. Settings-side validation rejects HTTP_-prefixed values at save+ * time (StoreSecuritySettings::rules), so admins can't configure+ * the dangerous shape on new installs.+ * 2. Runtime guard in `loginViaRemoteUser` refuses to honor an+ * HTTP_-prefixed header name even when one is already persisted+ * (covers upgrade paths from a pre-fix install).+ */+class RemoteUserHeaderSpoofingTest extends TestCase+{+ public function test_settings_save_rejects_http_prefixed_header_name(): void+ {+ $superuser = User::factory()->superuser()->create();++ $this->actingAs($superuser)+ ->post(route('settings.security.save'), [+ 'pwd_secure_min' => 10,+ 'login_remote_user_enabled' => 1,+ 'login_remote_user_header_name' => 'HTTP_X_AUTH_USER',+ ])+ ->assertSessionHasErrors('login_remote_user_header_name');++ $this->assertNotSame(+ 'HTTP_X_AUTH_USER',+ Setting::getSettings()->fresh()->login_remote_user_header_name,+ 'HTTP_-prefixed header name must not persist.',+ );+ }++ public function test_settings_save_rejects_lowercase_http_prefix_too(): void+ {+ // Case-insensitive match catches "http_" too. PHP wouldn't+ // actually populate $_SERVER['http_x_auth_user'] from an+ // inbound header (keys are canonical uppercase), but blocking+ // it anyway heads off confused admins who think a lowercase+ // variant is somehow safer.+ $superuser = User::factory()->superuser()->create();++ $this->actingAs($superuser)+ ->post(route('settings.security.save'), [+ 'pwd_secure_min' => 10,+ 'login_remote_user_enabled' => 1,+ 'login_remote_user_header_name' => 'http_x_auth_user',+ ])+ ->assertSessionHasErrors('login_remote_user_header_name');+ }++ public function test_settings_save_allows_remote_user_default(): void+ {+ $superuser = User::factory()->superuser()->create();++ $this->actingAs($superuser)+ ->post(route('settings.security.save'), [+ 'pwd_secure_min' => 10,+ 'login_remote_user_enabled' => 1,+ 'login_remote_user_header_name' => 'REMOTE_USER',+ ])+ ->assertSessionDoesntHaveErrors('login_remote_user_header_name');+ }++ public function test_runtime_refuses_http_prefixed_header_even_when_already_persisted(): void+ {+ // Simulate an upgrade path from a pre-fix install where the+ // dangerous value was saved before the validation rule landed.+ // Settings persisted via query builder so the new validation+ // can't block it. The runtime guard inside loginViaRemoteUser+ // must still refuse to honor the value.+ $victim = User::factory()->superuser()->create(['username' => 'super']);++ \DB::table('settings')->update([+ 'login_remote_user_enabled' => 1,+ 'login_remote_user_header_name' => 'HTTP_X_AUTH_USER',+ ]);+ Setting::$_cache = null;++ $this->withServerVariables(['HTTP_X_AUTH_USER' => 'super'])+ ->get('/login')+ ->assertOk();++ $this->assertGuest(+ null,+ 'HTTP_-prefixed header name must not auto-login the user even when persisted pre-fix.',+ );+ $this->assertNotSame($victim->id, auth()->id(), 'No session should have been created from the spoofed header.');+ }++ public function test_legitimate_remote_user_login_still_works(): void+ {+ // Baseline: the fix must not break REMOTE_USER-based auth for+ // installs using Apache mod_auth_* or similar upstreams that+ // set REMOTE_USER server-side. The server variable is set+ // directly (not from an inbound header) via withServerVariables,+ // which mirrors what a legitimate upstream would do.+ $user = User::factory()->create(['username' => 'alice']);++ \DB::table('settings')->update([+ 'login_remote_user_enabled' => 1,+ 'login_remote_user_header_name' => 'REMOTE_USER',+ ]);+ Setting::$_cache = null;++ $this->withServerVariables(['REMOTE_USER' => 'alice'])+ ->get('/login')+ ->assertRedirect('/');++ $this->assertAuthenticatedAs($user);+ }+}