Harden the app: block project files, add rate limits and security headers

- Root .htaccess refuses everything outside public/, because local Apache
  serves the whole project folder (.env, logs with set-password links, .git).
- Login allows 20 failed attempts a minute per IP across accounts, besides
  5 per email and IP; the forgot-password page 5 requests a minute per IP.
- A global middleware sends X-Frame-Options, frame-ancestors, nosniff,
  Referrer-Policy, Permissions-Policy and HSTS over HTTPS, and drops
  X-Powered-By.
- The weekly time sheet locks the shown week and selected day, so the
  browser cannot set them.
- Users who may not see personal identity codes can save a new code at
  most 10 times an hour, so the duplicate check cannot be used to test codes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-11-24 06:25:26 +02:00
co-authored by Claude Opus 5.5
parent fcad0ea939
commit 1acdaa5b19
15 changed files with 229 additions and 8 deletions
@@ -7,6 +7,8 @@ use App\Modules\Customers\Domain\Enums\CustomerType;
use App\Modules\Customers\Domain\Exceptions\CustomerNotFound;
use App\Modules\Customers\Presentation\Livewire\Forms\CustomerForm;
use App\Modules\Shared\Domain\Exceptions\DomainException;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\RateLimiter;
use Livewire\Attributes\Locked;
use Livewire\Component;
@@ -15,6 +17,13 @@ use Livewire\Component;
*/
new class extends Component
{
/**
* Saves with a new personal identity code per hour for users who may not
* see codes. "Already in use" tells whether a person is a customer, so
* the form must not be usable to test many codes.
*/
public const int MAX_IDENTITY_CODE_SAVES_PER_HOUR = 10;
#[Locked]
public ?int $customerId = null;
@@ -52,6 +61,13 @@ new class extends Component
$this->authorize('customers.view-personal-identity-code');
}
if (! $this->identityCodeAttemptAllowed()) {
$this->resetErrorBag('form.personal_identity_code');
$this->addError('form.personal_identity_code', __('customers.errors.personal_identity_code_throttled'));
return;
}
$data = $this->form->toData();
try {
@@ -75,6 +91,27 @@ new class extends Component
$this->redirectRoute('customers.show', $customerId, navigate: true);
}
/**
* Counts a save that carries a new personal identity code; admins and
* management, who see the codes anyway, are not limited.
*/
private function identityCodeAttemptAllowed(): bool
{
if (trim($this->form->personal_identity_code) === '' || Gate::allows('customers.view-personal-identity-code')) {
return true;
}
$key = 'identity-code-save|'.auth()->id();
if (RateLimiter::tooManyAttempts($key, self::MAX_IDENTITY_CODE_SAVES_PER_HOUR)) {
return false;
}
RateLimiter::hit($key, 3600);
return true;
}
public function render(): mixed
{
return $this->view()->title(__($this->customerId === null ? 'customers.create_title' : 'customers.edit_title'));