T05 · Unauthorized Access and Privilege Escalation
Error
- Location
- server/mcp_server.py:31
- Finding
- Unauthenticated Network-Facing WebSocket Hardware Bridge<![CDATA[ ## Vulnerability Details **File Location**: `server/mcp_server.py:31-33`, `server/mcp_server.py:51-99`, and `server/mcp_server.py:115-132` **Vulnerability Type**: Unauthenticated remote access to a hardware-control service **Risk Level**: High ### Vulnerable Code ```python self._runner = web.AppRunner(self._app) await self._runner.setup() site = web.TCPSite(self._runner, '0.0.0.0', port) await site.start() ``` ```python async def _handle_ws(self, request): ws = web.WebSocketResponse() await ws.prepare(request) self._clients.add(ws) client_info = f"新客户端 (现有 {len(self._clients)} 个客户端)" print(f"[WS] {client_info}") try: async for msg in ws: if msg.type == aiohttp.WSMsgType.TEXT: data = json.loads(msg.data) msg_type = data.get('type', '') if msg_type == 'status': ble = data.get('data', {}).get('bleConnected', False) bridge = data.get('data', {}).get('bridge', 'unknown') print(f"[WS] 状态更新:BLE={ble}, bridge={bridge}") gateway.ble_connected = ble if ble: gateway.mode = gateway.mode or "web" elif msg_type == 'command': cmd_id = data.get('id', '') self._pending[cmd_id] = ws print(f"[WS] 收到命令:{data.get('command', {})}") for client in self._clients: if client != ws and not client.closed: await client.send_json(data) print(f"[WS] 转发命令到客户端") elif msg_type == 'result': fid = data.get('id', '') requester_ws = self._pending.pop(fid, None) if requester_ws and not requester_ws.closed: await requester_ws.send_json(data) print(f"[WS] 转发结果给请求者") ...[truncated 2557 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. Bind the service to `127.0.0.1` by default. Require an explicit, security-conscious configuration option before listening on external interfaces. 2. Generate an unpredictable, short-lived session token when the bridge starts and require it during the WebSocket handshake. 3. Authenticate clients and assign explicit roles, such as MCP requester, mobile bridge, or browser bridge. 4. Authorize each message type according to the authenticated client role. A bridge should not automatically be permitted to act as an arbitrary command requester. 5. Validate the `Origin` header for browser clients and reject unexpected origins. 6. Use `wss://` with a valid TLS configuration for remote or local-network deployment. 7. Validate incoming JSON against strict schemas, including bounded lengths, recognized commands, parameter types, and unique command identifiers. 8. Associate pending commands with the specific bridge client that received them and accept results only from that client. 9. Add connection limits, message-size limits, timeouts, and rate limiting. 10. Document firewall requirements and warn users before exposing port 18790 outside the loopback interface. ]]>
