The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.
TypeScript ai-videocomfyuicreative-toolsgenerative-aiideogramimage-generationimage-to-3dimage-to-videoltx-videominimax-h3nextjsopen-sourceopen-weightsqwen-imagetext-to-imagevideo-generationwan
Fuobe Connect: answer the guard question, and test it as ComfyUI loads it
Adarsh8c5bbf91b062
diff --git a/integrations/fuobe-connect/fuobe_connect/middleware.py b/integrations/fuobe-connect/fuobe_connect/middleware.pyindex 1fbe2dbb..10fba8b2 100644--- a/integrations/fuobe-connect/fuobe_connect/middleware.py+++ b/integrations/fuobe-connect/fuobe_connect/middleware.py@@ -67,6 +67,18 @@ def preflight(request: web.Request, origin: str) -> web.Response | None:return web.Response(status=204, headers=headers)+GUARD_PATHS = ("/fuobe/guard", "/api/fuobe/guard")+++async def _pass(request: web.Request, handler):+ """The request goes on to ComfyUI, except Fuobe's guard question: Fuobe Connect answers it (it keeps other websites+ out, as the guard did), so a Fuobe page that asks for the guard hears yes. Answered here, never as a route, so a+ guard file installed beside it can't clash with it."""+ if request.method == "GET" and request.path in GUARD_PATHS:+ return web.json_response({"guard": 1, "connect": 1})+ return await handler(request)++def make_middleware(trusted: Callable[[], Iterable[str]], cors_flag: str | None = None):"""`trusted`: the trusted origins now (read on each request, so a change applies at once).`cors_flag`: ComfyUI's --enable-cors-header value, when it was started with one."""@@ -89,7 +101,7 @@ def make_middleware(trusted: Callable[[], Iterable[str]], cors_flag: str | Noneheaders.popall("Origin", None)headers.popall("Sec-Fetch-Site", None)headers[MARKER] = origin- response = await handler(request.clone(headers=headers))+ response = await _pass(request.clone(headers=headers), handler)if isinstance(response, web.StreamResponse) and not response.prepared:response.headers.update(_cors_headers(origin))return response@@ -105,6 +117,9 @@ def make_middleware(trusted: Callable[[], Iterable[str]], cors_flag: str | Noneheaders = request.headers.copy()headers.popall(MARKER, None)request = request.clone(headers=headers)- return await handler(request)+ # Another website's request meets ComfyUI's own check first, the guard question too.+ if raw is not None and not same_host(raw, request.headers.get("Host")):+ return await handler(request)+ return await _pass(request, handler)return fuobe_connectdiff --git a/integrations/fuobe-connect/tests/test_middleware.py b/integrations/fuobe-connect/tests/test_middleware.pyindex 63469372..ef4cbaeb 100644--- a/integrations/fuobe-connect/tests/test_middleware.py+++ b/integrations/fuobe-connect/tests/test_middleware.py@@ -217,6 +217,18 @@ class DirectModeTest(unittest.TestCase):run(go())+ def test_answers_the_guard_question(self):+ async def go():+ app, _ = with_connect()+ async with Server(app) as s:+ status, h, body = await s.req("GET", "/fuobe/guard", browser(FUOBE))+ self.assertEqual((status, h.get("Access-Control-Allow-Origin")), (200, FUOBE))+ self.assertIn(b'"guard": 1', body)+ status, _, _ = await s.req("GET", "/fuobe/guard", browser(EVIL))+ self.assertEqual(status, 403)++ run(go())+def test_status_says_less_to_a_studio(self):async def go():app, _ = with_connect()diff --git a/integrations/fuobe-connect/tests/test_plugin.py b/integrations/fuobe-connect/tests/test_plugin.pynew file mode 100644index 00000000..6df78131--- /dev/null+++ b/integrations/fuobe-connect/tests/test_plugin.py@@ -0,0 +1,155 @@+"""Fuobe Connect as ComfyUI loads it: __init__.py run against a stand-in of ComfyUI's server module (its PromptServer+with ComfyUI's own middleware, tests/comfy_stack.py), then pairing through ComfyUI's page with a stand-in studio.++Ports: FUOBE_CONNECT_TEST_PORT + 5 for the ComfyUI stand-in, + 6 for the studio stand-in (3365 and 3366 by default).+"""++from __future__ import annotations++import asyncio+import importlib.util+import json+import os+import sys+import tempfile+import time+import types+import unittest++import aiohttp+from aiohttp import web++HERE = os.path.dirname(os.path.abspath(__file__))+PACK = os.path.dirname(HERE)+sys.path.insert(0, HERE)++from comfy_stack import build_app # noqa: E402++BASE_PORT = int(os.environ.get("FUOBE_CONNECT_TEST_PORT", "3360"))+COMFY_PORT = BASE_PORT + 5+SITE_PORT = BASE_PORT + 6+SITE = f"http://127.0.0.1:{SITE_PORT}"+++class FakeStudio:+ """Answers the pairing and worker calls the way a Fuobe studio does, with fixed values."""++ def __init__(self):+ self.polls = 0+ self.calls: list[str] = []+ self.app = web.Application()+ self.app.router.add_post("/api/gpu/pair/start", self.start)+ self.app.router.add_post("/api/gpu/pair/poll", self.poll)+ self.app.router.add_post("/api/gpu/computer/beat", self.beat)+ self.app.router.add_post("/api/gpu/computer/claim", self.claim)+ self.app.router.add_post("/api/gpu/computer/unpair", self.unpair)++ async def start(self, request):+ self.calls.append("start")+ body = await request.json()+ self.name = body.get("name")+ return web.json_response({"id": "pair-1", "secret": "s" * 64, "code": "ABCD-EFGH", "url": f"{SITE}/connect?code=ABCD-EFGH", "expiresAt": 0, "pollMs": 1000})++ async def poll(self, request):+ self.polls += 1+ if self.polls < 2:+ return web.json_response({"state": "waiting"})+ return web.json_response({"state": "approved", "token": "fuobe_wkr_" + "T" * 43, "workerId": "w1", "name": self.name})++ async def beat(self, request):+ self.calls.append(f"beat:{request.headers.get('Authorization', '')[:20]}")+ return web.json_response({"runner": False, "heldElsewhere": True, "waiting": 0, "stops": []})++ async def claim(self, request):+ return web.Response(status=204)++ async def unpair(self, request):+ self.calls.append("unpair")+ return web.json_response({"ok": True})+++def load_pack(user_dir: str):+ """ComfyUI's view of the world, as much of it as Fuobe Connect reads, then the pack itself."""+ app, _ = build_app()+ instance = types.SimpleNamespace(app=app, routes=web.RouteTableDef())+ server = types.ModuleType("server")+ server.PromptServer = types.SimpleNamespace(instance=instance)+ folder_paths = types.ModuleType("folder_paths")+ folder_paths.get_user_directory = lambda: user_dir+ cli_args = types.ModuleType("comfy.cli_args")+ cli_args.args = types.SimpleNamespace(port=COMFY_PORT, listen="127.0.0.1", enable_cors_header=None, tls_keyfile=None)+ comfy = types.ModuleType("comfy")+ comfy.cli_args = cli_args+ sys.modules.update({"server": server, "folder_paths": folder_paths, "comfy": comfy, "comfy.cli_args": cli_args})+ os.environ["FUOBE_CONNECT_ORIGINS"] = SITE+ spec = importlib.util.spec_from_file_location("fuobe_connect_pack", os.path.join(PACK, "__init__.py"), submodule_search_locations=[PACK])+ module = importlib.util.module_from_spec(spec)+ sys.modules["fuobe_connect_pack"] = module+ spec.loader.exec_module(module)+ return module, instance+++class PluginTest(unittest.TestCase):+ def test_loads_into_comfyui_and_pairs(self):+ user_dir = tempfile.mkdtemp(prefix="fuobe-connect-user-")+ module, instance = load_pack(user_dir)+ self.assertEqual(module.NODE_CLASS_MAPPINGS, {})+ self.assertEqual(module.WEB_DIRECTORY, "./web")+ self.assertEqual(getattr(instance.app.middlewares[0], "__name__", ""), "fuobe_connect")+ paths = {(r.method, r.path) for r in instance.routes}+ self.assertIn(("GET", "/fuobe/connect"), paths)+ self.assertIn(("POST", "/fuobe/connect/pair"), paths)+ instance.app.add_routes(instance.routes)+ studio = FakeStudio()++ async def go():+ runners = []+ for app, port in ((instance.app, COMFY_PORT), (studio.app, SITE_PORT)):+ r = web.AppRunner(app)+ await r.setup()+ await web.TCPSite(r, "127.0.0.1", port).start()+ runners.append(r)+ try:+ async with aiohttp.ClientSession() as s:+ base = f"http://127.0.0.1:{COMFY_PORT}"+ async with s.get(f"{base}/fuobe/connect") as res:+ st = await res.json()+ self.assertEqual(st["studios"], [SITE])+ self.assertEqual(st["state"], "unpaired")+ # The studio itself can't start a pairing, or change the studios.+ async with s.post(f"{base}/fuobe/connect/pair", json={}, headers={"Origin": SITE, "Sec-Fetch-Site": "cross-site"}) as res:+ self.assertEqual(res.status, 403)+ # ComfyUI's own page can.+ async with s.post(f"{base}/fuobe/connect/pair", json={}, headers={"Origin": base}) as res:+ body = await res.json()+ self.assertEqual(res.status, 200, body)+ self.assertEqual(body["pairing"]["code"], "ABCD-EFGH")+ deadline = time.time() + 15+ while time.time() < deadline:+ async with s.get(f"{base}/fuobe/connect") as res:+ st = await res.json()+ if st.get("paired") and any(c.startswith("beat:Bearer fuobe_wkr_") for c in studio.calls):+ break+ await asyncio.sleep(0.3)+ self.assertEqual(st["paired"]["site"], SITE)+ self.assertTrue(any(c.startswith("beat:Bearer fuobe_wkr_") for c in studio.calls), studio.calls)+ with open(os.path.join(user_dir, "fuobe-connect.json"), encoding="utf-8") as f:+ saved = json.load(f)+ self.assertTrue(saved["token"].startswith("fuobe_wkr_"))+ if os.name != "nt":+ self.assertEqual(os.stat(os.path.join(user_dir, "fuobe-connect.json")).st_mode & 0o777, 0o600)+ async with s.post(f"{base}/fuobe/connect/unpair", json={}, headers={"Origin": base}) as res:+ st = await res.json()+ self.assertNotIn("paired", st)+ self.assertIn("unpair", studio.calls)+ with open(os.path.join(user_dir, "fuobe-connect.json"), encoding="utf-8") as f:+ self.assertNotIn("token", json.load(f))+ finally:+ for r in runners:+ await r.cleanup()++ asyncio.run(go())+++if __name__ == "__main__":+ unittest.main()