{
  "schema_version": 1,
  "diagram_type": "architecture",
  "meta": {
    "title": "Dify Plugin Daemon, Sandbox, and SSRF Proxy Boundaries",
    "subtitle": "langgenius/dify @ 8394eb1 · fixed-commit static source & Compose wiring",
    "output": "docs/cases/dify-plugin-sandbox.architecture.html",
    "visual_preset": "signal-flow",
    "quality_profile": "showcase",
    "repository": {
      "url": "https://github.com/langgenius/dify",
      "revision": "8394eb190b16544e0d6c8b005fd18d7f07a00e71"
    }
  },
  "components": [
    {
      "id": "nginx",
      "type": "frontend",
      "label": "Nginx Edge",
      "sublabel": "/console/api, /api, /v1 → api; /e/ → plugin_daemon",
      "pos": [40, 170],
      "size": [250, 88],
      "sources": [
        { "path": "docker/nginx/conf.d/default.conf.template", "line": 8, "label": "API upstream routes" },
        { "path": "docker/nginx/conf.d/default.conf.template", "line": 47, "label": "direct /e/ proxy to plugin_daemon:5002" }
      ]
    },
    {
      "id": "plugin_daemon",
      "type": "backend",
      "label": "Plugin Daemon",
      "sublabel": "plugin_daemon:5002 · ssrf_proxy_network + default",
      "pos": [430, 60],
      "size": [270, 92],
      "tag": "dual-network",
      "sources": [
        { "path": "docker/docker-compose.yaml", "line": 657, "label": "plugin_daemon service & networks" },
        { "path": "docker/envs/core-services/plugin-daemon.defaults", "line": 69, "label": "DIFY_INNER_API_URL → api:5001" }
      ]
    },
    {
      "id": "api_worker",
      "type": "backend",
      "label": "Dify API & Celery Workers",
      "sublabel": "api:5001 · ssrf_proxy_network + default",
      "pos": [430, 260],
      "size": [270, 92],
      "tag": "dual-network",
      "sources": [
        { "path": "docker/docker-compose.yaml", "line": 76, "label": "api & worker networks" },
        { "path": "api/core/plugin/impl/base.py", "line": 76, "label": "plugin httpx client (trust_env=False)" },
        { "path": "api/core/helper/code_executor/code_executor.py", "line": 43, "label": "sandbox httpx client (inherits env proxy)" }
      ]
    },
    {
      "id": "sandbox",
      "type": "security",
      "label": "Dify Code Sandbox",
      "sublabel": "sandbox:8194 · ssrf_proxy_network only (internal: true)",
      "pos": [960, 260],
      "size": [280, 92],
      "tag": "isolated network",
      "sources": [
        { "path": "docker/docker-compose.yaml", "line": 604, "label": "sandbox network isolation" },
        { "path": "docker/envs/core-services/sandbox.defaults", "line": 5, "label": "HTTP(S)_PROXY → ssrf_proxy:3128" }
      ]
    },
    {
      "id": "ssrf_proxy",
      "type": "security",
      "label": "Squid SSRF Proxy",
      "sublabel": "ssrf_proxy:3128 · denies to_private_networks",
      "pos": [1380, 260],
      "size": [260, 92],
      "sources": [
        { "path": "docker/docker-compose.yaml", "line": 840, "label": "ssrf_proxy service" },
        { "path": "docker/ssrf_proxy/squid.conf.template", "line": 13, "label": "deny private CIDRs, allow marketplace" }
      ]
    },
    {
      "id": "local_sandbox",
      "type": "security",
      "label": "Agent Local Sandbox",
      "sublabel": "local_sandbox:5004 (shellctl) · separate proxy plane",
      "pos": [960, 460],
      "size": [280, 92],
      "tag": "separate sandbox",
      "sources": [
        { "path": "docker/docker-compose.yaml", "line": 631, "label": "local_sandbox shellctl & proxy env" },
        { "path": "docker/docker-compose.yaml", "line": 1426, "label": "local_sandbox_proxy_network (internal: true)" }
      ]
    },
    {
      "id": "agent_ssrf_proxy",
      "type": "security",
      "label": "Agent SSRF Proxy",
      "sublabel": "agent_ssrf_proxy:3128 · dedicated Squid for shellctl",
      "pos": [1380, 460],
      "size": [260, 92],
      "sources": [
        { "path": "docker/docker-compose.yaml", "line": 813, "label": "dedicated agent_ssrf_proxy service" }
      ]
    }
  ],
  "boundaries": [
    {
      "kind": "region",
      "label": "Core control plane · default + ssrf_proxy_network",
      "wraps": ["nginx", "plugin_daemon", "api_worker"]
    },
    {
      "kind": "security-group",
      "label": "ssrf_proxy_network (internal: true) · Code execution egress gate",
      "wraps": ["sandbox", "ssrf_proxy"]
    },
    {
      "kind": "security-group",
      "label": "Dedicated Agent shell sandbox plane",
      "wraps": ["local_sandbox", "agent_ssrf_proxy"]
    }
  ],
  "connections": [
    {
      "id": "nginx-to-plugin",
      "from": "nginx",
      "to": "plugin_daemon",
      "label": "/e/ bypasses API (Dify-Hook-Url)",
      "variant": "security",
      "fromSide": "top",
      "toSide": "left"
    },
    {
      "id": "nginx-to-api",
      "from": "nginx",
      "to": "api_worker",
      "label": "/console/api, /api, /v1 → api:5001",
      "variant": "emphasis",
      "fromSide": "bottom",
      "toSide": "left"
    },
    {
      "id": "api-to-plugin",
      "from": "api_worker",
      "to": "plugin_daemon",
      "label": "PLUGIN_DAEMON_URL (trust_env=False)",
      "variant": "emphasis",
      "fromSide": "top",
      "toSide": "bottom"
    },
    {
      "id": "plugin-to-api",
      "from": "plugin_daemon",
      "to": "api_worker",
      "label": "DIFY_INNER_API_URL callback",
      "variant": "dashed",
      "fromSide": "right",
      "toSide": "top"
    },
    {
      "id": "api-to-sandbox",
      "from": "api_worker",
      "to": "sandbox",
      "label": "POST /v1/sandbox/run (env proxy active)",
      "variant": "emphasis",
      "fromSide": "right",
      "toSide": "left"
    },
    {
      "id": "sandbox-to-ssrf",
      "from": "sandbox",
      "to": "ssrf_proxy",
      "label": "HTTP(S)_PROXY :3128",
      "variant": "security",
      "fromSide": "right",
      "toSide": "left"
    },
    {
      "id": "local-sandbox-to-agent-proxy",
      "from": "local_sandbox",
      "to": "agent_ssrf_proxy",
      "label": "HTTP(S)_PROXY :3128",
      "variant": "security",
      "fromSide": "right",
      "toSide": "left"
    }
  ],
  "cards": [
    {
      "dot": "cyan",
      "title": "Public /e/ bypasses API",
      "items": [
        "nginx default.conf.template:47-52 routes /e/ directly to plugin_daemon:5002",
        "Plugin Daemon also calls back into api:5001 via DIFY_INNER_API_URL"
      ]
    },
    {
      "dot": "amber",
      "title": "Asymmetric httpx proxy handling",
      "items": [
        "api/core/plugin/impl/base.py:78 sets trust_env=False (ignores HTTP_PROXY)",
        "code_executor.py:44-47 omits trust_env=False when calling sandbox:8194"
      ]
    },
    {
      "dot": "emerald",
      "title": "Two separate sandbox & proxy planes",
      "items": [
        "sandbox sits only on internal ssrf_proxy_network → ssrf_proxy:3128",
        "local_sandbox (shellctl:5004) uses dedicated agent_ssrf_proxy:3128"
      ]
    }
  ]
}
