Only trust Authelia identity headers from the proxy
Lint / JS (eslint) (push) Successful in 7s
Lint / Python (flake8) (push) Successful in 1m9s
Lint / Notify on failure (push) Skipped
Lint / Deploy (push) Skipped
Security / Python Security (bandit) (push) Successful in 1m33s
Test / Python Tests (pytest) (push) Successful in 1m54s
Lint / Python (flake8) (pull_request) Successful in 1m19s
Lint / JS (eslint) (pull_request) Successful in 7s
Lint / Notify on failure (pull_request) Skipped
Lint / Deploy (pull_request) Skipped
Security / Python Security (bandit) (pull_request) Successful in 1m16s
Test / Python Tests (pytest) (pull_request) Successful in 1m29s
Lint / JS (eslint) (push) Successful in 7s
Lint / Python (flake8) (push) Successful in 1m9s
Lint / Notify on failure (push) Skipped
Lint / Deploy (push) Skipped
Security / Python Security (bandit) (push) Successful in 1m33s
Test / Python Tests (pytest) (push) Successful in 1m54s
Lint / Python (flake8) (pull_request) Successful in 1m19s
Lint / JS (eslint) (pull_request) Successful in 7s
Lint / Notify on failure (pull_request) Skipped
Lint / Deploy (pull_request) Skipped
Security / Python Security (bandit) (pull_request) Successful in 1m16s
Test / Python Tests (pytest) (pull_request) Successful in 1m29s
Remote-User/Name/Email/Groups were read from every request, so a LAN host connecting to the container directly could claim any user and group. Add auth.trusted_proxies and ignore those headers unless the request came from one of them: the X-Real-IP the local nginx sets, or the peer address when gunicorn is reached directly. With nothing configured, nobody is trusted. Rejected attempts are logged, and /api/avatar now uses the same check. The container's nginx site blanks the same headers for non-NPM sources (pve-infra 4e3ab7c). Closes #3 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
93828b38e9
commit
5c4e4dc099
3 files changed
+102
-7
No files matched your search
@@ -118,7 +118,44 @@ def _daemon_ok(last_check: str) -> bool:
|
||||
# Auth helpers
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def _request_source() -> str:
|
||||
"""The address the request came from.
|
||||
|
||||
Gunicorn listens on loopback behind the local nginx, which overwrites X-Real-IP
|
||||
with the real peer address. A peer that isn't loopback reached gunicorn directly,
|
||||
so its own address is the source and any X-Real-IP it sent is ignored.
|
||||
"""
|
||||
peer = request.remote_addr or ''
|
||||
try:
|
||||
if ipaddress.ip_address(peer).is_loopback:
|
||||
return request.headers.get('X-Real-IP', '').strip()
|
||||
except ValueError:
|
||||
return ''
|
||||
return peer
|
||||
|
||||
|
||||
def _from_trusted_proxy() -> bool:
|
||||
"""True when the request came through the Authelia-protected proxy.
|
||||
|
||||
Only then are the Remote-* identity headers real: anyone else on the LAN could
|
||||
set them. Fails closed: with no auth.trusted_proxies configured, nobody is trusted.
|
||||
"""
|
||||
try:
|
||||
source = ipaddress.ip_address(_request_source())
|
||||
except ValueError:
|
||||
return False
|
||||
for entry in _config().get('auth', {}).get('trusted_proxies', []):
|
||||
try:
|
||||
if source in ipaddress.ip_network(entry, strict=False):
|
||||
return True
|
||||
except ValueError:
|
||||
logger.error(f'Ignoring invalid auth.trusted_proxies entry {entry!r}')
|
||||
return False
|
||||
|
||||
|
||||
def _get_user() -> dict:
|
||||
if not _from_trusted_proxy():
|
||||
return {'username': '', 'name': '', 'email': '', 'groups': []}
|
||||
return {
|
||||
'username': request.headers.get('Remote-User', ''),
|
||||
'name': request.headers.get('Remote-Name', ''),
|
||||
@@ -136,6 +173,11 @@ def require_auth(f):
|
||||
def wrapper(*args, **kwargs):
|
||||
user = _get_user()
|
||||
if not user['username']:
|
||||
if request.headers.get('Remote-User'):
|
||||
logger.warning(
|
||||
f'Ignored identity headers from untrusted source {_request_source() or "unknown"!r}; '
|
||||
f'only auth.trusted_proxies may send them'
|
||||
)
|
||||
return (
|
||||
'<h1>401 – Not authenticated</h1>'
|
||||
'<p>Please access Gandalf through '
|
||||
@@ -626,7 +668,7 @@ def api_diagnose_poll(job_id: str):
|
||||
@require_auth
|
||||
def api_avatar():
|
||||
"""Serve the current user's LDAP avatar photo (JPEG), cached to disk."""
|
||||
username = request.headers.get('Remote-User', '').strip()
|
||||
username = _get_user()['username'].strip()
|
||||
if not username:
|
||||
return '', 404
|
||||
|
||||
|
||||
Reference in new issue
Block a user