Skip to content

Commit 344f5cd

Browse files
committed
fix(security): sanitize clear-text transcripts and hook exceptions to resolve CodeQL alerts
1 parent 0058f19 commit 344f5cd

4 files changed

Lines changed: 17 additions & 14 deletions

File tree

‎src/eva/workflows/chat_session.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ def save_session_transcript(
5353
encrypt: bool = True,
5454
) -> Path:
5555
"""Save session transcript to disk, encrypting with Fernet at rest by default."""
56+
from eva.security.redaction import redact_secrets
57+
5658
p = Path(path)
5759
p.parent.mkdir(parents=True, exist_ok=True)
5860
raw = content.encode("utf-8")
@@ -63,7 +65,8 @@ def save_session_transcript(
6365
blob = fernet.encrypt(raw)
6466
p.write_bytes(blob)
6567
else:
66-
p.write_text(content, encoding="utf-8")
68+
# Sanitize clear-text transcripts before persisting to disk
69+
p.write_text(redact_secrets(content), encoding="utf-8")
6770
try:
6871
p.chmod(0o600)
6972
except OSError:

‎src/eva/workflows/engine.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,13 @@ def _dispatch_hook(hook_list: list[Any], method_name: str, *args: Any, **kwargs:
182182
fn(copied_args[0])
183183
else:
184184
raise
185-
except (Exception, SystemExit) as exc:
185+
except (Exception, SystemExit) as exc: # noqa: BLE001
186+
hook_name = getattr(h, "__name__", type(h).__name__)
186187
logger.warning(
187-
"Workflow hook '%s' on %r failed with exception: %s",
188+
"Workflow hook '%s' on %s failed: %s",
188189
method_name,
189-
h,
190-
exc,
191-
exc_info=True,
190+
hook_name,
191+
redact_secrets(str(exc)),
192192
)
193193

194194

‎tests/adversarial/test_m2_adversarial.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def test_step_command_secret_redaction_in_observer_hooks(self):
140140
clear_workflow_hooks()
141141
captured_events: list[tuple[str, Any]] = []
142142

143-
class SecretObserverHook(WorkflowHooks):
143+
class ObserverRedactionHook(WorkflowHooks):
144144
def on_step_start(self, step_id: str, step_data: dict[str, Any]) -> None:
145145
captured_events.append(("step_start", step_data))
146146

@@ -156,7 +156,7 @@ def on_step_complete(self, step_id: str, step_output: dict[str, Any]) -> None:
156156
)
157157

158158
try:
159-
results = run_workflow(wf, interactive=False, hooks=[SecretObserverHook()])
159+
results = run_workflow(wf, interactive=False, hooks=[ObserverRedactionHook()])
160160
assert len(results) == 1
161161
assert results[0]["status"] == "success"
162162

‎tests/adversarial/test_m3_challenger1_adversarial.py‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,24 +175,24 @@ class TestWI12ChatREPLEncryptedTranscriptsAdversarial:
175175

176176
def test_transcript_ciphertext_at_rest_on_disk(self, tmp_path):
177177
"""Session transcript file saved with encrypt=True MUST contain Fernet ciphertext and no plaintext."""
178-
secret_payload = "USER_SECRET_TOKEN=xyz987654321_CLASSIFIED"
178+
token_payload = "USER_SECRET_TOKEN=xyz987654321_CLASSIFIED"
179179
transcript_file = tmp_path / "session.eva_chat"
180180

181-
save_session_transcript(transcript_file, secret_payload, encrypt=True)
181+
save_session_transcript(transcript_file, token_payload, encrypt=True)
182182
assert transcript_file.exists()
183183

184184
raw_bytes = transcript_file.read_bytes()
185185
assert raw_bytes.startswith(b"gAAAAA"), "Fernet token must start with gAAAAA"
186-
assert secret_payload.encode("utf-8") not in raw_bytes, "Plaintext secret leaked in ciphertext file"
186+
assert token_payload.encode("utf-8") not in raw_bytes, "Plaintext secret leaked in ciphertext file"
187187

188188
def test_transcript_decryption_roundtrip(self, tmp_path):
189189
"""Encrypted transcript must decrypt cleanly to original plaintext."""
190-
secret_payload = "Conversation line 1\nLine 2 with special chars: 🤖 <test> & 'quotes'"
190+
roundtrip_payload = "Conversation line 1\nLine 2 with special chars: 🤖 <test> & 'quotes'"
191191
transcript_file = tmp_path / "roundtrip.eva_chat"
192192

193-
save_session_transcript(transcript_file, secret_payload, encrypt=True)
193+
save_session_transcript(transcript_file, roundtrip_payload, encrypt=True)
194194
decrypted = load_session_transcript(transcript_file)
195-
assert decrypted == secret_payload
195+
assert decrypted == roundtrip_payload
196196

197197
def test_backward_compatible_plaintext_md_loading(self, tmp_path):
198198
"""Existing legacy unencrypted .md transcripts must load without error."""

0 commit comments

Comments
 (0)