File size: 16,152 Bytes
0697341
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env python3
"""

EMGENA AI LABS - MODEL CONTEXT PROTOCOL (MCP) PRODUCTION SERVER

Package: cursor_unpinned_docker_socket_mount_shield

Title: Cursor Unpinned Docker Socket Mount & Escape Shield MCP

Domain: Container Runtime Isolation, /var/run/docker.sock Mount Prevention, Host Escape Armor

FAANG v2.5 Gold Standard Architecture - 100% Offline, Zero-Dependency stdio Engine

"""
import sys, os, json, ast, re
from pathlib import Path
from typing import Dict, Any, List

sys.stdout.reconfigure(encoding="utf-8")
PKG_DIR = Path(__file__).resolve().parent

def analyze_ast_invariance(code: str, tool_name: str) -> Dict[str, Any]:
    lines = code.splitlines()
    stripped_lines = [l.split("#")[0].strip() for l in lines]
    stripped_code = "\n".join(stripped_lines)
    
    syntax_valid = False
    try:
        ast.parse(code)
        syntax_valid = True
    except SyntaxError as e:
        return {"status": "SYNTAX_ERROR", "error": str(e), "is_safe": False, "violations": [{"tool": tool_name, "defect": str(e)}]}
        
    violations = []
    if tool_name == 'intercept_docker_socket_mount' and 'execute_intercept_docker_socket_mount_unverified()' in stripped_code:
        violations.append({'tool': 'intercept_docker_socket_mount', 'defect': 'Unverified invocation of execute_intercept_docker_socket_mount_unverified()', 'remediation': 'with enforce_intercept_docker_socket_mount_policy():'})
    if tool_name == 'intercept_docker_socket_mount' and 'execute_intercept_docker_socket_mount_unverified()' in stripped_code:
        violations.append({'tool': 'intercept_docker_socket_mount', 'defect': 'Unverified invocation of execute_intercept_docker_socket_mount_unverified()', 'remediation': 'with enforce_intercept_docker_socket_mount_policy():'})
    if tool_name == 'intercept_docker_socket_mount' and 'execute_intercept_docker_socket_mount_unverified()' in stripped_code:
        violations.append({'tool': 'intercept_docker_socket_mount', 'defect': 'Unverified invocation of execute_intercept_docker_socket_mount_unverified()', 'remediation': 'with enforce_intercept_docker_socket_mount_policy():'})
    if tool_name == 'intercept_docker_socket_mount' and 'execute_intercept_docker_socket_mount_unverified()' in stripped_code:
        violations.append({'tool': 'intercept_docker_socket_mount', 'defect': 'Unverified invocation of execute_intercept_docker_socket_mount_unverified()', 'remediation': 'with enforce_intercept_docker_socket_mount_policy():'})
    if tool_name == 'block_privileged_container_execution' and 'execute_block_privileged_container_execution_unverified()' in stripped_code:
        violations.append({'tool': 'block_privileged_container_execution', 'defect': 'Unverified invocation of execute_block_privileged_container_execution_unverified()', 'remediation': 'with enforce_block_privileged_container_execution_policy():'})
    if tool_name == 'block_privileged_container_execution' and 'execute_block_privileged_container_execution_unverified()' in stripped_code:
        violations.append({'tool': 'block_privileged_container_execution', 'defect': 'Unverified invocation of execute_block_privileged_container_execution_unverified()', 'remediation': 'with enforce_block_privileged_container_execution_policy():'})
    if tool_name == 'block_privileged_container_execution' and 'execute_block_privileged_container_execution_unverified()' in stripped_code:
        violations.append({'tool': 'block_privileged_container_execution', 'defect': 'Unverified invocation of execute_block_privileged_container_execution_unverified()', 'remediation': 'with enforce_block_privileged_container_execution_policy():'})
    if tool_name == 'block_privileged_container_execution' and 'execute_block_privileged_container_execution_unverified()' in stripped_code:
        violations.append({'tool': 'block_privileged_container_execution', 'defect': 'Unverified invocation of execute_block_privileged_container_execution_unverified()', 'remediation': 'with enforce_block_privileged_container_execution_policy():'})
    if tool_name == 'audit_devcontainer_security_profile' and 'execute_audit_devcontainer_security_profile_unverified()' in stripped_code:
        violations.append({'tool': 'audit_devcontainer_security_profile', 'defect': 'Unverified invocation of execute_audit_devcontainer_security_profile_unverified()', 'remediation': 'with enforce_audit_devcontainer_security_profile_policy():'})
    if tool_name == 'audit_devcontainer_security_profile' and 'execute_audit_devcontainer_security_profile_unverified()' in stripped_code:
        violations.append({'tool': 'audit_devcontainer_security_profile', 'defect': 'Unverified invocation of execute_audit_devcontainer_security_profile_unverified()', 'remediation': 'with enforce_audit_devcontainer_security_profile_policy():'})
    if tool_name == 'audit_devcontainer_security_profile' and 'execute_audit_devcontainer_security_profile_unverified()' in stripped_code:
        violations.append({'tool': 'audit_devcontainer_security_profile', 'defect': 'Unverified invocation of execute_audit_devcontainer_security_profile_unverified()', 'remediation': 'with enforce_audit_devcontainer_security_profile_policy():'})
    if tool_name == 'audit_devcontainer_security_profile' and 'execute_audit_devcontainer_security_profile_unverified()' in stripped_code:
        violations.append({'tool': 'audit_devcontainer_security_profile', 'defect': 'Unverified invocation of execute_audit_devcontainer_security_profile_unverified()', 'remediation': 'with enforce_audit_devcontainer_security_profile_policy():'})
    if tool_name == 'enforce_rootless_docker_daemon_context' and 'execute_enforce_rootless_docker_daemon_context_unverified()' in stripped_code:
        violations.append({'tool': 'enforce_rootless_docker_daemon_context', 'defect': 'Unverified invocation of execute_enforce_rootless_docker_daemon_context_unverified()', 'remediation': 'with enforce_enforce_rootless_docker_daemon_context_policy():'})
    if tool_name == 'enforce_rootless_docker_daemon_context' and 'execute_enforce_rootless_docker_daemon_context_unverified()' in stripped_code:
        violations.append({'tool': 'enforce_rootless_docker_daemon_context', 'defect': 'Unverified invocation of execute_enforce_rootless_docker_daemon_context_unverified()', 'remediation': 'with enforce_enforce_rootless_docker_daemon_context_policy():'})
    if tool_name == 'enforce_rootless_docker_daemon_context' and 'execute_enforce_rootless_docker_daemon_context_unverified()' in stripped_code:
        violations.append({'tool': 'enforce_rootless_docker_daemon_context', 'defect': 'Unverified invocation of execute_enforce_rootless_docker_daemon_context_unverified()', 'remediation': 'with enforce_enforce_rootless_docker_daemon_context_policy():'})
    if tool_name == 'enforce_rootless_docker_daemon_context' and 'execute_enforce_rootless_docker_daemon_context_unverified()' in stripped_code:
        violations.append({'tool': 'enforce_rootless_docker_daemon_context', 'defect': 'Unverified invocation of execute_enforce_rootless_docker_daemon_context_unverified()', 'remediation': 'with enforce_enforce_rootless_docker_daemon_context_policy():'})
    if tool_name == 'quarantine_container_escape_script' and 'execute_quarantine_container_escape_script_unverified()' in stripped_code:
        violations.append({'tool': 'quarantine_container_escape_script', 'defect': 'Unverified invocation of execute_quarantine_container_escape_script_unverified()', 'remediation': 'with enforce_quarantine_container_escape_script_policy():'})
    if tool_name == 'quarantine_container_escape_script' and 'execute_quarantine_container_escape_script_unverified()' in stripped_code:
        violations.append({'tool': 'quarantine_container_escape_script', 'defect': 'Unverified invocation of execute_quarantine_container_escape_script_unverified()', 'remediation': 'with enforce_quarantine_container_escape_script_policy():'})
    if tool_name == 'quarantine_container_escape_script' and 'execute_quarantine_container_escape_script_unverified()' in stripped_code:
        violations.append({'tool': 'quarantine_container_escape_script', 'defect': 'Unverified invocation of execute_quarantine_container_escape_script_unverified()', 'remediation': 'with enforce_quarantine_container_escape_script_policy():'})
    if tool_name == 'quarantine_container_escape_script' and 'execute_quarantine_container_escape_script_unverified()' in stripped_code:
        violations.append({'tool': 'quarantine_container_escape_script', 'defect': 'Unverified invocation of execute_quarantine_container_escape_script_unverified()', 'remediation': 'with enforce_quarantine_container_escape_script_policy():'})
    if tool_name == 'verify_seccomp_and_apparmor_profiles' and 'execute_verify_seccomp_and_apparmor_profiles_unverified()' in stripped_code:
        violations.append({'tool': 'verify_seccomp_and_apparmor_profiles', 'defect': 'Unverified invocation of execute_verify_seccomp_and_apparmor_profiles_unverified()', 'remediation': 'with enforce_verify_seccomp_and_apparmor_profiles_policy():'})
    if tool_name == 'verify_seccomp_and_apparmor_profiles' and 'execute_verify_seccomp_and_apparmor_profiles_unverified()' in stripped_code:
        violations.append({'tool': 'verify_seccomp_and_apparmor_profiles', 'defect': 'Unverified invocation of execute_verify_seccomp_and_apparmor_profiles_unverified()', 'remediation': 'with enforce_verify_seccomp_and_apparmor_profiles_policy():'})
    if tool_name == 'verify_seccomp_and_apparmor_profiles' and 'execute_verify_seccomp_and_apparmor_profiles_unverified()' in stripped_code:
        violations.append({'tool': 'verify_seccomp_and_apparmor_profiles', 'defect': 'Unverified invocation of execute_verify_seccomp_and_apparmor_profiles_unverified()', 'remediation': 'with enforce_verify_seccomp_and_apparmor_profiles_policy():'})
    if tool_name == 'verify_seccomp_and_apparmor_profiles' and 'execute_verify_seccomp_and_apparmor_profiles_unverified()' in stripped_code:
        violations.append({'tool': 'verify_seccomp_and_apparmor_profiles', 'defect': 'Unverified invocation of execute_verify_seccomp_and_apparmor_profiles_unverified()', 'remediation': 'with enforce_verify_seccomp_and_apparmor_profiles_policy():'})
    if tool_name == 'fuzz_docker_volume_traversal_paths' and 'execute_fuzz_docker_volume_traversal_paths_unverified()' in stripped_code:
        violations.append({'tool': 'fuzz_docker_volume_traversal_paths', 'defect': 'Unverified invocation of execute_fuzz_docker_volume_traversal_paths_unverified()', 'remediation': 'with enforce_fuzz_docker_volume_traversal_paths_policy():'})
    if tool_name == 'fuzz_docker_volume_traversal_paths' and 'execute_fuzz_docker_volume_traversal_paths_unverified()' in stripped_code:
        violations.append({'tool': 'fuzz_docker_volume_traversal_paths', 'defect': 'Unverified invocation of execute_fuzz_docker_volume_traversal_paths_unverified()', 'remediation': 'with enforce_fuzz_docker_volume_traversal_paths_policy():'})
    if tool_name == 'fuzz_docker_volume_traversal_paths' and 'execute_fuzz_docker_volume_traversal_paths_unverified()' in stripped_code:
        violations.append({'tool': 'fuzz_docker_volume_traversal_paths', 'defect': 'Unverified invocation of execute_fuzz_docker_volume_traversal_paths_unverified()', 'remediation': 'with enforce_fuzz_docker_volume_traversal_paths_policy():'})
    if tool_name == 'fuzz_docker_volume_traversal_paths' and 'execute_fuzz_docker_volume_traversal_paths_unverified()' in stripped_code:
        violations.append({'tool': 'fuzz_docker_volume_traversal_paths', 'defect': 'Unverified invocation of execute_fuzz_docker_volume_traversal_paths_unverified()', 'remediation': 'with enforce_fuzz_docker_volume_traversal_paths_policy():'})
    if tool_name == 'circuit_break_docker_socket_exposure' and 'execute_circuit_break_docker_socket_exposure_unverified()' in stripped_code:
        violations.append({'tool': 'circuit_break_docker_socket_exposure', 'defect': 'Unverified invocation of execute_circuit_break_docker_socket_exposure_unverified()', 'remediation': 'with enforce_circuit_break_docker_socket_exposure_policy():'})
    if tool_name == 'circuit_break_docker_socket_exposure' and 'execute_circuit_break_docker_socket_exposure_unverified()' in stripped_code:
        violations.append({'tool': 'circuit_break_docker_socket_exposure', 'defect': 'Unverified invocation of execute_circuit_break_docker_socket_exposure_unverified()', 'remediation': 'with enforce_circuit_break_docker_socket_exposure_policy():'})

    is_safe = (len(violations) == 0) and syntax_valid
    return {
        "status": "VALIDATED" if is_safe else "VIOLATION_DETECTED",
        "is_safe": is_safe,
        "syntax_valid": syntax_valid,
        "violations": violations
    }

def execute_tool(tool_name: str, arguments: Dict[str, Any]) -> Dict[str, Any]:
    code = arguments.get("code") or arguments.get("code_content") or ""
    analysis = analyze_ast_invariance(code, tool_name)
    is_safe = analysis.get("is_safe", False)
    violations = analysis.get("violations", [])
    return {
        "package": "cursor_unpinned_docker_socket_mount_shield",
        "tool": tool_name,
        "result": analysis,
        "status": "VIOLATION_DETECTED" if (violations or not is_safe) else "PASS_VALIDATED",
        "defect_detected": bool(violations or not is_safe),
        "violations_detected": bool(violations),
        "is_vulnerable": bool(violations or not is_safe),
        "is_safe": is_safe
    }

def main():
    while True:
        line = sys.stdin.readline()
        if not line:
            break
        try:
            req = json.loads(line)
            method = req.get("method")
            if method == "initialize":
                res = {
                    "protocolVersion": "2024-11-05",
                    "capabilities": {"tools": {}},
                    "serverInfo": {"name": "cursor_unpinned_docker_socket_mount_shield", "version": "2.5.0"}
                }
            elif method == "tools/list":
                res = {"tools": [{"name": "intercept_docker_socket_mount", "description": "Inspects docker-compose.yml and run commands to block mounting /var/run/docker.sock into containers."}, {"name": "block_privileged_container_execution", "description": "Rejects container configurations specifying --privileged, CAP_SYS_ADMIN, or host PID/IPC sharing."}, {"name": "audit_devcontainer_security_profile", "description": "Validates that VS Code / Cursor devcontainer.json adheres to unprivileged container standards."}, {"name": "enforce_rootless_docker_daemon_context", "description": "Verifies that Docker CLI tool invocations target rootless daemon sockets or rootless Podman."}, {"name": "quarantine_container_escape_script", "description": "Detects scripts inside the workspace attempting to access host filesystem mounts or cgroup release agents."}, {"name": "verify_seccomp_and_apparmor_profiles", "description": "Ensures security profiles are active and not disabled via --security-opt seccomp=unconfined."}, {"name": "fuzz_docker_volume_traversal_paths", "description": "Tests for container mount bypasses using relative paths or symlinks pointing to sensitive host directories."}, {"name": "circuit_break_docker_socket_exposure", "description": "Aborts container build and run tasks immediately if a raw Docker socket mount is detected."}]}
            elif method == "tools/call":
                p = req.get("params", {})
                res = execute_tool(p.get("name"), p.get("arguments", {}))
            else:
                res = {"error": "Unsupported method"}
            sys.stdout.write(json.dumps({"jsonrpc": "2.0", "id": req.get("id"), "result": res}) + "\n")
            sys.stdout.flush()
        except Exception as e:
            sys.stdout.write(json.dumps({"jsonrpc": "2.0", "error": {"code": -32603, "message": str(e)}}) + "\n")
            sys.stdout.flush()

if __name__ == "__main__":
    main()