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.

fix: harden env var pre-commit hook and fix remaining violations (#2166)

LearningCircuite776afad2dae
diff --git a/.pre-commit-hooks/check-env-vars.py b/.pre-commit-hooks/check-env-vars.py
index c636ea19d5..b3320a4965 100755
--- a/.pre-commit-hooks/check-env-vars.py
+++ b/.pre-commit-hooks/check-env-vars.py
@@ -58,12 +58,43 @@ class EnvVarChecker(ast.NodeVisitor):
self.filename = filename
self.errors = []
+ def visit_ImportFrom(self, node):
+ """Check for 'from os import environ/getenv' direct imports."""
+ if not self._is_file_allowed() and node.module == "os" and node.names:
+ for alias in node.names:
+ if alias.name in ("environ", "getenv"):
+ self.errors.append(
+ (
+ node.lineno,
+ f"Direct import 'from os import {alias.name}' — use SettingsManager instead of direct env var access",
+ )
+ )
+ self.generic_visit(node)
+
+ def visit_Assign(self, node):
+ """Check for aliasing: env = os.environ."""
+ if not self._is_file_allowed():
+ # Check the right-hand side for os.environ attribute access
+ if (
+ isinstance(node.value, ast.Attribute)
+ and node.value.attr == "environ"
+ and isinstance(node.value.value, ast.Name)
+ and node.value.value.id == "os"
+ ):
+ self.errors.append(
+ (
+ node.lineno,
+ "Aliasing os.environ to a local variable — use SettingsManager instead",
+ )
+ )
+ 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")
+ # Pattern 1: os.environ.get("VAR_NAME") or os.environ.get(variable)
if (
isinstance(node.func, ast.Attribute)
and node.func.attr == "get"
@@ -76,7 +107,7 @@ class EnvVarChecker(ast.NodeVisitor):
if node.args and isinstance(node.args[0], ast.Constant):
env_var_name = node.args[0].value
- # Pattern 2: os.getenv("VAR_NAME")
+ # Pattern 2: os.getenv("VAR_NAME") or os.getenv(variable)
elif (
isinstance(node.func, ast.Attribute)
and node.func.attr == "getenv"
@@ -87,27 +118,51 @@ class EnvVarChecker(ast.NodeVisitor):
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:
- # Allow system vars
- if env_var_name in SYSTEM_VARS:
- return self.generic_visit(node)
-
- # Check if file is in allowed location
+ # Pattern 3: os.environ.pop() / .setdefault() / .update()
+ if (
+ isinstance(node.func, ast.Attribute)
+ and node.func.attr in ("pop", "setdefault", "update")
+ 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"
+ ):
if not self._is_file_allowed():
- # For LDR_ vars, suggest using SettingsManager
- if env_var_name.startswith("LDR_"):
- self.errors.append(
- (
- node.lineno,
- f"Environment variable '{env_var_name}' should be accessed through SettingsManager, not os.environ",
- )
+ self.errors.append(
+ (
+ node.lineno,
+ f"Direct os.environ.{node.func.attr}() call — use SettingsManager instead",
)
- # For other vars, generic warning
- else:
+ )
+
+ if is_environ_get:
+ if env_var_name:
+ # Known constant key — allow system vars
+ if env_var_name in SYSTEM_VARS:
+ return self.generic_visit(node)
+
+ if not self._is_file_allowed():
+ if env_var_name.startswith("LDR_"):
+ self.errors.append(
+ (
+ node.lineno,
+ f"Environment variable '{env_var_name}' should be accessed through SettingsManager, not os.environ",
+ )
+ )
+ else:
+ self.errors.append(
+ (
+ node.lineno,
+ f"Direct access to environment variable '{env_var_name}' - consider using SettingsManager",
+ )
+ )
+ else:
+ # Dynamic key (variable, not a string literal)
+ if not self._is_file_allowed():
self.errors.append(
(
node.lineno,
- f"Direct access to environment variable '{env_var_name}' - consider using SettingsManager",
+ "Dynamic environment variable access (variable key) — use SettingsManager instead",
)
)
@@ -120,30 +175,63 @@ class EnvVarChecker(ast.NodeVisitor):
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 isinstance(node.slice, ast.Constant):
+ env_var_name = node.slice.value
- # Allow system vars
- if env_var_name in SYSTEM_VARS:
- return self.generic_visit(node)
+ # Allow system vars
+ if env_var_name in SYSTEM_VARS:
+ return self.generic_visit(node)
- if not self._is_file_allowed():
- if env_var_name.startswith("LDR_"):
+ if not self._is_file_allowed():
+ if env_var_name.startswith("LDR_"):
+ self.errors.append(
+ (
+ node.lineno,
+ f"Environment variable '{env_var_name}' should be accessed through SettingsManager, not os.environ",
+ )
+ )
+ else:
+ self.errors.append(
+ (
+ node.lineno,
+ f"Direct access to environment variable '{env_var_name}' - consider using SettingsManager",
+ )
+ )
+ else:
+ # Dynamic subscript: os.environ[variable]
+ 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",
+ "Dynamic os.environ[variable] access — use SettingsManager instead",
)
)
- else:
+
+ self.generic_visit(node)
+
+ def visit_Compare(self, node):
+ """Check for 'KEY in os.environ' containment checks."""
+ if not self._is_file_allowed():
+ for comparator in node.comparators:
+ if (
+ isinstance(comparator, ast.Attribute)
+ and comparator.attr == "environ"
+ and isinstance(comparator.value, ast.Name)
+ and comparator.value.id == "os"
+ ):
+ # Check if the left side is a system var
+ if (
+ isinstance(node.left, ast.Constant)
+ and node.left.value in SYSTEM_VARS
+ ):
+ continue
self.errors.append(
(
node.lineno,
- f"Direct access to environment variable '{env_var_name}' - consider using SettingsManager",
+ "'... in os.environ' check — use SettingsManager instead of direct env var access",
)
)
-
self.generic_visit(node)
def _is_file_allowed(self) -> bool:
diff --git a/src/local_deep_research/api/settings_utils.py b/src/local_deep_research/api/settings_utils.py
index 023e831116..6c4496eb12 100644
--- a/src/local_deep_research/api/settings_utils.py
+++ b/src/local_deep_research/api/settings_utils.py
@@ -6,14 +6,13 @@ without requiring database access, reusing the same mechanisms as the
web interface.
"""
-import os
import copy
from typing import Any
from loguru import logger
from ..settings import SettingsManager
from ..settings.base import ISettingsManager
-from ..settings.manager import _UI_ELEMENT_TO_SETTING_TYPE
+from ..settings.manager import _UI_ELEMENT_TO_SETTING_TYPE, check_env_setting
from ..utilities.type_utils import to_bool # noqa: F401
@@ -73,8 +72,7 @@ class InMemorySettingsManager(ISettingsManager):
self._settings[key] = setting_data.copy()
# Check environment variable override
- env_key = f"LDR_{key.upper().replace('.', '_')}"
- env_value = os.environ.get(env_key)
+ env_value = check_env_setting(key)
if env_value is not None:
# Use the typed value conversion
self._settings[key]["value"] = self._get_typed_value(
diff --git a/src/local_deep_research/security/security_settings.py b/src/local_deep_research/security/security_settings.py
index 20059ada78..acc31bc2be 100644
--- a/src/local_deep_research/security/security_settings.py
+++ b/src/local_deep_research/security/security_settings.py
@@ -6,13 +6,14 @@ the full settings system is available.
"""
import json
-import os
from functools import lru_cache
from pathlib import Path
from typing import Any, Optional, TypeVar, Union
from loguru import logger
+from local_deep_research.settings.manager import check_env_setting
+
T = TypeVar("T", int, float, str, bool)
# Path to security settings JSON
@@ -140,7 +141,7 @@ def get_security_default(key: str, default: T) -> T:
# Check environment variable first (e.g., LDR_SECURITY_SESSION_REMEMBER_ME_DAYS)
env_key = f"LDR_{key.upper().replace('.', '_')}"
- env_value = os.getenv(env_key)
+ env_value = check_env_setting(key)
if env_value is not None:
converted = _convert_value(env_value, type(default), env_key)