Skip to content

The agent that grows with you

fix(cron): only accept a Python executable as a job interpreter

kshitijk4poor4450cc9d9068
diff --git a/cron/scheduler_script.py b/cron/scheduler_script.py
index e5faf9c64d9..d559860a810 100644
--- a/cron/scheduler_script.py
+++ b/cron/scheduler_script.py
@@ -369,7 +369,11 @@ def _resolve_script_path(script_path: str) -> tuple[Optional[Path], Optional[str
def _resolve_cron_interpreter(interpreter: str) -> tuple[Optional[str], Optional[str]]:
"""``(python_exe, error)`` for a job's ``interpreter`` field. Checked at run time, not create
time: a user venv can be rebuilt or moved while the job lives. Bare names are refused — they
- silently change meaning with PATH."""
+ silently change meaning with PATH. Only a Python image is accepted (link target included):
+ the lifecycle guard classifies ``.py`` scripts as Python and skips its shell reference walk,
+ so ``interpreter=/bin/bash`` would run an unscanned ``.py`` body as shell."""
+ from cron.lifecycle_guard import _INTERPRETER_IMAGE_RE
+
raw = interpreter.strip()
try:
resolved = Path(raw).expanduser()
@@ -377,6 +381,7 @@ def _resolve_cron_interpreter(interpreter: str) -> tuple[Optional[str], Optional
return None, (f"Interpreter must be an absolute or ~-prefixed path (got {raw!r}). "
"Bare names like 'python3' are not stable across PATH changes.")
mode = resolved.stat().st_mode
+ names = {resolved.name.lower(), resolved.resolve().name.lower()}
except FileNotFoundError:
return None, f"Interpreter not found: {raw}"
except (RuntimeError, OSError) as exc: # unknown ~user, broken symlink, unreadable parent
@@ -385,6 +390,8 @@ def _resolve_cron_interpreter(interpreter: str) -> tuple[Optional[str], Optional
return None, f"Interpreter path is not a file: {resolved}"
if sys.platform != "win32" and not mode & 0o111:
return None, f"Interpreter is not executable: {resolved}"
+ if not all(_INTERPRETER_IMAGE_RE.match(name) for name in names):
+ return None, f"Interpreter must be a Python executable (python, python3, python3.12, ...): {resolved}"
return str(resolved), None
diff --git a/tests/cron/test_cron_no_agent.py b/tests/cron/test_cron_no_agent.py
index 1d30f9b86b0..17d6664f52a 100644
--- a/tests/cron/test_cron_no_agent.py
+++ b/tests/cron/test_cron_no_agent.py
@@ -291,7 +291,8 @@ def test_run_job_no_agent_uses_configured_interpreter(hermes_env):
from cron.scheduler import run_job
# A wrapper that re-execs the real interpreter with an env marker.
- wrapper = hermes_env / "scripts" / "python-wrapper"
+ wrapper = hermes_env / "venv" / "bin" / "python3"
+ wrapper.parent.mkdir(parents=True)
wrapper.write_text(
f"#!{sys.executable}\n"
"import os, sys\n"
diff --git a/tests/cron/test_cron_script.py b/tests/cron/test_cron_script.py
index b22feba154e..7d983d3c381 100644
--- a/tests/cron/test_cron_script.py
+++ b/tests/cron/test_cron_script.py
@@ -97,6 +97,31 @@ class TestRunJobScript:
assert success is True
assert output == "hello from script"
+ @pytest.mark.platforms("posix")
+ @pytest.mark.parametrize("make_interpreter, expected", [
+ (lambda d: "python3", "absolute or ~-prefixed"),
+ (lambda d: str(d / "missing" / "python3"), "not found"),
+ (lambda d: str(d), "not a file"),
+ (lambda d: (d / "python3").write_text("") or str(d / "python3"), "not executable"),
+ (lambda d: "/bin/bash", "must be a Python executable"),
+ (lambda d: (d / "python").symlink_to("/bin/bash") or str(d / "python"),
+ "must be a Python executable"),
+ ])
+ def test_configured_interpreter_is_refused_unless_a_python_path(
+ self, cron_env, tmp_path, make_interpreter, expected
+ ):
+ """#70500: a bad job ``interpreter`` fails the run with a clear message instead of
+ raising — and never runs a ``.py`` body under a non-Python image, which would let an
+ unscanned script execute as shell."""
+ from cron.scheduler_script import _run_job_script
+
+ script = cron_env / "scripts" / "job.py"
+ script.write_text('print("ran")\n')
+
+ success, output = _run_job_script(str(script), interpreter=make_interpreter(tmp_path))
+ assert success is False
+ assert expected in output
+
def test_script_stdout_non_utf8_decoded_lossily(self, cron_env):
"""A stray non-UTF-8 byte in script stdout must not fail the run (#105582).
diff --git a/tests/cron/test_monitor_kind.py b/tests/cron/test_monitor_kind.py
index b2b2047b48a..6ca920c0104 100644
--- a/tests/cron/test_monitor_kind.py
+++ b/tests/cron/test_monitor_kind.py
@@ -289,7 +289,8 @@ def test_monitor_script_uses_configured_interpreter(hermes_env, monkeypatch):
from cron.jobs import create_job
from cron.scheduler import run_job
- wrapper = hermes_env / "scripts" / "python-wrapper"
+ wrapper = hermes_env / "venv" / "bin" / "python3"
+ wrapper.parent.mkdir(parents=True)
wrapper.write_text(
f"#!{sys.executable}\n"
"import os, sys\n"
diff --git a/website/docs/guides/cron-script-only.md b/website/docs/guides/cron-script-only.md
index b950e90ff27..8c4de0b267c 100644
--- a/website/docs/guides/cron-script-only.md
+++ b/website/docs/guides/cron-script-only.md
@@ -187,6 +187,7 @@ Rules:
- The venv is **user-managed**. Hermes does not create, freeze, restore, or install packages into it — it just invokes the path you give.
- The path must be **absolute or `~`-prefixed** (e.g. `~/venvs/reporting/bin/python3`). Bare names like `python3` are rejected, because they are not stable across `PATH` changes.
+- It must be a **Python executable** (`python`, `python3`, `python3.12`, …), including a symlink's target — `/bin/bash` or other interpreters are refused.
- Applies **only to Python scripts**. `.sh` / `.bash` always run under bash regardless.
- The job-level setting applies to both `script` and `monitor_script` when they are Python files.
- It is validated **at run time**, not at creation — a cron job is long-lived, and the venv may be rebuilt or moved between when you create the job and when it fires. A missing or non-executable interpreter produces a clear script failure that is delivered like any other error.
diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/cron-script-only.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/cron-script-only.md
index 7f541f9c208..57cc789f63a 100644
--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/cron-script-only.md
+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/cron-script-only.md
@@ -187,6 +187,7 @@ hermes cron create "0 8 * * *" \
- 该 venv 是**用户自管**的。Hermes 不会创建、冻结、恢复或向其中安装包——它只是调用你指定的路径。
- 路径必须是**绝对路径或以 `~` 开头**(例如 `~/venvs/reporting/bin/python3`)。像 `python3` 这样的裸名会被拒绝,因为它们在 `PATH` 变化时并不稳定。
+- 必须是 **Python 可执行文件**(`python`、`python3`、`python3.12` 等),符号链接的目标同样要求如此——`/bin/bash` 或其他解释器会被拒绝。
- 仅对 **Python 脚本**生效。`.sh` / `.bash` 始终用 bash 运行,不受影响。
- 这是任务级设置;当 `script` 或 `monitor_script` 是 Python 文件时都会使用它。
- 解释器在**运行时**校验,而非创建时——cron 任务是长期存在的,venv 可能在你创建任务和它真正触发之间被重建或移动。缺失或不可执行的解释器会产生一个清晰的脚本失败,像其他错误一样被投递。