Skip to content

Commit 12b20c3

Browse files
authored
Return from maintenance middleware early if URL is excluded (#48218)
1 parent 40b6059 commit 12b20c3

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,18 @@ public function __construct(Application $app)
4545
*/
4646
public function handle($request, Closure $next)
4747
{
48+
if ($this->inExceptArray($request)) {
49+
return $next($request);
50+
}
51+
4852
if ($this->app->maintenanceMode()->active()) {
4953
$data = $this->app->maintenanceMode()->data();
5054

5155
if (isset($data['secret']) && $request->path() === $data['secret']) {
5256
return $this->bypassResponse($data['secret']);
5357
}
5458

55-
if ($this->hasValidBypassCookie($request, $data) ||
56-
$this->inExceptArray($request)) {
59+
if ($this->hasValidBypassCookie($request, $data)) {
5760
return $next($request);
5861
}
5962

tests/Integration/Foundation/MaintenanceModeTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,19 @@
1111
use Illuminate\Support\Carbon;
1212
use Illuminate\Support\Facades\Event;
1313
use Illuminate\Support\Facades\Route;
14+
use Orchestra\Testbench\Http\Middleware\PreventRequestsDuringMaintenance as TestbenchPreventRequestsDuringMaintenance;
1415
use Orchestra\Testbench\TestCase;
1516
use Symfony\Component\HttpFoundation\Cookie;
1617

1718
class MaintenanceModeTest extends TestCase
1819
{
20+
protected function setUp(): void
21+
{
22+
parent::setUp();
23+
24+
$this->withoutMiddleware(TestbenchPreventRequestsDuringMaintenance::class);
25+
}
26+
1927
protected function tearDown(): void
2028
{
2129
@unlink(storage_path('framework/down'));
@@ -113,6 +121,25 @@ public function testMaintenanceModeCanBeBypassedWithValidCookie()
113121
$this->assertSame('Hello World', $response->original);
114122
}
115123

124+
public function testMaintenanceModeCanBeBypassedOnExcludedUrls()
125+
{
126+
$this->app->instance(PreventRequestsDuringMaintenance::class, new class($this->app) extends PreventRequestsDuringMaintenance
127+
{
128+
protected $except = ['/test'];
129+
});
130+
131+
file_put_contents(storage_path('framework/down'), json_encode([
132+
'retry' => 60,
133+
]));
134+
135+
Route::get('/test', fn () => 'Hello World')->middleware(PreventRequestsDuringMaintenance::class);
136+
137+
$response = $this->get('/test');
138+
139+
$response->assertStatus(200);
140+
$this->assertSame('Hello World', $response->original);
141+
}
142+
116143
public function testMaintenanceModeCantBeBypassedWithInvalidCookie()
117144
{
118145
file_put_contents(storage_path('framework/down'), json_encode([

0 commit comments

Comments
 (0)