eva-cli 3.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- eva/__init__.py +3 -0
- eva/budget.py +31 -0
- eva/cache/__init__.py +17 -0
- eva/cache/cache.py +40 -0
- eva/cache.py +19 -0
- eva/chat_session.py +5 -0
- eva/cli/__init__.py +3 -0
- eva/cli/app.py +851 -0
- eva/cli.py +5 -0
- eva/config/__init__.py +33 -0
- eva/config/config.py +129 -0
- eva/config.py +35 -0
- eva/context/__init__.py +19 -0
- eva/context/finder.py +5 -0
- eva/context/gitignore.py +5 -0
- eva/context/io.py +5 -0
- eva/context/tokenizer.py +5 -0
- eva/context/tree.py +5 -0
- eva/diagnostics.py +9 -0
- eva/git_ops.py +15 -0
- eva/indexing/__init__.py +18 -0
- eva/indexing/finder.py +42 -0
- eva/indexing/io.py +35 -0
- eva/indexing/repo_index.py +240 -0
- eva/indexing/tokenizer.py +37 -0
- eva/indexing/tree.py +60 -0
- eva/plugins/__init__.py +3 -0
- eva/prompts/__init__.py +36 -0
- eva/providers/__init__.py +150 -0
- eva/providers/gemini_provider.py +72 -0
- eva/providers/groq_provider.py +37 -0
- eva/providers/llamacpp_provider.py +69 -0
- eva/providers/ollama_provider.py +70 -0
- eva/providers/openai_compat.py +57 -0
- eva/providers/opencode_zen_provider.py +53 -0
- eva/providers/openrouter_provider.py +55 -0
- eva/replay/__init__.py +11 -0
- eva/replay/player.py +91 -0
- eva/replay/recorder.py +67 -0
- eva/router/__init__.py +25 -0
- eva/router/gemini_provider.py +5 -0
- eva/router/groq_provider.py +5 -0
- eva/router/openai_compat.py +5 -0
- eva/router/opencode_zen_provider.py +5 -0
- eva/router/openrouter_provider.py +5 -0
- eva/security/__init__.py +32 -0
- eva/security/audit.py +126 -0
- eva/security/redaction.py +80 -0
- eva/security/sandbox.py +55 -0
- eva/security/work_safety.py +85 -0
- eva/shell/__init__.py +3 -0
- eva/shell/runner.py +6 -0
- eva/telemetry/__init__.py +10 -0
- eva/telemetry/diagnostics.py +43 -0
- eva/telemetry/metrics.py +68 -0
- eva/ui/formatter.py +46 -0
- eva/ui/streaming.py +21 -0
- eva/work_safety.py +21 -0
- eva/workflows/__init__.py +39 -0
- eva/workflows/budget.py +139 -0
- eva/workflows/builtins/dependency_audit.yaml +12 -0
- eva/workflows/builtins/repo_health.yaml +16 -0
- eva/workflows/builtins/security_scan.yaml +12 -0
- eva/workflows/chat_session.py +54 -0
- eva/workflows/engine.py +267 -0
- eva/workspace/__init__.py +39 -0
- eva/workspace/git_ops.py +202 -0
- eva/workspace/gitignore.py +47 -0
- eva/workspace/session.py +184 -0
- eva_cli-3.0.0.dist-info/METADATA +344 -0
- eva_cli-3.0.0.dist-info/RECORD +74 -0
- eva_cli-3.0.0.dist-info/WHEEL +4 -0
- eva_cli-3.0.0.dist-info/entry_points.txt +2 -0
- eva_cli-3.0.0.dist-info/licenses/LICENSE +21 -0
eva/replay/player.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import logging
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
from rich.console import Console
|
|
7
|
+
from rich.table import Table
|
|
8
|
+
|
|
9
|
+
from eva.replay.recorder import get_replays_dir, get_session_dir
|
|
10
|
+
|
|
11
|
+
logger = logging.getLogger(__name__)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def list_replay_sessions(replays_dir: Path | None = None) -> list[dict[str, Any]]:
|
|
15
|
+
"""List all available recorded replay sessions."""
|
|
16
|
+
root = replays_dir or get_replays_dir()
|
|
17
|
+
if not root.exists():
|
|
18
|
+
return []
|
|
19
|
+
|
|
20
|
+
sessions = []
|
|
21
|
+
for d in sorted(root.iterdir()):
|
|
22
|
+
if d.is_dir():
|
|
23
|
+
meta_file = d / "meta.json"
|
|
24
|
+
events_file = d / "events.jsonl"
|
|
25
|
+
created_at = "Unknown"
|
|
26
|
+
count = 0
|
|
27
|
+
if meta_file.exists():
|
|
28
|
+
try:
|
|
29
|
+
meta = json.loads(meta_file.read_text(encoding="utf-8"))
|
|
30
|
+
created_at = meta.get("created_at", created_at)
|
|
31
|
+
except (OSError, ValueError) as exc:
|
|
32
|
+
logger.debug("Failed to read replay metadata %s: %s", meta_file, exc)
|
|
33
|
+
if events_file.exists():
|
|
34
|
+
try:
|
|
35
|
+
count = len([line for line in events_file.read_text(encoding="utf-8").splitlines() if line.strip()])
|
|
36
|
+
except (OSError, ValueError) as exc:
|
|
37
|
+
logger.debug("Failed to read replay events %s: %s", events_file, exc)
|
|
38
|
+
sessions.append(
|
|
39
|
+
{
|
|
40
|
+
"session_id": d.name,
|
|
41
|
+
"created_at": created_at,
|
|
42
|
+
"event_count": count,
|
|
43
|
+
}
|
|
44
|
+
)
|
|
45
|
+
return sessions
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def load_replay_session(session_id: str, replays_dir: Path | None = None) -> list[dict[str, Any]]:
|
|
49
|
+
"""Load all recorded events for a given session."""
|
|
50
|
+
target_dir = (replays_dir / session_id) if replays_dir else get_session_dir(session_id)
|
|
51
|
+
events_file = target_dir / "events.jsonl"
|
|
52
|
+
|
|
53
|
+
if not events_file.exists():
|
|
54
|
+
raise FileNotFoundError(f"Replay session '{session_id}' not found.")
|
|
55
|
+
|
|
56
|
+
events = []
|
|
57
|
+
with open(events_file, "r", encoding="utf-8") as f:
|
|
58
|
+
for line in f:
|
|
59
|
+
if line.strip():
|
|
60
|
+
events.append(json.loads(line))
|
|
61
|
+
return events
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def display_replay_session(session_id: str, console: Console | None = None, replays_dir: Path | None = None):
|
|
65
|
+
"""Display recorded terminal replay session events."""
|
|
66
|
+
c = console or Console()
|
|
67
|
+
events = load_replay_session(session_id, replays_dir=replays_dir)
|
|
68
|
+
|
|
69
|
+
if not events:
|
|
70
|
+
c.print(f"[warning]No recorded events found in session '{session_id}'.[/warning]")
|
|
71
|
+
return
|
|
72
|
+
|
|
73
|
+
c.print(f"[bold cyan]▶ Terminal Replay: Session '{session_id}'[/bold cyan] ({len(events)} events)\n")
|
|
74
|
+
|
|
75
|
+
for idx, ev in enumerate(events, start=1):
|
|
76
|
+
ts = ev.get("timestamp", "")[:19].replace("T", " ")
|
|
77
|
+
code = ev.get("exit_code", 0)
|
|
78
|
+
dur = ev.get("duration_s", 0.0)
|
|
79
|
+
status_str = "[green]✓ 0[/green]" if code == 0 else f"[red]✗ {code}[/red]"
|
|
80
|
+
|
|
81
|
+
table = Table(show_header=False, box=None, padding=(0, 1))
|
|
82
|
+
table.add_row(f"[bold yellow]#{idx}[/bold yellow]", f"[dim]{ts}[/dim]", f"dir: {ev.get('cwd', '')}")
|
|
83
|
+
table.add_row(
|
|
84
|
+
"[bold cyan]Command:[/bold cyan]", f"[bold]{ev.get('command', '')}[/bold]", f"({dur}s, {status_str})"
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
c.print(table)
|
|
88
|
+
output = ev.get("output", "")
|
|
89
|
+
if output.strip():
|
|
90
|
+
c.print(f"[dim]Output:[/dim]\n{output.strip()}\n")
|
|
91
|
+
c.print("-" * 50)
|
eva/replay/recorder.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import json
|
|
2
|
+
from datetime import datetime, timezone
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
from eva.config import get_config_dir
|
|
6
|
+
from eva.security.redaction import redact_secrets
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def get_replays_dir() -> Path:
|
|
10
|
+
d = get_config_dir() / "replays"
|
|
11
|
+
d.mkdir(parents=True, exist_ok=True)
|
|
12
|
+
return d
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def get_session_dir(session_id: str) -> Path:
|
|
16
|
+
safe_name = "".join(c if c.isalnum() or c in "._-" else "_" for c in session_id)
|
|
17
|
+
d = get_replays_dir() / safe_name
|
|
18
|
+
d.mkdir(parents=True, exist_ok=True)
|
|
19
|
+
return d
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def record_replay_event(
|
|
23
|
+
session_id: str,
|
|
24
|
+
command: str,
|
|
25
|
+
output: str,
|
|
26
|
+
exit_code: int,
|
|
27
|
+
duration_s: float,
|
|
28
|
+
cwd: str | Path,
|
|
29
|
+
replays_dir: Path | None = None,
|
|
30
|
+
):
|
|
31
|
+
"""Record a terminal execution event to session replay log.
|
|
32
|
+
|
|
33
|
+
Secrets in command, output, and cwd are redacted at write time.
|
|
34
|
+
"""
|
|
35
|
+
target_dir = (replays_dir / session_id) if replays_dir else get_session_dir(session_id)
|
|
36
|
+
target_dir.mkdir(parents=True, exist_ok=True)
|
|
37
|
+
|
|
38
|
+
meta_file = target_dir / "meta.json"
|
|
39
|
+
if not meta_file.exists():
|
|
40
|
+
meta_file.write_text(
|
|
41
|
+
json.dumps(
|
|
42
|
+
{
|
|
43
|
+
"session_id": session_id,
|
|
44
|
+
"created_at": datetime.now(timezone.utc).isoformat(),
|
|
45
|
+
},
|
|
46
|
+
indent=2,
|
|
47
|
+
),
|
|
48
|
+
encoding="utf-8",
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
events_file = target_dir / "events.jsonl"
|
|
52
|
+
|
|
53
|
+
clean_command = redact_secrets(command)
|
|
54
|
+
clean_output = redact_secrets(output)
|
|
55
|
+
clean_cwd = redact_secrets(str(cwd))
|
|
56
|
+
|
|
57
|
+
record = {
|
|
58
|
+
"timestamp": datetime.now(timezone.utc).isoformat(),
|
|
59
|
+
"command": clean_command,
|
|
60
|
+
"output": clean_output,
|
|
61
|
+
"exit_code": exit_code,
|
|
62
|
+
"duration_s": round(duration_s, 4),
|
|
63
|
+
"cwd": clean_cwd,
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
with open(events_file, "a", encoding="utf-8") as f:
|
|
67
|
+
f.write(json.dumps(record) + "\n")
|
eva/router/__init__.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"""Compatibility re-exports for router."""
|
|
2
|
+
|
|
3
|
+
from eva.providers import (
|
|
4
|
+
AuthError,
|
|
5
|
+
Provider,
|
|
6
|
+
QuotaExhaustedError,
|
|
7
|
+
RateLimitError,
|
|
8
|
+
ServerError,
|
|
9
|
+
call_provider,
|
|
10
|
+
dispatch,
|
|
11
|
+
get_provider,
|
|
12
|
+
register_provider,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
__all__ = [
|
|
16
|
+
"AuthError",
|
|
17
|
+
"Provider",
|
|
18
|
+
"QuotaExhaustedError",
|
|
19
|
+
"RateLimitError",
|
|
20
|
+
"ServerError",
|
|
21
|
+
"call_provider",
|
|
22
|
+
"dispatch",
|
|
23
|
+
"get_provider",
|
|
24
|
+
"register_provider",
|
|
25
|
+
]
|
eva/security/__init__.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
from eva.security.audit import (
|
|
2
|
+
compute_entry_hash,
|
|
3
|
+
verify_audit_chain,
|
|
4
|
+
)
|
|
5
|
+
from eva.security.redaction import redact_secrets
|
|
6
|
+
from eva.security.sandbox import get_sandboxed_env, run_sandboxed
|
|
7
|
+
from eva.security.work_safety import (
|
|
8
|
+
BLAST_RADIUS_PATTERNS,
|
|
9
|
+
CommandExtractionError,
|
|
10
|
+
ParsedCommand,
|
|
11
|
+
UnsafeCommandError,
|
|
12
|
+
append_command_audit,
|
|
13
|
+
extract_single_command,
|
|
14
|
+
get_command_audit_log,
|
|
15
|
+
parse_safe_command,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
__all__ = [
|
|
19
|
+
"BLAST_RADIUS_PATTERNS",
|
|
20
|
+
"CommandExtractionError",
|
|
21
|
+
"ParsedCommand",
|
|
22
|
+
"UnsafeCommandError",
|
|
23
|
+
"append_command_audit",
|
|
24
|
+
"compute_entry_hash",
|
|
25
|
+
"extract_single_command",
|
|
26
|
+
"get_command_audit_log",
|
|
27
|
+
"get_sandboxed_env",
|
|
28
|
+
"parse_safe_command",
|
|
29
|
+
"redact_secrets",
|
|
30
|
+
"run_sandboxed",
|
|
31
|
+
"verify_audit_chain",
|
|
32
|
+
]
|
eva/security/audit.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import hashlib
|
|
2
|
+
import json
|
|
3
|
+
import logging
|
|
4
|
+
from datetime import datetime, timezone
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from eva.config import get_config_dir
|
|
9
|
+
from eva.security.redaction import redact_secrets
|
|
10
|
+
|
|
11
|
+
logger = logging.getLogger(__name__)
|
|
12
|
+
|
|
13
|
+
GENESIS_HASH = "0" * 64
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def get_command_audit_log() -> Path:
|
|
17
|
+
return get_config_dir() / "command_audit.jsonl"
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _sanitize_dict(data: Any) -> Any:
|
|
21
|
+
"""Recursively redact secrets in dict keys/values and list items."""
|
|
22
|
+
if isinstance(data, str):
|
|
23
|
+
return redact_secrets(data)
|
|
24
|
+
if isinstance(data, dict):
|
|
25
|
+
return {k: _sanitize_dict(v) for k, v in data.items()}
|
|
26
|
+
if isinstance(data, list):
|
|
27
|
+
return [_sanitize_dict(item) for item in data]
|
|
28
|
+
return data
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def compute_entry_hash(prev_hash: str, payload: dict[str, Any]) -> str:
|
|
32
|
+
"""Compute sha256 hash over prev_hash and canonical JSON of entry payload."""
|
|
33
|
+
canonical_json = json.dumps(payload, sort_keys=True, separators=(",", ":"))
|
|
34
|
+
content = f"{prev_hash}:{canonical_json}"
|
|
35
|
+
return hashlib.sha256(content.encode("utf-8")).hexdigest()
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def append_command_audit(entry: dict[str, Any], log_file: Path | None = None):
|
|
39
|
+
"""Append a hash-chained audit entry to the audit log."""
|
|
40
|
+
target_file = log_file or get_command_audit_log()
|
|
41
|
+
target_file.parent.mkdir(parents=True, exist_ok=True)
|
|
42
|
+
|
|
43
|
+
sanitized_entry = _sanitize_dict(entry)
|
|
44
|
+
|
|
45
|
+
last_hash = GENESIS_HASH
|
|
46
|
+
seq = 1
|
|
47
|
+
|
|
48
|
+
if target_file.exists() and target_file.stat().st_size > 0:
|
|
49
|
+
with open(target_file, "r", encoding="utf-8") as f:
|
|
50
|
+
lines = [line.strip() for line in f if line.strip()]
|
|
51
|
+
if lines:
|
|
52
|
+
try:
|
|
53
|
+
last_record = json.loads(lines[-1])
|
|
54
|
+
last_hash = last_record.get("hash", GENESIS_HASH)
|
|
55
|
+
seq = last_record.get("seq", len(lines)) + 1
|
|
56
|
+
except (OSError, ValueError) as exc:
|
|
57
|
+
logger.debug("Failed to read last audit record: %s", exc)
|
|
58
|
+
|
|
59
|
+
payload = {
|
|
60
|
+
"seq": seq,
|
|
61
|
+
"timestamp": datetime.now(timezone.utc).isoformat(),
|
|
62
|
+
"prev_hash": last_hash,
|
|
63
|
+
**sanitized_entry,
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
entry_hash = compute_entry_hash(last_hash, payload)
|
|
67
|
+
record = {"hash": entry_hash, **payload}
|
|
68
|
+
|
|
69
|
+
with open(target_file, "a", encoding="utf-8") as f:
|
|
70
|
+
f.write(json.dumps(record, sort_keys=True) + "\n")
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def verify_audit_chain(log_file: Path | None = None) -> tuple[bool, int, str]:
|
|
74
|
+
"""Verify the integrity of the hash-chained audit log.
|
|
75
|
+
|
|
76
|
+
Returns:
|
|
77
|
+
(is_valid, last_valid_seq, message)
|
|
78
|
+
"""
|
|
79
|
+
target_file = log_file or get_command_audit_log()
|
|
80
|
+
|
|
81
|
+
if not target_file.exists() or target_file.stat().st_size == 0:
|
|
82
|
+
return True, 0, "Audit log is empty or missing"
|
|
83
|
+
|
|
84
|
+
expected_prev_hash = GENESIS_HASH
|
|
85
|
+
count = 0
|
|
86
|
+
|
|
87
|
+
with open(target_file, "r", encoding="utf-8") as f:
|
|
88
|
+
for idx, line in enumerate(f, start=1):
|
|
89
|
+
line_str = line.strip()
|
|
90
|
+
if not line_str:
|
|
91
|
+
continue
|
|
92
|
+
|
|
93
|
+
try:
|
|
94
|
+
record = json.loads(line_str)
|
|
95
|
+
except json.JSONDecodeError as exc:
|
|
96
|
+
return False, count, f"Corrupted JSON on line {idx}: {exc}"
|
|
97
|
+
|
|
98
|
+
stored_hash = record.get("hash")
|
|
99
|
+
seq = record.get("seq")
|
|
100
|
+
prev_hash = record.get("prev_hash")
|
|
101
|
+
|
|
102
|
+
if not stored_hash or not prev_hash:
|
|
103
|
+
return False, count, f"Missing hash fields on line {idx} (seq {seq})"
|
|
104
|
+
|
|
105
|
+
if prev_hash != expected_prev_hash:
|
|
106
|
+
return (
|
|
107
|
+
False,
|
|
108
|
+
count,
|
|
109
|
+
f"Previous hash mismatch at sequence {seq}: expected {expected_prev_hash}, got {prev_hash}",
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
# Reconstruct payload to compute hash
|
|
113
|
+
payload = {k: v for k, v in record.items() if k != "hash"}
|
|
114
|
+
computed = compute_entry_hash(prev_hash, payload)
|
|
115
|
+
|
|
116
|
+
if computed != stored_hash:
|
|
117
|
+
return (
|
|
118
|
+
False,
|
|
119
|
+
count,
|
|
120
|
+
f"Hash signature mismatch at sequence {seq}: computed {computed}, stored {stored_hash}",
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
expected_prev_hash = stored_hash
|
|
124
|
+
count += 1
|
|
125
|
+
|
|
126
|
+
return True, count, "Audit log chain verified successfully"
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import math
|
|
2
|
+
import re
|
|
3
|
+
from collections import Counter
|
|
4
|
+
|
|
5
|
+
# Known secret key patterns
|
|
6
|
+
SECRET_PATTERNS = [
|
|
7
|
+
# OpenAI, Anthropic, OpenRouter, etc. sk-* keys
|
|
8
|
+
(r"\b(sk-[A-Za-z0-9_-]{20,})\b", "[REDACTED_SECRET_KEY]"),
|
|
9
|
+
# GitHub Personal Access Tokens (ghp, gho, ghu, ghs, ghr, etc.)
|
|
10
|
+
(r"\b(gh[pousr]_[A-Za-z0-9_]{36,})\b", "[REDACTED_GITHUB_TOKEN]"),
|
|
11
|
+
# GitLab Personal Access Tokens (glpat-)
|
|
12
|
+
(r"\b(glpat-[A-Za-z0-9_-]{20,})\b", "[REDACTED_GITLAB_TOKEN]"),
|
|
13
|
+
# AWS Access Key ID
|
|
14
|
+
(r"\b(AKIA[0-9A-Z]{16})\b", "[REDACTED_AWS_KEY_ID]"),
|
|
15
|
+
# Slack Tokens
|
|
16
|
+
(r"\b(xox[bpsra]-[0-9a-zA-Z-]{10,})\b", "[REDACTED_SLACK_TOKEN]"),
|
|
17
|
+
# Eva specific API key env vars
|
|
18
|
+
(r"\b(EVA_[A-Z0-9_]+_API_KEY=)[^\s]+\b", r"\1[REDACTED_API_KEY]"),
|
|
19
|
+
# Bearer and Basic authentication headers
|
|
20
|
+
(r"\b(Bearer\s+)[A-Za-z0-9._~+/-]+=*\b", r"\1[REDACTED_BEARER_TOKEN]"),
|
|
21
|
+
(r"\b(Basic\s+)[A-Za-z0-9._~+/-]+=*\b", r"\1[REDACTED_BASIC_AUTH]"),
|
|
22
|
+
# Generic key assignment patterns (api_key, secret, password, auth_token, etc.)
|
|
23
|
+
(
|
|
24
|
+
r"\b(api[_-]?key|secret|password|passwd|auth[_-]?token|access[_-]?token)\s*[:=]\s*['\"]?[A-Za-z0-9._~+/-]{10,}['\"]?",
|
|
25
|
+
r"\1: [REDACTED]",
|
|
26
|
+
),
|
|
27
|
+
# PEM Private key blocks
|
|
28
|
+
(
|
|
29
|
+
r"-----BEGIN\s+(?:RSA\s+|EC\s+|DSA\s+|OPENSSH\s+)?PRIVATE\s+KEY-----[\s\S]*?-----END\s+(?:RSA\s+|EC\s+|DSA\s+|OPENSSH\s+)?PRIVATE\s+KEY-----",
|
|
30
|
+
"[REDACTED_PRIVATE_KEY]",
|
|
31
|
+
),
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
COMPILED_SECRET_PATTERNS = [(re.compile(p, re.IGNORECASE), repl) for p, repl in SECRET_PATTERNS]
|
|
35
|
+
|
|
36
|
+
# Token pattern for entropy checking (non-whitespace tokens >= 16 chars)
|
|
37
|
+
HIGH_ENTROPY_TOKEN_RE = re.compile(r"\b[A-Za-z0-9+/=_\-.~!@#$%^&*()]{16,}\b")
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def shannon_entropy(s: str) -> float:
|
|
41
|
+
"""Calculate the Shannon entropy of a string (in bits per character)."""
|
|
42
|
+
if not s:
|
|
43
|
+
return 0.0
|
|
44
|
+
length = len(s)
|
|
45
|
+
counts = Counter(s)
|
|
46
|
+
entropy = 0.0
|
|
47
|
+
for count in counts.values():
|
|
48
|
+
p = count / length
|
|
49
|
+
entropy -= p * math.log2(p)
|
|
50
|
+
return entropy
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def redact_secrets(text: str, entropy_threshold: float = 3.5) -> str:
|
|
54
|
+
"""Redact secrets, API keys, tokens, private keys, and high-entropy strings from text.
|
|
55
|
+
|
|
56
|
+
Applied BEFORE writing data to disk (logs, replay, cache, memory) and BEFORE provider calls.
|
|
57
|
+
"""
|
|
58
|
+
if not text:
|
|
59
|
+
return text
|
|
60
|
+
|
|
61
|
+
redacted = text
|
|
62
|
+
|
|
63
|
+
# Step 1: Regex pattern redaction
|
|
64
|
+
for pattern, repl in COMPILED_SECRET_PATTERNS:
|
|
65
|
+
redacted = pattern.sub(repl, redacted)
|
|
66
|
+
|
|
67
|
+
# Step 2: High-entropy string redaction
|
|
68
|
+
def replace_high_entropy(match: re.Match) -> str:
|
|
69
|
+
token = match.group(0)
|
|
70
|
+
# Skip tokens that look like already-redacted markers
|
|
71
|
+
if token.startswith(("[REDACTED", "REDACTED")):
|
|
72
|
+
return token
|
|
73
|
+
# Skip standard UUIDs (hex hyphenated) or common hex strings with low character set variance
|
|
74
|
+
if shannon_entropy(token) > entropy_threshold:
|
|
75
|
+
return "[REDACTED_HIGH_ENTROPY]"
|
|
76
|
+
return token
|
|
77
|
+
|
|
78
|
+
redacted = HIGH_ENTROPY_TOKEN_RE.sub(replace_high_entropy, redacted)
|
|
79
|
+
|
|
80
|
+
return redacted
|
eva/security/sandbox.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import subprocess
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
SAFE_ENV_VARS = {"PATH", "HOME", "USER", "LANG", "LC_ALL", "TERM", "TMPDIR", "PWD"}
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def get_sandboxed_env() -> dict[str, str]:
|
|
9
|
+
"""Construct a stripped, restricted environment dictionary."""
|
|
10
|
+
env = {}
|
|
11
|
+
for var in SAFE_ENV_VARS:
|
|
12
|
+
val = os.getenv(var)
|
|
13
|
+
if val is not None:
|
|
14
|
+
env[var] = val
|
|
15
|
+
env["EVA_SANDBOX"] = "1"
|
|
16
|
+
return env
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def run_sandboxed(
|
|
20
|
+
command: str,
|
|
21
|
+
*,
|
|
22
|
+
cwd: Path | str | None = None,
|
|
23
|
+
timeout: float = 30.0,
|
|
24
|
+
) -> subprocess.CompletedProcess[str]:
|
|
25
|
+
"""Execute a command in a restricted sandboxed subprocess environment.
|
|
26
|
+
|
|
27
|
+
Restricts environment variables, closes stdin, enforces timeout, and isolates execution.
|
|
28
|
+
"""
|
|
29
|
+
env = get_sandboxed_env()
|
|
30
|
+
work_dir = Path(cwd) if cwd else Path.cwd()
|
|
31
|
+
|
|
32
|
+
try:
|
|
33
|
+
result = subprocess.run(
|
|
34
|
+
command,
|
|
35
|
+
shell=True,
|
|
36
|
+
cwd=str(work_dir),
|
|
37
|
+
env=env,
|
|
38
|
+
stdin=subprocess.DEVNULL,
|
|
39
|
+
capture_output=True,
|
|
40
|
+
text=True,
|
|
41
|
+
timeout=timeout,
|
|
42
|
+
check=False,
|
|
43
|
+
)
|
|
44
|
+
return result
|
|
45
|
+
except subprocess.TimeoutExpired as exc:
|
|
46
|
+
stdout = exc.stdout.decode("utf-8", errors="replace") if isinstance(exc.stdout, bytes) else (exc.stdout or "")
|
|
47
|
+
stderr = (
|
|
48
|
+
exc.stderr.decode("utf-8", errors="replace") if isinstance(exc.stderr, bytes) else (exc.stderr or "")
|
|
49
|
+
) + f"\n[Eva Sandbox] Execution timed out after {timeout} seconds."
|
|
50
|
+
return subprocess.CompletedProcess(
|
|
51
|
+
args=command,
|
|
52
|
+
returncode=124,
|
|
53
|
+
stdout=stdout,
|
|
54
|
+
stderr=stderr,
|
|
55
|
+
)
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import re
|
|
2
|
+
import shlex
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
|
|
5
|
+
from eva.security.audit import append_command_audit, get_command_audit_log
|
|
6
|
+
|
|
7
|
+
__all__ = [
|
|
8
|
+
"CommandExtractionError",
|
|
9
|
+
"ParsedCommand",
|
|
10
|
+
"UnsafeCommandError",
|
|
11
|
+
"append_command_audit",
|
|
12
|
+
"extract_single_command",
|
|
13
|
+
"get_command_audit_log",
|
|
14
|
+
"parse_safe_command",
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@dataclass(frozen=True)
|
|
19
|
+
class ParsedCommand:
|
|
20
|
+
command: str
|
|
21
|
+
argv: list[str]
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class CommandExtractionError(ValueError):
|
|
25
|
+
pass
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class UnsafeCommandError(ValueError):
|
|
29
|
+
pass
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
# Irreversible / blast-radius patterns (hard-blocked by default)
|
|
33
|
+
BLAST_RADIUS_PATTERNS = [
|
|
34
|
+
# Recursive delete on system paths
|
|
35
|
+
r"\brm\s+-[a-zA-Z]*r[a-zA-Z]*\s+(/|/\*|/bin|/sbin|/usr|/etc|/var|/home|/root|/boot|/sys|/proc|/dev)(\s+|$)",
|
|
36
|
+
r"\brm\s+-[a-zA-Z]*\s+-[a-zA-Z]*\s+(/|/\*|/bin|/sbin|/usr|/etc|/var|/home|/root|/boot|/sys|/proc|/dev)(\s+|$)",
|
|
37
|
+
r"\brm\s+-[a-zA-Z]*r[a-zA-Z]*f?\s+--no-preserve-root\b",
|
|
38
|
+
# Disk formatting
|
|
39
|
+
r"\bmkfs(\.[a-zA-Z0-9]+)?\b",
|
|
40
|
+
r"\bmke2fs\b",
|
|
41
|
+
# Direct writes to block devices
|
|
42
|
+
r"\bdd\s+.*of=/dev/(sd[a-z][0-9]?|nvme[0-9]n[0-9](p[0-9])?|hd[a-z][0-9]?|vd[a-z][0-9]?|loop[0-9]+)\b",
|
|
43
|
+
# Piping a remote script straight into a shell (curl|bash, wget|sh, etc.)
|
|
44
|
+
r"\b(curl|wget|fetch)\b.*\|\s*(sudo\s+)?(bash|sh|zsh|dash|ksh)\b",
|
|
45
|
+
# Fork bombs
|
|
46
|
+
r":\(\)\s*\{\s*:\|:&\s*\};:",
|
|
47
|
+
r":\(\)\{:\|\:&?\};:",
|
|
48
|
+
# Recursive chown/chmod on system paths
|
|
49
|
+
r"\b(chmod|chown)\s+(-[a-zA-Z]*R[a-zA-Z]*)\s+.*\s+(/|/\*|/bin|/sbin|/usr|/etc|/var|/home|/root|/boot|/sys|/proc|/dev)(\s+|$)",
|
|
50
|
+
]
|
|
51
|
+
|
|
52
|
+
COMPILED_BLAST_PATTERNS = [re.compile(pattern, re.IGNORECASE) for pattern in BLAST_RADIUS_PATTERNS]
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def extract_single_command(model_output: str) -> str:
|
|
56
|
+
text = model_output.strip()
|
|
57
|
+
if not text:
|
|
58
|
+
raise CommandExtractionError("model returned an empty command")
|
|
59
|
+
|
|
60
|
+
if text.startswith("```"):
|
|
61
|
+
match = re.fullmatch(r"```(?:[A-Za-z0-9_-]+)?\s*\n(?P<body>.*?)\n```", text, flags=re.DOTALL)
|
|
62
|
+
if not match:
|
|
63
|
+
raise CommandExtractionError("model output contained a malformed or trailing fenced block")
|
|
64
|
+
text = match.group("body").strip()
|
|
65
|
+
elif "```" in text:
|
|
66
|
+
raise CommandExtractionError("model output contained an inline or malformed code block")
|
|
67
|
+
|
|
68
|
+
return text
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def parse_safe_command(model_output: str) -> ParsedCommand:
|
|
72
|
+
command = extract_single_command(model_output)
|
|
73
|
+
|
|
74
|
+
# Check for irreversible/blast-radius patterns
|
|
75
|
+
for pattern in COMPILED_BLAST_PATTERNS:
|
|
76
|
+
if pattern.search(command):
|
|
77
|
+
raise UnsafeCommandError(f"Command matches blast-radius pattern: {command}")
|
|
78
|
+
|
|
79
|
+
# Parse argv via shlex for validation
|
|
80
|
+
try:
|
|
81
|
+
shlex.split(command)
|
|
82
|
+
except ValueError as exc:
|
|
83
|
+
raise CommandExtractionError(f"Failed to parse command arguments: {exc}") from exc
|
|
84
|
+
|
|
85
|
+
return ParsedCommand(command=command, argv=[])
|
eva/shell/__init__.py
ADDED
eva/shell/runner.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
from eva.telemetry.diagnostics import LOG_FORMAT, get_log_file, setup_logging
|
|
2
|
+
from eva.telemetry.metrics import get_telemetry_dir, record_provider_metric
|
|
3
|
+
|
|
4
|
+
__all__ = [
|
|
5
|
+
"LOG_FORMAT",
|
|
6
|
+
"get_log_file",
|
|
7
|
+
"get_telemetry_dir",
|
|
8
|
+
"record_provider_metric",
|
|
9
|
+
"setup_logging",
|
|
10
|
+
]
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import os
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
from eva.config import get_config_dir
|
|
6
|
+
|
|
7
|
+
LOG_FORMAT = "%(asctime)s %(levelname)s %(name)s: %(message)s"
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def get_log_file() -> Path:
|
|
11
|
+
return get_config_dir() / "eva.log"
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def setup_logging(verbose: bool = False):
|
|
15
|
+
if getattr(setup_logging, "_configured", False):
|
|
16
|
+
root = logging.getLogger()
|
|
17
|
+
root.setLevel(logging.DEBUG if verbose else logging.INFO)
|
|
18
|
+
for handler in root.handlers:
|
|
19
|
+
if isinstance(handler, logging.StreamHandler):
|
|
20
|
+
handler.setLevel(logging.DEBUG if verbose else logging.CRITICAL + 1)
|
|
21
|
+
return
|
|
22
|
+
|
|
23
|
+
env_verbose = os.getenv("EVA_VERBOSE", "").lower() in {"1", "true", "yes", "debug"}
|
|
24
|
+
verbose = verbose or env_verbose
|
|
25
|
+
|
|
26
|
+
log_file = get_log_file()
|
|
27
|
+
log_file.parent.mkdir(parents=True, exist_ok=True)
|
|
28
|
+
|
|
29
|
+
file_handler = logging.FileHandler(log_file)
|
|
30
|
+
file_handler.setLevel(logging.DEBUG)
|
|
31
|
+
file_handler.setFormatter(logging.Formatter(LOG_FORMAT))
|
|
32
|
+
|
|
33
|
+
stream_handler = logging.StreamHandler()
|
|
34
|
+
stream_handler.setLevel(logging.DEBUG if verbose else logging.CRITICAL + 1)
|
|
35
|
+
stream_handler.setFormatter(logging.Formatter("%(levelname)s: %(message)s"))
|
|
36
|
+
|
|
37
|
+
root = logging.getLogger()
|
|
38
|
+
root.setLevel(logging.DEBUG if verbose else logging.INFO)
|
|
39
|
+
root.handlers.clear()
|
|
40
|
+
root.addHandler(file_handler)
|
|
41
|
+
root.addHandler(stream_handler)
|
|
42
|
+
|
|
43
|
+
setup_logging._configured = True
|