Loheide-eu-mitarbeiterplatt.../app/Http/Controllers/UploadController.php
Friederich Loheide d91570f118
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 7m25s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 6m46s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 7m52s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 8m5s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 10m33s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 10m53s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 9m1s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 8m49s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 54s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m4s
Dateien nur mit Anmeldung, nur Microsoft-Login, mehrere Titelbilder (0.47.0)
- /uploads nur über das Portal: UploadController prüft Sitzung bzw. App-Token;
  frei nur die Dateien der Anmeldeseite (Logos, Symbole, Anmeldehintergrund,
  Stylesheet, Schrift); public/uploads/.htaccess leitet alles an index.php
  (Migration schreibt sie, da der Ordner vor dem Updater geschützt ist);
  Systemzustand prüft die Regel; nginx-Hinweis in docs/SECURITY.md
- Passwort-Anmeldung abschaltbar (PasswordLogin): Login-Formular, Passwort
  vergessen, App-Token und /api/v1/config; nur mit verknüpftem Konto
  abschaltbar, automatische Rückkehr ohne nutzbaren Dienst, Notausgang
  INHAUS_PASSWORD_LOGIN
- Titelbilder: profile_covers (bis 12), zufällig je Seitenaufruf
- ImageCompressor: Uploads im Backend (SafeUpload) und WordPress-Import auf
  ≤ 2400 px komprimiert, Format bleibt

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

81 lines
3.1 KiB
PHP
Raw 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() || 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;
}
}