File size: 2,629 Bytes
3dfb537
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from helpers.extension import Extension
from helpers import dotenv
import re


class UnsecuredConnectionCheck(Extension):
    """Check: non-local without credentials, or credentials over non-HTTPS."""

    async def execute(self, banners: list = [], frontend_context: dict = {}, **kwargs):
        hostname = frontend_context.get("hostname", "")
        protocol = frontend_context.get("protocol", "")
        
        auth_login = dotenv.get_dotenv_value(dotenv.KEY_AUTH_LOGIN, "")
        auth_password = dotenv.get_dotenv_value(dotenv.KEY_AUTH_PASSWORD, "")
        has_credentials = bool(auth_login and auth_login.strip() and auth_password and auth_password.strip())
        
        is_local = self._is_localhost(hostname)
        is_https = protocol == "https:"
        
        if not is_local and not has_credentials:
            banners.append({
                "id": "unsecured-connection",
                "type": "warning",
                "priority": 80,
                "title": "Unsecured Connection",
                "html": """You are accessing Agent Zero from a non-local address without authentication. 
                         <a href="#" onclick="document.getElementById('settings').click(); return false;">
                         Configure credentials</a> in Settings → External Services → Authentication.""",
                "dismissible": True,
                "source": "backend"
            })
        
        if has_credentials and not is_local and not is_https:
            banners.append({
                "id": "credentials-unencrypted",
                "type": "warning", 
                "priority": 90,
                "title": "Credentials May Be Sent Unencrypted",
                "html": """Your connection is not using HTTPS. Login credentials may be transmitted in plain text. 
                         Consider using HTTPS or a secure tunnel.""",
                "dismissible": True,
                "source": "backend"
            })

    def _is_localhost(self, hostname: str) -> bool:
        local_patterns = ["localhost", "127.0.0.1", "::1", "0.0.0.0"]
        
        if hostname in local_patterns:
            return True
        
        # RFC1918 private ranges
        if re.match(r"^192\.168\.\d{1,3}\.\d{1,3}$", hostname):
            return True
        if re.match(r"^10\.\d{1,3}\.\d{1,3}\.\d{1,3}$", hostname):
            return True
        if re.match(r"^172\.(1[6-9]|2\d|3[01])\.\d{1,3}\.\d{1,3}$", hostname):
            return True
        
        # .local domains
        if hostname.endswith(".local"):
            return True
        
        return False