Skip to content

Commit 514b20e

Browse files
Refuse a push that sends a library encrypted to a project or library pushed without encryption
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 35aa9ba commit 514b20e

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

‎lean/components/cloud/push_manager.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,22 @@ def push_projects(self, projects_to_push: List[Path], associated_libraries_to_pu
100100
f"locally with 'lean encrypt <library> --key' to keep it encrypted, or push it on its own "
101101
f"with --decrypt --key to decrypt it")
102102

103+
# the cloud refuses to link an encrypted library to a project or library pushed without encryption, but only after
104+
# it wrote their files, so nothing is pushed when one of them uses a library that this push sends encrypted
105+
def is_pushed_encrypted(path: Path) -> bool:
106+
if encryption_action is not None and encryption_key is not None and libraries_cloud_keys.get(path, '') is not None:
107+
return encryption_action == ActionType.ENCRYPT
108+
return self._project_config_manager.get_project_config(path).get("encrypted", False)
109+
encrypted = {path for path in all_projects_to_push if associated_libraries_to_push and is_pushed_encrypted(path)}
110+
unencrypted_users = "', '".join(
111+
path.relative_to(Path.cwd()).as_posix() for path in all_projects_to_push
112+
if path not in encrypted and any(LeanLibraryReference(**library).path.expanduser().resolve() in encrypted
113+
for library in self._project_config_manager.get_project_config(path).get("libraries", [])))
114+
if unencrypted_users != "":
115+
raise RuntimeError(f"The cloud doesn't let projects and libraries pushed without encryption use the encrypted libraries "
116+
f"pushed with them: '{unencrypted_users}'. Encrypt each of them locally with 'lean encrypt "
117+
f"<project> --key' to push it encrypted too, or decrypt the encrypted libraries they use")
118+
103119
for index, path in enumerate(all_projects_to_push, start=1):
104120

105121
# Check if it's an associated library to push, we don't encrypt those unless they are encrypted in the cloud with the command's key.

‎tests/components/util/test_push_manager.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,28 @@ def test_push_project_pushes_library_encrypted_in_the_cloud_with_the_key_the_pro
852852
push_manager._logger.warn.assert_not_called()
853853

854854

855+
@pytest.mark.parametrize("library_encrypted_locally", [False, True])
856+
def test_push_project_creates_nothing_when_an_unencrypted_library_uses_a_library_pushed_encrypted(
857+
library_encrypted_locally: bool) -> None:
858+
api_client, project_path, library_path, encryption_key_path = _create_project_with_library_in_encrypted_cloud_project()
859+
if library_encrypted_locally:
860+
_encrypt_project_locally(library_path, encryption_key_path)
861+
new_library_path = Path.cwd() / "Library/New Library"
862+
container.project_manager.create_new_project(new_library_path, QCLanguage.Python)
863+
container.library_manager.remove_lean_library_reference_from_project(project_path, library_path)
864+
container.library_manager.add_lean_library_reference_to_project(project_path, new_library_path)
865+
container.library_manager.add_lean_library_reference_to_project(new_library_path, library_path)
866+
867+
push_manager = _create_push_manager(api_client, container.project_manager)
868+
with pytest.raises(RuntimeError, match="pushed without encryption") as error:
869+
push_manager.push_project(project_path, ActionType.ENCRYPT, encryption_key_path)
870+
871+
assert "'Library/New Library'" in str(error.value)
872+
api_client.projects.create.assert_not_called()
873+
api_client.projects.update.assert_not_called()
874+
assert container.project_config_manager.get_project_config(new_library_path).get("cloud-id") is None
875+
876+
855877
def test_push_projects_decrypts_encrypted_cloud_library_pushed_on_its_own() -> None:
856878
api_client, _, library_path, _ = _create_project_with_library_in_encrypted_cloud_project()
857879

0 commit comments

Comments
 (0)