-
-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved force https logic to a middleware; Changed default for config s…
…ession.secure
- Loading branch information
Showing
14 changed files
with
147 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Middleware; | ||
|
||
use Closure; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\URL; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
class ForceHttps | ||
{ | ||
/** | ||
* Handle an incoming request. | ||
* | ||
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next | ||
*/ | ||
public function handle(Request $request, Closure $next, string ...$guards): Response | ||
{ | ||
if (config('app.force_https', false)) { | ||
URL::forceScheme('https'); | ||
$request->server->set('HTTPS', 'on'); | ||
$request->headers->set('X-Forwarded-Proto', 'https'); | ||
} | ||
|
||
return $next($request); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Unit\Middleware; | ||
|
||
use App\Http\Middleware\ForceHttps; | ||
use Illuminate\Support\Facades\Config; | ||
use Illuminate\Support\Facades\Route; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\Attributes\UsesClass; | ||
|
||
#[CoversClass(ForceHttps::class)] | ||
#[UsesClass(ForceHttps::class)] | ||
class ForceHttpsMiddlewareTest extends MiddlewareTestAbstract | ||
{ | ||
private function createTestRoute(): string | ||
{ | ||
return Route::get('/test-route', function () { | ||
return [ | ||
'is_secure' => request()->secure(), | ||
]; | ||
})->middleware(ForceHttps::class)->uri; | ||
} | ||
|
||
public function test_if_config_app_force_https_is_true_then_the_request_will_be_modified_to_make_the_app_think_it_was_a_https_request(): void | ||
{ | ||
// Arrange | ||
Config::set('app.force_https', true); | ||
$route = $this->createTestRoute(); | ||
|
||
// Act | ||
$response = $this->get($route); | ||
|
||
// Assert | ||
$response->assertSuccessful(); | ||
$response->assertJson(['is_secure' => true]); | ||
} | ||
|
||
public function test_if_config_app_force_https_is_true_then_the_request_will_be_modified_to_make_the_app_think_it_was_a_https_request_even_if_a_load_balancer_says_it_was_a_http_request(): void | ||
{ | ||
// Arrange | ||
Config::set('app.force_https', true); | ||
$route = $this->createTestRoute(); | ||
|
||
// Act | ||
$response = $this->get($route, ['X-Forwarded-Proto' => 'http']); | ||
|
||
// Assert | ||
$response->assertSuccessful(); | ||
$response->assertJson(['is_secure' => true]); | ||
} | ||
|
||
public function test_if_config_app_force_https_is_false_then_the_request_will_not_be_modified_to_make_the_app_think_it_was_a_https_request(): void | ||
{ | ||
// Arrange | ||
Config::set('app.force_https', false); | ||
$route = $this->createTestRoute(); | ||
|
||
// Act | ||
$response = $this->get($route); | ||
|
||
// Assert | ||
$response->assertSuccessful(); | ||
$response->assertJson(['is_secure' => false]); | ||
} | ||
|
||
public function test_if_config_app_force_https_is_false_then_the_request_will_not_be_modified_but_the_request_can_still_be_https(): void | ||
{ | ||
// Arrange | ||
Config::set('app.force_https', false); | ||
$route = $this->createTestRoute(); | ||
|
||
// Act | ||
$response = $this->get($route, ['X-Forwarded-Proto' => 'https']); | ||
|
||
// Assert | ||
$response->assertSuccessful(); | ||
$response->assertJson(['is_secure' => true]); | ||
} | ||
} |