|
| 1 | +use std::ops::Deref; |
| 2 | +use std::str::FromStr; |
| 3 | + |
| 4 | +use headers::HeaderMapExt; |
| 5 | +use http::Extensions; |
| 6 | +use reqwest::header::{HeaderMap, HeaderName}; |
| 7 | + |
| 8 | +use crate::auth::ValidToken; |
| 9 | + |
| 10 | +/// List of header names to forward from MCP clients to GraphQL API |
| 11 | +pub type ForwardHeaders = Vec<String>; |
| 12 | + |
| 13 | +/// Build headers for a GraphQL request by combining static headers with forwarded headers |
| 14 | +pub fn build_request_headers( |
| 15 | + static_headers: &HeaderMap, |
| 16 | + forward_header_names: &ForwardHeaders, |
| 17 | + incoming_headers: &HeaderMap, |
| 18 | + extensions: &Extensions, |
| 19 | + disable_auth_token_passthrough: bool, |
| 20 | +) -> HeaderMap { |
| 21 | + // Starts with static headers |
| 22 | + let mut headers = static_headers.clone(); |
| 23 | + |
| 24 | + // Forward headers dynamically |
| 25 | + forward_headers(forward_header_names, incoming_headers, &mut headers); |
| 26 | + |
| 27 | + // Optionally extract the validated token and propagate it to upstream servers if present |
| 28 | + if !disable_auth_token_passthrough && let Some(token) = extensions.get::<ValidToken>() { |
| 29 | + headers.typed_insert(token.deref().clone()); |
| 30 | + } |
| 31 | + |
| 32 | + // Forward the mcp-session-id header if present |
| 33 | + if let Some(session_id) = incoming_headers.get("mcp-session-id") { |
| 34 | + headers.insert("mcp-session-id", session_id.clone()); |
| 35 | + } |
| 36 | + |
| 37 | + headers |
| 38 | +} |
| 39 | + |
| 40 | +/// Forward matching headers from incoming headers to outgoing headers |
| 41 | +fn forward_headers(names: &[String], incoming: &HeaderMap, outgoing: &mut HeaderMap) { |
| 42 | + for header in names { |
| 43 | + if let Ok(header_name) = HeaderName::from_str(header) |
| 44 | + && let Some(value) = incoming.get(&header_name) |
| 45 | + // Hop-by-hop headers are blocked per RFC 7230: https://datatracker.ietf.org/doc/html/rfc7230#section-6.1 |
| 46 | + && !matches!( |
| 47 | + header_name.as_str().to_lowercase().as_str(), |
| 48 | + "connection" |
| 49 | + | "keep-alive" |
| 50 | + | "proxy-authenticate" |
| 51 | + | "proxy-authorization" |
| 52 | + | "te" |
| 53 | + | "trailers" |
| 54 | + | "transfer-encoding" |
| 55 | + | "upgrade" |
| 56 | + | "content-length" |
| 57 | + ) |
| 58 | + { |
| 59 | + outgoing.insert(header_name, value.clone()); |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +#[cfg(test)] |
| 65 | +mod tests { |
| 66 | + use super::*; |
| 67 | + use headers::Authorization; |
| 68 | + use http::Extensions; |
| 69 | + use reqwest::header::HeaderValue; |
| 70 | + |
| 71 | + use crate::auth::ValidToken; |
| 72 | + |
| 73 | + #[test] |
| 74 | + fn test_build_request_headers_includes_static_headers() { |
| 75 | + let mut static_headers = HeaderMap::new(); |
| 76 | + static_headers.insert("x-api-key", HeaderValue::from_static("static-key")); |
| 77 | + static_headers.insert("user-agent", HeaderValue::from_static("mcp-server")); |
| 78 | + |
| 79 | + let forward_header_names = vec![]; |
| 80 | + let incoming_headers = HeaderMap::new(); |
| 81 | + let extensions = Extensions::new(); |
| 82 | + |
| 83 | + let result = build_request_headers( |
| 84 | + &static_headers, |
| 85 | + &forward_header_names, |
| 86 | + &incoming_headers, |
| 87 | + &extensions, |
| 88 | + false, |
| 89 | + ); |
| 90 | + |
| 91 | + assert_eq!(result.get("x-api-key").unwrap(), "static-key"); |
| 92 | + assert_eq!(result.get("user-agent").unwrap(), "mcp-server"); |
| 93 | + } |
| 94 | + |
| 95 | + #[test] |
| 96 | + fn test_build_request_headers_forwards_configured_headers() { |
| 97 | + let static_headers = HeaderMap::new(); |
| 98 | + let forward_header_names = vec!["x-tenant-id".to_string(), "x-trace-id".to_string()]; |
| 99 | + |
| 100 | + let mut incoming_headers = HeaderMap::new(); |
| 101 | + incoming_headers.insert("x-tenant-id", HeaderValue::from_static("tenant-123")); |
| 102 | + incoming_headers.insert("x-trace-id", HeaderValue::from_static("trace-456")); |
| 103 | + incoming_headers.insert("other-header", HeaderValue::from_static("ignored")); |
| 104 | + |
| 105 | + let extensions = Extensions::new(); |
| 106 | + |
| 107 | + let result = build_request_headers( |
| 108 | + &static_headers, |
| 109 | + &forward_header_names, |
| 110 | + &incoming_headers, |
| 111 | + &extensions, |
| 112 | + false, |
| 113 | + ); |
| 114 | + |
| 115 | + assert_eq!(result.get("x-tenant-id").unwrap(), "tenant-123"); |
| 116 | + assert_eq!(result.get("x-trace-id").unwrap(), "trace-456"); |
| 117 | + assert!(result.get("other-header").is_none()); |
| 118 | + } |
| 119 | + |
| 120 | + #[test] |
| 121 | + fn test_build_request_headers_adds_oauth_token_when_enabled() { |
| 122 | + let static_headers = HeaderMap::new(); |
| 123 | + let forward_header_names = vec![]; |
| 124 | + let incoming_headers = HeaderMap::new(); |
| 125 | + |
| 126 | + let mut extensions = Extensions::new(); |
| 127 | + let token = ValidToken(Authorization::bearer("test-token").unwrap()); |
| 128 | + extensions.insert(token); |
| 129 | + |
| 130 | + let result = build_request_headers( |
| 131 | + &static_headers, |
| 132 | + &forward_header_names, |
| 133 | + &incoming_headers, |
| 134 | + &extensions, |
| 135 | + false, |
| 136 | + ); |
| 137 | + |
| 138 | + assert!(result.get("authorization").is_some()); |
| 139 | + assert_eq!(result.get("authorization").unwrap(), "Bearer test-token"); |
| 140 | + } |
| 141 | + |
| 142 | + #[test] |
| 143 | + fn test_build_request_headers_skips_oauth_token_when_disabled() { |
| 144 | + let static_headers = HeaderMap::new(); |
| 145 | + let forward_header_names = vec![]; |
| 146 | + let incoming_headers = HeaderMap::new(); |
| 147 | + |
| 148 | + let mut extensions = Extensions::new(); |
| 149 | + let token = ValidToken(Authorization::bearer("test-token").unwrap()); |
| 150 | + extensions.insert(token); |
| 151 | + |
| 152 | + let result = build_request_headers( |
| 153 | + &static_headers, |
| 154 | + &forward_header_names, |
| 155 | + &incoming_headers, |
| 156 | + &extensions, |
| 157 | + true, |
| 158 | + ); |
| 159 | + |
| 160 | + assert!(result.get("authorization").is_none()); |
| 161 | + } |
| 162 | + |
| 163 | + #[test] |
| 164 | + fn test_build_request_headers_forwards_mcp_session_id() { |
| 165 | + let static_headers = HeaderMap::new(); |
| 166 | + let forward_header_names = vec![]; |
| 167 | + |
| 168 | + let mut incoming_headers = HeaderMap::new(); |
| 169 | + incoming_headers.insert("mcp-session-id", HeaderValue::from_static("session-123")); |
| 170 | + |
| 171 | + let extensions = Extensions::new(); |
| 172 | + |
| 173 | + let result = build_request_headers( |
| 174 | + &static_headers, |
| 175 | + &forward_header_names, |
| 176 | + &incoming_headers, |
| 177 | + &extensions, |
| 178 | + false, |
| 179 | + ); |
| 180 | + |
| 181 | + assert_eq!(result.get("mcp-session-id").unwrap(), "session-123"); |
| 182 | + } |
| 183 | + |
| 184 | + #[test] |
| 185 | + fn test_build_request_headers_combined_scenario() { |
| 186 | + // Static headers |
| 187 | + let mut static_headers = HeaderMap::new(); |
| 188 | + static_headers.insert("x-api-key", HeaderValue::from_static("static-key")); |
| 189 | + |
| 190 | + // Forward specific headers |
| 191 | + let forward_header_names = vec!["x-tenant-id".to_string()]; |
| 192 | + |
| 193 | + // Incoming headers |
| 194 | + let mut incoming_headers = HeaderMap::new(); |
| 195 | + incoming_headers.insert("x-tenant-id", HeaderValue::from_static("tenant-123")); |
| 196 | + incoming_headers.insert("mcp-session-id", HeaderValue::from_static("session-456")); |
| 197 | + incoming_headers.insert( |
| 198 | + "ignored-header", |
| 199 | + HeaderValue::from_static("should-not-appear"), |
| 200 | + ); |
| 201 | + |
| 202 | + // OAuth token |
| 203 | + let mut extensions = Extensions::new(); |
| 204 | + let token = ValidToken(Authorization::bearer("oauth-token").unwrap()); |
| 205 | + extensions.insert(token); |
| 206 | + |
| 207 | + let result = build_request_headers( |
| 208 | + &static_headers, |
| 209 | + &forward_header_names, |
| 210 | + &incoming_headers, |
| 211 | + &extensions, |
| 212 | + false, |
| 213 | + ); |
| 214 | + |
| 215 | + // Verify all parts combined correctly |
| 216 | + assert_eq!(result.get("x-api-key").unwrap(), "static-key"); |
| 217 | + assert_eq!(result.get("x-tenant-id").unwrap(), "tenant-123"); |
| 218 | + assert_eq!(result.get("mcp-session-id").unwrap(), "session-456"); |
| 219 | + assert_eq!(result.get("authorization").unwrap(), "Bearer oauth-token"); |
| 220 | + assert!(result.get("ignored-header").is_none()); |
| 221 | + } |
| 222 | + |
| 223 | + #[test] |
| 224 | + fn test_forward_headers_no_headers_by_default() { |
| 225 | + let names: Vec<String> = vec![]; |
| 226 | + |
| 227 | + let mut incoming = HeaderMap::new(); |
| 228 | + incoming.insert("x-tenant-id", HeaderValue::from_static("tenant-123")); |
| 229 | + |
| 230 | + let mut outgoing = HeaderMap::new(); |
| 231 | + |
| 232 | + forward_headers(&names, &incoming, &mut outgoing); |
| 233 | + |
| 234 | + assert!(outgoing.is_empty()); |
| 235 | + } |
| 236 | + |
| 237 | + #[test] |
| 238 | + fn test_forward_headers_only_specific_headers() { |
| 239 | + let names = vec![ |
| 240 | + "x-tenant-id".to_string(), // Multi-tenancy |
| 241 | + "x-trace-id".to_string(), // Distributed tracing |
| 242 | + "x-geo-country".to_string(), // Geo information from CDN |
| 243 | + "x-experiment-id".to_string(), // A/B testing |
| 244 | + "ai-client-name".to_string(), // Client identification |
| 245 | + ]; |
| 246 | + |
| 247 | + let mut incoming = HeaderMap::new(); |
| 248 | + incoming.insert("x-tenant-id", HeaderValue::from_static("tenant-123")); |
| 249 | + incoming.insert("x-trace-id", HeaderValue::from_static("trace-456")); |
| 250 | + incoming.insert("x-geo-country", HeaderValue::from_static("US")); |
| 251 | + incoming.insert("x-experiment-id", HeaderValue::from_static("exp-789")); |
| 252 | + incoming.insert("ai-client-name", HeaderValue::from_static("claude")); |
| 253 | + incoming.insert("other-header", HeaderValue::from_static("ignored")); |
| 254 | + |
| 255 | + let mut outgoing = HeaderMap::new(); |
| 256 | + |
| 257 | + forward_headers(&names, &incoming, &mut outgoing); |
| 258 | + |
| 259 | + assert_eq!(outgoing.get("x-tenant-id").unwrap(), "tenant-123"); |
| 260 | + assert_eq!(outgoing.get("x-trace-id").unwrap(), "trace-456"); |
| 261 | + assert_eq!(outgoing.get("x-geo-country").unwrap(), "US"); |
| 262 | + assert_eq!(outgoing.get("x-experiment-id").unwrap(), "exp-789"); |
| 263 | + assert_eq!(outgoing.get("ai-client-name").unwrap(), "claude"); |
| 264 | + |
| 265 | + assert!(outgoing.get("other-header").is_none()); |
| 266 | + } |
| 267 | + |
| 268 | + #[test] |
| 269 | + fn test_forward_headers_blocks_hop_by_hop_headers() { |
| 270 | + let names = vec!["connection".to_string(), "content-length".to_string()]; |
| 271 | + |
| 272 | + let mut incoming = HeaderMap::new(); |
| 273 | + incoming.insert("connection", HeaderValue::from_static("keep-alive")); |
| 274 | + incoming.insert("content-length", HeaderValue::from_static("1234")); |
| 275 | + |
| 276 | + let mut outgoing = HeaderMap::new(); |
| 277 | + |
| 278 | + forward_headers(&names, &incoming, &mut outgoing); |
| 279 | + |
| 280 | + assert!(outgoing.get("connection").is_none()); |
| 281 | + assert!(outgoing.get("content-length").is_none()); |
| 282 | + } |
| 283 | + |
| 284 | + #[test] |
| 285 | + fn test_forward_headers_case_insensitive_matching() { |
| 286 | + let names = vec!["X-Tenant-ID".to_string()]; |
| 287 | + |
| 288 | + let mut incoming = HeaderMap::new(); |
| 289 | + incoming.insert("x-tenant-id", HeaderValue::from_static("tenant-123")); |
| 290 | + |
| 291 | + let mut outgoing = HeaderMap::new(); |
| 292 | + forward_headers(&names, &incoming, &mut outgoing); |
| 293 | + |
| 294 | + assert_eq!(outgoing.get("x-tenant-id").unwrap(), "tenant-123"); |
| 295 | + } |
| 296 | +} |
0 commit comments