Loheide-eu-mitarbeiterplatt.../app/Http/Controllers/UploadController.php
Friederich Loheide b2f5d95d39
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 7m2s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 7m21s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 7m32s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 7m47s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 10m53s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 10m36s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 8m59s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 49s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 9m13s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m13s
App-Anmeldung reparieren, Titelbilder erneut verkleinern (0.47.1)
- Updater baut die Paketliste neu (package:discover); Migration einmalig – auf
  älteren Installationen fehlte Sanctum (API 500)
- UploadController: ohne Sanctum-Guard sauber 404 statt 500
- Systemzustand prüft die Anmeldung der App
- Titelbild-Verkleinerung erneut (lief beim Update auf 0.47.0 mit der alten
  Theme-Klasse im Speicher ins Leere)

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-28 17:48:49 +00:00

87 lines
3.3 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Http\Controllers;
use App\Support\Theme\Theme;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Response;
/**
* Hochgeladene Dateien (public/uploads) nur für angemeldete Personen – Browser-Sitzung oder
* App-Token. Der Webserver reicht jede Anfrage unter /uploads an das Portal weiter
* (public/uploads/.htaccess), direkte Links funktionieren ohne Anmeldung also nicht.
*
* Ausnahme: was die Anmeldeseite braucht (Logos, App-Symbol, Favicons, Hintergrundbild,
* Stylesheet und eigene Schrift des veröffentlichten Erscheinungsbilds).
*/
class UploadController extends Controller
{
/** Ausgelieferte Dateitypen */
public const TYPES = [
'webp' => '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() || $this->appUser(), 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;
}
/** Anmeldung der App (Token) – nur, wenn der Sanctum-Guard geladen ist */
private function appUser(): mixed
{
return config('auth.guards.sanctum') ? auth('sanctum')->user() : null;
}
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;
}
}