Skip to content

Official Codebase of 3DCodeVerse

Python

eval/llm: model-written code never sees a credential — every tool subprocess gets tool_env, which drops keys and tokens

gaoypengbe303eff034d
diff --git a/eval/llm/config.py b/eval/llm/config.py
index 446dac61..fc72dd95 100644
--- a/eval/llm/config.py
+++ b/eval/llm/config.py
@@ -86,9 +86,16 @@ def cadquery_python() -> str:
return os.environ.get("C3D_CQ_PYTHON") or sys.executable
+#: a variable whose NAME carries one of these words is a credential: model-written code runs in the
+#: tool subprocesses, so it never sees one (the harness's own rule is proc/cli_common clean_env; this
+#: package does not import the harness)
+_SECRET_WORDS = ("KEY", "TOKEN", "SECRET", "PASSWORD", "PASSWD", "CREDENTIAL", "AUTH", "COOKIE", "SESSION")
+
+
def tool_env(**extra: str) -> dict[str, str]:
- """Environment for Blender / OpenSCAD subprocesses: private HOME, extra libs, no audio."""
- env = dict(os.environ)
+ """Environment for every subprocess that runs or compiles model-written code (Blender, CadQuery,
+ OpenSCAD, glslang): no credentials, private HOME, extra libs."""
+ env = {k: v for k, v in os.environ.items() if not any(w in k.upper() for w in _SECRET_WORDS)}
if XLIBS.exists():
env["LD_LIBRARY_PATH"] = f"{XLIBS}:{env.get('LD_LIBRARY_PATH', '')}"
env.update(extra)
diff --git a/eval/llm/executors/cadquery.py b/eval/llm/executors/cadquery.py
index facdeb35..c543616d 100644
--- a/eval/llm/executors/cadquery.py
+++ b/eval/llm/executors/cadquery.py
@@ -71,7 +71,7 @@ def run(code: str, workdir: str, timeout: int = 90) -> dict:
try:
p = subprocess.run([config.cadquery_python(), str(runner), str(script), str(stl)], cwd=str(wd),
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, timeout=timeout, text=True, errors="replace",
- env={**os.environ, "OMP_NUM_THREADS": "1"})
+ env=config.tool_env(OMP_NUM_THREADS="1"))
m = re.search(r"CQ_REPORT (\{.*\})", p.stdout)
rep = json.loads(m.group(1)) if m else {"status": "CRASH", "error": p.stdout[-800:]}
except subprocess.TimeoutExpired:
diff --git a/eval/llm/executors/glsl.py b/eval/llm/executors/glsl.py
index 347f1f09..412cc75a 100644
--- a/eval/llm/executors/glsl.py
+++ b/eval/llm/executors/glsl.py
@@ -97,7 +97,7 @@ def compile_check(src: str, workdir: Path, timeout: int) -> dict:
fp.write_text(PRELUDE + src + MAIN)
try:
p = subprocess.run([str(exe), "-S", "frag", str(fp)], stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
- timeout=timeout, text=True, errors="replace")
+ timeout=timeout, text=True, errors="replace", env=config.tool_env())
return {"status": "OK" if p.returncode == 0 else "FAIL", "error": None if p.returncode == 0 else p.stdout[-600:]}
except subprocess.TimeoutExpired:
return {"status": "TIMEOUT", "error": "glslang timeout"}
diff --git a/eval/llm/tests/test_smoke.py b/eval/llm/tests/test_smoke.py
index ed7a7e92..07699ec4 100644
--- a/eval/llm/tests/test_smoke.py
+++ b/eval/llm/tests/test_smoke.py
@@ -101,3 +101,19 @@ def test_a_torn_results_line_does_not_crash_the_render_stage():
with tempfile.TemporaryDirectory() as d:
(Path(d) / "exec_results.jsonl").write_text('{"id": "a", "status": "FAIL"}\n{"id": "b", "sta')
assert render_views.render_dir(Path(d)) == {}
+
+
+def test_model_written_code_never_sees_a_credential(monkeypatch, tmp_path):
+ """The tool subprocesses run model-written code: API keys and tokens must not reach them."""
+ from llm import config
+ from llm.executors import cadquery
+
+ for k in ("GEMINI_API_KEY", "GEMINI_API_KEYS", "OPENAI_API_KEY", "HF_TOKEN", "AWS_SECRET_ACCESS_KEY"):
+ monkeypatch.setenv(k, "secret-value")
+ monkeypatch.setenv("PATH", "/usr/bin:/bin")
+ env = config.tool_env(HOME="/tmp")
+ assert "secret-value" not in env.values() and env["PATH"] == "/usr/bin:/bin" and env["HOME"] == "/tmp"
+ # the script reports what it can see through its crash message, which the report keeps
+ rep = cadquery.run("import os\nraise SystemExit('LEAK' if any('secret' in v for v in os.environ.values()) else 'CLEAN')\n",
+ str(tmp_path / "w"))
+ assert rep["error"] == "SystemExit: CLEAN"