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(security): use O_EXCL atomic creation for secret key file (#3128)

LearningCircuit07795ac7edac
diff --git a/src/local_deep_research/web/app_factory.py b/src/local_deep_research/web/app_factory.py
index 0d75ce4551..629f1782dc 100644
--- a/src/local_deep_research/web/app_factory.py
+++ b/src/local_deep_research/web/app_factory.py
@@ -246,26 +246,28 @@ def create_app():
from ..config.paths import get_data_directory
secret_key_file = Path(get_data_directory()) / ".secret_key"
- if secret_key_file.exists():
+ secret_key_file.parent.mkdir(parents=True, exist_ok=True)
+ new_key = secrets.token_hex(32)
+ try:
+ fd = os.open(
+ str(secret_key_file), os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600
+ )
+ try:
+ os.write(fd, new_key.encode())
+ finally:
+ os.close(fd)
+ app.config["SECRET_KEY"] = new_key
+ logger.info("Generated new SECRET_KEY for this installation")
+ except FileExistsError:
try:
with open(secret_key_file, "r") as f:
app.config["SECRET_KEY"] = f.read().strip()
except Exception:
logger.warning("Could not read secret key file")
- app.config["SECRET_KEY"] = secrets.token_hex(32)
- else:
- # Generate a new key on first run
- new_key = secrets.token_hex(32)
- try:
- secret_key_file.parent.mkdir(parents=True, exist_ok=True)
- with open(secret_key_file, "w") as f:
- f.write(new_key)
- secret_key_file.chmod(0o600) # Secure file permissions
- app.config["SECRET_KEY"] = new_key
- logger.info("Generated new SECRET_KEY for this installation")
- except Exception:
- logger.warning("Could not save secret key file")
app.config["SECRET_KEY"] = new_key
+ except OSError:
+ logger.warning("Could not save secret key file")
+ app.config["SECRET_KEY"] = new_key
# Session cookie security settings
# SECURE flag is added dynamically based on request context (see after_request below)
# This allows localhost HTTP to work for development while keeping production secure