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:
@@ -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