feat(6838): read the client address and HTTPS from the trusted proxy hop
CI & Build / Python lint (push) Successful in 2s
CI & Build / Plugin hooks (push) Successful in 11s
CI & Build / TypeScript typecheck (push) Successful in 37s
CI & Build / Python tests (push) Failing after 1m20s
CI & Build / integration (push) Successful in 2m27s
CI & Build / Build & push image (push) Skipped
CI & Build / Python lint (push) Successful in 2s
CI & Build / Plugin hooks (push) Successful in 11s
CI & Build / TypeScript typecheck (push) Successful in 37s
CI & Build / Python tests (push) Failing after 1m20s
CI & Build / integration (push) Successful in 2m27s
CI & Build / Build & push image (push) Skipped
grimoire/proxy.py is the one reader of X-Forwarded-For and X-Forwarded-Proto. It takes the entry TRUSTED_PROXY_HOPS in from the right (default 1; a shorter header reads its leftmost entry; 0 trusts no proxy), so an entry a client puts left of the proxy's is never believed. Before this, with TRUST_PROXY_HEADERS on, the leftmost X-Forwarded-For entry keyed the login, register, forgot and reset rate limits and the audit log, and a client could choose it. The session cookie is Secure, and HSTS (max-age=31536000) is sent, exactly when the request is HTTPS by the trusted proxy's word. Plain HTTP is untouched and nothing redirects. SECURE_COOKIES is gone (a startup warning says so when it is still set); TRUST_PROXY_HEADERS=false still maps to 0 hops, with a warning to move to TRUSTED_PROXY_HOPS. Admin audit lines now use the same client address instead of the socket peer. The placeholder-key refusal keyed on SECURE_COOKIES, so it now refuses either placeholder key outright; the instance makes its own key when none is set. Family idea #5004 (IsHTTPS, trusted-hop X-Forwarded-Proto), canon v2. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
"""What the trusted reverse proxy says about a request: who sent it, and
|
||||
whether it arrived over HTTPS.
|
||||
|
||||
Grimoire serves plain HTTP; TLS ends at the operator's proxy. The proxy
|
||||
reports the client's address in X-Forwarded-For and the scheme in
|
||||
X-Forwarded-Proto, and each proxy on the road APPENDS its entry, so only the
|
||||
right-hand end of either header was written by a proxy we trust. Anything to
|
||||
the left of that is whatever the client chose to send.
|
||||
|
||||
`TRUSTED_PROXY_HOPS` says how many proxies stand in front of the app (default
|
||||
1). The entry read is that many in from the right. Zero trusts no proxy: the
|
||||
headers are ignored and the socket peer is the client.
|
||||
|
||||
Never read X-Forwarded-For or X-Forwarded-Proto anywhere else. The client
|
||||
address keys the rate limits and the audit log, and the scheme decides the
|
||||
session cookie's Secure flag and HSTS. A second reader is a second trust rule.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from quart import has_request_context, request
|
||||
from quart.sessions import SecureCookieSessionInterface
|
||||
|
||||
from grimoire.config import Config
|
||||
|
||||
# HSTS for a year, sent only on a request the trusted proxy says is HTTPS.
|
||||
# No includeSubDomains or preload: the app does not own the operator's domain.
|
||||
HSTS_VALUE = "max-age=31536000"
|
||||
|
||||
|
||||
def trusted_entry(header: str, hops: int) -> str | None:
|
||||
"""The entry `hops` in from the right of a forwarding header.
|
||||
|
||||
A header shorter than that is read at its leftmost entry, the one the
|
||||
first proxy wrote. A road with fewer proxies than the count (a LAN road
|
||||
beside a Cloudflare one) still reads right that way. None when no proxy
|
||||
is trusted or there is no header. Pure, so the trust boundary is
|
||||
testable without a request."""
|
||||
if hops <= 0:
|
||||
return None
|
||||
entries = [part.strip() for part in header.split(",") if part.strip()]
|
||||
if not entries:
|
||||
return None
|
||||
return entries[-min(hops, len(entries))]
|
||||
|
||||
|
||||
def _forwarded(name: str) -> str | None:
|
||||
# Joined, because a header sent twice arrives as two values, and each
|
||||
# proxy's append may land on either one.
|
||||
return trusted_entry(",".join(request.headers.getlist(name)), Config.TRUSTED_PROXY_HOPS)
|
||||
|
||||
|
||||
def client_ip() -> str:
|
||||
"""The caller's address, as the trusted proxy saw it."""
|
||||
if not has_request_context():
|
||||
return ""
|
||||
return (_forwarded("X-Forwarded-For") or request.remote_addr or "")[:64]
|
||||
|
||||
|
||||
def is_https() -> bool:
|
||||
"""Whether this request arrived over HTTPS, by the trusted proxy's word."""
|
||||
if not has_request_context():
|
||||
return False
|
||||
if request.scheme == "https":
|
||||
return True
|
||||
return (_forwarded("X-Forwarded-Proto") or "").lower() == "https"
|
||||
|
||||
|
||||
class PerRequestSecureSession(SecureCookieSessionInterface):
|
||||
"""The session cookie is Secure exactly when the request is HTTPS, so a
|
||||
public deployment gets it and a plain-HTTP one keeps working."""
|
||||
|
||||
def get_cookie_secure(self, app) -> bool:
|
||||
return is_https()
|
||||
|
||||
|
||||
def apply_https_headers(response) -> None:
|
||||
"""HSTS on an HTTPS request, and only then. The app never redirects to
|
||||
HTTPS; plain HTTP is a supported way to run it."""
|
||||
if is_https():
|
||||
response.headers.setdefault("Strict-Transport-Security", HSTS_VALUE)
|
||||
Reference in New Issue
Block a user