ARES - authorized red-team engagement automation with dashboard, campaign scope, module orchestration, OPSEC controls, encrypted vault, and reporting.
Python active-directorycloud-securitydefensive-validationfastapigood-first-issuemitre-attackopsecreactred-teamreportingsecurity
fix(observability): replace silent except pass with structured error logging in record_credential and record_finding (AUD-007)
Mafifrizi957397ef51ea
diff --git a/ares/core/context.py b/ares/core/context.pyindex 25588bc..426c28b 100644--- a/ares/core/context.py+++ b/ares/core/context.py@@ -34,6 +34,9 @@ from dataclasses import dataclass, fieldfrom typing import Any, Generic, TYPE_CHECKING, TypeVarfrom ares.core.errors import InvalidContext+from ares.core.logger import get_logger++logger = get_logger("ares.core.context")P = TypeVar("P")T = TypeVar("T")@@ -188,11 +191,22 @@ class ExecutionContext(Generic[P]):if self.runtime_state and hasattr(self.runtime_state, "record_finding"):try:self.runtime_state.record_finding(finding)- except Exception:- pass+ except Exception as exc:+ logger.error(+ "runtime_state_record_finding_failed",+ finding_id=finding.id,+ title=finding.title,+ module_id=self.module_id,+ error=str(exc),+ exc_info=True,+ )+ return Falseself.findings.append(finding)return finding+ # Alias for API consistency and backwards compatibility+ record_finding = emit_finding+def store_artifact(self, artifact: Any) -> Any:"""Store an artifact in the execution context's artifact store if available."""if self.artifact_store and hasattr(self.artifact_store, "add"):@@ -224,8 +238,7 @@ class ExecutionContext(Generic[P]):))if not has_io:- from ares.core.logger import get_logger- get_logger("ares.core.context").warning(+ logger.warning("vault_write_blocked_zero_io",username=username,cred_type=cred_type,@@ -250,8 +263,16 @@ class ExecutionContext(Generic[P]):ctx=self,**kwargs,)- except Exception:- pass+ except Exception as exc:+ logger.error(+ "vault_record_credential_failed",+ username=username,+ cred_type=str(cred_type),+ module_id=self.module_id,+ error=str(exc),+ exc_info=True,+ )+ return Falsereturn Nonedef record_loot(diff --git a/tests/unit/test_context_exception_logging_aud007.py b/tests/unit/test_context_exception_logging_aud007.pynew file mode 100644index 0000000..1e7b019--- /dev/null+++ b/tests/unit/test_context_exception_logging_aud007.py@@ -0,0 +1,104 @@+"""+Tests for AUD-007: Verification of structured error logging and explicit return False+when vault.add() or runtime_state.record_finding() raises exceptions.+"""+from unittest.mock import MagicMock, patch+import pytest++from ares.core.campaign import Finding, Severity+from ares.core.context import ExecutionContext+++def test_record_credential_vault_exception_logs_error_and_returns_false():+ """When vault.add() raises an unexpected exception, it logs structured error and returns False."""+ mock_vault = MagicMock()+ mock_vault.add.side_effect = RuntimeError("Disk I/O error or vault constraint violation")++ ctx = ExecutionContext(+ target="10.0.0.1",+ module_id="test.module",+ vault=mock_vault,+ )+ ctx.mark_network_io_occurred(True)++ with patch("ares.core.context.logger") as mock_logger:+ res = ctx.record_credential(+ username="target_admin",+ secret="supersecret",+ cred_type="password",+ io_verified=True,+ )++ assert res is False+ mock_logger.error.assert_called_once()+ call_args, call_kwargs = mock_logger.error.call_args+ assert call_args[0] == "vault_record_credential_failed"+ assert call_kwargs.get("username") == "target_admin"+ assert call_kwargs.get("cred_type") == "password"+ assert call_kwargs.get("module_id") == "test.module"+ assert "Disk I/O error" in call_kwargs.get("error", "")+ assert call_kwargs.get("exc_info") is True+++def test_record_finding_runtime_state_exception_logs_error_and_returns_false():+ """When runtime_state.record_finding() raises an exception, it logs structured error and returns False."""+ mock_runtime_state = MagicMock()+ mock_runtime_state.record_finding.side_effect = RuntimeError("Runtime state sync failed")++ ctx = ExecutionContext(+ target="10.0.0.1",+ module_id="test.module",+ runtime_state=mock_runtime_state,+ )++ with patch("ares.core.context.logger") as mock_logger:+ res = ctx.record_finding(+ title="Critical SMB Vulnerability",+ severity=Severity.HIGH,+ description="MS17-010 discovered",+ )++ assert res is False+ mock_logger.error.assert_called_once()+ call_args, call_kwargs = mock_logger.error.call_args+ assert call_args[0] == "runtime_state_record_finding_failed"+ assert call_kwargs.get("title") == "Critical SMB Vulnerability"+ assert call_kwargs.get("module_id") == "test.module"+ assert "Runtime state sync failed" in call_kwargs.get("error", "")+ assert call_kwargs.get("exc_info") is True+++def test_normal_operation_unaffected():+ """Normal operations for record_finding and record_credential succeed and return expected values."""+ mock_vault = MagicMock()+ mock_vault.add.return_value = {"status": "saved", "id": "cred-1"}++ mock_runtime_state = MagicMock()++ ctx = ExecutionContext(+ target="10.0.0.1",+ module_id="test.module",+ vault=mock_vault,+ runtime_state=mock_runtime_state,+ )+ ctx.mark_network_io_occurred(True)++ # 1. record_finding succeeds+ finding = ctx.record_finding(+ title="Valid Finding",+ severity=Severity.MEDIUM,+ )+ assert isinstance(finding, Finding)+ assert finding.title == "Valid Finding"+ assert len(ctx.findings) == 1+ mock_runtime_state.record_finding.assert_called_once_with(finding)++ # 2. record_credential succeeds+ cred_res = ctx.record_credential(+ username="normal_user",+ secret="validpass",+ cred_type="password",+ io_verified=True,+ )+ assert cred_res == {"status": "saved", "id": "cred-1"}+ mock_vault.add.assert_called_once()