| 1 | # ws.py DOX |
| 2 | |
| 3 | ## Purpose |
| 4 | |
| 5 | - Own the `ws.py` helper module. |
| 6 | - This module defines WebSocket handler registration, origin validation, and security checks. |
| 7 | - Keep this file-level DOX profile synchronized with `ws.py` because this directory is intentionally flat. |
| 8 | |
| 9 | ## Ownership |
| 10 | |
| 11 | - `ws.py` owns the runtime implementation. |
| 12 | - `ws.py.dox.md` owns durable notes about responsibilities, contracts, side effects, and verification for that implementation. |
| 13 | - Classes: |
| 14 | - `ConnectionNotFoundError` (`RuntimeError`) |
| 15 | - `_SecurityContext` (no explicit base class) |
| 16 | - `WsHandler` (no explicit base class) |
| 17 | - `namespace(self) -> str` |
| 18 | - `manager(self) -> 'WsManager'` |
| 19 | - `identifier(self) -> str` |
| 20 | - `bind_manager(self, manager: 'WsManager', namespace: str | None=...) -> None` |
| 21 | - `requires_loopback(cls) -> bool` |
| 22 | - `requires_api_key(cls) -> bool` |
| 23 | - `requires_auth(cls) -> bool` |
| 24 | - `requires_csrf(cls) -> bool` |
| 25 | - Top-level functions: |
| 26 | - `_ws_debug_enabled() -> bool`: Check A0_WS_DEBUG env var - lightweight, no heavy imports. |
| 27 | - `ws_debug(message: str) -> None`: Log *message* via :class:`PrintStyle` when ``A0_WS_DEBUG`` is active. |
| 28 | - `_default_port_for_scheme(scheme: str) -> int | None` |
| 29 | - `normalize_origin(value: Any) -> str | None`: Normalize an Origin/Referer header value to scheme://host[:port]. |
| 30 | - `_parse_host_header(value: Any) -> tuple[str | None, int | None]` |
| 31 | - `validate_ws_origin(environ: dict[str, Any]) -> tuple[bool, str | None]`: Validate the browser Origin during the Socket.IO handshake. |
| 32 | - `_check_security(handler_cls: type[WsHandler], ctx: _SecurityContext) -> dict[str, Any] | None`: Return an error payload dict if the check fails, or ``None`` on success. |
| 33 | - `register_ws_namespace(socketio_server: socketio.AsyncServer, webapp: Flask, lock: ThreadLockType, manager: 'WsManager | None'=...) -> None` |
| 34 | - `_error_response(code: str, message: str, correlation_id: str) -> dict[str, Any]` |
| 35 | - Notable constants/configuration names: `NAMESPACE`, `CACHE_AREA`. |
| 36 | |
| 37 | ## Runtime Contracts |
| 38 | |
| 39 | - Helper modules own reusable framework APIs and must preserve public callers unless all callers, tests, and docs are updated together. |
| 40 | - Update this file whenever public functions, classes, persistence behavior, path/security assumptions, side effects, or cross-module contracts change. |
| 41 | - `WsHandler` defines `process(...)`. |
| 42 | - `WsHandler` defines `requires_auth(...)`. |
| 43 | - `WsHandler` defines `requires_csrf(...)`. |
| 44 | - `WsHandler` defines `requires_api_key(...)`. |
| 45 | - `WsHandler` defines `requires_loopback(...)`. |
| 46 | - Observed side-effect areas: filesystem reads, filesystem deletion, network calls, WebSocket state, plugin state, settings/state persistence, secret handling. |
| 47 | - Imported dependency areas include: `abc`, `dataclasses`, `flask`, `helpers`, `helpers.errors`, `helpers.network`, `helpers.print_style`, `os`, `pathlib`, `socketio`, `threading`, `typing`, `urllib.parse`, `uuid`. |
| 48 | |
| 49 | ## Key Concepts |
| 50 | |
| 51 | - Important called helpers/classes observed in the source: `threading.Lock`, `os.getenv.strip.lower`, `_ws_debug_enabled`, `urlparse`, `normalize_origin`, `_parse_host_header`, `handler_cls.requires_loopback`, `handler_cls.requires_auth`, `handler_cls.requires_csrf`, `handler_cls.requires_api_key`, `socketio_server.on`, `PrintStyle.debug`, `value.strip`, `origin_parsed.hostname.lower`, `_default_port_for_scheme`, `req_host.lower`, `forwarded_host_raw.strip`, `forwarded_host_raw.split.strip`, `forwarded_proto_raw.strip`, `forwarded_proto_raw.split.strip.lower`. |
| 52 | - Keep request/response, tool, or helper semantics documented here at the same time as source changes. |
| 53 | |
| 54 | ## Work Guidance |
| 55 | |
| 56 | - Preserve public helper APIs used by core code and plugins unless every caller is updated. |
| 57 | - Keep path, auth, secret, persistence, network, and subprocess behavior explicit and bounded. |
| 58 | - Prefer adding cohesive helper functions here only when behavior is reused across modules. |
| 59 | |
| 60 | ## Verification |
| 61 | |
| 62 | - Run targeted tests for changed helper behavior; run security regressions for auth, filesystem, WebSocket, tunnel, upload, or secret-handling helpers. |
| 63 | - Related tests observed by source search: |
| 64 | - `tests/test_a0_connector_computer_use_metadata.py` |
| 65 | - `tests/test_a0_connector_prompt_gating.py` |
| 66 | - `tests/test_browser_agent_regressions.py` |
| 67 | - `tests/test_docker_release_plan.py` |
| 68 | - `tests/test_download_toast_regressions.py` |
| 69 | - `tests/test_git_version_label.py` |
| 70 | - `tests/test_host_browser_connector.py` |
| 71 | - `tests/test_multi_tab_isolation.py` |
| 72 | |
| 73 | ## Child DOX Index |
| 74 | |
| 75 | No child DOX files. |