#!/usr/bin/env python3 # scripts/test_cdp_origins.py # Tries several Origin headers when connecting to Chromium DevTools websocket import requests, websocket, json, sys, time def try_origin(ws_url, origin_header): try: # websocket-client accepts header=[...] or origin=... depending on version try: ws = websocket.create_connection(ws_url, timeout=5, header=[f"Origin: {origin_header}"]) except TypeError: ws = websocket.create_connection(ws_url, timeout=5, origin=origin_header) return True, "connected" except Exception as e: return False, str(e) def main(): try: tabs = requests.get('http://127.0.0.1:9222/json', timeout=3).json() except Exception as e: print("ERROR: could not fetch DevTools json:", e) return if not tabs: print("No DevTools targets found") return target = tabs[0] ws_url = target.get('webSocketDebuggerUrl') print("Using target url:", target.get('url')) print("DevTools websocket:", ws_url) if not ws_url: print("No webSocketDebuggerUrl found in target") return # candidate origins to try candidates = [ "http://127.0.0.1", "http://localhost", "https://www.computerbase.de", # page origin (use the exact scheme + host of the page) "https://computerbase.de", "null", "chrome-devtools://devtools", "" ] print("\nTrying origins (may need to match page origin exactly):") for orig in candidates: ok, msg = try_origin(ws_url, orig) print(f"Origin={repr(orig):30} -> {ok} : {msg}") # If one of them connected, try to send a Runtime.evaluate to confirm: # (try the first that succeeded) for orig in candidates: try: try: ws = websocket.create_connection(ws_url, timeout=5, header=[f"Origin: {orig}"]) except TypeError: ws = websocket.create_connection(ws_url, timeout=5, origin=orig) print("\nConnected with origin:", orig) msg_id = 1 payload = {"id": msg_id, "method": "Runtime.enable"} ws.send(json.dumps(payload)) print("Runtime.enable ->", ws.recv()) msg_id += 1 payload = {"id": msg_id, "method": "Runtime.evaluate", "params": {"expression": "1+2", "returnByValue": True}} ws.send(json.dumps(payload)) print("Runtime.evaluate ->", ws.recv()) ws.close() break except Exception as e: # try next origin continue if __name__ == "__main__": main()