Steuerkram/tests/90_SsrfGuardAssertPublicHostTest.php

40 lines
1.5 KiB
PHP

<?php
declare(strict_types=1);
use Steuerkram\Services\SsrfGuard;
class SsrfGuardAssertPublicHostTest extends TestCase
{
protected string $testName = 'Test 90: SSRF-Schutz für SMTP-Host (assertPublicHost)';
public function run(): void
{
// Private/reservierte IP-Literale werden abgelehnt
$this->assertRejected('127.0.0.1', 'Loopback-IP (127.0.0.1) wird abgelehnt');
$this->assertRejected('localhost', 'localhost wird abgelehnt');
$this->assertRejected('10.0.0.5', 'RFC1918-IP (10.0.0.5) wird abgelehnt');
$this->assertRejected('192.168.1.1', 'RFC1918-IP (192.168.1.1) wird abgelehnt');
$this->assertRejected('169.254.169.254', 'Link-Local/Metadata-IP (169.254.169.254) wird abgelehnt');
$this->assertRejected('[::1]', 'IPv6-Loopback wird abgelehnt');
$this->assertRejected('', 'Leerer Host wird abgelehnt');
// Öffentliche IP-Literale werden akzeptiert
try {
SsrfGuard::assertPublicHost('8.8.8.8');
$this->assert(true, 'Öffentliches IP-Literal (8.8.8.8) wird akzeptiert');
} catch (\RuntimeException $e) {
$this->assert(false, 'Öffentliches IP-Literal (8.8.8.8) wird akzeptiert (Fehler: ' . $e->getMessage() . ')');
}
}
private function assertRejected(string $host, string $message): void
{
try {
SsrfGuard::assertPublicHost($host);
$this->assert(false, $message);
} catch (\RuntimeException $e) {
$this->assert(true, $message);
}
}
}