Tighten proxy role resolution (#25064)

* deny proxy users whose role can't be resolved instead of falling back to viewer

Proxy role resolution fell back to viewer, which grants every camera, in several accidental cases. A default_role that was misspelled, null, or named a deleted role became viewer. A role_map group whose mapped role had been deleted (for example, emptied by a camera delete) got the default role. Direct role headers were lowercased but compared to case-sensitive role names, so a custom role like Garage never matched, and a custom role named Admin resolved to the built-in admin.

- match header values, default_role and role_map roles against configured roles regardless of case
- deny when default_role or a matched role_map role doesn't exist, and warn at config load for an unknown default_role
- reserve admin and viewer in every casing and reject role names that differ only by case, in config validation and the Create Role dialog
- show a null default_role as none in the settings widget

* normalize a null proxy default_role to none

/config drops null fields, so the settings form filled in viewer for a null default_role while /auth denied those users. The config now stores a null default_role as none, which the API returns and the widget shows, and the unreachable null branch in the widget is removed.
This commit is contained in:
Josh Hawkins
2026-11-24 06:34:46 -07:00
committed by GitHub
parent 4554b93cbe
commit 212ebca746
8 changed files with 90 additions and 30 deletions
+13 -16
View File
@@ -580,8 +580,9 @@ def resolve_role(
(split by proxy_config.separator) and map to roles.
Admin matches short-circuit to admin.
- If no role_map is configured, treat the header as role names directly.
2. If no valid role is found, return proxy_config.default_role if it's valid in config_roles, else 'viewer'.
The literal value 'none' is a valid default and means access should be denied.
2. If no valid role is found, return proxy_config.default_role if it's valid in config_roles, else 'none'.
The literal value 'none' means access should be denied.
A role_map match whose mapped roles no longer exist in config_roles also resolves to 'none'.
Args:
headers (dict): Incoming request headers (case-insensitive).
@@ -594,17 +595,11 @@ def resolve_role(
default_role = proxy_config.default_role
role_header = proxy_config.header_map.role
# Validate default_role against config; fallback to 'viewer' if invalid.
# "none" is a sentinel meaning "deny access when no mapping matches"; it is
# reserved in AuthConfig.validate_roles so it is never a configured role.
validated_default = (
default_role
if default_role in config_roles or default_role == "none"
else "viewer"
)
if not config_roles:
# Edge case: no roles defined
validated_default = "none" if default_role == "none" else "viewer"
# Role names match case-insensitively, as header values are lowercased.
# "none" is the deny sentinel; an unknown or missing default also denies,
# since falling back to viewer would grant every camera.
roles_by_name = {r.lower(): r for r in config_roles}
validated_default = roles_by_name.get((default_role or "").lower(), "none")
if not role_header:
logger.debug(
@@ -631,7 +626,7 @@ def resolve_role(
logger.debug("Parsed groups from role header: %s", groups)
matched_roles = {
role_name
role_name.lower()
for role_name, required_groups in proxy_config.header_map.role_map.items()
if any(group in groups for group in required_groups)
}
@@ -643,9 +638,11 @@ def resolve_role(
logger.debug("Resolved role (with role_map) to 'admin'.")
return "admin"
# A mapped role that was deleted or renamed denies rather than
# widening to the default role
if matched_roles:
resolved = next(
(r for r in config_roles if r in matched_roles), validated_default
(r for r in config_roles if r.lower() in matched_roles), "none"
)
logger.debug("Resolved role (with role_map) to '%s'.", resolved)
return resolved
@@ -664,7 +661,7 @@ def resolve_role(
logger.debug("Parsed roles directly from header: %s", roles_from_header)
resolved = next(
(r for r in config_roles if r in roles_from_header),
(r for r in config_roles if r.lower() in roles_from_header),
validated_default,
)
if resolved == validated_default and roles_from_header: