BigPizzaV3/CodexPlusPlus · error · anyhow::Error

CDP WebSocket URL must use ws or wss

Error message

CDP WebSocket URL must use ws or wss

What it means

Thrown by `validate_cdp_websocket_url` when a CDP WebSocket URL's scheme is neither `ws` nor `wss`. The library requires an actual WebSocket scheme because the URL will be handed to a WebSocket client; http/https URLs are rejected outright.

Solutions

  1. Use the `webSocketDebuggerUrl` field from the CDP target/version JSON — it already starts with `ws://`
  2. Convert the scheme explicitly: replace `http://` with `ws://` (or `https://` with `wss://`) before validating
  3. Fix any URL construction that drops or mangles the scheme

Example fix

// before
let url = format!("http://127.0.0.1:{port}/devtools/browser/{id}");
validate_cdp_websocket_url(&url, port)?;
// after
let url = format!("ws://127.0.0.1:{port}/devtools/browser/{id}");
validate_cdp_websocket_url(&url, port)?;
Defensive patterns

Strategy: validation

Validate before calling

fn has_ws_scheme(url: &str) -> bool {
    reqwest::Url::parse(url).map(|u| matches!(u.scheme(), "ws" | "wss")).unwrap_or(false)
}

Try / catch

let url = if url.starts_with("http://") { url.replacen("http://", "ws://", 1) } else { url.to_string() };
validate_cdp_websocket_url(&url, port).context("CDP WebSocket URL rejected")?;

Prevention

When it happens

Trigger: Passing an HTTP discovery URL (e.g. `http://127.0.0.1:9222/json/version`) or a hand-built string like `tcp://...` into `validate_cdp_websocket_url`, or feeding the wrong field of a CDP response (e.g. a devtoolsFrontendUrl).

Common situations: Confusing the HTTP `/json` endpoints with the WebSocket debugger URL, older code paths that returned http URLs, or string-concatenating a ws URL and accidentally dropping the scheme.

Understand the failure class

Background: "Invalid URL" errors: why new URL(), URI.parse, and reqwest::Url reject your string — missing scheme, whitespace, and bad path format — this error's family across 39 libraries.

Related errors


AI-assisted analysis of BigPizzaV3/CodexPlusPlus@b1ed92e5e4 (2026-09-19). Data as JSON: /api/errors/1b4d7ff33d97c635. Report an issue: GitHub.

Appendix: source

Thrown at crates/codex-plus-core/src/cdp.rs:214

    let targets = response
        .json::<Vec<CdpTarget>>()
        .await
        .context("failed to deserialize CDP targets")?;
    for target in &targets {
        if let Some(websocket_url) = target.web_socket_debugger_url.as_deref() {
            validate_cdp_websocket_url(websocket_url, debug_port).with_context(|| {
                format!("unsafe CDP target WebSocket URL for target {}", target.id)
            })?;
        }
    }
    Ok(targets)
}

pub fn validate_cdp_websocket_url(url: &str, expected_port: u16) -> anyhow::Result<()> {
    let parsed = reqwest::Url::parse(url).context("invalid CDP WebSocket URL")?;
    if !matches!(parsed.scheme(), "ws" | "wss") {
        bail!("CDP WebSocket URL must use ws or wss");
    }
    let host = parsed
        .host_str()
        .ok_or_else(|| anyhow::anyhow!("CDP WebSocket URL has no host"))?;
    let address = host
        .trim_start_matches('[')
        .trim_end_matches(']')
        .parse::<IpAddr>()
        .with_context(|| "CDP WebSocket host must be a loopback IP address")?;
    if !address.is_loopback() {
        bail!("CDP WebSocket host must be loopback");
    }
    let port = parsed
        .port()
        .ok_or_else(|| anyhow::anyhow!("CDP WebSocket URL must include an explicit port"))?;
    if port != expected_port {
        bail!("CDP WebSocket port {port} does not match debug port {expected_port}");
    }

View on GitHub (pinned to b1ed92e5e4)