Enhance encryption security#19253
Conversation
Signed-off-by: Ben Wilson <benjamin.wilson@databricks.com>
|
@TomeHirata @B-Step62 I was running through some attack scenarios this evening and I managed to find a few ways that security could be degraded / this feature could break if someone didn't read up on these algorithms. |
There was a problem hiding this comment.
Pull request overview
This PR enhances the security of MLflow's encryption implementation through three key defensive measures identified during red-team testing:
- Forces KEK version into the PBKDF2 salt to ensure unique KEKs even if an admin accidentally reuses a passphrase across rotations, protecting against leaked passphrase attack vectors
- Converts the test-verification nonce parameter to private (
_nonce) with keyword-only access and prominent warnings to prevent dangerous misuse in production code while preserving testability - Adds comprehensive NB (nota bene) comments documenting the critical immutability requirements of
secret_idandsecret_namefields used in AAD (Additional Authenticated Data) for AES-GCM encryption
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
mlflow/utils/crypto.py |
Implements versioned salt by appending KEK version as 4 big-endian bytes; refactors encrypt_with_aes_gcm to private _encrypt_with_aes_gcm with _nonce as keyword-only parameter; adds detailed security documentation |
tests/utils/test_crypto.py |
Adds test verifying same passphrase with different versions produces different KEKs; updates all function calls to use _encrypt_with_aes_gcm with keyword argument _nonce |
mlflow/store/tracking/dbmodels/models.py |
Documents immutability constraints for secret_id and secret_name database columns with cross-references to AAD implementation |
mlflow/entities/gateway_secrets.py |
Documents immutability requirements for secret_id and secret_name entity fields with explanation of AAD binding |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Documentation preview for bc10b82 is available at: More info
|
mlflow/utils/crypto.py
Outdated
| def encrypt_with_aes_gcm( | ||
| plaintext: bytes, key: bytes, nonce: bytes | None = None, aad: bytes | None = None | ||
| def _encrypt_with_aes_gcm( | ||
| plaintext: bytes, key: bytes, *, aad: bytes | None = None, _nonce: bytes | None = None |
There was a problem hiding this comment.
Why do we use _nonce instead of nonce? This is already a private function
There was a problem hiding this comment.
Even though it's end user private, this interface is callable internally by developers. I wanted to make this particularly field extra clear to devs that this is somewhat dangerous to provide a value for outside of testing (as it degrades the security of encryption to a large degree if a static value is passed in). It's intentionally meant to look abnormal.
mlflow/utils/crypto.py
Outdated
| aad: Optional Additional Authenticated Data. If provided, this data is | ||
| authenticated but not encrypted. Useful for binding encryption to | ||
| metadata (e.g., secret_id + secret_name) to prevent substitution attacks. | ||
| _nonce: TESTING ONLY. 12-byte nonce. If None (default), generates random nonce. |
There was a problem hiding this comment.
I'll change the argument name here to make it even more clear :)
| secret_id: Unique identifier for this secret. IMMUTABLE - used in AAD for encryption. | ||
| secret_name: User-friendly name for the secret. IMMUTABLE - used in AAD for encryption. |
There was a problem hiding this comment.
I think this docstring makes no difference, we need to update code to make it really immutable
There was a problem hiding this comment.
The DB API layer already enforces immutability. But, what it doesn't do is actually block updates to this field via a DB trigger that enforces this. In order to prevent any future manipulation, I'll add that constraint.
Thanks for the perspective!
| assert all(s.provider == "openai" for s in openai_secrets) | ||
|
|
||
|
|
||
| def test_secret_id_and_name_are_immutable_at_database_level(store: SqlAlchemyStore): |
There was a problem hiding this comment.
Could we run this test file in database job
mlflow/.github/workflows/master.yml
Lines 132 to 135 in 312edd5
There was a problem hiding this comment.
Great idea!! Updated CI config :D
|
|
||
|
|
||
| @dataclass | ||
| @dataclass(frozen=True) |
There was a problem hiding this comment.
This means last_updated_at can't be updated as well, if it's never modified then it's fine
There was a problem hiding this comment.
Yep - totally fine, we return a new instance when the API is called, but on the update route for a key, the DB merges the record and updates the last_update_time.
mlflow/utils/crypto.py
Outdated
| nonce = os.urandom(GCM_NONCE_LENGTH) | ||
| elif len(nonce) != GCM_NONCE_LENGTH: | ||
| raise ValueError(f"Nonce must be {GCM_NONCE_LENGTH} bytes (96 bits), got {len(nonce)}") | ||
| elif len(_nonce_for_testing) != GCM_NONCE_LENGTH: |
There was a problem hiding this comment.
nit: do we still need this validation if this argument is test only?
There was a problem hiding this comment.
Thanks for the catch! This is totally not needed :)
serena-ruan
left a comment
There was a problem hiding this comment.
LGTM! Do we need to add v3.8.0 label?
Signed-off-by: Ben Wilson <benjamin.wilson@databricks.com>
🛠 DevTools 🛠
Install mlflow from this PR
For Databricks, use the following command:
Related Issues/PRs
#xxxWhat changes are proposed in this pull request?
Changes a few things that I noticed while red-teaming the implementation:
How is this PR tested?
Does this PR require documentation update?
Release Notes
Is this a user-facing change?
What component(s), interfaces, languages, and integrations does this PR affect?
Components
area/tracking: Tracking Service, tracking client APIs, autologgingarea/models: MLmodel format, model serialization/deserialization, flavorsarea/model-registry: Model Registry service, APIs, and the fluent client calls for Model Registryarea/scoring: MLflow Model server, model deployment tools, Spark UDFsarea/evaluation: MLflow model evaluation features, evaluation metrics, and evaluation workflowsarea/gateway: MLflow AI Gateway client APIs, server, and third-party integrationsarea/prompts: MLflow prompt engineering features, prompt templates, and prompt managementarea/tracing: MLflow Tracing features, tracing APIs, and LLM tracing functionalityarea/projects: MLproject format, project running backendsarea/uiux: Front-end, user experience, plotting, JavaScript, JavaScript dev serverarea/build: Build and test infrastructure for MLflowarea/docs: MLflow documentation pagesHow should the PR be classified in the release notes? Choose one:
rn/none- No description will be included. The PR will be mentioned only by the PR number in the "Small Bugfixes and Documentation Updates" sectionrn/breaking-change- The PR will be mentioned in the "Breaking Changes" sectionrn/feature- A new user-facing feature worth mentioning in the release notesrn/bug-fix- A user-facing bug fix worth mentioning in the release notesrn/documentation- A user-facing documentation change worth mentioning in the release notesShould this PR be included in the next patch release?
Yesshould be selected for bug fixes, documentation updates, and other small changes.Noshould be selected for new features and larger changes. If you're unsure about the release classification of this PR, leave this unchecked to let the maintainers decide.What is a minor/patch release?
Bug fixes, doc updates and new features usually go into minor releases.
Bug fixes and doc updates usually go into patch releases.