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.

refactor: move is_private_ip to security/network_utils.py

LearningCircuitfcaf5292f521
diff --git a/src/local_deep_research/security/network_utils.py b/src/local_deep_research/security/network_utils.py
new file mode 100644
index 0000000000..f998785a48
--- /dev/null
+++ b/src/local_deep_research/security/network_utils.py
@@ -0,0 +1,52 @@
+"""Network utility functions for IP address classification.
+
+This module provides utilities for classifying IP addresses and hostnames
+as private/local vs public. Used for URL normalization and security validation.
+"""
+
+import ipaddress
+
+
+def is_private_ip(hostname: str) -> bool:
+ """Check if hostname is a private/local IP address.
+
+ Recognizes:
+ - Localhost values (127.0.0.1, localhost, [::1], 0.0.0.0)
+ - RFC 1918 private IPv4 ranges (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16)
+ - IPv6 private addresses (fc00::/7, fe80::/10)
+ - mDNS .local domains
+
+ Args:
+ hostname: The hostname or IP address to check
+
+ Returns:
+ True if the hostname is a private/local address, False otherwise
+
+ Examples:
+ >>> is_private_ip("192.168.1.100")
+ True
+ >>> is_private_ip("172.16.0.50")
+ True
+ >>> is_private_ip("10.0.0.1")
+ True
+ >>> is_private_ip("8.8.8.8")
+ False
+ >>> is_private_ip("api.openai.com")
+ False
+ """
+ # Known localhost values
+ if hostname in ("localhost", "127.0.0.1", "[::1]", "0.0.0.0"):
+ return True
+
+ # Handle bracketed IPv6
+ if hostname.startswith("[") and hostname.endswith("]"):
+ hostname = hostname[1:-1]
+
+ try:
+ ip = ipaddress.ip_address(hostname)
+ # Check if private (includes 10.x, 172.16-31.x, 192.168.x, fc00::/7, etc.)
+ # Also check loopback and link-local
+ return ip.is_private or ip.is_loopback or ip.is_link_local
+ except ValueError:
+ # Not a valid IP address, check for .local domain (mDNS)
+ return hostname.endswith(".local")
diff --git a/src/local_deep_research/utilities/url_utils.py b/src/local_deep_research/utilities/url_utils.py
index d9305a6b17..3be377d03a 100644
--- a/src/local_deep_research/utilities/url_utils.py
+++ b/src/local_deep_research/utilities/url_utils.py
@@ -1,33 +1,9 @@
"""URL utility functions for the local deep research application."""
-import ipaddress
+from ..security.network_utils import is_private_ip
-
-def is_private_ip(hostname: str) -> bool:
- """Check if hostname is a private/local IP address.
-
- Args:
- hostname: The hostname or IP address to check
-
- Returns:
- True if the hostname is a private/local address, False otherwise
- """
- # Known localhost values
- if hostname in ("localhost", "127.0.0.1", "[::1]", "0.0.0.0"):
- return True
-
- # Handle bracketed IPv6
- if hostname.startswith("[") and hostname.endswith("]"):
- hostname = hostname[1:-1]
-
- try:
- ip = ipaddress.ip_address(hostname)
- # Check if private (includes 10.x, 172.16-31.x, 192.168.x, fc00::/7, etc.)
- # Also check loopback and link-local
- return ip.is_private or ip.is_loopback or ip.is_link_local
- except ValueError:
- # Not a valid IP address, check for .local domain (mDNS)
- return hostname.endswith(".local")
+# Re-export for backwards compatibility
+__all__ = ["normalize_url", "is_private_ip"]
def normalize_url(raw_url: str) -> str: