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:
@@ -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'));
|
||||
|
||||
@@ -6,6 +6,8 @@ use App\Modules\Shared\Application\Contracts\EventDispatcher;
|
||||
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
||||
use App\Modules\Shared\Infrastructure\Events\LaravelEventDispatcher;
|
||||
use App\Modules\Shared\Infrastructure\Persistence\LaravelTransactionManager;
|
||||
use App\Modules\Shared\Presentation\Http\Middleware\SetSecurityHeaders;
|
||||
use Illuminate\Contracts\Http\Kernel;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class SharedServiceProvider extends ServiceProvider
|
||||
@@ -17,4 +19,9 @@ class SharedServiceProvider extends ServiceProvider
|
||||
TransactionManager::class => LaravelTransactionManager::class,
|
||||
EventDispatcher::class => LaravelEventDispatcher::class,
|
||||
];
|
||||
|
||||
public function boot(): void
|
||||
{
|
||||
$this->app->make(Kernel::class)->pushMiddleware(SetSecurityHeaders::class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Shared\Presentation\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
/**
|
||||
* Browser security headers on every response of the application.
|
||||
*
|
||||
* - No framing by other sites (clickjacking).
|
||||
* - No MIME sniffing.
|
||||
* - Only the origin goes to other sites in Referer, so set-password tokens
|
||||
* in the URL never leave the application.
|
||||
* - HSTS when the request came over HTTPS.
|
||||
*
|
||||
* A full Content-Security-Policy is left out: Alpine and Livewire need
|
||||
* 'unsafe-eval', which would take away most of its protection.
|
||||
*/
|
||||
class SetSecurityHeaders
|
||||
{
|
||||
public const int HSTS_MAX_AGE = 31536000;
|
||||
|
||||
public function handle(Request $request, Closure $next): Response
|
||||
{
|
||||
$response = $next($request);
|
||||
|
||||
$response->headers->set('X-Frame-Options', 'DENY');
|
||||
$response->headers->set('Content-Security-Policy', "frame-ancestors 'none'; base-uri 'self'; object-src 'none'");
|
||||
$response->headers->set('X-Content-Type-Options', 'nosniff');
|
||||
$response->headers->set('Referrer-Policy', 'strict-origin-when-cross-origin');
|
||||
$response->headers->set('Permissions-Policy', 'geolocation=(), microphone=(), payment=(), usb=()');
|
||||
|
||||
if ($request->isSecure()) {
|
||||
$response->headers->set('Strict-Transport-Security', 'max-age='.self::HSTS_MAX_AGE.'; includeSubDomains');
|
||||
}
|
||||
|
||||
if (! headers_sent()) {
|
||||
header_remove('X-Powered-By');
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
@@ -27,8 +27,10 @@ use Livewire\Component;
|
||||
new class extends Component
|
||||
{
|
||||
/**
|
||||
* The Monday of the week shown (Y-m-d).
|
||||
* The Monday of the week shown (Y-m-d). Read from the URL on mount and
|
||||
* then changed only by showWeek(), so the browser cannot set it.
|
||||
*/
|
||||
#[Locked]
|
||||
#[Url(as: 'viikko', except: '')]
|
||||
public string $week = '';
|
||||
|
||||
@@ -58,6 +60,11 @@ new class extends Component
|
||||
#[Locked]
|
||||
public array $rejected = [];
|
||||
|
||||
/**
|
||||
* The day shown on phones; changed only by selectDay(), because it is
|
||||
* written into element ids and wire:change calls.
|
||||
*/
|
||||
#[Locked]
|
||||
public string $selectedDay = '';
|
||||
|
||||
public string $taskSearch = '';
|
||||
|
||||
@@ -2,10 +2,13 @@
|
||||
|
||||
use App\Modules\Users\Application\UseCases\RequestPasswordReset;
|
||||
use App\Modules\Shared\Domain\ValueObjects\EmailAddress;
|
||||
use Illuminate\Support\Facades\RateLimiter;
|
||||
use Livewire\Component;
|
||||
|
||||
new class extends Component
|
||||
{
|
||||
public const int MAX_REQUESTS_PER_IP_PER_MINUTE = 5;
|
||||
|
||||
public string $email = '';
|
||||
|
||||
public bool $linkRequested = false;
|
||||
@@ -23,6 +26,17 @@ new class extends Component
|
||||
['email.required' => __('auth.email_required'), 'email.email' => __('auth.email_invalid'), 'email.max' => __('auth.email_invalid')],
|
||||
);
|
||||
|
||||
// Laravel allows one link a minute per account; this also limits one
|
||||
// address asking links for many accounts (mail flooding).
|
||||
$throttleKey = 'forgot-password|'.request()->ip();
|
||||
|
||||
if (RateLimiter::tooManyAttempts($throttleKey, self::MAX_REQUESTS_PER_IP_PER_MINUTE)) {
|
||||
$this->addError('email', __('auth.forgot_throttle', ['seconds' => RateLimiter::availableIn($throttleKey)]));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
RateLimiter::hit($throttleKey);
|
||||
$requestPasswordReset->execute(new EmailAddress($this->email));
|
||||
|
||||
$this->linkRequested = true;
|
||||
|
||||
@@ -11,6 +11,12 @@ new class extends Component
|
||||
{
|
||||
public const int MAX_ATTEMPTS_PER_MINUTE = 5;
|
||||
|
||||
/**
|
||||
* Failed attempts from one address across all accounts, so one common
|
||||
* password cannot be tried on every account (password spraying).
|
||||
*/
|
||||
public const int MAX_ATTEMPTS_PER_IP_PER_MINUTE = 20;
|
||||
|
||||
public string $email = '';
|
||||
|
||||
public string $password = '';
|
||||
@@ -44,11 +50,14 @@ new class extends Component
|
||||
|
||||
$email = Str::lower(trim($this->email));
|
||||
$throttleKey = Str::transliterate($email.'|'.request()->ip());
|
||||
$ipThrottleKey = 'login-ip|'.request()->ip();
|
||||
|
||||
if (RateLimiter::tooManyAttempts($throttleKey, self::MAX_ATTEMPTS_PER_MINUTE)) {
|
||||
throw ValidationException::withMessages([
|
||||
'email' => __('auth.throttle', ['seconds' => RateLimiter::availableIn($throttleKey)]),
|
||||
]);
|
||||
foreach ([$throttleKey => self::MAX_ATTEMPTS_PER_MINUTE, $ipThrottleKey => self::MAX_ATTEMPTS_PER_IP_PER_MINUTE] as $key => $maxAttempts) {
|
||||
if (RateLimiter::tooManyAttempts($key, $maxAttempts)) {
|
||||
throw ValidationException::withMessages([
|
||||
'email' => __('auth.throttle', ['seconds' => RateLimiter::availableIn($key)]),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
$signedIn = Auth::attemptWhen(
|
||||
@@ -59,6 +68,7 @@ new class extends Component
|
||||
|
||||
if (! $signedIn) {
|
||||
RateLimiter::hit($throttleKey);
|
||||
RateLimiter::hit($ipThrottleKey);
|
||||
$this->reset('password');
|
||||
|
||||
// One message for a wrong password and a disabled account, so the
|
||||
|
||||
Reference in New Issue
Block a user