Skip to content

Commit 40cc55a

Browse files
committed
Secure cache handling for logged in users
1 parent 20a25f9 commit 40cc55a

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

.idea/codebuddy.xml

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/Core/Middleware/SetCacheHeaders.php

+17-1
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,19 @@
55
use Closure;
66
use Illuminate\Support\Carbon;
77
use Illuminate\Support\Str;
8+
use Leantime\Core\Services\AuthService;
89
use Symfony\Component\HttpFoundation\BinaryFileResponse;
910
use Symfony\Component\HttpFoundation\StreamedResponse;
1011

1112
class SetCacheHeaders
1213
{
14+
private AuthService $authService;
15+
16+
public function __construct(AuthService $authService)
17+
{
18+
$this->authService = $authService;
19+
}
20+
1321
/**
1422
* Specify the options for the middleware.
1523
*
@@ -39,7 +47,6 @@ public static function using($options)
3947
* Add cache related HTTP headers.
4048
*
4149
* @param \Illuminate\Http\Request $request
42-
* @param \Closure $next
4350
* @param string|array $options
4451
* @return \Symfony\Component\HttpFoundation\Response
4552
*
@@ -49,6 +56,15 @@ public function handle($request, Closure $next, $options = [])
4956
{
5057
$response = $next($request);
5158

59+
// For authenticated routes, set strict no-cache headers
60+
if ($this->authService->loggedIn()) {
61+
$response->headers->set('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0');
62+
$response->headers->set('Pragma', 'no-cache');
63+
$response->headers->set('Expires', 'Sat, 01 Jan 2000 00:00:00 GMT');
64+
65+
return $response;
66+
}
67+
5268
if (! $request->isMethodCacheable() || (! $response->getContent() && ! $response instanceof BinaryFileResponse && ! $response instanceof StreamedResponse)) {
5369
return $response;
5470
}

0 commit comments

Comments
 (0)