Skip to content

Commit d924fa7

Browse files
committed
Moved force https logic to a middleware; Changed default for config session.secure
1 parent adf0d35 commit d924fa7

File tree

14 files changed

+148
-33
lines changed

14 files changed

+148
-33
lines changed

.env.ci

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ APP_DEBUG=true
55
APP_URL=http://localhost
66
APP_FORCE_HTTPS=false
77
APP_ENABLE_REGISTRATION=true
8-
SESSION_SECURE_COOKIE=false
98

109
# Logging
1110
LOG_CHANNEL=stack

.env.production

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ VITE_APP_NAME=solidtime
55
APP_ENV=production
66
APP_DEBUG=false
77
APP_FORCE_HTTPS=true
8-
SESSION_SECURE_COOKIE=true
98
OCTANE_SERVER=frankenphp
109
PAGINATION_PER_PAGE_DEFAULT=500
1110

app/Filament/Resources/UserResource.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public static function form(Form $form): Form
4242
{
4343
/** @var User|null $record */
4444
$record = $form->getRecord();
45+
4546
return $form
4647
->columns(1)
4748
->schema([

app/Http/Controllers/Web/HealthCheckController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public function debug(Request $request): JsonResponse
6464
$response['app_env'] = app()->environment();
6565
$response['app_timezone'] = config('app.timezone');
6666
$response['app_force_https'] = config('app.force_https');
67+
$response['session_secure'] = config('session.secure');
6768
$response['trusted_proxies'] = config('trustedproxy.proxies');
6869
$headers = $request->headers->all();
6970
if (isset($headers['cookie'])) {

app/Http/Kernel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Kernel extends HttpKernel
1818
* @var array<int, class-string|string>
1919
*/
2020
protected $middleware = [
21-
// \App\Http\Middleware\TrustHosts::class,
21+
\App\Http\Middleware\ForceHttps::class,
2222
\App\Http\Middleware\TrustProxies::class,
2323
\Illuminate\Http\Middleware\HandleCors::class,
2424
\App\Http\Middleware\PreventRequestsDuringMaintenance::class,

app/Http/Middleware/ForceHttps.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Http\Middleware;
6+
7+
use Closure;
8+
use Illuminate\Http\Request;
9+
use Illuminate\Support\Facades\URL;
10+
use Symfony\Component\HttpFoundation\Response;
11+
12+
class ForceHttps
13+
{
14+
/**
15+
* Handle an incoming request.
16+
*
17+
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
18+
*/
19+
public function handle(Request $request, Closure $next, string ...$guards): Response
20+
{
21+
if (config('app.force_https', false)) {
22+
URL::forceScheme('https');
23+
$request->server->set('HTTPS', 'on');
24+
$request->headers->set('X-Forwarded-Proto', 'https');
25+
}
26+
27+
return $next($request);
28+
}
29+
}

app/Http/Middleware/TrustHosts.php

Lines changed: 0 additions & 22 deletions
This file was deleted.

app/Providers/AppServiceProvider.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
use Illuminate\Database\Eloquent\Relations\Relation;
3030
use Illuminate\Foundation\Application;
3131
use Illuminate\Support\Facades\Route;
32-
use Illuminate\Support\Facades\URL;
3332
use Illuminate\Support\ServiceProvider;
3433

3534
class AppServiceProvider extends ServiceProvider
@@ -90,12 +89,6 @@ public function boot(): void
9089
);
9190
});
9291

93-
if (config('app.force_https', false)) {
94-
URL::forceScheme('https');
95-
request()->server->set('HTTPS', 'on');
96-
request()->headers->set('X-Forwarded-Proto', 'https');
97-
}
98-
9992
$this->app->scoped(PermissionStore::class, function (Application $app): PermissionStore {
10093
return new PermissionStore;
10194
});

config/session.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@
168168
|
169169
*/
170170

171-
'secure' => env('SESSION_SECURE_COOKIE'),
171+
'secure' => env('SESSION_SECURE_COOKIE', env('APP_FORCE_HTTPS')),
172172

173173
/*
174174
|--------------------------------------------------------------------------

phpunit.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
</source>
3030
<php>
3131
<env name="APP_ENV" value="testing"/>
32+
<env name="APP_FORCE_HTTPS" value="false"/>
33+
<env name="TRUSTED_PROXIES" value="0.0.0.0/0,2000:0:0:0:0:0:0:0/3"/>
3234
<env name="BCRYPT_ROUNDS" value="4"/>
3335
<env name="CACHE_DRIVER" value="array"/>
3436
<env name="DB_CONNECTION" value="pgsql_test"/>

0 commit comments

Comments
 (0)