hermes-agent/tests/tools/test_snapshot_multiline_session_env_injection.py
ethernet 59624b7ace tests: replace every bare host skipif with platforms()
The OS lanes are marker-driven: list_os_marked_tests.py picks the files
a lane imports from their platforms() specs and the lane selects with
-m platforms. A test gated with skipif(sys.platform != "win32") is
therefore never imported on the Windows lane and skipped everywhere else
— it runs on no host. skipif(sys.platform == "win32") tests were merely
invisible to the lane bookkeeping, but the rule the tree now follows is
one host marker, never a bare skipif.

Mechanical mapping, semantics preserved: skip-on-Windows → "posix",
skip-off-Windows → "windows", skip-off-Linux → "linux", skip-on-macOS →
"not macos". The former skip reasons stay as trailing comments. A
non-host condition (os.geteuid() == 0) stays a separate skipif beside
the marker, spelled getattr(os, "geteuid", ...) so the decorator still
imports on Windows.

Where the conversion would stack two platforms() marks on one test (the
conftest rejects that at collection) the narrower mark wins:
- test_update_wedged_gateway: the class is already platforms("linux");
  its per-test "needs UNIX sockets" marks were redundant and are gone.
- test_process_registry.TestSystemdCgroupIsolation: the class-level
  skip-on-Windows moves onto the 11 methods that had no host mark; the
  11 platforms("linux") methods keep theirs.
- test_file_ops_single_roundtrip: the two fifo tests drop their
  platforms("linux") in favour of the module's "posix" (mkfifo exists on
  macOS; both tests already skip when it does not).
- test_linux_desktop_entry / test_gateway_job_teardown_live: duplicate
  or wider marks removed.
2026-09-21 19:18:15 -04:00

99 lines
2.8 KiB
Python

"""Newline in bridged session env must not become shell code via the snapshot.
Regression for issue #71296: bash 3.2 ``export -p`` prints a value containing
a newline as a multi-line ``declare -x NAME="…`` block. The old line-based
``grep -vE`` filter removed only the opener; continuation lines (e.g.
``curl … | bash #`` smuggled into a Matrix room/display name) persisted into
the shared terminal snapshot and executed on the next ``source``, with stdout
discarded by the wrapper.
"""
from __future__ import annotations
import os
import shlex
import subprocess
import sys
from pathlib import Path
import pytest
from tools.environments.base_session_env import _export_dump_excluding_session_vars
def _bash() -> str:
return "/bin/bash" if os.path.exists("/bin/bash") else "bash"
def _run_dump_and_source(
*,
tmp_path: Path,
env_name: str,
env_value: str,
marker: Path,
) -> subprocess.CompletedProcess:
snap = tmp_path / "snap"
dump = _export_dump_excluding_session_vars(shlex.quote(str(snap)))
q_snap = shlex.quote(str(snap))
q_marker = shlex.quote(str(marker))
script = f"""
set -e
export {env_name}
{dump}
if grep -qE 'pwned|touch |{env_name}' {q_snap}; then
echo "LEAKED_INTO_SNAPSHOT" >&2
exit 2
fi
bash -c 'source {q_snap} >/dev/null 2>&1 || true'
if [ -e {q_marker} ]; then
echo "PAYLOAD_EXECUTED" >&2
exit 3
fi
if ! grep -qE '^declare -x PATH=' {q_snap}; then
echo "PATH_MISSING" >&2
exit 4
fi
"""
env = os.environ.copy()
env[env_name] = env_value
return subprocess.run(
[_bash(), "-c", script],
cwd=str(tmp_path),
capture_output=True,
text=True,
env=env,
)
@pytest.mark.platforms("posix") # POSIX bash snapshot path
def test_multiline_session_chat_name_not_executed_via_snapshot(tmp_path: Path):
"""Continuation lines of HERMES_SESSION_CHAT_NAME must not run on source."""
marker = tmp_path / "pwned"
chat_name = f"demo\ntouch {marker} #"
proc = _run_dump_and_source(
tmp_path=tmp_path,
env_name="HERMES_SESSION_CHAT_NAME",
env_value=chat_name,
marker=marker,
)
assert proc.returncode == 0, (
f"rc={proc.returncode}\nstdout={proc.stdout!r}\nstderr={proc.stderr!r}"
)
assert not marker.exists()
@pytest.mark.platforms("posix") # POSIX bash snapshot path
def test_multiline_session_user_name_not_executed_via_snapshot(tmp_path: Path):
"""Same hole via HERMES_SESSION_USER_NAME (display-name path)."""
marker = tmp_path / "pwned_user"
user_name = f"alice\ntouch {marker} #"
proc = _run_dump_and_source(
tmp_path=tmp_path,
env_name="HERMES_SESSION_USER_NAME",
env_value=user_name,
marker=marker,
)
assert proc.returncode == 0, (
f"rc={proc.returncode}\nstdout={proc.stdout!r}\nstderr={proc.stderr!r}"
)
assert not marker.exists()