Fixes the nine findings of security audit run 1 on 2.4.1 (T1-T11). F1: Uploads that are not complete now count only the bytes the server received, and a chunk that goes above the storage quota gets 422. An anonymous uploader can no longer fill the quota with declared sizes. F2: The share password and the admin login have a second limit without the address (20 attempts per minute per share and per e-mail), because a forged X-Forwarded-For changes the address. F3: The system password gate and the admin check now also run on Livewire updates, so an old uploader snapshot cannot register files. F4: The session keeps a fingerprint of the system password's hash. When the admin changes the system password, earlier verifications end. F5: The page after the upload opens only in the session that created the share, and only while recipients can still reach the share. F6: The share password limit counts an attempt before the password is checked, so parallel attempts cannot pass it. F7: The system password has the same two limits (5 per address, 20 in total per minute). F8: A password change or reset signs the user's other sessions out. F9: The only admin cannot delete the account, which would open /setup to every visitor. The setup wizard also holds the admin password to the password defaults, and Livewire applies EnsureAdmin to updates. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
116 lines
4.4 KiB
PHP
116 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire;
|
|
|
|
use App\Models\Share;
|
|
use App\Services\ShareService;
|
|
use Carbon\CarbonInterval;
|
|
use Illuminate\Support\Facades\RateLimiter;
|
|
use Livewire\Attributes\Layout;
|
|
use Livewire\Attributes\Locked;
|
|
use Livewire\Attributes\Renderless;
|
|
use Livewire\Attributes\Validate;
|
|
use Livewire\Component;
|
|
|
|
#[Layout('layouts.app')]
|
|
class ShareDownload extends Component
|
|
{
|
|
public Share $share;
|
|
|
|
/** Set only by mount() and verifyPassword(): the browser must never unlock the file list. */
|
|
#[Locked]
|
|
public bool $authenticated = false;
|
|
|
|
#[Validate('required|string')]
|
|
public string $password = '';
|
|
|
|
public function mount(Share $share, ShareService $shareService): void
|
|
{
|
|
$this->share = $share->load('files');
|
|
|
|
// A share at its download limit stays open for the recipient who took its last download.
|
|
if (! $share->isCompleted() || $share->isExpired()
|
|
|| ($share->hasReachedDownloadLimit() && $shareService->downloadWindowEndsAt($share, session()->driver()) === null)) {
|
|
abort(404);
|
|
}
|
|
|
|
$this->authenticated = ! $share->isPasswordProtected() || (bool) session('share_key_'.$share->token);
|
|
}
|
|
|
|
public function verifyPassword(ShareService $shareService): void
|
|
{
|
|
$ipKey = 'share-password:'.$this->share->token.'|'.request()->ip();
|
|
$shareKey = 'share-password:'.$this->share->token;
|
|
|
|
// Count before the slow hash check, so parallel requests cannot all pass a read-only check.
|
|
$ipHits = RateLimiter::hit($ipKey, 60);
|
|
$shareHits = RateLimiter::hit($shareKey, 60);
|
|
|
|
if ($ipHits > 5 || $shareHits > 20) {
|
|
$seconds = max(
|
|
$ipHits > 5 ? RateLimiter::availableIn($ipKey) : 0,
|
|
$shareHits > 20 ? RateLimiter::availableIn($shareKey) : 0,
|
|
);
|
|
$this->addError('password', __('Too many attempts. Please try again in :seconds seconds.', ['seconds' => $seconds]));
|
|
|
|
return;
|
|
}
|
|
|
|
$this->validate();
|
|
|
|
if (! $shareService->verifyPassword($this->share, $this->password)) {
|
|
$this->addError('password', __('The password is incorrect.'));
|
|
|
|
return;
|
|
}
|
|
|
|
RateLimiter::clear($ipKey);
|
|
RateLimiter::clear($shareKey);
|
|
|
|
$encryptionKey = $shareService->getDecryptionKey($this->share, $this->password);
|
|
session(['share_key_'.$this->share->token => $encryptionKey]);
|
|
$this->authenticated = true;
|
|
}
|
|
|
|
/**
|
|
* The share's private text, for the recipient who pressed "Show text": counted as their
|
|
* download, as a file would be. Returned to the browser only, never kept in a property, so it
|
|
* is not in the component's snapshot. Null when the text is no longer available; no abort(),
|
|
* which would open Livewire's error modal. Renderless: a re-render would morph the download
|
|
* note Alpine just switched back into the one it hid.
|
|
*/
|
|
#[Renderless]
|
|
public function revealText(ShareService $shareService): ?string
|
|
{
|
|
$share = $this->share;
|
|
|
|
if (! $share->isCompleted() || $share->isExpired() || ! $share->hasText()
|
|
|| ($share->isPasswordProtected() && ! session('share_key_'.$share->token))
|
|
|| ! $shareService->claimDownload($share, session()->driver())) {
|
|
return null;
|
|
}
|
|
|
|
return $shareService->readText($share, $shareService->sessionDecryptionKey($share, session()->driver()));
|
|
}
|
|
|
|
public function render(): mixed
|
|
{
|
|
$shareService = app(ShareService::class);
|
|
$files = $this->share->files->where('is_text', false)->values();
|
|
|
|
return view('livewire.share-download', [
|
|
'files' => $files,
|
|
'downloads' => $files->map(fn ($file): array => [
|
|
'id' => $file->id,
|
|
'path' => $file->relative_path ?: $file->original_name,
|
|
'size' => $file->file_size,
|
|
'url' => route('share.download.file', [$this->share, $file]),
|
|
]),
|
|
'hasText' => $this->share->files->contains('is_text', true),
|
|
'downloadWindowEndsAt' => $shareService->downloadWindowEndsAt($this->share, session()->driver()),
|
|
'remainingDownloads' => $this->share->max_downloads ? max($this->share->max_downloads - $this->share->download_count, 0) : null,
|
|
'downloadWindow' => CarbonInterval::minutes(ShareService::DOWNLOAD_WINDOW_MINUTES)->cascade()->forHumans(),
|
|
]);
|
|
}
|
|
}
|