Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

feat: Add pre-commit hook for environment variable access

LearningCircuitbf6065d5d488
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 2eb5b11119..ccac20ee68 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -29,3 +29,9 @@ repos:
language: script
files: \.py$
description: "Check for loguru usage, logger.exception vs logger.error, and raw SQL"
+ - id: check-env-vars
+ name: Environment Variable Access Check
+ entry: .pre-commit-hooks/check-env-vars.py
+ language: script
+ files: \.py$
+ description: "Ensure environment variables are accessed through SettingsManager"
diff --git a/.pre-commit-hooks/check-env-vars.py b/.pre-commit-hooks/check-env-vars.py
new file mode 100755
index 0000000000..aaecf6de86
--- /dev/null
+++ b/.pre-commit-hooks/check-env-vars.py
@@ -0,0 +1,229 @@
+#!/usr/bin/env python3
+"""
+Pre-commit hook to check for direct environment variable access.
+Ensures environment variables are accessed through SettingsManager where appropriate.
+"""
+
+import ast
+import sys
+
+
+# Allowed patterns - these can use os.environ directly
+ALLOWED_PATTERNS = {
+ # Configuration files and initialization
+ "config/",
+ "settings/",
+ "__init__.py",
+ "setup.py",
+ # Test files
+ "test_",
+ "_test.py",
+ "tests/",
+ # Scripts and utilities
+ "scripts/",
+ "utils/",
+ "cli.py",
+ # Specific modules that need direct access
+ "log_utils.py", # Logging configuration
+ "paths.py", # Path configuration
+ "queue_config.py", # Queue configuration
+ "memory_cache/", # Cache configuration
+ # Database and migrations
+ "alembic/",
+ "migrations/",
+ "encrypted_db.py", # Database encryption setup
+}
+
+# Specific environment variables that are always allowed
+ALWAYS_ALLOWED_VARS = {
+ # Development and debugging
+ "DEBUG",
+ "PYTHONPATH",
+ "PATH",
+ # System configuration
+ "HOME",
+ "USER",
+ "TMPDIR",
+ "TEMP",
+ # LDR system-level configuration
+ "LDR_DATA_DIR",
+ "LDR_CONFIG_DIR",
+ "LDR_LOG_DIR",
+ # Security-sensitive vars that shouldn't be in DB
+ "LDR_ENCRYPTION_KEY",
+ "LDR_SECRET_KEY",
+ "LDR_DATABASE_URL",
+ # CI/CD and deployment
+ "CI",
+ "GITHUB_ACTIONS",
+ "DOCKER_CONTAINER",
+}
+
+
+class EnvVarChecker(ast.NodeVisitor):
+ def __init__(self, filename: str):
+ self.filename = filename
+ self.errors = []
+ self.has_settings_manager_import = False
+
+ def visit_ImportFrom(self, node):
+ if node.module and "settings" in node.module.lower():
+ for alias in node.names:
+ if "SettingsManager" in (alias.name or ""):
+ self.has_settings_manager_import = True
+ self.generic_visit(node)
+
+ def visit_Call(self, node):
+ # Check for os.environ.get() or os.getenv()
+ is_environ_get = False
+ env_var_name = None
+
+ # Pattern 1: os.environ.get("VAR_NAME")
+ if (
+ isinstance(node.func, ast.Attribute)
+ and node.func.attr == "get"
+ and isinstance(node.func.value, ast.Attribute)
+ and node.func.value.attr == "environ"
+ and isinstance(node.func.value.value, ast.Name)
+ and node.func.value.value.id == "os"
+ ):
+ is_environ_get = True
+ if node.args and isinstance(node.args[0], ast.Constant):
+ env_var_name = node.args[0].value
+
+ # Pattern 2: os.getenv("VAR_NAME")
+ elif (
+ isinstance(node.func, ast.Attribute)
+ and node.func.attr == "getenv"
+ and isinstance(node.func.value, ast.Name)
+ and node.func.value.value.id == "os"
+ ):
+ is_environ_get = True
+ if node.args and isinstance(node.args[0], ast.Constant):
+ env_var_name = node.args[0].value
+
+ if is_environ_get and env_var_name:
+ # Check if this variable is always allowed
+ if env_var_name not in ALWAYS_ALLOWED_VARS:
+ # Check if file is in allowed locations
+ if not self._is_file_allowed():
+ self.errors.append(
+ (
+ node.lineno,
+ f"Environment variable '{env_var_name}' should be accessed through SettingsManager, not os.environ",
+ )
+ )
+
+ self.generic_visit(node)
+
+ def visit_Subscript(self, node):
+ # Check for os.environ["VAR_NAME"] pattern
+ if (
+ isinstance(node.value, ast.Attribute)
+ and node.value.attr == "environ"
+ and isinstance(node.value.value, ast.Name)
+ and node.value.value.id == "os"
+ and isinstance(node.slice, ast.Constant)
+ ):
+ env_var_name = node.slice.value
+ if env_var_name not in ALWAYS_ALLOWED_VARS:
+ if not self._is_file_allowed():
+ self.errors.append(
+ (
+ node.lineno,
+ f"Environment variable '{env_var_name}' should be accessed through SettingsManager, not os.environ",
+ )
+ )
+
+ self.generic_visit(node)
+
+ def _is_file_allowed(self) -> bool:
+ """Check if this file is allowed to use os.environ directly."""
+ for pattern in ALLOWED_PATTERNS:
+ if pattern in self.filename:
+ return True
+ return False
+
+
+def check_file(filename: str) -> bool:
+ """Check a single Python file for direct env var access."""
+ if not filename.endswith(".py"):
+ return True
+
+ try:
+ with open(filename, "r", encoding="utf-8") as f:
+ content = f.read()
+ except Exception as e:
+ print(f"Error reading {filename}: {e}")
+ return False
+
+ try:
+ tree = ast.parse(content, filename=filename)
+ checker = EnvVarChecker(filename)
+ checker.visit(tree)
+
+ if checker.errors:
+ print(f"\n{filename}:")
+ for line_num, error in checker.errors:
+ print(f" Line {line_num}: {error}")
+ return False
+
+ except SyntaxError:
+ # Skip files with syntax errors
+ pass
+ except Exception as e:
+ print(f"Error parsing {filename}: {e}")
+ return False
+
+ return True
+
+
+def main():
+ """Main function to check all staged Python files."""
+ if len(sys.argv) < 2:
+ print("Usage: check-env-vars.py <file1> <file2> ...")
+ sys.exit(1)
+
+ files_to_check = sys.argv[1:]
+ has_errors = False
+
+ print("Checking environment variable access patterns...")
+
+ for filename in files_to_check:
+ if not check_file(filename):
+ has_errors = True
+
+ if has_errors:
+ print("\n❌ Environment variable checks failed!")
+ print("\nGuidelines:")
+ print(
+ "1. Use SettingsManager for application settings instead of os.environ"
+ )
+ print("2. Direct os.environ access is allowed in:")
+ print(" - Configuration and initialization files")
+ print(" - Test files")
+ print(" - Scripts and utilities")
+ print("3. Always allowed environment variables:")
+ print(" - System vars: HOME, PATH, USER, etc.")
+ print(" - Security vars: encryption keys, secrets")
+ print(" - LDR system paths: LDR_DATA_DIR, etc.")
+ print("\nTo add a setting:")
+ print(
+ " 1. Add it to defaults/default_settings.json with 'env_var' field"
+ )
+ print(" 2. Access via: settings_manager.get_setting('category.name')")
+ print(
+ " 3. The env var will automatically override the DB value if set"
+ )
+ print("\nNOTE: This is being migrated - see issue #598")
+ print(
+ "Run this check on all files: find . -name '*.py' | xargs python .pre-commit-hooks/check-env-vars.py"
+ )
+ sys.exit(1)
+ else:
+ print("✅ Environment variable checks passed!")
+ sys.exit(0)
+
+
+if __name__ == "__main__":
+ main()