OpenRAG is a comprehensive, single package Retrieval-Augmented Generation platform built on Langflow, Docling, and Opensearch.
Python
"""Unit tests for EnvManager file permission security.
These tests verify that every code path that writes a .env file enforces
0o600 (owner read/write only) permissions to prevent cleartext secret
exposure. All tests use pytest's built-in ``tmp_path`` fixture and
``unittest.mock.patch``; no running infrastructure is required.
"""
import os
import stat
import sys
from pathlib import Path
from unittest.mock import patch
import pytest
# Skip all tests on Windows — file permission model differs from Unix.
pytestmark = pytest.mark.skipif(sys.platform == "win32", reason="Unix file permissions only")
def _perms(path: Path) -> int:
"""Return the permission bits of *path* as an integer (e.g. 0o600)."""
return stat.S_IMODE(os.stat(path).st_mode)
@pytest.fixture
def env_manager(tmp_path):
"""EnvManager pointed at a temp directory (no real ~/.openrag I/O)."""
from tui.managers.env_manager import EnvManager
return EnvManager(env_file=tmp_path / ".env")
# ---------------------------------------------------------------------------
# save_env_file
# ---------------------------------------------------------------------------
class TestSaveEnvFilePermissions:
"""save_env_file must write the .env file with 0o600 permissions."""
def test_new_file_creation_has_secure_permissions(self, env_manager, tmp_path):
"""A brand-new .env must be created with 0o600."""
env_file = tmp_path / ".env"
assert not env_file.exists(), "pre-condition: no .env yet"
with patch("tui.utils.version_check.get_current_version", return_value="1.0.0"):
result = env_manager.save_env_file()
assert result is True
assert env_file.exists()
assert _perms(env_file) == 0o600, f"expected 0o600, got {oct(_perms(env_file))}"
def test_overwrite_existing_file_has_secure_permissions(self, env_manager, tmp_path):
"""Overwriting a permissive .env (0o644) must produce a new .env with 0o600."""
env_file = tmp_path / ".env"
env_file.write_text("OPENSEARCH_PASSWORD='old'\n")
env_file.chmod(0o644)
assert _perms(env_file) == 0o644, "pre-condition: file starts permissive"
with patch("tui.utils.version_check.get_current_version", return_value="1.0.0"):
result = env_manager.save_env_file()
assert result is True
assert env_file.exists()
assert _perms(env_file) == 0o600, f"expected 0o600, got {oct(_perms(env_file))}"
def test_backup_file_has_secure_permissions(self, env_manager, tmp_path):
"""The timestamped backup of a permissive .env (0o644) must be 0o600."""
env_file = tmp_path / ".env"
env_file.write_text("OPENSEARCH_PASSWORD='old'\n")
env_file.chmod(0o644)
with patch("tui.utils.version_check.get_current_version", return_value="1.0.0"):
env_manager.save_env_file()
# After save, the original .env was renamed to the backup; find it.
backups = [f for f in tmp_path.iterdir() if f.name != ".env"]
assert len(backups) == 1, (
f"expected exactly 1 backup file, found: {[f.name for f in backups]}"
)
assert _perms(backups[0]) == 0o600, f"expected 0o600, got {oct(_perms(backups[0]))}"
def test_preserves_unmanaged_env_variables(self, env_manager, tmp_path):
"""Saving config must keep existing .env keys that TUI does not manage."""
env_file = tmp_path / ".env"
env_file.write_text("OPENRAG_BACKEND_HOST='my-host'\nOPENSEARCH_PASSWORD='old-password'\n")
env_manager.config.opensearch_password = "NewSecurePass!123"
with patch("tui.utils.version_check.get_current_version", return_value="1.0.0"):
result = env_manager.save_env_file()
assert result is True
content = env_file.read_text()
assert "OPENRAG_BACKEND_HOST='my-host'" in content
assert content.count("OPENRAG_BACKEND_HOST=") == 1
assert "OPENSEARCH_PASSWORD='NewSecurePass!123'" in content
assert "OPENSEARCH_PASSWORD='old-password'" not in content
def test_preserves_unmanaged_multiline_quoted_value(self, env_manager, tmp_path):
"""Unmanaged python-dotenv–style multiline quoted values are preserved."""
env_file = tmp_path / ".env"
multiline_block = 'UNMANAGED_MULTILINE="line1\\nline2\\nline3"\n'
env_file.write_text(multiline_block + 'OPENSEARCH_PASSWORD="old-password"\n')
env_manager.config.opensearch_password = "NewSecurePass!456"
with patch("tui.utils.version_check.get_current_version", return_value="1.0.0"):
result = env_manager.save_env_file()
assert result is True
content = env_file.read_text()
# Multiline unmanaged value should be preserved exactly once.
assert multiline_block in content
assert content.count("UNMANAGED_MULTILINE=") == 1
# Managed password should be updated, not duplicated.
assert "OPENSEARCH_PASSWORD='NewSecurePass!456'" in content
assert "OPENSEARCH_PASSWORD='old-password'" not in content
def test_preserves_unmanaged_continued_line(self, env_manager, tmp_path):
"""Unmanaged values using backslash continuation are preserved."""
env_file = tmp_path / ".env"
continued_block = "UNMANAGED_LONG_VALUE=first part \\\n second part \\\n third part\n"
env_file.write_text(continued_block + 'OPENSEARCH_PASSWORD="old-password"\n')
env_manager.config.opensearch_password = "AnotherNewPass!789"
with patch("tui.utils.version_check.get_current_version", return_value="1.0.0"):
result = env_manager.save_env_file()
assert result is True
content = env_file.read_text()
# Continued-line unmanaged value should be preserved exactly once.
assert continued_block in content
assert content.count("UNMANAGED_LONG_VALUE=") == 1
# Managed password should be updated, not duplicated.
assert "OPENSEARCH_PASSWORD='AnotherNewPass!789'" in content
assert 'OPENSEARCH_PASSWORD="old-password"' not in content
# ---------------------------------------------------------------------------
# ensure_openrag_version
# ---------------------------------------------------------------------------
class TestEnsureOpenragVersionPermissions:
"""ensure_openrag_version must enforce 0o600 on every .env it touches."""
def test_existing_file_update_has_secure_permissions(self, env_manager, tmp_path, monkeypatch):
"""Updating OPENRAG_VERSION in a permissive .env (0o644) must set 0o600."""
env_file = tmp_path / ".env"
env_file.write_text("OPENSEARCH_PASSWORD='test'\n")
env_file.chmod(0o644)
assert _perms(env_file) == 0o644, "pre-condition: file starts permissive"
# Prevent stale OPENRAG_VERSION in the process environment from
# causing ensure_openrag_version to bail out early.
monkeypatch.delenv("OPENRAG_VERSION", raising=False)
with patch("tui.utils.version_check.get_current_version", return_value="1.2.3"):
env_manager.ensure_openrag_version()
assert env_file.exists()
assert _perms(env_file) == 0o600, f"expected 0o600, got {oct(_perms(env_file))}"
assert "OPENRAG_VERSION='1.2.3'" in env_file.read_text()
def test_new_file_creation_has_secure_permissions(self, env_manager, tmp_path):
"""When no .env exists ensure_openrag_version must create one with 0o600."""
env_file = tmp_path / ".env"
assert not env_file.exists(), "pre-condition: no .env yet"
with patch("tui.utils.version_check.get_current_version", return_value="1.2.3"):
env_manager.ensure_openrag_version()
assert env_file.exists()
assert _perms(env_file) == 0o600, f"expected 0o600, got {oct(_perms(env_file))}"
# ---------------------------------------------------------------------------
# Legacy migration path (__init__)
# ---------------------------------------------------------------------------
class TestLegacyMigrationPermissions:
"""EnvManager.__init__ migration path must protect the copied .env with 0o600."""
def test_migrated_file_has_secure_permissions(self, tmp_path):
"""A legacy .env (0o644) copied to the new location must get 0o600."""
# Create the legacy file with deliberately permissive permissions.
legacy_dir = tmp_path / "legacy_dir"
legacy_dir.mkdir()
legacy_env = legacy_dir / ".env"
legacy_env.write_text("OPENSEARCH_PASSWORD='secret'\n")
legacy_env.chmod(0o644)
assert _perms(legacy_env) == 0o644, "pre-condition: legacy file is permissive"
# Target path that does not yet exist; its parent will be created by
# EnvManager.__init__ via Path.mkdir(parents=True, exist_ok=True).
target_env = tmp_path / "new_location" / ".env"
assert not target_env.exists(), "pre-condition: target not present"
with (
patch("utils.paths.get_tui_env_file", return_value=target_env),
patch(
"utils.paths.get_legacy_paths",
return_value={"tui_env": legacy_env},
),
):
from tui.managers.env_manager import EnvManager
EnvManager() # no explicit env_file — triggers the migration branch
assert target_env.exists(), "migrated file must be present"
assert _perms(target_env) == 0o600, f"expected 0o600, got {oct(_perms(target_env))}"
class TestLangflowPasswordAndAutoLogin:
"""Verify Langflow superuser password auto-generation, validation, and auto-login settings."""
def test_setup_secure_defaults_generates_langflow_password_and_keeps_autologin(
self, env_manager
):
env_manager.config.langflow_superuser_password = ""
env_manager.setup_secure_defaults()
assert bool(env_manager.config.langflow_superuser_password)
assert env_manager.config.langflow_auto_login == "True"
assert env_manager.config.langflow_new_user_is_active == "True"
assert env_manager.config.langflow_enable_superuser_cli == "True"
def test_save_env_file_writes_langflow_password_and_autologin(self, env_manager, tmp_path):
env_file = tmp_path / ".env"
env_manager.config.opensearch_password = "OpenSearchPass123!"
env_manager.config.langflow_superuser_password = "LangflowPass123!"
with patch("tui.utils.version_check.get_current_version", return_value="1.0.0"):
assert env_manager.save_env_file() is True
content = env_file.read_text()
assert "LANGFLOW_SUPERUSER_PASSWORD='LangflowPass123!'" in content
assert "LANGFLOW_AUTO_LOGIN='True'" in content
assert "LANGFLOW_SUPERUSER='langflow'" in content
def test_validate_config_requires_langflow_password(self, env_manager):
env_manager.config.opensearch_password = "Pass123!"
env_manager.config.langflow_superuser_password = ""
assert env_manager.validate_config() is False
assert "langflow_superuser_password" in env_manager.config.validation_errors
# ---------------------------------------------------------------------------
# Backend tracing settings
# ---------------------------------------------------------------------------
class TestBackendTracingSettings:
"""The TUI manages backend tracing alongside the Langfuse keys it reuses."""
def test_tracing_flags_round_trip(self, env_manager, tmp_path):
from tui.managers.env_manager import EnvManager
env_manager.config.opensearch_password = "OpenSearchPass123!"
env_manager.config.langflow_superuser_password = "LangflowPass123!"
env_manager.config.langfuse_public_key = "pk-lf-test"
env_manager.config.openrag_tracing_enabled = "true"
env_manager.config.openrag_tracing_capture_content = "true"
with patch("tui.utils.version_check.get_current_version", return_value="1.0.0"):
assert env_manager.save_env_file() is True
content = (tmp_path / ".env").read_text()
assert content.count("OPENRAG_TRACING_ENABLED=") == 1
assert content.count("OPENRAG_TRACING_CAPTURE_CONTENT=") == 1
reloaded = EnvManager(env_file=tmp_path / ".env")
reloaded.load_existing_env()
assert reloaded.config.openrag_tracing_enabled == "true"
assert reloaded.config.openrag_tracing_capture_content == "true"
def test_unset_tracing_flags_are_not_written(self, env_manager, tmp_path):
env_manager.config.opensearch_password = "OpenSearchPass123!"
env_manager.config.langflow_superuser_password = "LangflowPass123!"
with patch("tui.utils.version_check.get_current_version", return_value="1.0.0"):
assert env_manager.save_env_file() is True
assert "OPENRAG_TRACING_" not in (tmp_path / ".env").read_text()
def test_tracing_fields_are_in_the_langfuse_section(self):
from tui.config_fields import CONFIG_SECTIONS
(langfuse,) = [s for s in CONFIG_SECTIONS if s.name == "Langfuse"]
env_vars = {f.env_var for f in langfuse.fields}
assert {"OPENRAG_TRACING_ENABLED", "OPENRAG_TRACING_CAPTURE_CONTENT"} <= env_vars
class TestLangfuseBaseUrl:
"""LANGFUSE_BASE_URL is the host name Langfuse's setup steps use; LANGFUSE_HOST is the older alias."""
@pytest.fixture(autouse=True)
def _clear_langfuse_host_env(self, monkeypatch):
# load_dotenv writes into os.environ, so clear what earlier loads left.
for var in ("LANGFUSE_HOST", "LANGFUSE_BASE_URL"):
monkeypatch.setenv(var, "")
monkeypatch.delenv(var)
def _save(self, env_manager):
env_manager.config.opensearch_password = "OpenSearchPass123!"
env_manager.config.langflow_superuser_password = "LangflowPass123!"
with patch("tui.utils.version_check.get_current_version", return_value="1.0.0"):
assert env_manager.save_env_file() is True
def test_host_is_written_as_base_url(self, env_manager, tmp_path):
env_manager.config.langfuse_base_url = "https://us.cloud.langfuse.com"
self._save(env_manager)
from tui.managers.env_manager import EnvManager
assert "LANGFUSE_HOST" not in (tmp_path / ".env").read_text()
reloaded = EnvManager(env_file=tmp_path / ".env")
reloaded.load_existing_env()
assert reloaded.config.langfuse_base_url == "https://us.cloud.langfuse.com"
def test_legacy_langfuse_host_is_migrated_to_base_url(self, tmp_path):
from tui.managers.env_manager import EnvManager
(tmp_path / ".env").write_text("LANGFUSE_HOST=http://langfuse.internal:3000\n")
manager = EnvManager(env_file=tmp_path / ".env")
manager.load_existing_env()
assert manager.config.langfuse_base_url == "http://langfuse.internal:3000"
self._save(manager)
content = (tmp_path / ".env").read_text()
assert "LANGFUSE_BASE_URL=" in content
assert "LANGFUSE_HOST" not in content
def test_base_url_wins_over_legacy_host(self, tmp_path):
from tui.managers.env_manager import EnvManager
(tmp_path / ".env").write_text(
"LANGFUSE_BASE_URL=https://us.cloud.langfuse.com\n"
"LANGFUSE_HOST=https://cloud.langfuse.com\n"
)
manager = EnvManager(env_file=tmp_path / ".env")
manager.load_existing_env()
assert manager.config.langfuse_base_url == "https://us.cloud.langfuse.com"
def test_langfuse_section_offers_base_url(self):
from tui.config_fields import CONFIG_SECTIONS
(langfuse,) = [s for s in CONFIG_SECTIONS if s.name == "Langfuse"]
env_vars = {f.env_var for f in langfuse.fields}
assert "LANGFUSE_BASE_URL" in env_vars
assert "LANGFUSE_HOST" not in env_vars