test(utils): Add test and error handling for wiped InMemorySecret

- Updated `InMemorySecret.get_bytes()` in `src/utils/memory_protection.py` to raise a `RuntimeError` if the secret has been wiped, replacing the previous `AttributeError`.
- Added `test_access_after_wipe_raises_error` to `src/tests/test_memory_protection.py` to verify this behavior.

Co-authored-by: PR0M3TH3AN <[email protected]>
This commit is contained in:
google-labs-jules[bot]andPR0M3TH3AN committed 2026-02-16 17:21:45 +00:00
1 parent 335de7abe3
commit d729466513
2 files changed
+15

No files matched your search

+13
View File
@@ -27,3 +27,16 @@ def test_inmemory_secret_wipe_clears_attributes():
assert secret._nonce is None
assert secret._cipher is None
assert secret._encrypted is None
def test_access_after_wipe_raises_error():
secret = InMemorySecret(b"secret data")
secret.wipe()
with pytest.raises(RuntimeError) as excinfo:
secret.get_bytes()
assert str(excinfo.value) == "Secret has been wiped"
with pytest.raises(RuntimeError) as excinfo:
secret.get_str()
assert str(excinfo.value) == "Secret has been wiped"
+2
View File
@@ -17,6 +17,8 @@ class InMemorySecret:
def get_bytes(self) -> bytes:
"""Decrypt and return the plaintext bytes."""
if self._cipher is None:
raise RuntimeError("Secret has been wiped")
return self._cipher.decrypt(self._nonce, self._encrypted, None)
def wipe(self) -> None: