frigate-hass-integration: Media browser does not work when accessing frigate with authentication.
Describe the bug
When browsing media via HA the proxied URLs for snapshots / clips / recordings all return 401 errors. At the same time I see the resolved URL in the frigate container:
2024-12-15 19:27:13.663300410 192.168.18.3 - - [15/Dec/2024:19:27:13 +0100] "GET /api/events/1734285996.292472-4ggity/snapshot.jpg HTTP/1.1" 401 179 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:133.0) Gecko/20100101 Firefox/133.0" "192.168.18.5"
I altered the nginx config inside the container to do a bit more logging, inside auth_request.conf I added:
add_header X-Requested-Uri $request_uri always;
add_header X-Requested-Auth $http_authorization always;
This helps me in seeing what the frigate request looks like through HA’s internal proxy.
The resulting response headers from the 401 I get from HA, which is forwarded from frigate:
X-Requested-Uri: /api/events/1734270241.350298-6easg5/thumbnail.jpg
This means that there is no authorization header in the request from HA to Frigate.
I’m not sure how the unauthenticated request is possible since in https://github.com/blakeblackshear/frigate-hass-integration/blob/master/custom_components/frigate/api.py#L155 it uses the api wrapper which should be adding headers.
Debug logs
These show nothing interesting, which is weird.
2024-12-15 21:11:35.200 DEBUG (MainThread) [custom_components.frigate] Finished fetching frigate data in 0.010 seconds (success: True)
2024-12-15 21:11:40.196 DEBUG (MainThread) [custom_components.frigate] Finished fetching frigate data in 0.005 seconds (success: True)
2024-12-15 21:11:45.198 DEBUG (MainThread) [custom_components.frigate] Finished fetching frigate data in 0.008 seconds (success: True)
Version of the custom_component
5.6.0
For now this is all the time I can spend on this, I’ll do more research and come back with more info if I have it. If anyone else using the authenticated port 8197 to connect from this integration to frigate hosted on a different machine could confirm that their media browser works properly that’d be great.
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 12
- Comments: 64 (4 by maintainers)
I have updated the integration files on my local, and was able to get a bunch of broken functionality when using port 8971 to work:
I believe this covers many of the existing issues currently present for this integration.
I have a fork with the changes - though turns out somebody else made similar changes to mine for the default camera card / WebRTC stuff. I didn’t update the tests yet, because I don’t have it all setup to do so.
https://github.com/blakeblackshear/frigate-hass-integration/commit/9f270923c5661fdc3d16412aecacb43ab8378d50
Ah, apologies. I’m speaking about the camera entity card if you don’t have a live feed. It gets a similar 401 error. But I think they’re related, in the sense that it doesn’t seem like auth is properly applied to both of these, though they are probably in fact separate issues.
I do also have media browser issues too though: I get a 401 error in my frigate nginx logs trying to access a recording. The endpoint being called is:
The query that fails from HA’s front end:
It sends along the “authSig” as a query parameter (not familiar with this, would it be the auth token for HA?)
In any case, it seems like this URL is being “proxied” into another (and so maybe avoids AIOHttp Client altogether?)
Though I’m not familiar with the code base, it looks like this is a proxy URL from here in the code: https://github.com/blakeblackshear/frigate-hass-integration/blob/4f095b82ae816de8dcc87afc39e042a86d533adb/custom_components/frigate/views.py#L319C1-L340C10
I also don’t see how the auth is being passed around in that code (unless it’s the authSig), which might explain the 401 error.
I will continue digging when I have some time, that’s as far as I got for now.
I have encountered the same problem as well. The streaming widgets worked fine if I connect to frigate via port 5000 but no stream while connecting to port 8971. I have been switching between them a dozen times.
Environment:
I’m not seeing this personally, snapshots and clips are loading for me using auth and the HA companion app
Created a PR for at least the view portion. If it does need testing, I may not be able to get to it right away.
https://github.com/blakeblackshear/frigate-hass-integration/pull/917
@pbcoyle I confirm it works!!! Thank you so much!!! For information, I just substituted the 3 files
api.py,camera.pyandviews.pyfrom your branch.Now I hope this can make its way to the main branch soon! @NickM-27
I dont know if their change would have fixed that in the advanced camera card. It seems to work with my branch though, at least. Without looking more closely, advanced camera card probably uses a view for the event, which are fixed in my branch. Their change just fixed webRTC and snapshots, merged into master just yesterday.
Yes I think authSig is working properly. As @NickM-27 says, it would be HA’s auth, not frigate.
I’m pretty sure the problem is that some code paths within the integration aren’t adding the auth header - in the API class its pretty clear we are adding the auth header, but in MediaBrowser and Camera, I’m not seeing where the auth gets added.
As mentioned previously though, I may be missing something as it’s strange this isnt easily reproducible otherwise. Maybe someone familiar with those classes can explain how auth gets applied at all? Otherwise when I have some spare time I will continue to investigate my theory
the
authSig=is home assistant’s camera proxy key. That is working correctly and not part of the issue. Unfortunately without being able to reproduce the issue, we are not able to understand the problem.I think this issue has the same root cause for #876 and #873 as well
I’ve been able to clearly reproduce this issue using JSMpeg in Advanced Camera Card Not that I intend to use JSMpeg, but it gives clear errors, and the other provides still error, but just less detailed logs
Frigate nginx logs: <FRIGATE_IP> - - [<TIME>] “GET /live/jsmpeg/<CAMERA> HTTP/1.1” 401 581 “-” “<USER_AGENT>” “127.0.0.1, 127.0.0.1”
HA Logs: Reverse proxy error for /api/frigate/frigate/jsmpeg/<CAMERA>?authSig=<Part1>.<Part2>.<Part3>: 401, message=‘Invalid response status’, url=‘http://<FRIGATE_IP>:8971/live/jsmpeg/<CAMERA>’
One thing to point out here is the authSig is clearly a jwt, by looking at the 3 parts split by
., which I’d guess is actually HA’s token not a token intended for Frigate?@koudonojinchuu - I think you’re not looking at the right API calls. I did a similar investigation, but inside “camera.py”, and specifically at the /api/{camera}/latest.jpg endpoint, which I know wasn’t working for me (and others)
_default_headers inside the websession client created by the “async_camera_image” method in camera.py does not contain the authorization header, prior to the request being made in camera.py. In fact, I manually modified camera.py to add the bearer token, and then that got it working for me. Because we are directly getting the client HTTP client there, it doesn’t have the auth and extra bits of info the API client gives us.
Seems to me the core issue is that creating an AIOClient using
async_get_clientsessiondoesn’t necessarily have the auth header set. Though I’m not sure why some people aren’t seeing this behavior, so I feel I may still be missing something. Can we update the API class with these other calls, so that we can instead use the API client (which already properly applies configs / auth?)They are unrelated. The issue is about not sending an authorization token
I’m not sure what you are trying to debug here… I already put the problem in the original ticket.
The authorisation header is not sent to frigate at all… jwt internals are not relevant if the jwt is not sent
iatandexpare UNIX timestamps and define when the JWT was issued and when it is going to expire. The issue date is 17 hours ago, and expiry is 16 hours ago. That lines up with the time you made the comment, so the JWT is probably fine.Yes, that is a JWT. You can paste the JWT into https://jwt.io/ to get its content disected.
I debugged this in the OP: X-Requested-Uri: /api/events/1734270241.350298-6easg5/thumbnail.jpg
The request in the browser goes to HA, it uses authentication and is then forwarded to the nginx from frigate, where it has no authentication information. So the component is not attaching it properly.
The response is a 401.