Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .jules/sentinel.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@
**Vulnerability:** Telemetry HTTP endpoints (`/status`, `/`) were completely unprotected, allowing any local user to view training state, usage, and costs.
**Learning:** Initial implementation prioritized ease of use and local-only binding (`127.0.0.1`) but neglected defense-in-depth requirements for multi-user or shared environments.
**Prevention:** Always implement at least Basic Authentication for any endpoint exposing state or metadata, even if restricted to loopback. Use random session-specific credentials if no configuration is provided.

## 2026-05-14 - Environment Leakage in Unit Test Gate
**Vulnerability:** The unit test gate script inherited all environment variables when executing generated code samples, potentially exposing sensitive API keys and tokens to untrusted code.
**Learning:** Python's `subprocess.run` inherits `os.environ` by default. In a pipeline that generates and tests code, this allows the generated code to exfiltrate secrets from the environment.
**Prevention:** Explicitly filter the `env` argument in `subprocess` calls to an allowlist of essential, non-sensitive variables when executing untrusted code.
5 changes: 0 additions & 5 deletions heidi_engine/telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,11 +732,6 @@ def get_state(run_id: Optional[str] = None) -> Dict[str, Any]:
"usage": get_default_usage(),
}

# BOLT OPTIMIZATION: Check thread-safe state cache
cached = _state_cache.get(target_run_id, state_file)
if cached:
return cached

try:
with open(state_file) as f:
state = json.load(f)
Expand Down
3 changes: 2 additions & 1 deletion scripts/02_validate_clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,8 @@ def save_jsonl(samples: List[Dict[str, Any]], path: str) -> None:
"""
Save samples to JSONL file.
"""
os.makedirs(os.path.dirname(path), exist_ok=True)
if os.path.dirname(path):
os.makedirs(os.path.dirname(path), exist_ok=True)

with open(path, "w") as f:
for sample in samples:
Expand Down
29 changes: 22 additions & 7 deletions scripts/03_unit_test_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import subprocess
import sys
import tempfile
import textwrap
from typing import Any, Dict, List, Tuple

# =============================================================================
Expand Down Expand Up @@ -67,17 +68,20 @@
# TUNABLE: Add more dangerous patterns to block
DANGEROUS_PATTERNS = [
# Dangerous imports (including comma-separated and aliased)
r"\bimport\s+[^#\n]*\b(os|subprocess|sys|shutil|socket|requests|urllib|pathlib|pickle|pty|code|bdb|pdb|multiprocessing|threading|tempfile|ftplib|smtplib|telnetlib|http|xmlrpc)\b",
r"\bfrom\s+(os|subprocess|sys|shutil|socket|requests|urllib|pathlib|pickle|pty|code|bdb|pdb|multiprocessing|threading|tempfile|ftplib|smtplib|telnetlib|http|xmlrpc)\b",
# Dangerous built-ins
r"\bimport\s+[^#\n]*\b(os|subprocess|sys|shutil|socket|requests|urllib|pathlib|pickle|pty|code|bdb|pdb|multiprocessing|threading|tempfile|ftplib|smtplib|telnetlib|http|xmlrpc|importlib|pkgutil|pydoc)\b",
r"\bfrom\s+(os|subprocess|sys|shutil|socket|requests|urllib|pathlib|pickle|pty|code|bdb|pdb|multiprocessing|threading|tempfile|ftplib|smtplib|telnetlib|http|xmlrpc|importlib|pkgutil|pydoc)\b",
Comment on lines +71 to +72
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security-high high

The list of dangerous imports should include builtins. While you have blocked access to the __builtins__ attribute, an attacker could still use import builtins to access sensitive functions like eval or exec if they can bypass the other regex patterns (e.g., via builtins.__dict__).

Suggested change
r"\bimport\s+[^#\n]*\b(os|subprocess|sys|shutil|socket|requests|urllib|pathlib|pickle|pty|code|bdb|pdb|multiprocessing|threading|tempfile|ftplib|smtplib|telnetlib|http|xmlrpc|importlib|pkgutil|pydoc)\b",
r"\bfrom\s+(os|subprocess|sys|shutil|socket|requests|urllib|pathlib|pickle|pty|code|bdb|pdb|multiprocessing|threading|tempfile|ftplib|smtplib|telnetlib|http|xmlrpc|importlib|pkgutil|pydoc)\b",
r"\bimport\s+[^#\n]*\b(os|subprocess|sys|shutil|socket|requests|urllib|pathlib|pickle|pty|code|bdb|pdb|multiprocessing|threading|tempfile|ftplib|smtplib|telnetlib|http|xmlrpc|importlib|pkgutil|pydoc|builtins)\b",
r"\bfrom\s+(os|subprocess|sys|shutil|socket|requests|urllib|pathlib|pickle|pty|code|bdb|pdb|multiprocessing|threading|tempfile|ftplib|smtplib|telnetlib|http|xmlrpc|importlib|pkgutil|pydoc|builtins)\b",

# Dangerous built-ins and internal attributes
r"\beval\s*\(",
r"\bexec\s*\(",
r"\b__import__\s*\(",
r"\bgetattr\s*\(",
r"\bsetattr\s*\(",
r"\bbreakpoint\s*\(",
r"\b__builtins__\b",
r"\b__globals__\b",
r"\b__subclasses__\b",
# Dangerous module functions
r"\bos\.(system|popen|spawn|remove|unlink|rmdir|mkdir|chmod|chown|kill|exec|fork|pipe)\b",
r"\bos\.(system|popen|spawn|remove|unlink|rmdir|mkdir|chmod|chown|kill|exec|fork|pipe|environ)\b",
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security-high high

The regex for dangerous os functions is incomplete and easily bypassed.

  1. It misses environb (the bytes version of environ), getenv, and putenv.
  2. The use of \b around exec and spawn fails to catch common variants like os.execv, os.execve, os.spawnv, etc., as \b does not match the transition between a word character and another word character (like v).
Suggested change
r"\bos\.(system|popen|spawn|remove|unlink|rmdir|mkdir|chmod|chown|kill|exec|fork|pipe|environ)\b",
r"\bos\.(system|popen|spawn\w*|remove|unlink|rmdir|mkdir|chmod|chown|kill|exec\w*|fork|pipe|environb?|getenv|putenv)\b",

r"\bsubprocess\.(run|call|check_call|check_output|Popen)\b",
r"\bshutil\.(rmtree|move|copy|copy2|copyfile|copymode|copystat|chown)\b",
r"\bpickle\.(load|loads)\b",
Expand Down Expand Up @@ -214,6 +218,10 @@ def test_python_code(code: str, temp_dir: str, execution_timeout: int = 5) -> Tu
test_file = os.path.join(temp_dir, "test_code.py")

# Wrap code to capture output safely
# SECURITY: Indent the user code correctly before inserting into template
# to avoid SyntaxError and ensure proper execution.
indented_code = textwrap.indent(code, " ")

wrapped_code = f"""
import sys
import io
Expand All @@ -229,7 +237,7 @@ def test_python_code(code: str, temp_dir: str, execution_timeout: int = 5) -> Tu
sys.stderr = stderr_capture

# Execute the user's code
{code}
{indented_code}

sys.stdout = original_stdout
sys.stderr = original_stderr
Expand Down Expand Up @@ -257,13 +265,19 @@ def test_python_code(code: str, temp_dir: str, execution_timeout: int = 5) -> Tu

# Try to execute with timeout
try:
# SECURITY: Filter environment to prevent secret leakage from parent process.
# Only allow essential variables for execution.
allowed_env_keys = {"PATH", "PYTHONPATH", "LANG", "PYTHONIOENCODING"}
allowed_env = {k: os.environ[k] for k in allowed_env_keys if k in os.environ}
allowed_env["PYTHONPATH"] = temp_dir
Comment on lines +270 to +272
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Including PYTHONPATH in allowed_env_keys is redundant because it is immediately overwritten by temp_dir on line 272. For clarity and to avoid confusion about whether the parent's PYTHONPATH is being preserved, it should be removed from the allowlist if the intention is to strictly isolate the execution to the temporary directory.

Suggested change
allowed_env_keys = {"PATH", "PYTHONPATH", "LANG", "PYTHONIOENCODING"}
allowed_env = {k: os.environ[k] for k in allowed_env_keys if k in os.environ}
allowed_env["PYTHONPATH"] = temp_dir
allowed_env_keys = {"PATH", "LANG", "PYTHONIOENCODING"}
allowed_env = {k: os.environ[k] for k in allowed_env_keys if k in os.environ}
allowed_env["PYTHONPATH"] = temp_dir


result = subprocess.run(
[sys.executable, test_file],
capture_output=True,
text=True,
timeout=execution_timeout,
cwd=temp_dir,
env={**os.environ, "PYTHONPATH": temp_dir},
env=allowed_env,
)

stdout = result.stdout
Expand Down Expand Up @@ -367,7 +381,8 @@ def load_jsonl(path: str) -> List[Dict[str, Any]]:

def save_jsonl(samples: List[Dict[str, Any]], path: str) -> None:
"""Save samples to JSONL file."""
os.makedirs(os.path.dirname(path), exist_ok=True)
if os.path.dirname(path):
os.makedirs(os.path.dirname(path), exist_ok=True)

with open(path, "w") as f:
for sample in samples:
Expand Down