Device auth and request validation (REVIEW SEC-002, SEC-003)
The plumbing every Phase 2 endpoint needs, before any endpoint exists. api/schemas.py: Pydantic models for the whole device contract - enrollment, the shared /ingest + /heartbeat envelope, the conditions body and command acks. Enums are imported from the models, so a schema cannot drift from the CHECK constraint that backs it. api/deps.py: credential generation, hashing and bearer resolution. Written against firmware/API.md and, where prose and code disagree, against firmware/src/api_client.cpp. Four places where REVIEW.AI.md's reference code would not have worked: - Its /enroll returns devices.token_hash to the device. The device would then present the hash, the server would hash it again, and every request after enrollment would 401. Plaintext goes back once, only the hash is stored. - It checks only the shared bootstrap secret on re-enrollment. The firmware swaps that for a per-device reenroll_secret after first enrollment, so that check would lock out every device that had ever succeeded - the exact failure the re-enrollment path exists to prevent. - Its /ingest is multipart. API.md deviates from SPEC 4.1 deliberately: the body is the raw JPEG and metadata rides in headers, so the device does not assemble a second buffer. Modelled as IngestMeta. - It uses secrets.token_urlsafe(32) - 43 non-hex chars. API.md says 64 hex, and the firmware's char[65] buffers make that exact. token_hex(32) it is. capture_id is documented as an int but captures.id is a UUID. parseEnvelope never reads the field, so it is sent as a UUID string. The conditions models accept unknown keys on purpose: firmware grows fields before the server learns them, device_status.raw keeps the whole body, and a 422 would take a camera offline for reporting something new. devices.token_hash is now indexed and unique - it is resolved on every authenticated request, and two devices must never share a token. No migration: the live database already had exactly this index, so the model had drifted from it. 'alembic check' is clean. Tests: 17 -> 57. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
3344a9e9b1
commit
8038060578
5 files changed
+905
-7
No files matched your search
@@ -64,7 +64,14 @@ class Device(Base):
|
||||
|
||||
# Only the hash is stored. The token is high-entropy, so a fast hash is
|
||||
# defensible here (API.md).
|
||||
token_hash: Mapped[str | None] = mapped_column(String(128))
|
||||
#
|
||||
# Indexed because every authenticated device request resolves a bearer
|
||||
# token by this column: it is the hottest lookup in the device API.
|
||||
#
|
||||
# Unique so that two devices can never end up sharing a token. NULLs stay
|
||||
# distinct in Postgres, so any number of devices may sit here un-enrolled
|
||||
# or revoked.
|
||||
token_hash: Mapped[str | None] = mapped_column(String(128), unique=True, index=True)
|
||||
# Per-device re-enrollment credential, so a rotated token cannot strand a
|
||||
# camera that has no USB access once mounted (SPEC 0).
|
||||
reenroll_secret_hash: Mapped[str | None] = mapped_column(String(128))
|
||||
@@ -96,9 +103,7 @@ class Device(Base):
|
||||
responsive_mode: Mapped[bool] = mapped_column(Boolean, default=False, server_default="false")
|
||||
|
||||
enabled: Mapped[bool] = mapped_column(Boolean, default=True, server_default="true")
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), server_default=func.now()
|
||||
)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
|
||||
|
||||
commands: Mapped[list[DeviceCommand]] = relationship(
|
||||
back_populates="device", cascade="all, delete-orphan"
|
||||
@@ -132,9 +137,7 @@ class DeviceCommand(Base):
|
||||
payload: Mapped[dict | None] = mapped_column(JSONB)
|
||||
|
||||
created_by: Mapped[int | None] = mapped_column(ForeignKey("users.id", ondelete="SET NULL"))
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), server_default=func.now()
|
||||
)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
|
||||
delivered_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
|
||||
acked_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
|
||||
ack_status: Mapped[str | None] = mapped_column(String(16))
|
||||
|
||||
Reference in new issue
Block a user