mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-24 22:09:56 +00:00
* feat(proxy)!: refuse to start in fips_mode without a hex salt key With general_settings.fips_mode on, the proxy now refuses to start unless the key it encrypts stored secrets with (LITELLM_SALT_KEY, or the master key when LITELLM_SALT_KEY is not set) is at least 32 bytes of hex. The fips_mode key derivations already need that shape, so a deployment with any other key used to boot and then fail on its first secret write. The hex pattern moves to fips.py so the boot gate and the KBKDF helper share one definition. Standard mode is unchanged. * feat(proxy)!: refuse fips_mode salt keys under 112 bits instead of non-hex keys The fips_mode boot gate, derive_key_from_salt_key and their tests now share key_derivation_key: a salt key of 32+ bytes of hex (optionally sk-) is decoded as before, any other key is taken as its UTF-8 bytes, and a key under 14 bytes is refused. The refusal names the master key when LITELLM_SALT_KEY is not set. * fix(proxy): keep the raw salt key out of the frames that raise fips_mode refusals derive_key_from_salt_key no longer binds LITELLM_SALT_KEY to a local before refusing it, and enforce_fips_mode now takes get_salt_key instead of the key itself, so only fips_mode_refusal sees the key and it returns before the raise. An error reporter that captures frame locals can no longer pick up the key from a refused boot or a refused key derivation. * feat(proxy)!: derive fips_mode keys from the salt key's own bytes, without decoding hex fips_mode is only for new deployments, so there is no derived key to keep byte-identical with main. key_derivation_key now always takes the UTF-8 bytes of the salt key and only enforces the 112-bit floor. * feat(proxy)!: require LITELLM_SALT_KEY to start in fips_mode The master key no longer stands in for a missing salt key. It also signs UI session tokens, and NIST SP 800-108r1 section 5 says a key-derivation key is used for nothing else. enforce_fips_mode and fips_mode_refusal go back to main's one-argument signatures and read LITELLM_SALT_KEY themselves.
288 lines
12 KiB
Python
288 lines
12 KiB
Python
import _hashlib
|
|
from collections.abc import Iterator, Mapping
|
|
from contextlib import nullcontext
|
|
from traceback import walk_tb
|
|
from typing import Final
|
|
from unittest.mock import Mock
|
|
|
|
import pytest
|
|
from cryptography.exceptions import InternalError
|
|
from cryptography.hazmat.bindings._rust import openssl as rust_openssl
|
|
from cryptography.hazmat.primitives import hashes
|
|
|
|
from litellm.proxy.common_utils.fips import (
|
|
FipsModeError,
|
|
cryptography_enforces_fips,
|
|
enforce_fips_mode,
|
|
fips_mode_active,
|
|
key_derivation_key,
|
|
openssl_enforces_fips,
|
|
)
|
|
|
|
_HEX_SALT_KEY: Final = "sk-" + bytes(range(32)).hex()
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _fips_mode_off_after_each_test() -> Iterator[None]:
|
|
yield
|
|
enforce_fips_mode({})
|
|
|
|
|
|
def _fake_openssl(monkeypatch: pytest.MonkeyPatch, *, fips_flag: int, scrypt_refused: bool) -> None:
|
|
monkeypatch.setattr(_hashlib, "get_fips_mode", lambda: fips_flag)
|
|
if scrypt_refused:
|
|
monkeypatch.setattr(_hashlib, "scrypt", Mock(side_effect=ValueError("[digital envelope routines] unsupported")))
|
|
else:
|
|
monkeypatch.setattr(_hashlib, "scrypt", Mock(return_value=b"\0" * 16))
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"fips_flag, scrypt_refused, enforces",
|
|
[(1, True, True), (0, True, False), (1, False, False)],
|
|
ids=["fips", "fips-flag-off", "scrypt-allowed"],
|
|
)
|
|
def test_openssl_enforces_fips_needs_the_fips_flag_and_a_refused_scrypt(
|
|
monkeypatch: pytest.MonkeyPatch, fips_flag: int, scrypt_refused: bool, enforces: bool
|
|
) -> None:
|
|
_fake_openssl(monkeypatch, fips_flag=fips_flag, scrypt_refused=scrypt_refused)
|
|
assert openssl_enforces_fips() is enforces
|
|
|
|
|
|
def test_openssl_enforces_fips_while_md5_is_still_served_for_non_security_use(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
_fake_openssl(monkeypatch, fips_flag=1, scrypt_refused=True)
|
|
monkeypatch.setattr(_hashlib, "new", Mock(return_value=Mock()))
|
|
assert openssl_enforces_fips() is True
|
|
|
|
|
|
def test_openssl_without_get_fips_mode_does_not_enforce_fips(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
monkeypatch.delattr(_hashlib, "get_fips_mode")
|
|
assert openssl_enforces_fips() is False
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"fips_enabled, sha256_broken, enforces",
|
|
[(True, False, True), (False, False, False), (True, True, False)],
|
|
ids=["fips", "fips-flag-off", "sha256-broken"],
|
|
)
|
|
def test_cryptography_enforces_fips_needs_the_fips_flag_and_a_working_sha256(
|
|
monkeypatch: pytest.MonkeyPatch, fips_enabled: bool, sha256_broken: bool, enforces: bool
|
|
) -> None:
|
|
monkeypatch.setattr(rust_openssl, "is_fips_enabled", lambda: fips_enabled)
|
|
if sha256_broken:
|
|
monkeypatch.setattr(hashes, "Hash", Mock(side_effect=InternalError("Unknown OpenSSL error", [])))
|
|
assert cryptography_enforces_fips() is enforces
|
|
|
|
|
|
def test_cryptography_without_is_fips_enabled_does_not_enforce_fips(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
monkeypatch.delattr(rust_openssl, "is_fips_enabled")
|
|
assert cryptography_enforces_fips() is False
|
|
|
|
|
|
def test_this_runners_python_openssl_does_not_enforce_fips() -> None:
|
|
if _hashlib.get_fips_mode() == 1:
|
|
pytest.skip("this runner's Python OpenSSL is in FIPS mode")
|
|
assert openssl_enforces_fips() is False
|
|
|
|
|
|
def test_this_runners_cryptography_openssl_does_not_enforce_fips() -> None:
|
|
if rust_openssl.is_fips_enabled():
|
|
pytest.skip("this runner's cryptography OpenSSL is in FIPS mode")
|
|
assert cryptography_enforces_fips() is False
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"general_settings, openssl_fips, cryptography_fips, refusal",
|
|
[
|
|
({}, False, False, None),
|
|
({"fips_mode": False}, False, False, None),
|
|
({"fips_mode": "false"}, False, False, None),
|
|
({"fips_mode": True}, True, True, None),
|
|
({"fips_mode": "true"}, True, True, None),
|
|
({"fips_mode": True}, False, True, "general_settings.fips_mode is on but OpenSSL does not enforce FIPS"),
|
|
({"fips_mode": " TRUE "}, False, True, "general_settings.fips_mode is on but OpenSSL does not enforce FIPS"),
|
|
({"fips_mode": True}, True, False, "cryptography library's OpenSSL has no working FIPS provider"),
|
|
({"fips_mode": "enforced"}, True, True, "general_settings.fips_mode='enforced' is not a boolean"),
|
|
({"fips_mode": 1}, True, True, "general_settings.fips_mode=1 is not a boolean"),
|
|
({"fips_mode": None}, True, True, "general_settings.fips_mode has no value"),
|
|
],
|
|
ids=[
|
|
"unset",
|
|
"false",
|
|
"false-from-a-string",
|
|
"fips",
|
|
"fips-from-a-string",
|
|
"openssl-not-fips",
|
|
"openssl-not-fips-from-a-padded-string",
|
|
"cryptography-not-fips",
|
|
"not-a-boolean",
|
|
"a-number",
|
|
"no-value",
|
|
],
|
|
)
|
|
def test_enforce_fips_mode(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
general_settings: Mapping[str, object],
|
|
openssl_fips: bool,
|
|
cryptography_fips: bool,
|
|
refusal: str | None,
|
|
) -> None:
|
|
_fake_openssl(monkeypatch, fips_flag=int(openssl_fips), scrypt_refused=openssl_fips)
|
|
monkeypatch.setattr(rust_openssl, "is_fips_enabled", lambda: cryptography_fips)
|
|
monkeypatch.setenv("LITELLM_SALT_KEY", _HEX_SALT_KEY)
|
|
|
|
with pytest.raises(FipsModeError, match=refusal) if refusal else nullcontext():
|
|
enforce_fips_mode(general_settings)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"general_settings, active",
|
|
[({"fips_mode": True}, True), ({"fips_mode": "true"}, True), ({"fips_mode": False}, False), ({}, False)],
|
|
ids=["on", "on-from-a-string", "off", "unset"],
|
|
)
|
|
def test_fips_mode_active_follows_the_setting_the_gate_accepted(
|
|
monkeypatch: pytest.MonkeyPatch, general_settings: Mapping[str, object], active: bool
|
|
) -> None:
|
|
_fake_openssl(monkeypatch, fips_flag=1, scrypt_refused=True)
|
|
monkeypatch.setattr(rust_openssl, "is_fips_enabled", lambda: True)
|
|
monkeypatch.setenv("LITELLM_SALT_KEY", _HEX_SALT_KEY)
|
|
enforce_fips_mode(general_settings)
|
|
assert fips_mode_active() is active
|
|
|
|
|
|
def test_fips_mode_stays_inactive_on_a_refused_boot(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
_fake_openssl(monkeypatch, fips_flag=0, scrypt_refused=False)
|
|
with pytest.raises(FipsModeError):
|
|
enforce_fips_mode({"fips_mode": True})
|
|
assert fips_mode_active() is False
|
|
|
|
|
|
def test_a_fips_openssl_alone_does_not_activate_fips_mode(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
_fake_openssl(monkeypatch, fips_flag=1, scrypt_refused=True)
|
|
assert fips_mode_active() is False
|
|
|
|
|
|
@pytest.fixture
|
|
def fips_openssl_on_both_sides(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
_fake_openssl(monkeypatch, fips_flag=1, scrypt_refused=True)
|
|
monkeypatch.setattr(rust_openssl, "is_fips_enabled", lambda: True)
|
|
|
|
|
|
_SHORT_SALT_KEYS: Final = ("thirteen-byte", "é" * 6 + "x", "sk-0a1b2c3d4e", "")
|
|
_SHORT_SALT_KEY_IDS: Final = ("13-ascii-bytes", "13-utf8-bytes-in-7-characters", "sk-and-10-hex-digits", "empty")
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"salt_key, key",
|
|
[
|
|
(_HEX_SALT_KEY, _HEX_SALT_KEY.encode()),
|
|
("fourteen-bytes", b"fourteen-bytes"),
|
|
("é" * 7, "é".encode() * 7),
|
|
("\udcff" * 14, b"\xff" * 14),
|
|
],
|
|
ids=[
|
|
"hex-is-not-decoded",
|
|
"14-ascii-bytes",
|
|
"14-utf8-bytes-in-7-characters",
|
|
"environment-bytes-that-are-not-utf8",
|
|
],
|
|
)
|
|
def test_key_derivation_key_is_the_utf8_bytes_of_the_salt_key(salt_key: str, key: bytes) -> None:
|
|
assert key_derivation_key(salt_key) == key
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"salt_key", (*_SHORT_SALT_KEYS, "\udcff" * 13), ids=(*_SHORT_SALT_KEY_IDS, "13-environment-bytes")
|
|
)
|
|
def test_key_derivation_key_refuses_a_salt_key_under_112_bits(salt_key: str) -> None:
|
|
assert key_derivation_key(salt_key) is None
|
|
|
|
|
|
@pytest.mark.usefixtures("fips_openssl_on_both_sides")
|
|
@pytest.mark.parametrize(
|
|
"salt_key",
|
|
[_HEX_SALT_KEY, "fourteen-bytes", "é" * 7],
|
|
ids=["openssl-rand-hex-32", "14-ascii-bytes", "14-utf8-bytes-in-7-characters"],
|
|
)
|
|
def test_fips_mode_starts_with_a_salt_key_of_at_least_112_bits(monkeypatch: pytest.MonkeyPatch, salt_key: str) -> None:
|
|
monkeypatch.setenv("LITELLM_SALT_KEY", salt_key)
|
|
enforce_fips_mode({"fips_mode": True})
|
|
assert fips_mode_active() is True
|
|
|
|
|
|
@pytest.mark.usefixtures("fips_openssl_on_both_sides")
|
|
@pytest.mark.parametrize("salt_key", _SHORT_SALT_KEYS, ids=_SHORT_SALT_KEY_IDS)
|
|
def test_fips_mode_refuses_to_start_with_a_salt_key_under_112_bits(
|
|
monkeypatch: pytest.MonkeyPatch, salt_key: str
|
|
) -> None:
|
|
monkeypatch.setenv("LITELLM_SALT_KEY", salt_key)
|
|
with pytest.raises(FipsModeError) as refused:
|
|
enforce_fips_mode({"fips_mode": True})
|
|
assert str(refused.value) == (
|
|
"general_settings.fips_mode is on but LITELLM_SALT_KEY is too short. fips_mode derives its encryption keys "
|
|
"from it, so it must be at least 112 bits (14 bytes). Set LITELLM_SALT_KEY=sk-$(openssl rand -hex 32), or "
|
|
"turn general_settings.fips_mode off."
|
|
)
|
|
assert fips_mode_active() is False
|
|
|
|
|
|
@pytest.mark.usefixtures("fips_openssl_on_both_sides")
|
|
def test_fips_mode_refuses_to_start_without_a_salt_key(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
monkeypatch.delenv("LITELLM_SALT_KEY", raising=False)
|
|
with pytest.raises(FipsModeError) as refused:
|
|
enforce_fips_mode({"fips_mode": True})
|
|
assert str(refused.value) == (
|
|
"general_settings.fips_mode is on but LITELLM_SALT_KEY is not set. fips_mode derives its encryption keys "
|
|
"from it and never from the master key, which also signs tokens. Set "
|
|
"LITELLM_SALT_KEY=sk-$(openssl rand -hex 32), or turn general_settings.fips_mode off."
|
|
)
|
|
assert fips_mode_active() is False
|
|
|
|
|
|
@pytest.mark.usefixtures("fips_openssl_on_both_sides")
|
|
@pytest.mark.parametrize("salt_key", _SHORT_SALT_KEYS[:3], ids=_SHORT_SALT_KEY_IDS[:3])
|
|
def test_the_fips_mode_salt_key_refusal_does_not_print_the_key(monkeypatch: pytest.MonkeyPatch, salt_key: str) -> None:
|
|
monkeypatch.setenv("LITELLM_SALT_KEY", salt_key)
|
|
with pytest.raises(FipsModeError) as refused:
|
|
enforce_fips_mode({"fips_mode": True})
|
|
assert salt_key not in str(refused.value)
|
|
assert salt_key[3:] not in str(refused.value)
|
|
|
|
|
|
_FRAME_SECRET: Final = "frame-secret"
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"openssl_fips, cryptography_fips",
|
|
[(True, True), (False, True), (True, False)],
|
|
ids=["salt-key-under-112-bits", "openssl-not-fips", "cryptography-not-fips"],
|
|
)
|
|
def test_a_refused_fips_mode_boot_leaves_the_salt_key_in_no_frame_local(
|
|
monkeypatch: pytest.MonkeyPatch, openssl_fips: bool, cryptography_fips: bool
|
|
) -> None:
|
|
_fake_openssl(monkeypatch, fips_flag=int(openssl_fips), scrypt_refused=openssl_fips)
|
|
monkeypatch.setattr(rust_openssl, "is_fips_enabled", lambda: cryptography_fips)
|
|
monkeypatch.setenv("LITELLM_SALT_KEY", _FRAME_SECRET)
|
|
with pytest.raises(FipsModeError) as refused:
|
|
enforce_fips_mode({"fips_mode": True})
|
|
held_by: Final = tuple(
|
|
frame.f_code.co_name for frame, _ in walk_tb(refused.tb) if _FRAME_SECRET in repr(frame.f_locals)
|
|
)
|
|
assert held_by == ()
|
|
|
|
|
|
@pytest.mark.usefixtures("fips_openssl_on_both_sides")
|
|
@pytest.mark.parametrize("general_settings", [{}, {"fips_mode": False}], ids=["unset", "off"])
|
|
@pytest.mark.parametrize(
|
|
"salt_key",
|
|
[_HEX_SALT_KEY, "thirteen-byte", "", None],
|
|
ids=["openssl-rand-hex-32", "13-bytes", "empty", "no-key"],
|
|
)
|
|
def test_standard_mode_starts_whatever_the_salt_key_looks_like(
|
|
monkeypatch: pytest.MonkeyPatch, general_settings: Mapping[str, object], salt_key: str | None
|
|
) -> None:
|
|
if salt_key is None:
|
|
monkeypatch.delenv("LITELLM_SALT_KEY", raising=False)
|
|
else:
|
|
monkeypatch.setenv("LITELLM_SALT_KEY", salt_key)
|
|
enforce_fips_mode(general_settings)
|
|
assert fips_mode_active() is False
|