From 883eeac99ffc8429ce59e5b35f87c379e19fbbdf Mon Sep 17 00:00:00 2001 From: Arnaud Dartois Date: Wed, 11 Nov 2026 21:20:47 +0100 Subject: [PATCH] fix(camera): restrict reports to assigned agent --- skills/camera-toggle-acceptance/SKILL.md | 4 ++-- src/Heimdall.Web/Controllers/AgentsController.cs | 3 ++- .../Controllers/CamerasControllerTests.cs | 4 ++++ 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/skills/camera-toggle-acceptance/SKILL.md b/skills/camera-toggle-acceptance/SKILL.md index 7f53e92..0521c8f 100644 --- a/skills/camera-toggle-acceptance/SKILL.md +++ b/skills/camera-toggle-acceptance/SKILL.md @@ -10,7 +10,7 @@ Read `AGENTS.md`, the Toggle caméra #59/#199 Gitea issue, `skills/heimdall-dotn ## Implementation path - `POST /api/cameras/{id}/toggle-status` requires the normal camera-write permission and persists `Camera.Status` in Central. Enabling a Disabled camera sets it to Offline (enabled, connectivity not yet confirmed); only an Agent status report can set it Online. Toggling any enabled status sets it to Disabled. -- Agent status reports require the API key to resolve to the same Agent ID in the route. Authenticate and authorize that identity with `ResourceAccess`; never trust the route ID alone. Keep the existing same-site camera check in `ReportCameraStatusCommandHandler`. +- Agent status reports require the API key to resolve to the same Agent ID in the route and the camera to be assigned to that Agent. Authenticate and authorize both resources with `ResourceAccess`; never trust the route ID alone. Keep the existing same-site camera check in `ReportCameraStatusCommandHandler`. - `AppDbContext.SaveChangesAsync` publishes a payload-free config invalidation after camera/assignment/profile changes. Agent refetches its authorized full camera snapshot over its ConfigHub/API path and acknowledges the digest. - `GetEffectiveAgentConfigQuery` emits `camera..enabled` from the persisted status. `CameraConfigStore` atomically replaces the snapshot and raises `Changed`. - `SnapshotPipelineService` reconciles enabled assignments with `CameraStreamService`; disabling/removing an assignment kills FFmpeg, awaits its read loop cleanup and releases its stream permit. Enabling starts the stream. Keep central status authoritative; an Agent status report must not re-enable a disabled camera. @@ -58,6 +58,6 @@ The sixth run kept the FFmpeg process deadline separate from the config ACK dead ## Camera status contract acceptance -For status semantics (issue #291), run `CamerasControllerTests.ToggleStatus_WithAuth_TogglesCamera` against PostgreSQL and the `ToggleCameraStatusCommandTests` suite. The relational test must assert Disabled → Offline and the online counter remains zero before connectivity, register and heartbeat a synthetic same-site Agent, assign the synthetic camera, accept that Agent's Online report, reject a request that uses its API key with a different route Agent ID, and then confirm admin toggle → Disabled. +For status semantics (issue #291), run `CamerasControllerTests.ToggleStatus_WithAuth_TogglesCamera` against PostgreSQL and the `ToggleCameraStatusCommandTests` suite. The relational test must assert Disabled → Offline and the online counter remains zero before connectivity, register and heartbeat a synthetic same-site Agent, reject its report before camera assignment, assign the synthetic camera, accept that Agent's Online report, reject a request that uses its API key with a different route Agent ID, and then confirm admin toggle → Disabled. A source-built disposable Central + PostgreSQL Compose run on 2026-11-11 used project `heimdall-291-acceptance-1794427561-8428`, synthetic bootstrap/site/Agent/camera data, loopback ports 18543/15439, and image `heimdall:heimdall-291-acceptance-1794427561-8428-central`. It observed health HTTP 200, activation Offline, online count 0, then Agent-confirmed Online and online count 1. Cleanup verified zero project containers, volumes and networks, the exact image tag absent, and temporary files removed. This is software acceptance only; it does not satisfy `validation:field` hardware/deployment evidence. diff --git a/src/Heimdall.Web/Controllers/AgentsController.cs b/src/Heimdall.Web/Controllers/AgentsController.cs index bd9a279..20055fe 100644 --- a/src/Heimdall.Web/Controllers/AgentsController.cs +++ b/src/Heimdall.Web/Controllers/AgentsController.cs @@ -158,7 +158,8 @@ public class AgentsController : ControllerBase { var requester = await _resourceAccess.ResolveAsync(User, ct); if (requester is null || requester.Kind != RequesterKind.Agent || - !await _resourceAccess.AgentAsync(requester, id, ct)) + !await _resourceAccess.AgentAsync(requester, id, ct) || + !await _resourceAccess.CameraAsync(requester, cameraId, ct)) return Forbid(); if (!Enum.TryParse(request.Status, true, out var status)) diff --git a/tests/Heimdall.IntegrationTests/Controllers/CamerasControllerTests.cs b/tests/Heimdall.IntegrationTests/Controllers/CamerasControllerTests.cs index 00b8999..d979789 100644 --- a/tests/Heimdall.IntegrationTests/Controllers/CamerasControllerTests.cs +++ b/tests/Heimdall.IntegrationTests/Controllers/CamerasControllerTests.cs @@ -96,6 +96,10 @@ public class CamerasControllerTests : PostgresTestBase $"api/agents/{agentId}/heartbeat", new { publicIp = "127.0.0.1" }); heartbeatResponse.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK); + var unassignedReport = await agentClient.PutAsJsonAsync( + $"api/agents/{agentId}/cameras/{cameraId}/status", new { status = "Online" }); + unassignedReport.StatusCode.ShouldBe(System.Net.HttpStatusCode.Forbidden); + var assignmentResponse = await client.PostAsJsonAsync("api/assignments", new { cameraId,