feat(firewall): add safeguarded rule creation
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -6,6 +6,7 @@ namespace KTXT\Unit\Service;
|
||||
|
||||
use KTXC\Models\Firewall\FirewallRuleObject;
|
||||
use KTXC\Service\FirewallRuleCache;
|
||||
use KTXC\Service\FirewallRuleConflictException;
|
||||
use KTXC\Service\FirewallRuleManager;
|
||||
use KTXC\Service\FirewallRuleScope;
|
||||
use KTXC\Stores\FirewallStore;
|
||||
@@ -77,6 +78,96 @@ class FirewallRuleManagerTest extends TestCase
|
||||
);
|
||||
}
|
||||
|
||||
#[TestDox('Manual rule creation requires a reason before persistence')]
|
||||
public function testManualReason(): void
|
||||
{
|
||||
$this->store->expects(self::never())->method('depositRule');
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
|
||||
$this->manager->createManualRule(
|
||||
FirewallRuleScope::tenant('tenant-a'),
|
||||
FirewallRuleObject::TYPE_IP,
|
||||
FirewallRuleObject::ACTION_BLOCK,
|
||||
'203.0.113.10',
|
||||
' ',
|
||||
'admin-a'
|
||||
);
|
||||
}
|
||||
|
||||
#[TestDox('Blocking the current exact IP requires explicit confirmation')]
|
||||
public function testCurrentIpSafeguard(): void
|
||||
{
|
||||
$this->store->expects(self::never())->method('depositRule');
|
||||
try {
|
||||
$this->manager->createManualRule(
|
||||
FirewallRuleScope::tenant('tenant-a'),
|
||||
FirewallRuleObject::TYPE_IP,
|
||||
FirewallRuleObject::ACTION_BLOCK,
|
||||
'2001:db8::1',
|
||||
'Confirmed abuse',
|
||||
'admin-a',
|
||||
currentIp: '2001:0db8:0:0:0:0:0:1'
|
||||
);
|
||||
self::fail('Current IP block should require confirmation.');
|
||||
} catch (FirewallRuleConflictException $error) {
|
||||
self::assertSame('current_ip_confirmation_required', $error->conflictCode);
|
||||
}
|
||||
}
|
||||
|
||||
#[TestDox('CIDR rules covering the current IP require explicit confirmation')]
|
||||
public function testCurrentIpCidrSafeguard(): void
|
||||
{
|
||||
$this->store->expects(self::never())->method('depositRule');
|
||||
$this->expectException(FirewallRuleConflictException::class);
|
||||
|
||||
$this->manager->createManualRule(
|
||||
FirewallRuleScope::system(),
|
||||
FirewallRuleObject::TYPE_IP_RANGE,
|
||||
FirewallRuleObject::ACTION_BLOCK,
|
||||
'203.0.113.0/24',
|
||||
'Network abuse',
|
||||
'admin-a',
|
||||
currentIp: '203.0.113.10'
|
||||
);
|
||||
}
|
||||
|
||||
#[TestDox('Confirmed current-IP blocks retain manual audit context')]
|
||||
public function testConfirmedCurrentIpBlock(): void
|
||||
{
|
||||
$this->store->method('findExactIpRule')->willReturn(null);
|
||||
$this->store->expects(self::once())->method('depositRule')->willReturnArgument(0);
|
||||
|
||||
$rule = $this->manager->createManualRule(
|
||||
FirewallRuleScope::tenant('tenant-a'),
|
||||
FirewallRuleObject::TYPE_IP,
|
||||
FirewallRuleObject::ACTION_BLOCK,
|
||||
'203.0.113.10',
|
||||
'Emergency lockout',
|
||||
'admin-a',
|
||||
currentIp: '203.0.113.10',
|
||||
confirmCurrentIp: true
|
||||
);
|
||||
|
||||
self::assertSame('Emergency lockout', $rule->getReason());
|
||||
self::assertSame(FirewallRuleManager::ORIGIN_MANUAL, $rule->getMetadata()['origin']);
|
||||
}
|
||||
|
||||
#[TestDox('Manual creation rejects unsupported type and action combinations')]
|
||||
public function testUnsupportedManualRule(): void
|
||||
{
|
||||
$this->store->expects(self::never())->method('depositRule');
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
|
||||
$this->manager->createManualRule(
|
||||
FirewallRuleScope::tenant('tenant-a'),
|
||||
FirewallRuleObject::TYPE_DEVICE,
|
||||
FirewallRuleObject::ACTION_ALLOW,
|
||||
'device-123',
|
||||
'Trusted device',
|
||||
'admin-a'
|
||||
);
|
||||
}
|
||||
|
||||
#[TestDox('Rule lifecycle operations cannot cross scope ownership')]
|
||||
public function testOwnership(): void
|
||||
{
|
||||
|
||||
@@ -122,6 +122,23 @@ class FirewallRuleServicesTest extends TestCase
|
||||
$this->systemService()->blockIp('203.0.113.10');
|
||||
}
|
||||
|
||||
#[TestDox('Generic creation remains behind tenant and system management permissions')]
|
||||
public function testGenericCreationPermissions(): void
|
||||
{
|
||||
$this->identity->method('hasPermission')->willReturn(false);
|
||||
$this->store->expects(self::never())->method('depositRule');
|
||||
|
||||
try {
|
||||
$this->tenantService()->createRule('ip', 'block', '203.0.113.10', 'Abuse');
|
||||
self::fail('Tenant creation should have been rejected.');
|
||||
} catch (\RuntimeException $error) {
|
||||
self::assertStringContainsString(TenantFirewallRuleService::PERMISSION_MANAGE, $error->getMessage());
|
||||
}
|
||||
|
||||
$this->expectExceptionMessage(SystemFirewallRuleService::PERMISSION_MANAGE);
|
||||
$this->systemService()->createRule('ip', 'block', '203.0.113.10', 'Abuse');
|
||||
}
|
||||
|
||||
private function allow(string $permission): void
|
||||
{
|
||||
$this->identity->method('hasPermission')->with($permission)->willReturn(true);
|
||||
|
||||
Reference in New Issue
Block a user