{"record":{"id":"2e996438a50ab455","repo":"Hmbown/CodeWhale","slug":"unsafe-mcp-http-header-name","errorCode":null,"errorMessage":"unsafe MCP HTTP header '{name}'","messagePattern":"unsafe MCP HTTP header '(.+?)'","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/tui/src/mcp/oauth.rs","lineNumber":1404,"sourceCode":"    env_headers: &HashMap<String, String>,\n) -> Result<HeaderMap> {\n    let mut headers = HeaderMap::new();\n    for (name, value) in http_headers {\n        insert_header(&mut headers, name, value)?;\n    }\n    for (name, env_var) in env_headers {\n        if let Ok(value) = std::env::var(env_var)\n            && !value.trim().is_empty()\n        {\n            insert_header(&mut headers, name, &value)?;\n        }\n    }\n    Ok(headers)\n}\n\nfn insert_header(headers: &mut HeaderMap, name: &str, value: &str) -> Result<()> {\n    if !super::headers::is_safe_custom_header(name, value) {\n        bail!(\"unsafe MCP HTTP header '{name}'\");\n    }\n    let name = HeaderName::from_bytes(name.as_bytes())\n        .with_context(|| format!(\"invalid MCP HTTP header name '{name}'\"))?;\n    let value = HeaderValue::from_str(value).with_context(|| \"invalid MCP HTTP header value\")?;\n    headers.insert(name, value);\n    Ok(())\n}\n\nfn contains_authorization_header(headers: &HashMap<String, String>) -> bool {\n    headers\n        .keys()\n        .any(|key| key.trim().eq_ignore_ascii_case(\"authorization\"))\n}\n\nfn normalize_scopes(scopes_supported: Option<Vec<String>>) -> Option<Vec<String>> {\n    let scopes_supported = scopes_supported?;\n    let mut normalized = Vec::new();\n    for scope in scopes_supported {","sourceCodeStart":1386,"sourceCodeEnd":1422,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/73e0f67d83c59909b571efdfc88c4bc28c309cb1/crates/tui/src/mcp/oauth.rs#L1386-L1422","documentation":"`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.","triggerScenarios":"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`.","commonSituations":"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.","solutions":["Remove the restricted header from the MCP server's custom headers config","Rely on the built-in OAuth/bearer-token mechanism instead of a manual `Authorization` header","Sanitize the header value (strip CR/LF and control characters) and rename to a safe `X-` prefixed custom header"],"exampleFix":"// before\n{\"headers\": {\"Authorization\": \"Bearer xyz\"}}\n// after\n// omit Authorization; set bearer_token_env_var or use MCP OAuth flow\n{\"headers\": {\"X-Custom-Trace\": \"abc\"}}","handlingStrategy":"validation","validationCode":"if !super::headers::is_safe_custom_header(name, value) {\n    // skip or surface to user before connecting\n    return Err(anyhow!(\"refusing unsafe header {name}\"));\n}","typeGuard":"fn is_safe_header(name: &str, value: &str) -> bool {\n    !matches!(name.to_ascii_lowercase().as_str(),\n        \"authorization\" | \"cookie\" | \"host\" | \"connection\" | \"content-length\")\n        && !value.chars().any(|c| c.is_control())\n}","tryCatchPattern":"match insert_header(&mut headers, name, value) {\n    Err(e) if e.to_string().contains(\"unsafe MCP HTTP header\") => {\n        eprintln!(\"skipped restricted header {name}\");\n    }\n    other => other?,\n}","preventionTips":["Never put Authorization/Cookie in custom headers — use the dedicated auth config","Reject header values containing CR/LF at config load","Keep custom headers to application-specific X- names"],"tags":["mcp","http","headers","security"],"backgroundTag":"invalid-argument-value","analyzedSha":"73e0f67d83c59909b571efdfc88c4bc28c309cb1","analyzedAt":"2026-09-22T01:30:00.501Z","contentChangedAt":"2026-09-22T01:30:00.501Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}