Repository navigation
feat(proxy): encrypt stored secrets with AES-256-GCM in fips_mode - #51828
yucheng-berri wants to merge 5 commits into
Conversation
There was a problem hiding this comment.
Devin Review found 1 potential issue.
1 flag not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)
| def _encrypt_aes_gcm(value: str, signing_key: str) -> str: | ||
| """Encrypt under AES-256-GCM and return the versioned ``v2:gcm:`` string.""" | ||
| sealed: Final = _seal_aes_gcm(value=value, signing_key=signing_key, aad=None) | ||
| sealed: Final = _seal_aes_gcm(value=value, key=_at_rest_key(signing_key), aad=None) |
There was a problem hiding this comment.
🟨 Non-approved encryption in FIPS mode
With fips_mode enabled, _encrypt_aes_gcm uses an application-generated nonce through the shared seal. The documented provider classifies external-IV GCM encryption as non-approved. Stored secrets therefore use a non-approved service despite FIPS mode.
Was this helpful? React with 👍 or 👎 to provide feedback.
There was a problem hiding this comment.
Declared in the PR body under What differs from #51429. The maintainer chose the library call that peer FIPS builds ship, knowing the indicator reads 0.
|
bugbot run |
[High impact] The PR appears safe to merge within its stated FIPS deployment and migration limits.
|
Merging this PR will not alter performance1 warning
Comparing |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
bugbot run |
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.
3b436a2 to
33f8446
Compare
|
bugbot run |
…n-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.
33f8446 to
1fb6388
Compare
|
bugbot run |
…ode 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.
1fb6388 to
52ce5d1
Compare
|
bugbot run |
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit 52ce5d1. Configure here.
Handwritten 1-3 sentence description
<make-me-human-readable>With
general_settings.fips_mode: true, the proxy still encrypted every stored secret (model keys, credentials, MCP tokens) with XSalsa20-Poly1305 from libsodium, which is not a FIPS algorithm and runs outside the validated OpenSSL module. Infips_modethose secrets are now written with AES-256-GCM through thecryptographylibrary, under a key the FIPS provider derives fromLITELLM_SALT_KEY, and values in any other format are refused on read. Withoutfips_modenothing changes</make-me-human-readable>Details
Replaces #51429: same change, with the seal done by
cryptographyinstead of ctypes.Stacked on #52099, which makes a
fips_modeproxy refuse to start when the salt key is under 112 bits (14 bytes). Rebased onto its head 75a29d1._fips_at_rest_keynow turns the key into bytes through #52099'skey_derivation_key, the same function MCP keys and plugin claims use. A key of 32+ bytes of hex derives the same at-rest key as 33f8446. Any other key of 14+ bytes, which 33f8446 refused, is used as its UTF-8 bytes.TLDR
Problem this solves:
fips_modeproxies stored secrets with libsodium and said nothingHow it solves it:
fips_modethe write algorithm is always AES-256-GCMcryptography'sAESGCM, the same call main's opt-in path usesos.urandom, as on main's opt-in pathv2:gcm:+ base64url(nonce + ciphertext + tag)fips_modea value without thev2:gcm:prefix is refusedUser Flow
Before: a
fips_modeproxy stores a model key with libsodiumgeneral_settings.fips_mode: trueapi_key, and gets 200After: the same request stores the key with AES-256-GCM from the FIPS provider
v2:gcm:and was encrypted by the FIPS provider's AES-256-GCMLinear ticket
Resolves LIT-8427
Behavior changes
Removed / renamed
Silent regressions to watch
fips_mode. The default write is still PyNaCl,encryption_algorithm: aes-256-gcmstill writes the samev2:gcm:values with the same key, and both are read as beforefips_mode: true, all of these are the same as in feat(fips): encrypt stored secrets with the FIPS provider's AES-256-GCM in fips_mode #51429LITELLM_SALT_KEY(or the master key, when no salt key is set) must be at least 112 bits (14 bytes), such assk-$(openssl rand -hex 32). Otherwise every write of a stored secret raises.POST /credentialsreturns 500 with that message andPOST /model/newreturns 500Failed to add model to db. Main accepted those writes and stored them with libsodium. Unreachable once feat(proxy)!: require a LITELLM_SALT_KEY of at least 112 bits to start in fips_mode #52099 lands: the proxy refuses to startsk-, is decoded, and any other key is used as its UTF-8 bytes, the same way feat(proxy)!: require a LITELLM_SALT_KEY of at least 112 bits to start in fips_mode #52099 reads it for MCP keys and plugin claimsfips_modethat write now fails with the same salt key message, so a secret is never stored in plaintext because of a bad key. With a bad key at startup this is unreachable once feat(proxy)!: require a LITELLM_SALT_KEY of at least 112 bits to start in fips_mode #52099 lands: the proxy refuses to startencryption_algorithm: xsalsa20-poly1305is ignoredfips_mode(PyNaCl values, andv2:gcm:values written in standard mode) are not decrypted. The models and credentials behind them do not load, and the log says why. Main read themfips_modecannot be read afterfips_modeis turned off, because the key differsencryption_algorithm: aes-256-gcm, so a response id becomesresp_v2:gcm:...fips_modeis only in v1.110.0 dev and rc tags, no stable releaseWhat differs from #51429
os.urandom(12)) and passed to the module. feat(fips): encrypt stored secrets with the FIPS provider's AES-256-GCM in fips_mode #51429 passed no nonce, so the FIPS provider drew itiv-generated=0for each encryption. Its security policy (page 22 Table 8, and page 53 section 2.7.9) lists "AES-GCM encryption with external IV" as a non-approved service. feat(fips): encrypt stored secrets with the FIPS provider's AES-256-GCM in fips_mode #51429 producediv-generated=1fips-indicator=1. Both are measured below at this headcryptographywithos.urandom(12)_fips_seal_aes_gcmand its "OpenSSL could not run AES-256-GCM" error are gone. The key derivation still reaches OpenSSL through main's existing ctypes KBKDF helper, which this PR does not touch, so on an OpenSSL without a FIPS provider afips_modewrite still fails with "OpenSSL could not run KBKDF"_KBKDF_PROPERTIESkeeps its name, andderive_key_from_salt_keyis not editedMigration for existing customers
fips_modeofffips_modeis for new deployments. A database with secrets written beforefips_modewas on has to be re-created or have those secrets entered againRegression tests
tests/unit/proxy/common_utils/test_encrypt_decrypt_utils.pytest_fips_mode_writes_aes_gcm_under_the_kbkdf_key: the value isv2:gcm:+ 12 + n + 16 bytes, plaincryptographyopens it under the SP 800-108 key computed by hand, two writes of the same plaintext differ, and anxsalsa20-poly1305setting does not change ittest_fips_mode_reads_a_value_sealed_by_cryptography_under_the_kbkdf_keytest_fips_mode_refuses_a_value_changed_after_it_was_written: one flipped bit in the nonce, ciphertext or tagtest_fips_mode_refuses_values_it_did_not_writetest_values_written_in_fips_mode_are_not_read_in_standard_modetest_fips_mode_encrypts_under_an_explicit_new_key: a hex and a non-hex new keytest_fips_mode_reads_known_answer_values_written_under_a_hex_salt_key: twov2:gcm:values written by 33f8446 still decrypttest_fips_mode_writes_under_the_kbkdf_key_of_the_utf8_bytes_of_a_salt_key_that_is_not_hex: 14 ASCII bytes, 14 UTF-8 bytes in 7 characters, and a hex key with a trailing newlinetest_fips_mode_refuses_to_encrypt_under_a_salt_key_under_112_bits: 13 ASCII bytes and 13 UTF-8 bytes, with the key absent from the messagetest_fips_mode_leaves_bearer_tokens_as_they_aretest_fips_mode_never_stores_a_secret_as_written_when_it_cannot_be_encrypted: callback vars, guardrail params and search tool paramstest_fips_mode_merge_user_env_vars_takes_its_lock_without_blake2bused a salt key that is not hex. It now sets a hex one_fips_at_rest_keyat 1fb6388 do too (7 of 7 killed): hex taken as text, the key stripped, the 112-bit refusal skipped, the message going back to the hex rule or printing the key, and the label or context changedScreenshots / Proof of Fix
Real proxies on the real Chainguard FIPS image (Python 3.13, OpenSSL 4.0.3, Chainguard FIPS Provider 3.4.0-r5), with each tree's
litellm/mounted over the installed package, real Postgres 16, and a local upstream that records theAuthorizationheader.CHAINGUARD_LEGACY_ALLOWED=0,fips_mode: true, a 64-hex salt key.Rebase onto #52099's 112-bit floor: 33f8446 (Before) vs 1fb6388 (After)
Same image, real Postgres 16, real OpenAI
gpt-4.1-minibehind a recorder, throwaway keys deleted after the run, 0 of them in any proxy log.Before (33f8446)
POST /model/newandPOST /credentialsreturn 200, both rows start withv2:gcm:, chat through both returns 200LITELLM_SALT_KEY is not at least 32 bytes of hexPOST /key/regeneratewith a 20-byte non-hexnew_master_key: 500LITELLM_SALT_KEY must be at least 32 bytes of hexAfter (1fb6388)
fee3a84c7b82021e, the same as 33f8446'sv2:gcm:, chat through both returns 200v2:gcm:, chat 200POST /key/regeneratewith a 20-byte non-hexnew_master_key: 200, rows stayv2:gcm:. With a 13-byte one: 500the key that encrypts stored secrets (LITELLM_SALT_KEY, or the master key when LITELLM_SALT_KEY is not set) must be at least 112 bits (14 bytes). Main returns 200 for bothBefore (3ad1643)
POST /credentialsand twoPOST /model/newreturn 200GET /v1/models200 lists both models, chat 200, and the upstream receivedBearer sk-b3-MODEL-PLAINTEXT-1andBearer sk-b3-CRED-PLAINTEXT-1_sodium mapped=True, libcrypto mapped=True, fips.so mapped=Trueencrypt_value_helperanddecrypt_value_helper:crypto_secretbox_easyandcrypto_secretbox_open_easyare hit, and no libcrypto cipher call is madeAfter (3401bdb)
POST /credentialsand twoPOST /model/newreturn 200encryption_algorithm: xsalsa20-poly1305in the configGET /v1/models200 lists both models, chat 200, and the upstream receivedBearer sk-b3-MODEL-PLAINTEXT-1andBearer sk-b3-CRED-PLAINTEXT-1.POST /v1/responses200 with an id ofresp_v2:gcm:..., and the follow-up withprevious_response_id200 with the upstream's own id passed on.POST /v2/login200. 0 "Error decrypting" lines in the log_sodium mapped=False, libcrypto mapped=True, fips.so mapped=Trueencrypt_value_helperinfips_mode(shortened, the provider string is the same on every line):EVP_DecryptFinal_exreturns 1 on the FIPS provider's AES-256-GCM, with 0 libsodium hits. A PyNaCl value and a standard-modev2:gcm:value both come back asNone, and'nacl' in sys.modulesstays FalseGET /ui/returns 500 on both sides withCHAINGUARD_LEGACY_ALLOWED=0, as it did for #51429. It is not part of this change.The other cells in #51429 (rows written in standard mode and then
fips_modeon,fips_moderows read withfips_modeoff, base and head on one Postgres in standard mode, cost per call) were measured there at 1ee6ac4 and were not rerun here. The code they exercise is the same in this PR except for the seal.Tests and gates
At 52ce5d1, on a Linux VM with Python 3.12.3, whose OpenSSL is a shared library, so no
fips_modetest skipped:pytest tests/unit/proxy/common_utils tests/unit/llms/custom_httpx/test_http_handler.py tests/unit/proxy/test_proxy_server_endpoints_and_startup.py tests/unit/proxy/mcp_server/outbound_credentials tests/unit/proxy/test_plugin_routes.py tests/unit/proxy/auth tests/unit/proxy/mcp_server/test_mcp_env_vars.py tests/unit/proxy/guardrails tests/unit/proxy/search_endpoints -n 16 --timeout=300:16124 passed, 8 skipped, 1 xfailed. The 8 skips are unrelated optional guardrail packagesruff_strict_gate.py,type_discipline_gate.py,test_quality_gate.pyandtype_check_gate.pywith--base 75a29d1ec46(this PR's base, feat(proxy)!: require a LITELLM_SALT_KEY of at least 112 bits to start in fips_mode #52099's head): all four printOK_fips_at_rest_key: 7 of 7 killedThe live run in the Before (3ad1643) and After (3401bdb) sections above and these numbers are from 3401bdb, before the rebase. The rebase changes only how
_fips_at_rest_keyturns the key into bytes, and the hex case above shows it derives the same key:pytest tests/unit/proxy -n 14on 3401bdb and on its merge base7 failed, 55482 passed, 88 skipped, 1 xfailed7 failed, 55461 passed, 88 skipped, 1 xfailedmcp_server/test_operations.py, 5 inspend_tracking/test_input_tokens.py), none touched by this PRProxy command, config, env vars and client calls
The image is the FIPS image built from
docker/Dockerfile.fipsin #50330 (taggedlitellm-fipsbelow), with this branch'slitellm/mounted over the installed package. Use main'slitellm/for the Before steps.The gdb lines come from host gdb attached to a Python process in the same image that calls
enforce_fips_mode({"fips_mode": True})and thenencrypt_value_helper("...")anddecrypt_value_helper(...), with breakpoints onEVP_KDF_fetch,EVP_KDF_derive,EVP_CIPHER_fetch, theEVP_EncryptInit_ex*andEVP_CipherInit_ex*family, update and final,EVP_DecryptFinal_ex, thegetrandomsyscall and libsodium'scrypto_secretbox_*, reading theiv-generatedparameter withEVP_CIPHER_CTX_get_paramsafter each call.Type
🆕 New Feature
Caveats (if any)
Severe
fips_moderefuses every stored secret written before it was onfips_modeneeds a salt key of at least 112 bits (14 bytes). Once feat(proxy)!: require a LITELLM_SALT_KEY of at least 112 bits to start in fips_mode #52099 lands, the proxy refuses to start without oneiv-generated=0(see "What differs from feat(fips): encrypt stored secrets with the FIPS provider's AES-256-GCM in fips_mode #51429")High
Found by an adversarial review of #51429, present here too,
fips_modeonly, not fixed in this PR:/v1/responsesreturns 500, and a stream ends withresponse.failedclient_secretsandtranscription_sessionsreturn 500 after the upstream call/authorizereturns 500v2:gcm:can reach callers as ciphertextreturn_original_value=True(DB env vars inproxy_server.py) andcredential_hydration.pyget the stored ciphertext back and use it as the secret/key/regeneratewraps old ciphertext inv2:gcm:LITELLM_MIGRATE_FROM_MASTER_KEYreports nothing to migrateprevious_response_idreturns 403Found by a live base-versus-head run at 3b436a2 on the Chainguard FIPS base image,
fips_modeonly, not fixed in this PR:fips_modealready on are refused too, not only rows written before it was on. That includes rows main wrote withencryption_algorithm: aes-256-gcm, which use the SHA-256 keyfips_modestays on loses every row this PR wrote, the same way turningfips_modeoff does/key/generatewith callback vars fails and leaves one orphan budget row./team/newand/team/updatefail with no partial write. Unreachable once feat(proxy)!: require a LITELLM_SALT_KEY of at least 112 bits to start in fips_mode #52099 lands: the proxy refuses to start/key/regeneratewith anew_master_keyunder 112 bits returns 500 while a valid salt key is set, and the error namesLITELLM_SALT_KEY. Main returns 200. Anew_master_keythat is not hex but is 14+ bytes now returns 200Medium
/v1/responsesanswer infips_moderuns one KBKDF with no cacheLow
envelope.py) still call SecretBox directly infips_modeencrypt_bearer_token) keep the SHA-256 key infips_modePOST /model/newhides the reason when encryption failsencryption_algorithm: xsalsa20-poly1305is ignored infips_mode, not refused at bootcredential_migration.pyreads the raw setting and does not knowfips_modepynaclis still installed in the FIPS imageNote
High Risk
Changes core secret encryption and decrypt compatibility in fips_mode; existing non-v2:gcm DB secrets stop decrypting, and failed encrypt paths now error instead of storing plaintext.
Overview
When
general_settings.fips_modeis on, new at-rest secret writes always use AES-256-GCM (v2:gcm:) viacryptography, with keys derived through KBKDF (_fips_at_rest_key) instead of libsodium or plain SHA-256.encryption_algorithmis ignored for writes in FIPS mode.Decrypt in FIPS mode accepts only
v2:gcm:ciphertext; legacy nacl and standard-mode GCM values are refused (_unreadable_in_fips_mode). Callback vars, guardrail params, and search tool params no longer fall back to plaintext when encryption fails—FIPS mode re-raises instead.Bearer tokens keep the existing SHA-256 key path. Unit tests add broad FIPS round-trip, tamper rejection, key-length, and store-path coverage.
Reviewed by Cursor Bugbot for commit 52ce5d1. Bugbot is set up for automated code reviews on this repo. Configure here.