From ba5b52ec4753b6d635eb0295cb5c1105ffcef45f Mon Sep 17 00:00:00 2001 From: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com> Date: Wed, 25 Nov 2026 06:07:34 -0600 Subject: [PATCH] Tighten media and review access (#25083) * deny admin-only media to viewer The media auth check allowed any role without a camera list before it looked at admin-only paths, so viewer could list and download the face library, classification train and dataset images, and GenAI request frames from /clips/ even though the matching API routes and UI pages are admin-only. Admin-only paths are now denied to every non-admin role before that check. * admin-only frigate+ frame upload the UI already hides this so this is just to fix the endpoint * scope review lookup by event id to allowed cameras The review lookup by event id matched the id with GLOB, so wildcard characters in the id matched other ids, and the camera check ran on whichever row matched first, returning a 403 that named a camera the user can't access. The id now matches literally and the query only searches the user's allowed cameras, so a review item on another camera returns the same 404 as a missing one. * fix test --- docs/static/frigate-api.yaml | 14 ++++----- frigate/api/media.py | 2 +- frigate/api/media_auth.py | 4 ++- frigate/api/review.py | 12 ++++++-- frigate/test/http_api/test_http_review.py | 37 +++++++++++++++++++++++ frigate/test/test_media_auth.py | 12 ++++++++ 6 files changed, 68 insertions(+), 13 deletions(-) diff --git a/docs/static/frigate-api.yaml b/docs/static/frigate-api.yaml index fb0e62c2b0..9f9aa13f00 100644 --- a/docs/static/frigate-api.yaml +++ b/docs/static/frigate-api.yaml @@ -2311,8 +2311,8 @@ paths: $ref: '#/components/schemas/HTTPValidationError' security: - frigateUserAuth: [] - x-required-role: camera - description: '**Access:** Authenticated user with access to the referenced camera.' + x-required-role: any + description: '**Access:** Any authenticated user.' /review/{review_id}: get: tags: @@ -6075,9 +6075,7 @@ paths: in: path required: true schema: - anyOf: - - type: string - - type: 'null' + type: string title: Camera Name - name: frame_time in: path @@ -6098,9 +6096,9 @@ paths: schema: $ref: '#/components/schemas/HTTPValidationError' security: - - frigateUserAuth: [] - x-required-role: camera - description: '**Access:** Authenticated user with access to the referenced camera.' + - frigateAdminAuth: [] + x-required-role: admin + description: '**Access:** Admin role required.' /{camera_name}/start/{start_ts}/end/{end_ts}/clip.mp4: get: tags: diff --git a/frigate/api/media.py b/frigate/api/media.py index a9c947f4b8..69e12b74ca 100644 --- a/frigate/api/media.py +++ b/frigate/api/media.py @@ -436,7 +436,7 @@ async def get_snapshot_from_recording( @router.post( - "/{camera_name}/plus/{frame_time}", dependencies=[Depends(require_camera_access)] + "/{camera_name}/plus/{frame_time}", dependencies=[Depends(require_role(["admin"]))] ) async def submit_recording_snapshot_to_plus( request: Request, camera_name: str, frame_time: str diff --git a/frigate/api/media_auth.py b/frigate/api/media_auth.py index df29cf5bce..6f70db6b8d 100644 --- a/frigate/api/media_auth.py +++ b/frigate/api/media_auth.py @@ -313,6 +313,9 @@ def deny_response_for_media_uri( if not role or role == "admin": return None + if resolution == MediaAuthResolution.ADMIN_ONLY: + return 403 + if not is_role_restricted(role, frigate_config): return None @@ -321,7 +324,6 @@ def deny_response_for_media_uri( if resolution in ( MediaAuthResolution.LISTING_MULTI_CAMERA, - MediaAuthResolution.ADMIN_ONLY, MediaAuthResolution.UNRESOLVED_MEDIA, ): return 403 diff --git a/frigate/api/review.py b/frigate/api/review.py index 29d8dc5b19..51acd80106 100644 --- a/frigate/api/review.py +++ b/frigate/api/review.py @@ -713,12 +713,18 @@ def motion_activity( response_model=ReviewSegmentResponse, dependencies=[Depends(allow_any_authenticated())], ) -async def get_review_from_event(request: Request, event_id: str): +async def get_review_from_event( + event_id: str, + allowed_cameras: list[str] = Depends(get_allowed_cameras_for_filter), +): try: review = ReviewSegment.get( - ReviewSegment.data["detections"].cast("text") % f'*"{event_id}"*' + (ReviewSegment.camera << allowed_cameras) + & ( + fn.instr(ReviewSegment.data["detections"].cast("text"), f'"{event_id}"') + > 0 + ) ) - await require_camera_access(review.camera, request=request) return JSONResponse(model_to_dict(review)) except DoesNotExist: return JSONResponse( diff --git a/frigate/test/http_api/test_http_review.py b/frigate/test/http_api/test_http_review.py index 8afe242263..eb3cbc455c 100644 --- a/frigate/test/http_api/test_http_review.py +++ b/frigate/test/http_api/test_http_review.py @@ -1,4 +1,5 @@ from datetime import UTC, datetime, timedelta +from urllib.parse import quote from fastapi import Request from peewee import DoesNotExist @@ -840,6 +841,42 @@ class TestHttpReview(BaseTestHttp): response_json, ) + def test_review_event_wildcards_match_literally(self): + now = datetime.now().timestamp() + super().insert_mock_review_segment( + "123456.review.random", + now + 1, + now + 2, + data={"detections": ["1763812345.0-abc123"]}, + ) + + with AuthTestClient(self.app) as client: + for pattern in ["1763812345*", "176381234?.0-abc123", "176381234[0-9]*"]: + response = client.get(f"/review/event/{quote(pattern, safe='')}") + assert response.status_code == 404 + + response = client.get("/review/event/1763812345.0-abc123") + assert response.status_code == 200 + + def test_review_event_on_forbidden_camera_is_not_found(self): + now = datetime.now().timestamp() + event_id = "1763812345.0-abc123" + super().insert_mock_review_segment( + "123456.review.random", + now + 1, + now + 2, + data={"detections": [event_id]}, + camera="back_door", + ) + + with AuthTestClient(self.app) as client: + response = client.get(f"/review/event/{event_id}") + assert response.status_code == 404 + self.assertDictEqual( + {"success": False, "message": "Review item not found"}, + response.json(), + ) + #################################################################################################################### ################################### GET /review/{review_id} Endpoint ####################################### #################################################################################################################### diff --git a/frigate/test/test_media_auth.py b/frigate/test/test_media_auth.py index ae6de27813..a94cad588c 100644 --- a/frigate/test/test_media_auth.py +++ b/frigate/test/test_media_auth.py @@ -345,6 +345,18 @@ class TestDenyResponseForMediaUri(unittest.TestCase): self.assertIsNone(self._deny("/clips/back_door-1.jpg", "viewer")) self.assertIsNone(self._deny("/clips/", "viewer")) + def test_unrestricted_role_blocked_admin_only(self): + for uri in ( + "/clips/faces/", + "/clips/faces/train/foo.webp", + "/clips/faces/alice/ref.webp", + "/clips/genai-requests/x/0.jpg", + "/clips/preview_restart_cache/x.mp4", + "/clips/some_model/train/x.jpg", + "/clips/some_model/dataset/cat/x.jpg", + ): + self.assertEqual(self._deny(uri, "viewer"), 403, uri) + def test_restricted_role_allowed_camera(self): self.assertIsNone(self._deny("/clips/front_door-1.jpg", "limited_user")) self.assertIsNone(