ARES - authorized red-team engagement automation with dashboard, campaign scope, module orchestration, OPSEC controls, encrypted vault, and reporting.
Python active-directorycloud-securitydefensive-validationfastapigood-first-issuemitre-attackopsecreactred-teamreportingsecurity
fix(cli): owner-only .env, stop printing admin password (run-1 H11) (#72)
kraii_e29a0e2fcfa3
diff --git a/ares/cli/typer_main.py b/ares/cli/typer_main.pyindex 0bcf828..8bb99f8 100644--- a/ares/cli/typer_main.py+++ b/ares/cli/typer_main.py@@ -2360,6 +2360,8 @@ def _run_python_setup(updated = _upsert_env_value(updated, "ARES_DEBUG", "true")env_path.write_text(updated, encoding="utf-8")+ from ares.core.security import restrict_file_to_owner+ restrict_file_to_owner(str(env_path))if force:console.print("[green]OK[/green] .env regenerated with fresh secure keys (--force).")@@ -2367,7 +2369,14 @@ def _run_python_setup(console.print("[green]OK[/green] Replaced CHANGE_ME placeholders in .env with secure generated keys.")if admin_password is not None:- console.print(f"Admin password: {admin_password}")+ # Never print the generated password to stdout (it lands in+ # shell history / terminal scrollback / CI logs). Point the+ # operator at the owner-only .env instead. (AUDIT run-1 H11)+ console.print(+ f"Admin password written to {env_path} "+ "(ARES_DEFAULT_ADMIN_PASSWORD). Open it to retrieve the "+ "password, then rotate it after first login."+ )else:console.print("[green]OK[/green] .env already exists; leaving it unchanged.")admin_password = None@@ -2387,8 +2396,14 @@ def _run_python_setup(if origin_in_template and "example.invalid" in origin_in_template:template = _upsert_env_value(template, "ARES_BROWSER_ORIGIN", "")env_path.write_text(template, encoding="utf-8")- console.print(f"[green]OK[/green] .env created at {env_path}")- console.print(f"Admin password: {admin_password}")+ from ares.core.security import restrict_file_to_owner+ restrict_file_to_owner(str(env_path))+ console.print(f"[green]OK[/green] .env created at {env_path} (owner-only permissions)")+ # Do not print the generated password to stdout. (AUDIT run-1 H11)+ console.print(+ f"Admin password written to {env_path} (ARES_DEFAULT_ADMIN_PASSWORD). "+ "Open it to retrieve the password, then rotate it after first login."+ )except OSError as exc:console.print(f"[red]ERROR[/red] setup failed: {exc.__class__.__name__}: {exc}")raise SystemExit(1) from Nonediff --git a/ares/core/security.py b/ares/core/security.pyindex 4408c5e..9687271 100644--- a/ares/core/security.py+++ b/ares/core/security.py@@ -672,6 +672,23 @@ def secure_mkstemp(return path, fd+def restrict_file_to_owner(path: str) -> None:+ """Best-effort: restrict an existing file to owner read/write only.++ Applies chmod 0o600 on POSIX and an owner-only ACL on Windows. Intended for+ files that hold secret material at rest (e.g. a generated .env containing+ ARES_SECRET_KEY / ARES_ENCRYPTION_KEY / ARES_DEFAULT_ADMIN_PASSWORD) so they+ are not left world-readable on a shared host. (AUDIT run-1 H11)+ """+ import os++ try:+ os.chmod(path, 0o600) # Windows reflects the owner bits through chmod too+ except OSError:+ pass+ _restrict_windows_acl(path)++def secure_mkdtemp(prefix: str = "ares-", campaign_id: str = "") -> str:"""Create a temp directory with restrictive permissions (0o700) for credential material.diff --git a/tests/unit/test_cli_doctor.py b/tests/unit/test_cli_doctor.pyindex 9c4226f..0d6f9e8 100644--- a/tests/unit/test_cli_doctor.py+++ b/tests/unit/test_cli_doctor.py@@ -2,6 +2,7 @@ from __future__ import annotationsimport importlib.metadataimport os+import reimport sysimport tomllibimport types@@ -643,12 +644,17 @@ def test_python_setup_replaces_change_me_placeholders_in_existing_env(tmp_path,output = capsys.readouterr().outassert "Replaced CHANGE_ME placeholders in .env with secure generated keys" in output- assert "Admin password:" in output+ # AUDIT run-1 H11: the generated admin password must NOT be printed to stdout.+ assert "Admin password:" not in outputenv_content = (tmp_path / ".env").read_text(encoding="utf-8")assert "CHANGE_ME" not in env_contentassert "YOUR_STRONG_ADMIN_PASSWORD_HERE" not in env_contentassert "ARES_API_PORT=8080" in env_content+ admin_pw = re.search(+ r"^ARES_DEFAULT_ADMIN_PASSWORD=(.+)$", env_content, re.MULTILINE+ ).group(1).strip()+ assert admin_pw and admin_pw not in outputdef test_python_setup_replaces_new_256bit_placeholder_in_existing_env(tmp_path, capsys):@@ -671,12 +677,17 @@ def test_python_setup_replaces_new_256bit_placeholder_in_existing_env(tmp_path,output = capsys.readouterr().outassert "Replaced CHANGE_ME placeholders in .env with secure generated keys" in output- assert "Admin password:" in output+ # AUDIT run-1 H11: the generated admin password must NOT be printed to stdout.+ assert "Admin password:" not in outputenv_content = (tmp_path / ".env").read_text(encoding="utf-8")assert "CHANGE_ME" not in env_contentassert "YOUR_STRONG_ADMIN_PASSWORD_HERE" not in env_contentassert "ARES_API_PORT=8080" in env_content+ admin_pw = re.search(+ r"^ARES_DEFAULT_ADMIN_PASSWORD=(.+)$", env_content, re.MULTILINE+ ).group(1).strip()+ assert admin_pw and admin_pw not in outputdef test_python_setup_leaves_configured_env_unchanged(tmp_path, capsys):@@ -724,11 +735,16 @@ def test_python_setup_force_regenerates_existing_env(tmp_path, capsys):output = capsys.readouterr().outassert ".env regenerated with fresh secure keys (--force)" in output- assert "Admin password:" in output+ # AUDIT run-1 H11: the generated admin password must NOT be printed to stdout.+ assert "Admin password:" not in outputenv_content = (tmp_path / ".env").read_text(encoding="utf-8")assert "old_secret" not in env_contentassert "CUSTOM_VAR=keep_me" in env_content+ admin_pw = re.search(+ r"^ARES_DEFAULT_ADMIN_PASSWORD=(.+)$", env_content, re.MULTILINE+ ).group(1).strip()+ assert admin_pw and admin_pw not in outputdef test_setup_entrypoint_parses_force(monkeypatch):diff --git a/tests/unit/test_setup_env_hardening_h11.py b/tests/unit/test_setup_env_hardening_h11.pynew file mode 100644index 0000000..433b73f--- /dev/null+++ b/tests/unit/test_setup_env_hardening_h11.py@@ -0,0 +1,49 @@+"""AUDIT run-1 H11: `ares setup` must not print the generated admin password and+must write the secret-bearing .env with owner-only permissions."""++import os+import re+import stat+from pathlib import Path++import pytest++from ares.cli import typer_main+++def _extract(env_text: str, key: str) -> str:+ m = re.search(rf"^{re.escape(key)}=(.*)$", env_text, flags=re.MULTILINE)+ return m.group(1).strip() if m else ""+++def test_setup_does_not_print_password_and_restricts_env(tmp_path, capsys):+ typer_main._run_python_setup(root=tmp_path)++ env_path = tmp_path / ".env"+ assert env_path.exists()+ env_text = env_path.read_text(encoding="utf-8")++ admin_pw = _extract(env_text, "ARES_DEFAULT_ADMIN_PASSWORD")+ assert admin_pw, "a password should have been generated into .env"++ # The generated password must never reach stdout.+ out = capsys.readouterr().out+ # Sanity: console output IS captured (otherwise the assertions below are+ # vacuously true). The setup prints a banner and an .env confirmation.+ assert "ARES Setup" in out+ assert ".env" in out+ assert admin_pw not in out+ assert "Admin password:" not in out # old literal-print banner is gone++ # Secret keys must also be absent from stdout.+ assert _extract(env_text, "ARES_SECRET_KEY") not in out+ assert _extract(env_text, "ARES_ENCRYPTION_KEY") not in out++[email protected](os.name == "nt", reason="POSIX file mode bits")+def test_setup_env_is_owner_only_on_posix(tmp_path):+ typer_main._run_python_setup(root=tmp_path)+ env_path = tmp_path / ".env"+ mode = stat.S_IMODE(Path(env_path).stat().st_mode)+ # No group/other permission bits.+ assert mode & 0o077 == 0, oct(mode)