From d85527ffd283486c66005c35896d53606a793d97 Mon Sep 17 00:00:00 2001 From: Nicolas Mowen Date: Fri, 20 Nov 2026 06:59:53 -0700 Subject: [PATCH] Miscellaneous Fixes (#25027) * Fix GenAI structured response not handling optional fields correctly * allow export thumbnails for camera-restricted roles Export thumbnails under /clips/export/ weren't classified by media auth, so roles with a camera allow-list got a 403 on every thumbnail. The thumbnail now resolves to its export's camera through a thumb_path lookup, and the camera access check applies as usual. * load i18n locales from the base url The locale load path was relative, so any URL with a trailing slash or more than one segment (like /admin/) fetched locales from under that path, got index.html back from the nginx fallback, and broke every UI string. Locales now load from the same base url as the API, which also covers HA ingress and FRIGATE_BASE_PATH. * make storage retained-segment test able to fail The old test put the retained segments after 59 older unretained ones, so cleanup freed its hourly target and stopped before it ever reached them. It passed even with the retention check removed. Retained segments now come first, and cleanup needs every unretained segment after them, so it has to skip the retained ones to finish. --------- Co-authored-by: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com> --- frigate/api/media_auth.py | 22 ++++++++---- frigate/genai/prompts.py | 33 +++++++++++++---- frigate/test/test_genai_prompts.py | 36 +++++++++++++++++++ frigate/test/test_media_auth.py | 32 +++++++++++++++-- frigate/test/test_storage.py | 58 ++++++++++-------------------- web/src/utils/i18n.ts | 3 +- 6 files changed, 128 insertions(+), 56 deletions(-) diff --git a/frigate/api/media_auth.py b/frigate/api/media_auth.py index 802077188a..49a3e6f8f9 100644 --- a/frigate/api/media_auth.py +++ b/frigate/api/media_auth.py @@ -17,10 +17,10 @@ import re from enum import Enum from urllib.parse import unquote -from peewee import DoesNotExist +from peewee import DoesNotExist, Field from frigate.config import FrigateConfig -from frigate.const import EXPORT_DIR +from frigate.const import CLIPS_DIR, EXPORT_DIR from frigate.models import Export, User logger = logging.getLogger(__name__) @@ -151,6 +151,7 @@ def _resolve_clip( # /clips/thumbs/{cam}/... → camera # /clips/previews/{cam}/... → camera # /clips/review/thumb-{cam}-{review_id}.webp → camera (parsed) + # /clips/export/{export_id}.{ext} → camera (DB lookup) # /clips/faces/... → admin-only # /clips/genai-requests/... → admin-only # /clips/preview_restart_cache/... → admin-only @@ -175,6 +176,11 @@ def _resolve_clip( return MediaAuthResolution.CAMERA, camera return MediaAuthResolution.UNRESOLVED_MEDIA, None + if second == "export" and len(parts) == 3: + return _export_camera( + Export.thumb_path, os.path.join(CLIPS_DIR, "export", parts[2]) + ) + if second in ("faces", "genai-requests", "preview_restart_cache"): return MediaAuthResolution.ADMIN_ONLY, None @@ -234,15 +240,19 @@ def _resolve_export( if len(parts) != 2: return MediaAuthResolution.UNRESOLVED_MEDIA, None - filename = parts[1] - full_path = os.path.join(EXPORT_DIR, filename) + return _export_camera(Export.video_path, os.path.join(EXPORT_DIR, parts[1])) + + +def _export_camera( + field: Field, full_path: str +) -> tuple[MediaAuthResolution, str | None]: try: - export = Export.get(Export.video_path == full_path) + export = Export.get(field == full_path) return MediaAuthResolution.CAMERA, export.camera except DoesNotExist: return MediaAuthResolution.UNRESOLVED_MEDIA, None except Exception as e: - logger.warning("Export DB lookup failed for %s: %s", filename, e) + logger.warning("Export DB lookup failed for %s: %s", full_path, e) return MediaAuthResolution.UNRESOLVED_MEDIA, None diff --git a/frigate/genai/prompts.py b/frigate/genai/prompts.py index 39bbbc10ec..8ef9d74d82 100644 --- a/frigate/genai/prompts.py +++ b/frigate/genai/prompts.py @@ -187,16 +187,35 @@ def build_review_description_response_format(concerns: list[str]) -> dict[str, A Strips the `time` field (populated server-side) and drops `other_concerns` when no concerns are configured. + + Every remaining property must be listed in `required` because strict + providers (OpenAI) reject a schema with any optional property. """ schema = ReviewMetadata.model_json_schema() - schema.get("properties", {}).pop("time", None) + properties = schema.setdefault("properties", {}) + required = schema.setdefault("required", []) + properties.pop("time", None) - if "time" in schema.get("required", []): - schema["required"].remove("time") - if not concerns: - schema.get("properties", {}).pop("other_concerns", None) - if "other_concerns" in schema.get("required", []): - schema["required"].remove("other_concerns") + if "time" in required: + required.remove("time") + + if concerns: + # The model field is nullable with a default so stored metadata + # without concerns still parses. For generation, require a plain list + # instead; the model returns an empty list when no concern applies. + properties["other_concerns"] = { + "type": "array", + "items": {"type": "string"}, + "description": properties["other_concerns"]["description"], + } + + if "other_concerns" not in required: + required.append("other_concerns") + else: + properties.pop("other_concerns", None) + + if "other_concerns" in required: + required.remove("other_concerns") return { "type": "json_schema", diff --git a/frigate/test/test_genai_prompts.py b/frigate/test/test_genai_prompts.py index 23ac3ee209..4d77868bb0 100644 --- a/frigate/test/test_genai_prompts.py +++ b/frigate/test/test_genai_prompts.py @@ -7,11 +7,47 @@ from frigate.genai.prompts import ( REVIEW_DESCRIPTION_FIELD_GUIDELINES, REVIEW_RESPONSE_STYLES, build_review_description_prompt, + build_review_description_response_format, build_review_summary_prompt, get_review_field_guidelines, ) +class TestReviewDescriptionResponseFormat(unittest.TestCase): + def _schema(self, concerns: list[str]) -> dict: + response_format = build_review_description_response_format(concerns) + self.assertTrue(response_format["json_schema"]["strict"]) + return response_format["json_schema"]["schema"] + + def test_every_property_is_required_for_strict_mode(self): + for concerns in ([], ["a person loitering near the gate"]): + with self.subTest(concerns=concerns): + schema = self._schema(concerns) + self.assertEqual(set(schema["properties"]), set(schema["required"])) + + def test_time_is_never_requested(self): + schema = self._schema(["a person loitering near the gate"]) + self.assertNotIn("time", schema["properties"]) + self.assertNotIn("time", schema["required"]) + + def test_concerns_omitted_when_not_configured(self): + schema = self._schema([]) + self.assertNotIn("other_concerns", schema["properties"]) + self.assertNotIn("other_concerns", schema["required"]) + + def test_concerns_requested_as_plain_list_when_configured(self): + schema = self._schema(["a person loitering near the gate"]) + concerns = schema["properties"]["other_concerns"] + self.assertEqual(concerns["type"], "array") + self.assertEqual(concerns["items"], {"type": "string"}) + self.assertNotIn("anyOf", concerns) + self.assertNotIn("default", concerns) + + def test_schema_is_not_shared_between_calls(self): + self._schema(["a person loitering near the gate"]) + self.assertNotIn("other_concerns", self._schema([])["properties"]) + + class TestReviewResponseStyle(unittest.TestCase): def _build_prompt(self, response_style: str = "default") -> str: review_data = { diff --git a/frigate/test/test_media_auth.py b/frigate/test/test_media_auth.py index 0afa9b44ac..98a84a74c9 100644 --- a/frigate/test/test_media_auth.py +++ b/frigate/test/test_media_auth.py @@ -20,7 +20,7 @@ from frigate.api.media_auth import ( resolve_media_uri, ) from frigate.config import FrigateConfig -from frigate.const import EXPORT_DIR +from frigate.const import CLIPS_DIR, EXPORT_DIR from frigate.models import Event, Export, Recordings, ReviewSegment from frigate.test.const import TEST_DB, TEST_DB_CLEANUPS, TEST_DB_PRAGMAS @@ -247,7 +247,7 @@ class TestExportResolution(unittest.TestCase): name=f"export-{export_id}", date=int(datetime.datetime.now().timestamp()), video_path=os.path.join(EXPORT_DIR, filename), - thumb_path=os.path.join(EXPORT_DIR, f"{filename}.jpg"), + thumb_path=os.path.join(CLIPS_DIR, "export", f"{export_id}.webp"), in_progress=False, ).execute() @@ -280,6 +280,34 @@ class TestExportResolution(unittest.TestCase): resolution, _ = resolve_media_uri("/exports/clip.mp4", self.config) self.assertEqual(resolution, MediaAuthResolution.UNRESOLVED_MEDIA) + def test_export_thumbnail_resolves_camera(self): + self._insert_export("back_door_abc123", "back_door", "back_door_clip.mp4") + resolution, camera = resolve_media_uri( + "/clips/export/back_door_abc123.webp", self.config + ) + self.assertEqual(resolution, MediaAuthResolution.CAMERA) + self.assertEqual(camera, "back_door") + + def test_unknown_export_thumbnail_is_unresolved(self): + resolution, _ = resolve_media_uri("/clips/export/missing.webp", self.config) + self.assertEqual(resolution, MediaAuthResolution.UNRESOLVED_MEDIA) + + def test_export_thumbnail_gated_by_camera_access(self): + self._insert_export("back_door_abc123", "back_door", "back_door_clip.mp4") + self._insert_export("front_door_def456", "front_door", "front_door_clip.mp4") + url = "/clips/export/{}.webp" + self.assertIsNone( + deny_response_for_media_uri( + url.format("front_door_def456"), "limited_user", self.config + ) + ) + self.assertEqual( + deny_response_for_media_uri( + url.format("back_door_abc123"), "limited_user", self.config + ), + 403, + ) + class TestDenyResponseForMediaUri(unittest.TestCase): """End-to-end decision check used by /auth.""" diff --git a/frigate/test/test_storage.py b/frigate/test/test_storage.py index 68dbbecc67..67094c562e 100644 --- a/frigate/test/test_storage.py +++ b/frigate/test/test_storage.py @@ -511,55 +511,33 @@ class TestHttp(unittest.TestCase): Recordings.get(Recordings.id == rec_d3_id) def test_storage_cleanup_keeps_retained(self): - """Ensure that all recordings are cleaned up when necessary.""" + """Ensure cleanup steps over retained segments to reach newer ones.""" config = FrigateConfig(**self.minimal_config) storage = StorageMaintainer(config, MagicMock()) - id = "123456.keep" - time_keep = datetime.datetime.now().timestamp() - _insert_mock_event( - id, - time_keep, - time_keep + 30, - True, - ) - rec_k_id = "1234567.keep" - rec_k2_id = "1234568.keep" - rec_k3_id = "1234569.keep" - _insert_mock_recording( - rec_k_id, - os.path.join(self.test_dir, f"{rec_k_id}.tmp"), - time_keep, - time_keep + 10, - ) - _insert_mock_recording( - rec_k2_id, - os.path.join(self.test_dir, f"{rec_k2_id}.tmp"), - time_keep + 10, - time_keep + 20, - ) - _insert_mock_recording( - rec_k3_id, - os.path.join(self.test_dir, f"{rec_k3_id}.tmp"), - time_keep + 20, - time_keep + 30, - ) + seg_len = 600 + base = datetime.datetime.now().timestamp() - 7200 + _insert_mock_event("123456.keep", base, base + 2.5 * seg_len, True) - time_delete = datetime.datetime.now().timestamp() - 7200 - for i in range(0, 59): - id = f"{123456 + i}.delete" + paths = {} + for i in range(10): + rec_id = f"segment-{i}" + paths[rec_id] = os.path.join(self.test_dir, f"{rec_id}.tmp") _insert_mock_recording( - id, - os.path.join(self.test_dir, f"{id}.tmp"), - time_delete, - time_delete + 600, + rec_id, paths[rec_id], base + i * seg_len, base + (i + 1) * seg_len ) + # Hourly bandwidth is 48 MB, so cleanup needs all 56 MB of unretained + # segments and must pass the older retained ones to get there. storage.calculate_camera_bandwidth() storage.reduce_storage_consumption() - assert Recordings.get(Recordings.id == rec_k_id) - assert Recordings.get(Recordings.id == rec_k2_id) - assert Recordings.get(Recordings.id == rec_k3_id) + + retained = {"segment-0", "segment-1", "segment-2"} + remaining = {r.id for r in Recordings.select(Recordings.id)} + on_disk = {rec_id for rec_id, path in paths.items() if os.path.exists(path)} + self.assertEqual(remaining, retained) + self.assertEqual(on_disk, retained) + self.assertFalse(storage.retention_unmet) def test_unmet_retention_sets_the_live_flag(self): config = FrigateConfig(**self.minimal_config) diff --git a/web/src/utils/i18n.ts b/web/src/utils/i18n.ts index 16de6dc405..6f13d6f2dd 100644 --- a/web/src/utils/i18n.ts +++ b/web/src/utils/i18n.ts @@ -2,6 +2,7 @@ import i18n, { t } from "i18next"; import { initReactI18next } from "react-i18next"; import HttpBackend from "i18next-http-backend"; import { EventType } from "@/types/search"; +import { baseUrl } from "@/api/baseUrl"; export const getTranslatedLabel = ( label: string, @@ -33,7 +34,7 @@ i18n fallbackLng: "en", // use en if detected lng is not available backend: { - loadPath: `locales/{{lng}}/{{ns}}.json?v=${import.meta.env.VITE_GIT_COMMIT_HASH || "unknown"}`, + loadPath: `${baseUrl}locales/{{lng}}/{{ns}}.json?v=${import.meta.env.VITE_GIT_COMMIT_HASH || "unknown"}`, }, ns: [