diff --git a/agent/secret_sources/bitwarden.py b/agent/secret_sources/bitwarden.py index 357f69cc6fb17..a80227f201dfc 100644 --- a/agent/secret_sources/bitwarden.py +++ b/agent/secret_sources/bitwarden.py @@ -47,9 +47,12 @@ from pathlib import Path from typing import Dict, List, Optional, Tuple -from cryptography.hazmat.primitives import hashes -from cryptography.hazmat.primitives.ciphers.aead import AESGCM -from cryptography.hazmat.primitives.kdf.hkdf import HKDF +# NOTE: cryptography is intentionally NOT imported at module level. It pulls +# in native bindings (cryptography._rust on Windows) that a mapped extension +# cannot replace once loaded — and this module is imported unconditionally by +# the secret-source registry for every CLI/gateway process, so an eager import +# trips `hermes update`'s self-lock preflight on Windows before it can even +# pull new commits (#86735). Import lazily inside the functions that use it. from agent.secret_sources._cache import ( CachedFetch as _CachedFetch, @@ -375,6 +378,11 @@ def _b64d(text: str) -> bytes: def _derive_encrypted_cache_key(access_token: str, salt: bytes) -> bytes: """Derive the local cache encryption key from the bootstrap BWS token.""" + # Lazy: cryptography must not load unless an encrypted cache is actually + # in use (see the module-level NOTE — #86735). + from cryptography.hazmat.primitives import hashes + from cryptography.hazmat.primitives.kdf.hkdf import HKDF + return HKDF( algorithm=hashes.SHA256(), length=32, @@ -407,6 +415,10 @@ def _write_encrypted_disk_cache( nonce = os.urandom(12) serialized_key = _cache_key_str(cache_key) key = _derive_encrypted_cache_key(access_token, salt) + # Lazy: cryptography must not load unless an encrypted cache is + # actually in use (see the module-level NOTE — #86735). + from cryptography.hazmat.primitives.ciphers.aead import AESGCM + plaintext = json.dumps( {"secrets": entry.secrets, "fetched_at": entry.fetched_at}, separators=(",", ":"), @@ -471,6 +483,10 @@ def _read_encrypted_disk_cache( nonce = _b64d(str(payload.get("nonce", ""))) ciphertext = _b64d(str(payload.get("ciphertext", ""))) key = _derive_encrypted_cache_key(access_token, salt) + # Lazy: cryptography must not load unless an encrypted cache is + # actually in use (see the module-level NOTE — #86735). + from cryptography.hazmat.primitives.ciphers.aead import AESGCM + raw = AESGCM(key).decrypt( nonce, ciphertext, serialized_key.encode("utf-8") ) diff --git a/tests/secret_sources/test_secret_source_registry.py b/tests/secret_sources/test_secret_source_registry.py index 3fca184844bf9..b2d785acb792e 100644 --- a/tests/secret_sources/test_secret_source_registry.py +++ b/tests/secret_sources/test_secret_source_registry.py @@ -9,7 +9,9 @@ from __future__ import annotations +import subprocess import sys +import textwrap import time from pathlib import Path @@ -361,3 +363,39 @@ def source(self, monkeypatch): monkeypatch.setattr(op, "find_op", lambda *_a, **_kw: None) monkeypatch.delenv("OP_SERVICE_ACCOUNT_TOKEN", raising=False) return op.OnePasswordSource() + + +class TestNoEagerCryptographyImport: + """#86735 regression: registering the bundled sources must not load + ``cryptography``. + + Every CLI/gateway process imports the secret-source registry, and + Bitwarden's module used to import ``cryptography.hazmat...`` at module + level. On Windows a mapped native extension (``cryptography._rust``) + cannot be replaced once loaded, so ``hermes update``'s self-lock + preflight deferred every update before it could even pull commits. + The imports are now lazy; this test proves it in a clean interpreter + (a subprocess, so the test runner's own imports cannot mask a + regression). + """ + + def test_builtin_registration_does_not_import_cryptography(self): + code = textwrap.dedent( + """ + import sys + from agent.secret_sources import registry + registry._ensure_builtin_sources() + print("cryptography" in sys.modules) + print("cryptography.hazmat.bindings._rust" in sys.modules) + """ + ) + result = subprocess.run( + [sys.executable, "-c", code], + capture_output=True, + text=True, + cwd=str(ROOT), + ) + assert result.returncode == 0, result.stderr + lines = result.stdout.strip().splitlines() + assert lines[0] == "False" + assert lines[1] == "False"