Skip to content

Enhance encryption security#19253

Merged
BenWilson2 merged 6 commits intomlflow:masterfrom
BenWilson2:kek-reuse-hardening
Dec 12, 2025
Merged

Enhance encryption security#19253
BenWilson2 merged 6 commits intomlflow:masterfrom
BenWilson2:kek-reuse-hardening

Conversation

@BenWilson2
Copy link
Member

@BenWilson2 BenWilson2 commented Dec 6, 2025

🛠 DevTools 🛠

Open in GitHub Codespaces

Install mlflow from this PR

# mlflow
pip install git+https://github.com/mlflow/mlflow.git@refs/pull/19253/merge
# mlflow-skinny
pip install git+https://github.com/mlflow/mlflow.git@refs/pull/19253/merge#subdirectory=libs/skinny

For Databricks, use the following command:

%sh curl -LsSf https://raw.githubusercontent.com/mlflow/mlflow/HEAD/dev/install-skinny.sh | sh -s pull/19253/merge

Related Issues/PRs

#xxx

What changes are proposed in this pull request?

Changes a few things that I noticed while red-teaming the implementation:

  1. Force the KEK version to be part of the salt. This ensures that if an admin reuses a passphrase in N >=2 rotations, the KEK will be unique even in this case, protecting against an attack vector of a leaked passphrase that wasn't graveyarded.
  2. Convert the test-verification nonce arg to be private with a clear warnings about how dangerous it is to not use a random nonce. We DO need this argument in order to test the encryption / decryption stack, but it does NOT need to be public.
  3. Add NB comments wherever someone might change the secret name in the backend. It is CRITICAL that this name never be made mutable (as it would break decryption). I felt that it was important to protect against foot-gunning in the future.

How is this PR tested?

  • Existing unit/integration tests
  • New unit/integration tests
  • Manual tests

Does this PR require documentation update?

  • No. You can skip the rest of this section.
  • Yes. I've updated:
    • Examples
    • API references
    • Instructions

Release Notes

Is this a user-facing change?

  • No. You can skip the rest of this section.
  • Yes. Give a description of this change to be included in the release notes for MLflow users.

What component(s), interfaces, languages, and integrations does this PR affect?

Components

  • area/tracking: Tracking Service, tracking client APIs, autologging
  • area/models: MLmodel format, model serialization/deserialization, flavors
  • area/model-registry: Model Registry service, APIs, and the fluent client calls for Model Registry
  • area/scoring: MLflow Model server, model deployment tools, Spark UDFs
  • area/evaluation: MLflow model evaluation features, evaluation metrics, and evaluation workflows
  • area/gateway: MLflow AI Gateway client APIs, server, and third-party integrations
  • area/prompts: MLflow prompt engineering features, prompt templates, and prompt management
  • area/tracing: MLflow Tracing features, tracing APIs, and LLM tracing functionality
  • area/projects: MLproject format, project running backends
  • area/uiux: Front-end, user experience, plotting, JavaScript, JavaScript dev server
  • area/build: Build and test infrastructure for MLflow
  • area/docs: MLflow documentation pages

How 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" section
  • rn/breaking-change - The PR will be mentioned in the "Breaking Changes" section
  • rn/feature - A new user-facing feature worth mentioning in the release notes
  • rn/bug-fix - A user-facing bug fix worth mentioning in the release notes
  • rn/documentation - A user-facing documentation change worth mentioning in the release notes

Should this PR be included in the next patch release?

Yes should be selected for bug fixes, documentation updates, and other small changes. No should 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?
  • Minor release: a release that increments the second part of the version number (e.g., 1.2.0 -> 1.3.0).
    Bug fixes, doc updates and new features usually go into minor releases.
  • Patch release: a release that increments the third part of the version number (e.g., 1.2.0 -> 1.2.1).
    Bug fixes and doc updates usually go into patch releases.
  • Yes (this PR will be cherry-picked and included in the next patch release)
  • No (this PR will be included in the next minor release)

Signed-off-by: Ben Wilson <benjamin.wilson@databricks.com>
Copilot AI review requested due to automatic review settings December 6, 2025 02:25
@github-actions github-actions bot added area/tracking Tracking service, tracking client APIs, autologging rn/feature Mention under Features in Changelogs. labels Dec 6, 2025
@BenWilson2 BenWilson2 added the team-review Trigger a team review request label Dec 6, 2025
@BenWilson2
Copy link
Member Author

@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.
I think it's pretty important to make these changes to harden the security implementation.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_id and secret_name fields 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.

@github-actions
Copy link
Contributor

github-actions bot commented Dec 6, 2025

Documentation preview for bc10b82 is available at:

More info
  • Ignore this comment if this PR does not change the documentation.
  • The preview is updated when a new commit is pushed to this PR.
  • This comment was created by this workflow run.
  • The documentation was built by this workflow run.

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
Copy link
Collaborator

@serena-ruan serena-ruan Dec 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we use _nonce instead of nonce? This is already a private function

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this testing only?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll change the argument name here to make it even more clear :)

Comment on lines +22 to +23
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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this docstring makes no difference, we need to update code to make it really immutable

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

Signed-off-by: Ben Wilson <benjamin.wilson@databricks.com>
assert all(s.provider == "openai" for s in openai_secrets)


def test_secret_id_and_name_are_immutable_at_database_level(store: SqlAlchemyStore):
Copy link
Collaborator

@serena-ruan serena-ruan Dec 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we run this test file in database job

./tests/db/compose.sh run --rm --no-TTY $service pytest \
tests/store/tracking/test_sqlalchemy_store.py \
tests/store/model_registry/test_sqlalchemy_store.py \
tests/db
?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea!! Updated CI config :D



@dataclass
@dataclass(frozen=True)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means last_updated_at can't be updated as well, if it's never modified then it's fine

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: do we still need this validation if this argument is test only?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the catch! This is totally not needed :)

Signed-off-by: Ben Wilson <benjamin.wilson@databricks.com>
Signed-off-by: Ben Wilson <benjamin.wilson@databricks.com>
Copy link
Collaborator

@serena-ruan serena-ruan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Do we need to add v3.8.0 label?

Signed-off-by: Ben Wilson <benjamin.wilson@databricks.com>
@BenWilson2 BenWilson2 added this pull request to the merge queue Dec 12, 2025
Merged via the queue into mlflow:master with commit b024559 Dec 12, 2025
47 checks passed
@BenWilson2 BenWilson2 deleted the kek-reuse-hardening branch December 12, 2025 22:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/tracking Tracking service, tracking client APIs, autologging rn/feature Mention under Features in Changelogs. team-review Trigger a team review request v3.8.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants