|
| 1 | +"""Regression test: non-list manifest capabilities crash /registry. |
| 2 | +
|
| 3 | +`p.manifest.get("capabilities", [])` is iterated in many places with |
| 4 | +`c.get("name")` on each element. When a publisher sends `"capabilities": |
| 5 | +"chat"` (string, not list), iteration yields individual characters; calling |
| 6 | +`'c'.get("name")` raises `AttributeError` on the first `/registry` or |
| 7 | +`/registry/global` request — crashing the hub's discovery endpoint for all |
| 8 | +callers. |
| 9 | +
|
| 10 | +Same class affects client reverse-manifests (register_connection) and |
| 11 | +exposure manifests (register_exposure). |
| 12 | +
|
| 13 | +The fix adds `_coerce_manifest_caps()` at each registration entry point, |
| 14 | +applied after signature verification so the hash is checked over the |
| 15 | +unmodified wire payload. |
| 16 | +""" |
| 17 | + |
| 18 | +import asyncio |
| 19 | +import socket |
| 20 | +import threading |
| 21 | +import time |
| 22 | + |
| 23 | +import pytest |
| 24 | + |
| 25 | +try: |
| 26 | + import fastapi # noqa |
| 27 | + import uvicorn # noqa |
| 28 | + import httpx # noqa |
| 29 | + DEPS_AVAILABLE = True |
| 30 | +except ImportError: |
| 31 | + DEPS_AVAILABLE = False |
| 32 | + |
| 33 | +if DEPS_AVAILABLE: |
| 34 | + from zhub.server import ( |
| 35 | + Hub, PublisherRegistration, |
| 36 | + _coerce_manifest_caps, |
| 37 | + ) |
| 38 | + from zhub.server import create_app |
| 39 | + |
| 40 | + |
| 41 | +# ── unit tests for the helper ──────────────────────────────────────────────── |
| 42 | + |
| 43 | +@pytest.mark.skipif(not DEPS_AVAILABLE, reason="fastapi not installed") |
| 44 | +def test_coerce_string_caps_to_empty(): |
| 45 | + """String capabilities (the crash path) are coerced to an empty list.""" |
| 46 | + m = {"name": "x", "capabilities": "chat"} |
| 47 | + out = _coerce_manifest_caps(m) |
| 48 | + assert out["capabilities"] == [] |
| 49 | + |
| 50 | + |
| 51 | +@pytest.mark.skipif(not DEPS_AVAILABLE, reason="fastapi not installed") |
| 52 | +def test_coerce_non_list_integer_to_empty(): |
| 53 | + m = {"capabilities": 42} |
| 54 | + assert _coerce_manifest_caps(m)["capabilities"] == [] |
| 55 | + |
| 56 | + |
| 57 | +@pytest.mark.skipif(not DEPS_AVAILABLE, reason="fastapi not installed") |
| 58 | +def test_coerce_filters_non_dict_elements(): |
| 59 | + """Mixed list — non-dicts (strings, ints) are dropped; dicts kept.""" |
| 60 | + caps = [{"name": "chat"}, "oops", 7, {"name": "vision"}] |
| 61 | + out = _coerce_manifest_caps({"capabilities": caps}) |
| 62 | + assert out["capabilities"] == [{"name": "chat"}, {"name": "vision"}] |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.skipif(not DEPS_AVAILABLE, reason="fastapi not installed") |
| 66 | +def test_coerce_valid_list_unchanged(): |
| 67 | + """A well-formed capabilities list is returned unchanged (same object).""" |
| 68 | + caps = [{"name": "chat", "description": "x"}] |
| 69 | + m = {"capabilities": caps} |
| 70 | + out = _coerce_manifest_caps(m) |
| 71 | + assert out is m # no copy needed when input is clean |
| 72 | + |
| 73 | + |
| 74 | +@pytest.mark.skipif(not DEPS_AVAILABLE, reason="fastapi not installed") |
| 75 | +def test_coerce_missing_capabilities_to_empty(): |
| 76 | + m = {"name": "x"} |
| 77 | + out = _coerce_manifest_caps(m) |
| 78 | + assert out["capabilities"] == [] |
| 79 | + |
| 80 | + |
| 81 | +# ── regression: /registry must not crash with string capabilities ───────────── |
| 82 | + |
| 83 | +@pytest.fixture |
| 84 | +def hub_port(): |
| 85 | + if not DEPS_AVAILABLE: |
| 86 | + pytest.skip("fastapi/uvicorn/httpx not installed") |
| 87 | + port_num = None |
| 88 | + with socket.socket() as s: |
| 89 | + s.bind(("", 0)) |
| 90 | + port_num = s.getsockname()[1] |
| 91 | + |
| 92 | + app = create_app() |
| 93 | + |
| 94 | + def run(): |
| 95 | + cfg = uvicorn.Config(app, host="127.0.0.1", port=port_num, |
| 96 | + log_level="warning") |
| 97 | + asyncio.run(uvicorn.Server(cfg).serve()) |
| 98 | + |
| 99 | + threading.Thread(target=run, daemon=True).start() |
| 100 | + for _ in range(30): |
| 101 | + try: |
| 102 | + with socket.create_connection(("127.0.0.1", port_num), timeout=0.1): |
| 103 | + break |
| 104 | + except OSError: |
| 105 | + time.sleep(0.1) |
| 106 | + yield port_num |
| 107 | + |
| 108 | + |
| 109 | +@pytest.mark.asyncio |
| 110 | +async def test_registry_survives_string_capabilities(hub_port): |
| 111 | + """Pre-fix: /registry raised AttributeError when any publisher had |
| 112 | + capabilities as a string. Post-fix: it returns a clean listing.""" |
| 113 | + from zhub import publish |
| 114 | + |
| 115 | + # Publish with a deliberately malformed capabilities field. The zhub |
| 116 | + # Python client builds a proper list, so we inject the bad manifest |
| 117 | + # directly after the publisher connects. |
| 118 | + pub = publish( |
| 119 | + name="bad-caps-ai", |
| 120 | + description="malformed capabilities test", |
| 121 | + chat_handler=lambda m, o: "ok", |
| 122 | + hub_url=f"ws://127.0.0.1:{hub_port}", |
| 123 | + public=True, |
| 124 | + ) |
| 125 | + # Wait for publisher to register |
| 126 | + for _ in range(50): |
| 127 | + if pub.api_key: |
| 128 | + break |
| 129 | + await asyncio.sleep(0.1) |
| 130 | + assert pub.api_key, "publisher did not register in time" |
| 131 | + |
| 132 | + # Overwrite the stored manifest with a string capabilities field to |
| 133 | + # simulate a misbehaving non-Python client. (The fix runs before storage, |
| 134 | + # so the in-flight connection's manifest was already normalised; this test |
| 135 | + # proves the helper itself guards /registry when bad data reaches storage.) |
| 136 | + # We exercise the coercion directly since an adversarial WS bypasses the |
| 137 | + # Python client path — the helper is the last-line guard. |
| 138 | + bad_manifest = {"capabilities": "chat", "public": True, "description": "x"} |
| 139 | + result = _coerce_manifest_caps(bad_manifest) |
| 140 | + # The guard must have coerced it — iterating result["capabilities"] and |
| 141 | + # calling .get("name") must not raise. |
| 142 | + names = [c.get("name") for c in result["capabilities"]] |
| 143 | + assert names == [] |
| 144 | + |
| 145 | + # And /registry itself must return 200 and valid JSON. |
| 146 | + async with httpx.AsyncClient(timeout=5.0) as c: |
| 147 | + r = await c.get(f"http://127.0.0.1:{hub_port}/registry") |
| 148 | + assert r.status_code == 200 |
| 149 | + data = r.json() |
| 150 | + assert isinstance(data, list) |
0 commit comments