Hmbown/CodeWhale · error

unsafe MCP HTTP header

Error message

unsafe MCP HTTP header '{name}'

What it means

`insert_header` validates every custom header name/value against `is_safe_custom_header` before injecting it into an MCP HTTP request. Headers that could alter transport semantics or leak credentials (e.g. `Authorization`, `Host`, hop-by-hop headers) are rejected rather than forwarded.

Solutions

  1. Remove the restricted header from the MCP server's custom headers config
  2. Rely on the built-in OAuth/bearer-token mechanism instead of a manual `Authorization` header
  3. Sanitize the header value (strip CR/LF and control characters) and rename to a safe `X-` prefixed custom header

Example fix

// before
{"headers": {"Authorization": "Bearer xyz"}}
// after
// omit Authorization; set bearer_token_env_var or use MCP OAuth flow
{"headers": {"X-Custom-Trace": "abc"}}
Defensive patterns

Strategy: validation

Validate before calling

if !super::headers::is_safe_custom_header(name, value) {
    // skip or surface to user before connecting
    return Err(anyhow!("refusing unsafe header {name}"));
}

Type guard

fn is_safe_header(name: &str, value: &str) -> bool {
    !matches!(name.to_ascii_lowercase().as_str(),
        "authorization" | "cookie" | "host" | "connection" | "content-length")
        && !value.chars().any(|c| c.is_control())
}

Try / catch

match insert_header(&mut headers, name, value) {
    Err(e) if e.to_string().contains("unsafe MCP HTTP header") => {
        eprintln!("skipped restricted header {name}");
    }
    other => other?,
}

Prevention

When it happens

Trigger: Configuring a custom MCP header whose name is on the unsafe list (e.g. `Authorization`, `Cookie`, `Host`) or a value with control characters/newlines, then initiating an OAuth-authenticated HTTP/SSE connection that calls `insert_header`.

Common situations: Users putting `Authorization` into a custom headers config block when the OAuth layer already manages auth; copy-pasted curl headers like `Host` or `Connection`; values containing embedded newlines from bad config.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@73e0f67d83 (2026-09-22). Data as JSON: /api/errors/2e996438a50ab455. Report an issue: GitHub.

Appendix: source

Thrown at crates/tui/src/mcp/oauth.rs:1404

    env_headers: &HashMap<String, String>,
) -> Result<HeaderMap> {
    let mut headers = HeaderMap::new();
    for (name, value) in http_headers {
        insert_header(&mut headers, name, value)?;
    }
    for (name, env_var) in env_headers {
        if let Ok(value) = std::env::var(env_var)
            && !value.trim().is_empty()
        {
            insert_header(&mut headers, name, &value)?;
        }
    }
    Ok(headers)
}

fn insert_header(headers: &mut HeaderMap, name: &str, value: &str) -> Result<()> {
    if !super::headers::is_safe_custom_header(name, value) {
        bail!("unsafe MCP HTTP header '{name}'");
    }
    let name = HeaderName::from_bytes(name.as_bytes())
        .with_context(|| format!("invalid MCP HTTP header name '{name}'"))?;
    let value = HeaderValue::from_str(value).with_context(|| "invalid MCP HTTP header value")?;
    headers.insert(name, value);
    Ok(())
}

fn contains_authorization_header(headers: &HashMap<String, String>) -> bool {
    headers
        .keys()
        .any(|key| key.trim().eq_ignore_ascii_case("authorization"))
}

fn normalize_scopes(scopes_supported: Option<Vec<String>>) -> Option<Vec<String>> {
    let scopes_supported = scopes_supported?;
    let mut normalized = Vec::new();
    for scope in scopes_supported {

View on GitHub (pinned to 73e0f67d83)