'image/webp', 'png' => 'image/png', 'jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'gif' => 'image/gif', 'svg' => 'image/svg+xml', 'ico' => 'image/x-icon', 'avif' => 'image/avif', 'css' => 'text/css', 'woff' => 'font/woff', 'woff2' => 'font/woff2', ]; /** Theme-Felder, die zwar Bilder sind, aber nicht auf die Anmeldeseite gehören */ private const PRIVATE_THEME_FIELDS = ['profile_cover', 'profile_covers', 'avatar_placeholder']; public function __invoke(Request $request, string $path): Response { $extension = strtolower(pathinfo($path, PATHINFO_EXTENSION)); abort_if(str_contains($path, '..') || str_contains($path, "\0") || ! isset(self::TYPES[$extension]), 404); $public = $this->isLoginAsset($path); // Nicht angemeldet: nicht verraten, ob es die Datei gibt abort_unless($public || $request->user() || auth('sanctum')->user(), 404); $disk = Storage::disk('uploads'); $root = realpath($disk->path('')); $file = realpath($disk->path($path)); abort_unless($root && $file && str_starts_with($file, $root.DIRECTORY_SEPARATOR) && is_file($file), 404); $response = new BinaryFileResponse($file, 200, [ 'Content-Type' => self::TYPES[$extension], 'X-Content-Type-Options' => 'nosniff', // SVG-Dateien dürfen kein Skript ausführen, auch wenn man sie direkt öffnet 'Content-Security-Policy' => "default-src 'none'; img-src 'self' data:; style-src 'unsafe-inline'; font-src 'self'; sandbox", // Dateinamen ändern sich mit jedem Upload; geschützte Dateien nur im eigenen Browser zwischenspeichern 'Cache-Control' => ($public ? 'public' : 'private').', max-age=31536000, immutable', ], $public, null, true, true); $response->isNotModified($request); return $response; } private function isLoginAsset(string $path): bool { if (! str_starts_with($path, 'theme/')) { return false; } $theme = app(Theme::class)->published(); foreach (self::PRIVATE_THEME_FIELDS as $field) { unset($theme[$field]); } $found = false; array_walk_recursive($theme, function ($value) use ($path, &$found) { $found = $found || $value === $path; }); return $found; } }