Skip to content

A free open source IT asset/license management system

Fixed #19457 - ignore storage proxy on login

snipe7257602e514b
diff --git a/app/Http/Middleware/CheckForTwoFactor.php b/app/Http/Middleware/CheckForTwoFactor.php
index 40424c72ca..f4b90609c5 100644
--- a/app/Http/Middleware/CheckForTwoFactor.php
+++ b/app/Http/Middleware/CheckForTwoFactor.php
@@ -10,9 +10,21 @@ use Illuminate\Support\Facades\Auth;
class CheckForTwoFactor
{
/**
- * Routes to ignore for Two Factor Auth
+ * Routes to ignore for Two Factor Auth.
+ *
+ * `storage-proxy` is on the list because the /storage-proxy/{path}
+ * route serves the "public" filesystem disk (branding logos, user
+ * avatars, model images) which pages like /two-factor-enroll
+ * reference via `<img src="…">`. Without the exemption, the
+ * browser's image fetches on those pre-2FA-complete pages get
+ * redirected here to /two-factor-enroll (broken images) AND
+ * clobber redirect()->setIntendedUrl() with the storage-proxy URL,
+ * so redirect()->intended() after MFA sends the user to the logo
+ * URL instead of the dashboard. Content on the public disk is
+ * public by design, so bypassing 2FA on this route is safe. See
+ * issue #19457.
*/
- public const IGNORE_ROUTES = ['two-factor', 'two-factor-enroll', 'setup', 'logout'];
+ public const IGNORE_ROUTES = ['two-factor', 'two-factor-enroll', 'setup', 'logout', 'storage-proxy'];
/**
* Whether the *session* attached to this request has cleared 2FA — i.e.
diff --git a/tests/Feature/Authentication/StorageProxyBypassesTwoFactorTest.php b/tests/Feature/Authentication/StorageProxyBypassesTwoFactorTest.php
new file mode 100644
index 0000000000..bf8f0fb290
--- /dev/null
+++ b/tests/Feature/Authentication/StorageProxyBypassesTwoFactorTest.php
@@ -0,0 +1,55 @@
+<?php
+
+namespace Tests\Feature\Authentication;
+
+use App\Models\Setting;
+use App\Models\User;
+use Tests\TestCase;
+
+/**
+ * When 2FA is universally required and the current user hasn't
+ * completed it, /two-factor-enroll and related pages reference
+ * branding images via <img src="/storage-proxy/…"> when PUBLIC_S3_PROXY
+ * is enabled. Those image fetches must NOT be intercepted by
+ * CheckForTwoFactor — otherwise the browser sees broken images AND
+ * the middleware's setIntendedUrl() call clobbers the real intended
+ * URL with a storage-proxy path, causing post-2FA redirects to land
+ * on a logo URL instead of the dashboard. See issue #19457.
+ */
+class StorageProxyBypassesTwoFactorTest extends TestCase
+{
+ public function test_storage_proxy_is_not_redirected_for_a_two_factor_incomplete_session(): void
+ {
+ // 2 = universal mandatory 2FA
+ Setting::factory()->create();
+ $settings = Setting::getSettings();
+ $settings->two_factor_enabled = '2';
+ $settings->save();
+
+ $user = User::factory()->create([
+ 'two_factor_enrolled' => 0,
+ 'two_factor_secret' => null,
+ ]);
+
+ // Sanity check: an ordinary web route DOES get redirected
+ // to /two-factor-enroll, so the middleware is actually
+ // engaged in this test.
+ $this->actingAs($user)
+ ->get('/')
+ ->assertRedirect(route('two-factor-enroll'));
+
+ // The storage-proxy route responds directly (either 200 with
+ // content, 304 not-modified, or 404 for a non-existent file)
+ // rather than 302 to /two-factor-enroll. What we're actually
+ // testing here is the absence of the 2FA-enrollment redirect.
+ $response = $this->actingAs($user)
+ ->get('/storage-proxy/setting-logo-999.svg');
+
+ $this->assertNotEquals(
+ route('two-factor-enroll'),
+ $response->headers->get('Location'),
+ 'storage-proxy must not redirect image requests to the 2FA enroll page'
+ );
+ $this->assertNotSame(302, $response->status(), 'storage-proxy must not 302 for a 2FA-incomplete session');
+ }
+}