Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions agent/secret_sources/bitwarden.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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=(",", ":"),
Expand Down Expand Up @@ -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")
)
Expand Down
38 changes: 38 additions & 0 deletions tests/secret_sources/test_secret_source_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

from __future__ import annotations

import subprocess
import sys
import textwrap
import time
from pathlib import Path

Expand Down Expand Up @@ -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"
Loading