Head office shows a camera's latest frame rather than live video, for a
reason that has not changed: the engine serves MJPEG on 127.0.0.1 on a PC
behind a shop's router with no inbound route, and relaying it needs
WebRTC/TURN. Pointing a browser straight at the shop PC is not the escape
either - the engine's API is Basic-authenticated with a credential it
generates locally and never sends anywhere, and shipping that to the
cloud so a web page could use it would put the key to the biometric API
and the live face feed in the server's database.
But that picture only worked if you had an S3 bucket. Without one,
attachSnapshots reported "This system is not storing images" for every
camera forever - on the two screens whose whole job is to show the
camera. Making them picture-led turned a missing feature into a wall of
empty tiles, on every local install and any self-hosted customer who does
not want a bucket.
migrations/009 adds camera_snapshots and the agent falls back to
PUT /api/agent/cameras/{camera}/snapshot when the presigned route answers
images_disabled - chosen by sentinel, never by matching the message, since
it picks between two routes. One row per camera is what makes this safe in
the database when face images are not: the key IS the camera, so storage
is (cameras x ~100 KB) and does not grow with footfall.
The read is session-authenticated rather than a signed link, which an
<img> cannot use - hence Shot.jsx and useAuthedImage, keyed on the URL
string rather than the snapshot object so a poll does not re-fetch 90 KB
per camera every few seconds, and revoking the object URL on cleanup.
Verified against the real office camera with no bucket configured: 90,587
bytes stored in Postgres, served as image/jpeg to a signed-in user, 401
without a session, rendered on both the Cameras and Shops cards.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HViLj9gYNRtSr7YVZmW5sn
103 lines
3.3 KiB
Go
103 lines
3.3 KiB
Go
package api
|
|
|
|
import (
|
|
"bytes"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// A minimal but real JPEG header: SOI + APP0. The endpoint checks the bytes,
|
|
// not the Content-Type header, so a test that sends anything else is not
|
|
// testing the same path a shop PC uses.
|
|
func jpegBytes(padTo int) []byte {
|
|
b := []byte{0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, 'J', 'F', 'I', 'F', 0}
|
|
for len(b) < padTo {
|
|
b = append(b, 0x00)
|
|
}
|
|
return append(b, 0xFF, 0xD9)
|
|
}
|
|
|
|
func TestAnAgentCanStoreItsCameraPicture(t *testing.T) {
|
|
srv, fs := newServer(t)
|
|
fs.addAgent("agent-token", AgentPrincipal{ClientID: "client-1", SiteID: "site-1"})
|
|
|
|
rr := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodPut,
|
|
"/api/agent/cameras/entrance/snapshot", bytes.NewReader(jpegBytes(64)))
|
|
req.Header.Set("Authorization", "Bearer agent-token")
|
|
srv.Routes().ServeHTTP(rr, req)
|
|
|
|
if rr.Code != http.StatusNoContent {
|
|
t.Fatalf("status %d: %s", rr.Code, rr.Body)
|
|
}
|
|
if len(fs.snapshots["entrance"]) == 0 {
|
|
t.Fatal("nothing was stored")
|
|
}
|
|
// The tenant and site come from the AGENT's credential, never the request.
|
|
// A camera id a caller can set must not be able to choose whose camera it
|
|
// decorates.
|
|
if fs.lastSnapshotClient != "client-1" || fs.lastSnapshotSite != "site-1" {
|
|
t.Fatalf("stored against %s/%s", fs.lastSnapshotClient, fs.lastSnapshotSite)
|
|
}
|
|
}
|
|
|
|
// This endpoint stores whatever it is handed and serves it back to a browser
|
|
// later, so the one thing it must not become is a way to park arbitrary content
|
|
// under a URL this server will serve.
|
|
func TestOnlyAJPEGIsAccepted(t *testing.T) {
|
|
srv, fs := newServer(t)
|
|
fs.addAgent("agent-token", AgentPrincipal{ClientID: "client-1", SiteID: "site-1"})
|
|
|
|
for _, body := range []string{
|
|
"<html><script>alert(1)</script></html>",
|
|
"GIF89a",
|
|
"",
|
|
} {
|
|
rr := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodPut,
|
|
"/api/agent/cameras/entrance/snapshot", strings.NewReader(body))
|
|
req.Header.Set("Authorization", "Bearer agent-token")
|
|
// Claiming to be a JPEG must not help: the check is on the bytes.
|
|
req.Header.Set("Content-Type", "image/jpeg")
|
|
srv.Routes().ServeHTTP(rr, req)
|
|
if rr.Code != http.StatusBadRequest {
|
|
t.Fatalf("body %q was accepted with status %d", body, rr.Code)
|
|
}
|
|
}
|
|
}
|
|
|
|
// One row per camera is what makes this safe to keep in the database at all,
|
|
// but a single oversized request still has to be bounded - it is read into
|
|
// memory before anything else looks at it.
|
|
func TestAnOversizedSnapshotIsRefused(t *testing.T) {
|
|
srv, fs := newServer(t)
|
|
fs.addAgent("agent-token", AgentPrincipal{ClientID: "client-1", SiteID: "site-1"})
|
|
|
|
rr := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodPut,
|
|
"/api/agent/cameras/entrance/snapshot",
|
|
bytes.NewReader(jpegBytes(maxSnapshotBytes+1024)))
|
|
req.Header.Set("Authorization", "Bearer agent-token")
|
|
srv.Routes().ServeHTTP(rr, req)
|
|
|
|
if rr.Code != http.StatusRequestEntityTooLarge {
|
|
t.Fatalf("status %d", rr.Code)
|
|
}
|
|
if len(fs.snapshots) != 0 {
|
|
t.Fatal("an oversized snapshot was stored anyway")
|
|
}
|
|
}
|
|
|
|
func TestAnUnauthenticatedAgentCannotStoreAPicture(t *testing.T) {
|
|
srv, _ := newServer(t)
|
|
rr := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodPut,
|
|
"/api/agent/cameras/entrance/snapshot", bytes.NewReader(jpegBytes(64)))
|
|
srv.Routes().ServeHTTP(rr, req)
|
|
if rr.Code != http.StatusUnauthorized {
|
|
t.Fatalf("status %d", rr.Code)
|
|
}
|
|
}
|