refactor(security): add typed rate-limit event
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -14,6 +14,7 @@ use KTXC\Service\TenantFirewallStatusService;
|
||||
use KTXC\Security\Event\AccessDeniedEvent;
|
||||
use KTXC\Security\Event\AuthenticationFailedEvent;
|
||||
use KTXC\Security\Event\BruteForceDetectedEvent;
|
||||
use KTXC\Security\Event\RateLimitExceededEvent;
|
||||
use KTXC\Security\Event\SecurityEvent;
|
||||
use KTXF\Event\DeliveryMode;
|
||||
use KTXF\Event\EventListenerRegistrarInterface;
|
||||
@@ -48,7 +49,7 @@ class Module extends ModuleInstanceAbstract implements ModuleConsoleInterface, M
|
||||
SecurityEvent::AUTH_SUCCESS,
|
||||
AccessDeniedEvent::class,
|
||||
BruteForceDetectedEvent::class,
|
||||
SecurityEvent::RATE_LIMIT_EXCEEDED,
|
||||
RateLimitExceededEvent::class,
|
||||
SecurityEvent::SUSPICIOUS_ACTIVITY,
|
||||
SecurityEvent::FIREWALL_RULE_CREATED,
|
||||
SecurityEvent::FIREWALL_RULE_EXTENDED,
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace KTXC\Security\Event;
|
||||
|
||||
use KTXF\Event\Event;
|
||||
|
||||
final class RateLimitExceededEvent extends Event implements SecurityRequestEventInterface
|
||||
{
|
||||
private readonly string $reason;
|
||||
|
||||
public function __construct(
|
||||
private readonly string $ipAddress,
|
||||
private readonly int $requestCount,
|
||||
private readonly int $windowSeconds,
|
||||
private readonly ?string $endpoint = null,
|
||||
?string $tenantId = null,
|
||||
) {
|
||||
if ($ipAddress === '') {
|
||||
throw new \InvalidArgumentException('Rate-limit detection requires an IP address.');
|
||||
}
|
||||
if ($requestCount < 1) {
|
||||
throw new \InvalidArgumentException('Rate-limit detection requires at least one request.');
|
||||
}
|
||||
if ($windowSeconds < 1) {
|
||||
throw new \InvalidArgumentException('Rate-limit detection requires a positive window.');
|
||||
}
|
||||
if ($endpoint === '') {
|
||||
throw new \InvalidArgumentException('A supplied rate-limit endpoint cannot be empty.');
|
||||
}
|
||||
|
||||
$this->reason = sprintf(
|
||||
'%d requests in %d seconds',
|
||||
$requestCount,
|
||||
$windowSeconds,
|
||||
);
|
||||
|
||||
parent::__construct(
|
||||
self::class,
|
||||
[
|
||||
'requestCount' => $requestCount,
|
||||
'windowSeconds' => $windowSeconds,
|
||||
'endpoint' => $endpoint,
|
||||
],
|
||||
$tenantId,
|
||||
);
|
||||
}
|
||||
|
||||
public function getIpAddress(): string
|
||||
{
|
||||
return $this->ipAddress;
|
||||
}
|
||||
|
||||
public function getRequestCount(): int
|
||||
{
|
||||
return $this->requestCount;
|
||||
}
|
||||
|
||||
public function getWindowSeconds(): int
|
||||
{
|
||||
return $this->windowSeconds;
|
||||
}
|
||||
|
||||
public function getEndpoint(): ?string
|
||||
{
|
||||
return $this->endpoint;
|
||||
}
|
||||
|
||||
public function getDeviceFingerprint(): ?string
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getUserAgent(): ?string
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getRequestPath(): ?string
|
||||
{
|
||||
return $this->endpoint;
|
||||
}
|
||||
|
||||
public function getRequestMethod(): ?string
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getUserId(): ?string
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getReason(): string
|
||||
{
|
||||
return $this->reason;
|
||||
}
|
||||
|
||||
public function getSeverity(): int
|
||||
{
|
||||
return SecurityEvent::SEVERITY_ERROR;
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,6 @@ final class SecurityEvent extends Event implements SecurityRequestEventInterface
|
||||
|
||||
public const ACCESS_GRANTED = 'security.access.granted';
|
||||
|
||||
public const RATE_LIMIT_EXCEEDED = 'security.rate_limit.exceeded';
|
||||
public const SUSPICIOUS_ACTIVITY = 'security.suspicious.activity';
|
||||
|
||||
public const IP_BLOCKED = 'security.ip.blocked';
|
||||
@@ -112,30 +111,6 @@ final class SecurityEvent extends Event implements SecurityRequestEventInterface
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a rate limit exceeded event
|
||||
*/
|
||||
public static function rateLimitExceeded(
|
||||
string $ipAddress,
|
||||
int $requestCount,
|
||||
int $windowSeconds,
|
||||
?string $endpoint = null,
|
||||
?string $tenantId = null,
|
||||
): self {
|
||||
return self::create(
|
||||
self::RATE_LIMIT_EXCEEDED,
|
||||
$ipAddress,
|
||||
data: [
|
||||
'requestCount' => $requestCount,
|
||||
'windowSeconds' => $windowSeconds,
|
||||
'endpoint' => $endpoint,
|
||||
],
|
||||
tenantId: $tenantId,
|
||||
requestPath: $endpoint,
|
||||
reason: sprintf('%d requests in %d seconds', $requestCount, $windowSeconds),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get default severity for event types
|
||||
*/
|
||||
@@ -149,7 +124,6 @@ final class SecurityEvent extends Event implements SecurityRequestEventInterface
|
||||
self::AUTH_LOGOUT,
|
||||
self::TOKEN_REVOKED => self::SEVERITY_WARNING,
|
||||
|
||||
self::RATE_LIMIT_EXCEEDED,
|
||||
self::SUSPICIOUS_ACTIVITY => self::SEVERITY_ERROR,
|
||||
|
||||
self::IP_BLOCKED,
|
||||
|
||||
@@ -13,6 +13,7 @@ use KTXC\Context\TenantContextInterface;
|
||||
use KTXC\Security\Event\AccessDeniedEvent;
|
||||
use KTXC\Security\Event\AuthenticationFailedEvent;
|
||||
use KTXC\Security\Event\BruteForceDetectedEvent;
|
||||
use KTXC\Security\Event\RateLimitExceededEvent;
|
||||
use KTXC\Security\Event\SecurityEvent;
|
||||
use KTXC\Security\Event\SecurityEventInterface;
|
||||
use KTXC\Security\Event\SecurityRequestEventInterface;
|
||||
@@ -283,7 +284,7 @@ class FirewallService
|
||||
AuthenticationFailedEvent::class => FirewallLogObject::EVENT_AUTH_FAILURE,
|
||||
SecurityEvent::AUTH_SUCCESS => FirewallLogObject::EVENT_ACCESS_CHECK,
|
||||
BruteForceDetectedEvent::class => FirewallLogObject::EVENT_BRUTE_FORCE,
|
||||
SecurityEvent::RATE_LIMIT_EXCEEDED => FirewallLogObject::EVENT_RATE_LIMIT,
|
||||
RateLimitExceededEvent::class => FirewallLogObject::EVENT_RATE_LIMIT,
|
||||
AccessDeniedEvent::class => FirewallLogObject::EVENT_RULE_MATCH,
|
||||
SecurityEvent::SUSPICIOUS_ACTIVITY => FirewallLogObject::EVENT_SUSPICIOUS,
|
||||
SecurityEvent::FIREWALL_RULE_CREATED => FirewallLogObject::EVENT_RULE_CREATED,
|
||||
|
||||
Reference in New Issue
Block a user