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 local proxies over HTTP without HTTPS requirement

LearningCircuit2f59610f1196
diff --git a/src/local_deep_research/__version__.py b/src/local_deep_research/__version__.py
index 75145de3c7..5309a58419 100644
--- a/src/local_deep_research/__version__.py
+++ b/src/local_deep_research/__version__.py
@@ -1 +1 @@
-__version__ = "1.3.6"
+__version__ = "1.3.7"
diff --git a/src/local_deep_research/web/app_factory.py b/src/local_deep_research/web/app_factory.py
index 0213ad8ee3..13c25ef318 100644
--- a/src/local_deep_research/web/app_factory.py
+++ b/src/local_deep_research/web/app_factory.py
@@ -136,28 +136,38 @@ def create_app():
return self.wsgi_app(environ, custom_start_response)
def _should_add_secure_flag(self, environ):
- """Determine if Secure flag should be added based on request context."""
+ """Determine if Secure flag should be added based on request context.
+
+ Security model:
+ - Check the ACTUAL connection IP (REMOTE_ADDR), not X-Forwarded-For header
+ - SecureCookieMiddleware is outer wrapper, so we see original REMOTE_ADDR
+ - If connection comes from private IP (client or proxy), allow HTTP
+ - If connection comes from public IP, require HTTPS
+
+ This is safe because:
+ - We never trust X-Forwarded-For header values (can be spoofed)
+ - We only check the actual TCP connection source IP
+ - Spoofing X-Forwarded-For from public IP doesn't bypass this check
+ - Local proxies (nginx on localhost/LAN) have private REMOTE_ADDR
+ """
# Skip if in explicit testing mode
if self.flask_app.config.get("LDR_TESTING_MODE"):
return False
- # Check for proxy indicators - if proxied, always require Secure
- # This prevents X-Forwarded-For spoofing attacks
- # Note: After ProxyFix, HTTP_X_FORWARDED_FOR is the original header
- xff = environ.get("HTTP_X_FORWARDED_FOR")
- if xff is not None:
- # Behind proxy = production, always add Secure flag
- return True
-
- # Direct connection - check if from private/local network (RFC 1918)
- # This allows LAN access over HTTP, matching Jellyfin/Home Assistant behavior
+ # Check actual connection source IP (before ProxyFix modifies it)
+ # This is either:
+ # - Direct client IP (if no proxy)
+ # - Proxy server IP (if behind proxy)
+ # Local proxies (nginx on localhost, Traefik on LAN) have private IPs
remote_addr = environ.get("REMOTE_ADDR", "")
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 private network
+ # Add Secure flag if:
+ # - Using HTTPS (always secure over HTTPS)
+ # - OR connection is from public IP (require HTTPS for public access)
return is_https or not is_private
# Wrap the app with our cookie security middleware
diff --git a/tests/security/test_cookie_security.py b/tests/security/test_cookie_security.py
index 7bf27f5ad4..546a085599 100644
--- a/tests/security/test_cookie_security.py
+++ b/tests/security/test_cookie_security.py
@@ -5,12 +5,20 @@ Tests for the dynamic cookie security behavior that allows private network HTTP
while requiring HTTPS for public internet connections.
Security model:
-- 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)
+- Private network HTTP (localhost, LAN IPs): Cookies work without Secure flag
+- Local proxy (nginx on localhost/LAN): Cookies work without Secure flag
+- Public proxy (Cloudflare, AWS): Get Secure flag (requires HTTPS)
- 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)
+Key insight: We check the ACTUAL connection IP (REMOTE_ADDR), not X-Forwarded-For.
+SecureCookieMiddleware is the outer wrapper, so it sees the original REMOTE_ADDR
+before ProxyFix modifies it. This means:
+- Local proxy (nginx on 127.0.0.1) → REMOTE_ADDR=127.0.0.1 (private) → HTTP works
+- Public proxy (Cloudflare) → REMOTE_ADDR=104.16.x.x (public) → requires HTTPS
+- Spoofed X-Forwarded-For from public IP → still blocked (REMOTE_ADDR is public)
+
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.
"""
@@ -200,47 +208,94 @@ class TestLANIPCookieSecurity:
)
-class TestProxiedRequestSecurity:
- """Test that proxied requests always get Secure flag."""
+class TestLocalProxySecurity:
+ """Test that local proxies (nginx on localhost/LAN) work without Secure flag."""
- def test_proxied_request_gets_secure_flag(self, app_production_mode):
- """Requests with X-Forwarded-For should get Secure flag."""
+ def test_local_proxy_no_secure_flag(self, app_production_mode):
+ """Local proxy (127.0.0.1) with X-Forwarded-For should NOT have Secure flag."""
app = app_production_mode
with app.test_client() as client:
- # Simulate proxied request with X-Forwarded-For header
- # Note: Flask test client always has REMOTE_ADDR=127.0.0.1
- # but our code checks for X-Forwarded-For header presence
+ # Proxy on localhost, client behind it
+ # REMOTE_ADDR is the proxy's IP (127.0.0.1 = private)
response = client.get(
"/auth/login",
headers={"X-Forwarded-For": "192.168.1.100"},
+ environ_base={"REMOTE_ADDR": "127.0.0.1"},
+ )
+
+ set_cookie = response.headers.get("Set-Cookie", "")
+
+ # Should NOT have Secure flag - proxy is on localhost (private)
+ assert "; Secure" not in set_cookie, (
+ f"Local proxy should NOT have Secure flag. Got: {set_cookie}"
+ )
+
+ def test_lan_proxy_no_secure_flag(self, app_production_mode):
+ """LAN proxy (192.168.x.x) with X-Forwarded-For should NOT have Secure flag."""
+ app = app_production_mode
+
+ with app.test_client() as client:
+ # Proxy on LAN, client could claim any IP
+ response = client.get(
+ "/auth/login",
+ headers={
+ "X-Forwarded-For": "8.8.8.8"
+ }, # Client claims public IP
+ environ_base={
+ "REMOTE_ADDR": "192.168.1.1"
+ }, # But proxy is on LAN
+ )
+
+ set_cookie = response.headers.get("Set-Cookie", "")
+
+ # Should NOT have Secure flag - proxy is on LAN (private)
+ assert "; Secure" not in set_cookie, (
+ f"LAN proxy should NOT have Secure flag. Got: {set_cookie}"
+ )
+
+ def test_public_proxy_gets_secure_flag(self, app_production_mode):
+ """Public proxy (Cloudflare, AWS) should get Secure flag."""
+ app = app_production_mode
+
+ with app.test_client() as client:
+ # Public proxy (e.g., Cloudflare IP)
+ response = client.get(
+ "/auth/login",
+ headers={
+ "X-Forwarded-For": "192.168.1.100"
+ }, # Client claims LAN
+ environ_base={
+ "REMOTE_ADDR": "104.16.0.1"
+ }, # Proxy is public IP
)
set_cookie = response.headers.get("Set-Cookie", "")
- # Should have Secure flag because X-Forwarded-For header is present
- # This indicates request came through a proxy
+ # Public proxy should have Secure flag
assert "; Secure" in set_cookie, (
- f"Proxied requests should have Secure flag. Got: {set_cookie}"
+ f"Public proxy should have Secure flag. Got: {set_cookie}"
)
- def test_spoofed_localhost_via_xff_still_secure(self, app_production_mode):
- """X-Forwarded-For: 127.0.0.1 should still get Secure flag (anti-spoofing)."""
+ def test_spoofed_xff_from_public_ip_blocked(self, app_production_mode):
+ """Spoofed X-Forwarded-For from public IP should still be blocked."""
app = app_production_mode
with app.test_client() as client:
- # Attacker tries to spoof localhost via X-Forwarded-For
+ # Attacker tries to spoof private IP in X-Forwarded-For header
response = client.get(
"/auth/login",
- headers={"X-Forwarded-For": "127.0.0.1"},
+ headers={"X-Forwarded-For": "127.0.0.1"}, # Spoofed localhost
+ environ_base={
+ "REMOTE_ADDR": "8.8.8.8"
+ }, # But actual IP is public
)
set_cookie = response.headers.get("Set-Cookie", "")
- # Should STILL have Secure flag - we detect the proxy header
- # and treat all proxied requests as production
+ # Should still have Secure flag - we check REMOTE_ADDR, not header
assert "; Secure" in set_cookie, (
- f"Spoofed localhost via X-Forwarded-For should still get Secure. Got: {set_cookie}"
+ f"Spoofed X-Forwarded-For from public IP should be blocked. Got: {set_cookie}"
)
@@ -311,24 +366,25 @@ class TestCookieSecurityDocumentation:
- 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 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)
+ 1. Check the ACTUAL connection IP (REMOTE_ADDR), not X-Forwarded-For
+ 2. SecureCookieMiddleware is outer wrapper, sees original REMOTE_ADDR
+ 3. If REMOTE_ADDR is private (RFC 1918): Skip Secure flag
+ - This includes both direct clients AND local proxy servers
+ 4. If REMOTE_ADDR is public: Add Secure flag (requires HTTPS)
+ 5. 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
+ - Local proxy (nginx on localhost/LAN): ✅ Works - proxy has private IP
+ - Public proxy (Cloudflare, AWS): ✅ Blocked - proxy has public IP
- Public IP HTTP: ✅ Blocked - requires HTTPS
- - Proxied requests: ✅ Secure - always requires HTTPS
- - X-Forwarded-For spoofing: ✅ Blocked - proxy presence = Secure
+ - X-Forwarded-For spoofing: ✅ Blocked - we check REMOTE_ADDR, not header
- 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
+ - X-Forwarded-For spoofing: Prevented by checking REMOTE_ADDR (actual IP)
- Private network convenience: Preserved for RFC 1918 IPs
"""
assert True # Documentation test
@@ -339,19 +395,18 @@ def test_cookie_security_summary():
Summary of cookie security behavior for CI validation.
Expected behavior:
- | Scenario | Secure Flag | Should Work? |
- |-----------------------------|-------------|--------------|
- | localhost:5000 (direct) | No | ✅ Yes |
- | 127.0.0.1:5000 (direct) | No | ✅ Yes |
- | 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.
+ | Scenario | REMOTE_ADDR | Secure Flag | Works? |
+ |-------------------------------|---------------|-------------|--------|
+ | localhost:5000 (direct) | 127.0.0.1 | No | ✅ Yes |
+ | LAN client (direct) | 192.168.x.x | No | ✅ Yes |
+ | Local proxy (nginx) | 127.0.0.1 | No | ✅ Yes |
+ | LAN proxy (nginx on LAN) | 192.168.x.x | No | ✅ Yes |
+ | Public proxy (Cloudflare) | 104.16.x.x | Yes | ❌ No |
+ | Public IP (direct) | 8.8.8.8 | Yes | ❌ No |
+ | Spoofed XFF from public IP | 8.8.8.8 | Yes | ❌ No |
+ | TESTING=1 mode | any | No | ✅ Yes |
+
+ Note: We check REMOTE_ADDR (actual connection IP), not X-Forwarded-For.
+ This allows local proxies while blocking spoofing attacks from public IPs.
"""
assert True # Documentation/summary test