-
Notifications
You must be signed in to change notification settings - Fork 24
/
ServeFile.php
60 lines (53 loc) · 1.67 KB
/
ServeFile.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
namespace Illuminate\Filesystem;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use League\Flysystem\PathTraversalDetected;
class ServeFile
{
/**
* Create a new invokable controller to serve files.
*/
public function __construct(
protected string $disk,
protected array $config,
protected bool $isProduction,
) {
//
}
/**
* Handle the incoming request.
*/
public function __invoke(Request $request, string $path)
{
abort_unless(
$this->hasValidSignature($request),
$this->isProduction ? 404 : 403
);
try {
abort_unless(Storage::disk($this->disk)->exists($path), 404);
$headers = [
'Cache-Control' => 'no-store, no-cache, must-revalidate, max-age=0',
'Content-Security-Policy' => "default-src 'none'; style-src 'unsafe-inline'; sandbox",
];
return tap(
Storage::disk($this->disk)->serve($request, $path, headers: $headers),
function ($response) use ($headers) {
if (! $response->headers->has('Content-Security-Policy')) {
$response->headers->replace($headers);
}
}
);
} catch (PathTraversalDetected $e) {
abort(404);
}
}
/**
* Determine if the request has a valid signature if applicable.
*/
protected function hasValidSignature(Request $request): bool
{
return ($this->config['visibility'] ?? 'private') === 'public' ||
$request->hasValidRelativeSignature();
}
}