Whether this request's PUBLIC-facing origin is HTTPS, for deciding whether a cookie set on the response should be marked ``Secure``. litellm only sees a plain-HTTP hop whenever TLS terminates at a reverse proxy, so ``request.url.scheme`` alone cannot answer this in
(
request: Request,
general_settings: Mapping[str, object] | None = None,
)
| 198 | |
| 199 | @staticmethod |
| 200 | def is_request_https( |
| 201 | request: Request, |
| 202 | general_settings: Mapping[str, object] | None = None, |
| 203 | ) -> bool: |
| 204 | """ |
| 205 | Whether this request's PUBLIC-facing origin is HTTPS, for deciding |
| 206 | whether a cookie set on the response should be marked ``Secure``. |
| 207 | |
| 208 | litellm only sees a plain-HTTP hop whenever TLS terminates at a |
| 209 | reverse proxy, so ``request.url.scheme`` alone cannot answer this in |
| 210 | that deployment shape. Resolved from the first trusted signal: |
| 211 | 1. ``PROXY_BASE_URL`` (operator-declared public origin). |
| 212 | 2. ``X-Forwarded-Proto``, only when the request's direct peer is a |
| 213 | configured trusted proxy -- see ``is_request_from_trusted_proxy``. |
| 214 | An untrusted caller cannot spoof this header to strip Secure. |
| 215 | 3. The request's own literal scheme (direct TLS termination, or no |
| 216 | reverse proxy in front of litellm). |
| 217 | """ |
| 218 | configured_base_url: Final = os.environ.get("PROXY_BASE_URL", "").strip() |
| 219 | if configured_base_url: |
| 220 | return urlparse(configured_base_url).scheme == "https" |
| 221 | |
| 222 | if IPAddressUtils.is_request_from_trusted_proxy(request, general_settings=general_settings): |
| 223 | forwarded_proto: Final = request.headers.get("X-Forwarded-Proto") |
| 224 | if forwarded_proto: |
| 225 | return forwarded_proto.split(",")[0].strip().lower() == "https" |
| 226 | |
| 227 | return request.url.scheme == "https" |
| 228 | |
| 229 | @staticmethod |
| 230 | def extract_client_ip_from_xff_hops( |