refactor(security): add typed suspicious-activity event

Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
2026-08-05 23:38:59 -04:00
parent 688afbe5a1
commit f4df769b3c
8 changed files with 176 additions and 16 deletions
@@ -19,10 +19,6 @@ final class SecurityEventTest extends TestCase
SecurityEvent::SEVERITY_WARNING,
(new SecurityEvent(SecurityEvent::AUTH_LOGOUT))->getSeverity(),
);
self::assertSame(
SecurityEvent::SEVERITY_ERROR,
(new SecurityEvent(SecurityEvent::SUSPICIOUS_ACTIVITY))->getSeverity(),
);
self::assertSame(
SecurityEvent::SEVERITY_CRITICAL,
(new SecurityEvent(SecurityEvent::DEVICE_BLOCKED))->getSeverity(),
@@ -0,0 +1,66 @@
<?php
declare(strict_types=1);
namespace KTXT\Unit\Event;
use KTXC\Security\Event\SecurityEvent;
use KTXC\Security\Event\SuspiciousActivityEvent;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
final class SuspiciousActivityEventTest extends TestCase
{
#[Test]
#[TestDox('Suspicious-activity state is typed and complete at construction')]
public function constructsTypedState(): void
{
$event = new SuspiciousActivityEvent(
ipAddress: '203.0.113.20',
detector: 'payload-signature',
detectionData: ['score' => 98],
tenantId: 'tenant-a',
identityId: 'identity-a',
deviceFingerprint: 'device-a',
userAgent: 'Test Agent',
requestPath: '/admin',
requestMethod: 'POST',
userId: 'user-a',
reason: 'Matched a blocked payload signature',
);
self::assertSame(SuspiciousActivityEvent::class, $event->getName());
self::assertSame('203.0.113.20', $event->getIpAddress());
self::assertSame('payload-signature', $event->getDetector());
self::assertSame(['score' => 98], $event->getDetectionData());
self::assertSame(['detector' => 'payload-signature', 'score' => 98], $event->getData());
self::assertSame('tenant-a', $event->getTenantId());
self::assertSame('identity-a', $event->getIdentityId());
self::assertSame('device-a', $event->getDeviceFingerprint());
self::assertSame('Test Agent', $event->getUserAgent());
self::assertSame('/admin', $event->getRequestPath());
self::assertSame('POST', $event->getRequestMethod());
self::assertSame('user-a', $event->getUserId());
self::assertSame('Matched a blocked payload signature', $event->getReason());
self::assertSame(SecurityEvent::SEVERITY_ERROR, $event->getSeverity());
}
#[Test]
#[TestDox('Suspicious activity rejects incomplete or conflicting detection context')]
public function rejectsInvalidDetectionContext(): void
{
foreach ([
['', 'payload-signature', []],
['203.0.113.20', '', []],
['203.0.113.20', 'payload-signature', ['detector' => 'replacement']],
] as $arguments) {
try {
new SuspiciousActivityEvent(...$arguments);
self::fail('Invalid suspicious-activity context was accepted.');
} catch (\InvalidArgumentException) {
$this->addToAssertionCount(1);
}
}
}
}
+2 -1
View File
@@ -20,6 +20,7 @@ use KTXC\Security\Event\AccessDeniedEvent;
use KTXC\Security\Event\AuthenticationFailedEvent;
use KTXC\Security\Event\BruteForceDetectedEvent;
use KTXC\Security\Event\RateLimitExceededEvent;
use KTXC\Security\Event\SuspiciousActivityEvent;
use KTXC\Security\Event\SecurityEvent;
use KTXF\Event\DeliveryMode;
use PHPUnit\Framework\Attributes\Test;
@@ -49,7 +50,7 @@ final class CoreModuleTest extends TestCase
AccessDeniedEvent::class,
BruteForceDetectedEvent::class,
RateLimitExceededEvent::class,
SecurityEvent::SUSPICIOUS_ACTIVITY,
SuspiciousActivityEvent::class,
SecurityEvent::FIREWALL_RULE_CREATED,
SecurityEvent::FIREWALL_RULE_EXTENDED,
SecurityEvent::FIREWALL_RULE_DISABLED,
@@ -14,6 +14,7 @@ use KTXC\Security\Event\AccessDeniedEvent;
use KTXC\Security\Event\AuthenticationFailedEvent;
use KTXC\Security\Event\BruteForceDetectedEvent;
use KTXC\Security\Event\RateLimitExceededEvent;
use KTXC\Security\Event\SuspiciousActivityEvent;
use KTXC\Service\FirewallService;
use KTXC\Service\FirewallRuleCache;
use KTXC\Service\FirewallRuleManager;
@@ -320,11 +321,9 @@ class FirewallServiceTest extends TestCase
&& $log->getMetadata()['detector'] === 'payload-signature';
}))
->willReturnArgument(0);
$event = \KTXC\Security\Event\SecurityEvent::create(
\KTXC\Security\Event\SecurityEvent::SUSPICIOUS_ACTIVITY,
'203.0.113.20',
null,
['detector' => 'payload-signature'],
$event = new SuspiciousActivityEvent(
ipAddress: '203.0.113.20',
detector: 'payload-signature',
tenantId: 'tenant-a',
requestPath: '/admin',
requestMethod: 'POST',