An unauthenticated attacker could guess a real device_id and 429-lock that camera by hammering /api/events before HMAC verification ran, dropping its accident events. Add a cheap pre-auth per-IP limiter (Config::ingestIpRateLimitPerMin, default 300/min) that fires before any device-secret lookup, and move the existing per-device limiter to run only after HMAC + timestamp + nonce checks succeed, so only authenticated requests consume a device's own quota.
86 lines
4.5 KiB
PHP
86 lines
4.5 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
namespace App\Tests;
|
|
use App\Config;
|
|
use App\Http\Request;
|
|
use App\Security\Hmac;
|
|
use App\Ingest\IngestController;
|
|
use App\Tests\Support\{InMemoryDeviceRepository, InMemoryNonceRepository,
|
|
InMemoryRateLimiter, InMemoryEventRepository, InMemorySnapshotRepository};
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
final class IngestControllerTest extends TestCase
|
|
{
|
|
private string $dir;
|
|
protected function setUp(): void { $this->dir = sys_get_temp_dir() . '/snaps' . bin2hex(random_bytes(4)); mkdir($this->dir); }
|
|
|
|
private function make(array $secrets = ['pi-1' => 'sekret']): array {
|
|
$cfg = Config::fromArray(['snapshot_dir' => $this->dir, 'timestamp_window' => 300, 'rate_limit_per_min' => 60]);
|
|
$dev = new InMemoryDeviceRepository($secrets);
|
|
$nonce = new InMemoryNonceRepository();
|
|
$rl = new InMemoryRateLimiter();
|
|
$events = new InMemoryEventRepository();
|
|
$snaps = new InMemorySnapshotRepository();
|
|
$c = new IngestController($cfg, $dev, $nonce, $rl, $events, $snaps);
|
|
return [$c, $events, $snaps, $nonce];
|
|
}
|
|
private function jpegB64(): string { $im = imagecreatetruecolor(8, 8); ob_start(); imagejpeg($im); $b = ob_get_clean(); return base64_encode($b); }
|
|
|
|
private function signedReq(string $secret, array $payload, string $nonce = 'n1', string $ts = '1000'): Request {
|
|
$body = json_encode($payload, JSON_UNESCAPED_UNICODE);
|
|
$sig = Hmac::sign($secret, $nonce, $ts, $body);
|
|
return new Request('POST', '/api/events',
|
|
['X-Nonce' => $nonce, 'X-Timestamp' => $ts, 'X-Signature' => $sig], $body, [], []);
|
|
}
|
|
private function payload(): array {
|
|
return ['device_id' => 'pi-1', 'camera_id' => 'cam-1', 'event_type' => 'fall',
|
|
'severity' => 'high', 'occurred_at' => 1000.0, 'snapshots' => [$this->jpegB64()]];
|
|
}
|
|
|
|
public function testAcceptsValidSignedEvent(): void {
|
|
[$c, $events, $snaps] = $this->make();
|
|
$res = $c->handle($this->signedReq('sekret', $this->payload()), now: 1000, clientIp: '1.2.3.4');
|
|
$this->assertSame(201, $res->status());
|
|
$this->assertCount(1, $events->rows);
|
|
$id = json_decode($res->body(), true)['event_id'];
|
|
$this->assertCount(1, $snaps->forEvent($id)); // snapshot re-encoded + stored
|
|
}
|
|
public function testRejectsBadSignature(): void {
|
|
[$c] = $this->make();
|
|
$req = $this->signedReq('WRONG', $this->payload());
|
|
$this->assertSame(401, $c->handle($req, now: 1000, clientIp: '1.2.3.4')->status());
|
|
}
|
|
public function testRejectsReplayedNonce(): void {
|
|
[$c] = $this->make();
|
|
$req = $this->signedReq('sekret', $this->payload());
|
|
$this->assertSame(201, $c->handle($req, now: 1000, clientIp: '1.2.3.4')->status());
|
|
$this->assertSame(401, $c->handle($req, now: 1000, clientIp: '1.2.3.4')->status()); // same nonce again
|
|
}
|
|
public function testRejectsStaleTimestamp(): void {
|
|
[$c] = $this->make();
|
|
$req = $this->signedReq('sekret', $this->payload(), ts: '1000');
|
|
$this->assertSame(401, $c->handle($req, now: 5000, clientIp: '1.2.3.4')->status()); // 4000s > 300 window
|
|
}
|
|
public function testRejectsUnknownDevice(): void {
|
|
[$c] = $this->make([]); // no secrets
|
|
$this->assertSame(401, $c->handle($this->signedReq('x', $this->payload()), now: 1000, clientIp: '1.2.3.4')->status());
|
|
}
|
|
public function testIpFloodRateLimited(): void {
|
|
// Attacker without a valid device secret floods from one IP; the pre-auth
|
|
// IP limiter must trip BEFORE any HMAC/device-secret work, protecting
|
|
// real devices from having their own per-device quota consumed.
|
|
$cfg = Config::fromArray(['snapshot_dir' => $this->dir, 'timestamp_window' => 300, 'ingest_ip_rate_limit_per_min' => 2]);
|
|
$dev = new InMemoryDeviceRepository(['pi-1' => 'sekret']);
|
|
$nonce = new InMemoryNonceRepository();
|
|
$rl = new InMemoryRateLimiter();
|
|
$events = new InMemoryEventRepository();
|
|
$snaps = new InMemorySnapshotRepository();
|
|
$c = new IngestController($cfg, $dev, $nonce, $rl, $events, $snaps);
|
|
|
|
$garbage = new Request('POST', '/api/events', [], 'not-json', [], []);
|
|
$this->assertSame(401, $c->handle($garbage, now: 1000, clientIp: '9.9.9.9')->status());
|
|
$this->assertSame(401, $c->handle($garbage, now: 1000, clientIp: '9.9.9.9')->status());
|
|
$res = $c->handle($garbage, now: 1000, clientIp: '9.9.9.9');
|
|
$this->assertSame(429, $res->status()); // 3rd request from same IP: rate limited pre-auth
|
|
}
|
|
}
|