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(security): allow LAN access over HTTP without HTTPS requirement

LearningCircuit97ce1d60cdf4
diff --git a/src/local_deep_research/web/app_factory.py b/src/local_deep_research/web/app_factory.py
index 219b0d03ca..0213ad8ee3 100644
--- a/src/local_deep_research/web/app_factory.py
+++ b/src/local_deep_research/web/app_factory.py
@@ -1,4 +1,5 @@
# import logging - replaced with loguru
+import ipaddress
import os
from pathlib import Path
from importlib import resources as importlib_resources
@@ -24,6 +25,21 @@ from ..security.file_upload_validator import FileUploadValidator
from .services.socket_service import SocketIOService
+def _is_private_ip(ip_str: str) -> bool:
+ """Check if IP is a private/local network address (RFC 1918 + localhost).
+
+ This allows LAN access over HTTP without requiring HTTPS, matching the
+ behavior of other self-hosted applications like Jellyfin and Home Assistant.
+
+ Private ranges: 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, plus localhost.
+ """
+ try:
+ ip = ipaddress.ip_address(ip_str)
+ return ip.is_private or ip.is_loopback
+ except ValueError:
+ return False
+
+
def create_app():
"""
Create and configure the Flask application.
@@ -133,15 +149,16 @@ def create_app():
# Behind proxy = production, always add Secure flag
return True
- # Direct connection - check if from localhost
+ # Direct connection - check if from private/local network (RFC 1918)
+ # This allows LAN access over HTTP, matching Jellyfin/Home Assistant behavior
remote_addr = environ.get("REMOTE_ADDR", "")
- is_localhost = remote_addr in ("127.0.0.1", "::1")
+ is_private = _is_private_ip(remote_addr)
# Check if HTTPS
is_https = environ.get("wsgi.url_scheme") == "https"
- # Add Secure if HTTPS or if NOT from localhost
- return is_https or not is_localhost
+ # Add Secure if HTTPS or if NOT from private network
+ return is_https or not is_private
# Wrap the app with our cookie security middleware
app.wsgi_app = SecureCookieMiddleware(app.wsgi_app, app)
@@ -700,13 +717,13 @@ def register_error_handlers(app):
"""Handle CSRF errors with helpful debugging info."""
# Check if this might be a Secure cookie issue over HTTP
is_http = not request.is_secure
- is_non_localhost = request.remote_addr not in ("127.0.0.1", "::1")
+ is_private = _is_private_ip(request.remote_addr or "")
is_proxied = request.headers.get("X-Forwarded-For") is not None
error_msg = str(error.description)
- # Provide detailed help for HTTP + non-localhost scenario
- if is_http and (is_non_localhost or is_proxied):
+ # Provide detailed help for HTTP + public IP or proxied scenario
+ if is_http and (not is_private or is_proxied):
logger.warning(
f"CSRF validation failed - likely due to Secure cookie over HTTP. "
f"remote_addr={request.remote_addr}, proxied={is_proxied}, "
@@ -714,19 +731,16 @@ def register_error_handlers(app):
)
error_msg = (
"Session cookie error: You're accessing over HTTP from a "
- "non-localhost address or through a proxy. "
+ "public IP address or through a proxy. "
"This is blocked for security reasons.\n\n"
"Solutions:\n"
"1. Use HTTPS with a reverse proxy (recommended for production)\n"
- "2. Access directly from localhost (http://127.0.0.1:5000)\n"
- "3. Use SSH tunnel: ssh -L 5000:localhost:5000 user@server, "
- "then access http://localhost:5000\n"
- "4. Set TESTING=1 environment variable when starting the server "
- "(INSECURE - development only!)\n"
- " Example: TESTING=1 python -m local_deep_research.web.app\n\n"
- "WARNING: Using TESTING=1 disables secure cookie protection. "
- "Session cookies can be intercepted by network attackers. "
- "Never use this in production or on public networks."
+ "2. Access from your local network (LAN IPs like 192.168.x.x work over HTTP)\n"
+ "3. Access directly from localhost (http://127.0.0.1:5000)\n"
+ "4. Use SSH tunnel: ssh -L 5000:localhost:5000 user@server, "
+ "then access http://localhost:5000\n\n"
+ "Note: LAN access (192.168.x.x, 10.x.x.x, 172.16-31.x.x) works over HTTP. "
+ "Only public internet access requires HTTPS."
)
return make_response(jsonify({"error": error_msg}), 400)
diff --git a/tests/security/test_cookie_security.py b/tests/security/test_cookie_security.py
index c66bea96e9..7bf27f5ad4 100644
--- a/tests/security/test_cookie_security.py
+++ b/tests/security/test_cookie_security.py
@@ -1,15 +1,18 @@
"""
Cookie Security Tests
-Tests for the dynamic cookie security behavior that allows localhost HTTP
-while requiring HTTPS for non-localhost connections.
+Tests for the dynamic cookie security behavior that allows private network HTTP
+while requiring HTTPS for public internet connections.
Security model:
-- Localhost HTTP (127.0.0.1, ::1): Cookies work without Secure flag
-- Proxied requests (X-Forwarded-For): Always get Secure flag
-- Non-localhost HTTP: Get Secure flag (will fail, by design)
+- Private network HTTP (localhost, LAN IPs like 192.168.x.x): Cookies work without Secure flag
+- Proxied requests (X-Forwarded-For): Always get Secure flag (production environment)
+- Public IP HTTP: Get Secure flag (will fail, by design - requires HTTPS)
- HTTPS: Always get Secure flag
- TESTING mode: Never get Secure flag (for CI/development)
+
+Private IPs (RFC 1918) include: 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, plus localhost.
+This matches the behavior of self-hosted apps like Jellyfin and Home Assistant.
"""
import pytest
@@ -122,6 +125,81 @@ class TestLocalhostProductionMode:
assert "session=" in set_cookie # Cookie should still be set
+class TestLANIPCookieSecurity:
+ """Test that LAN/private network IPs work without Secure flag (RFC 1918)."""
+
+ def test_lan_ip_192_168_no_secure_flag(self, app_production_mode):
+ """192.168.x.x LAN IPs should NOT have Secure flag."""
+ app = app_production_mode
+
+ with app.test_client() as client:
+ # Simulate request from 192.168.1.100 (common home network)
+ response = client.get(
+ "/auth/login",
+ environ_base={"REMOTE_ADDR": "192.168.1.100"},
+ )
+
+ set_cookie = response.headers.get("Set-Cookie", "")
+
+ # Should NOT have Secure flag for LAN IP over HTTP
+ assert "; Secure" not in set_cookie, (
+ f"LAN IP 192.168.x.x should NOT have Secure flag. Got: {set_cookie}"
+ )
+ assert "session=" in set_cookie # Cookie should still be set
+
+ def test_lan_ip_10_x_no_secure_flag(self, app_production_mode):
+ """10.x.x.x LAN IPs should NOT have Secure flag."""
+ app = app_production_mode
+
+ with app.test_client() as client:
+ # Simulate request from 10.0.0.50 (common corporate network)
+ response = client.get(
+ "/auth/login",
+ environ_base={"REMOTE_ADDR": "10.0.0.50"},
+ )
+
+ set_cookie = response.headers.get("Set-Cookie", "")
+
+ assert "; Secure" not in set_cookie, (
+ f"LAN IP 10.x.x.x should NOT have Secure flag. Got: {set_cookie}"
+ )
+
+ def test_lan_ip_172_16_no_secure_flag(self, app_production_mode):
+ """172.16-31.x.x LAN IPs should NOT have Secure flag."""
+ app = app_production_mode
+
+ with app.test_client() as client:
+ # Simulate request from 172.16.0.1
+ response = client.get(
+ "/auth/login",
+ environ_base={"REMOTE_ADDR": "172.16.0.1"},
+ )
+
+ set_cookie = response.headers.get("Set-Cookie", "")
+
+ assert "; Secure" not in set_cookie, (
+ f"LAN IP 172.16.x.x should NOT have Secure flag. Got: {set_cookie}"
+ )
+
+ def test_public_ip_gets_secure_flag(self, app_production_mode):
+ """Public IPs (non-RFC 1918) should get Secure flag over HTTP."""
+ app = app_production_mode
+
+ with app.test_client() as client:
+ # Simulate request from a public IP (Google DNS)
+ response = client.get(
+ "/auth/login",
+ environ_base={"REMOTE_ADDR": "8.8.8.8"},
+ )
+
+ set_cookie = response.headers.get("Set-Cookie", "")
+
+ # Public IP should have Secure flag
+ assert "; Secure" in set_cookie, (
+ f"Public IP should have Secure flag. Got: {set_cookie}"
+ )
+
+
class TestProxiedRequestSecurity:
"""Test that proxied requests always get Secure flag."""
@@ -228,28 +306,30 @@ class TestCookieSecurityDocumentation:
WHY THIS EXISTS:
- Session cookies with Secure flag only work over HTTPS
- - Local development typically uses HTTP
- - We want security in production but usability in development
+ - Local/LAN development typically uses HTTP
+ - We want security for public internet but usability for private networks
+ - Matches behavior of self-hosted apps like Jellyfin, Home Assistant
HOW IT WORKS:
1. Check if request has X-Forwarded-For header (proxy indicator)
2. If proxied: Always add Secure flag (production behind proxy)
- 3. If not proxied: Check request.remote_addr
- - 127.0.0.1 or ::1: Skip Secure flag (localhost development)
- - Other IPs: Add Secure flag (direct non-localhost = needs HTTPS)
+ 3. If not proxied: Check if request.remote_addr is private (RFC 1918)
+ - Private IPs (127.x, 10.x, 172.16-31.x, 192.168.x): Skip Secure flag
+ - Public IPs: Add Secure flag (requires HTTPS)
4. TESTING mode: Never add Secure flag (CI/test environments)
SECURITY PROPERTIES:
- Localhost HTTP: ✅ Works - traffic is local only
+ - LAN HTTP (192.168.x.x, 10.x.x.x, 172.16-31.x.x): ✅ Works - private network
+ - Public IP HTTP: ✅ Blocked - requires HTTPS
- Proxied requests: ✅ Secure - always requires HTTPS
- X-Forwarded-For spoofing: ✅ Blocked - proxy presence = Secure
- - Non-localhost HTTP: ✅ Blocked - requires HTTPS
- TESTING mode: ⚠️ Insecure but explicit opt-in
ATTACK PREVENTION:
- Session hijacking on public networks: Prevented by Secure flag
- X-Forwarded-For spoofing: Prevented by checking for proxy header
- - Development convenience: Preserved for localhost only
+ - Private network convenience: Preserved for RFC 1918 IPs
"""
assert True # Documentation test
@@ -263,9 +343,15 @@ def test_cookie_security_summary():
|-----------------------------|-------------|--------------|
| localhost:5000 (direct) | No | ✅ Yes |
| 127.0.0.1:5000 (direct) | No | ✅ Yes |
- | 192.168.x.x:5000 (direct) | Yes | ❌ No (HTTP) |
+ | 192.168.x.x:5000 (direct) | No | ✅ Yes (LAN) |
+ | 10.x.x.x:5000 (direct) | No | ✅ Yes (LAN) |
+ | 172.16-31.x.x:5000 (direct) | No | ✅ Yes (LAN) |
+ | Public IP (direct) | Yes | ❌ No (HTTP) |
| Behind proxy (any) | Yes | ✅ If HTTPS |
| X-Forwarded-For spoof | Yes | ❌ Blocked |
| TESTING=1 mode | No | ✅ Yes |
+
+ Note: LAN IPs (RFC 1918 private addresses) are now trusted like localhost,
+ matching the behavior of self-hosted apps like Jellyfin and Home Assistant.
"""
assert True # Documentation/summary test