MCPcopy Create free account
hub / github.com/NVIDIA/OpenShell / sanitize_request_headers

Function sanitize_request_headers

crates/openshell-router/src/backend.rs:106–149  ·  view source on GitHub ↗
(
    route: &ResolvedRoute,
    headers: &[(String, String)],
)

Source from the content-addressed store, hash-verified

104}
105
106fn sanitize_request_headers(
107 route: &ResolvedRoute,
108 headers: &[(String, String)],
109) -> Vec<(String, String)> {
110 let mut allowed = HashSet::new();
111 allowed.extend(
112 COMMON_INFERENCE_REQUEST_HEADERS
113 .iter()
114 .map(|name| (*name).to_string()),
115 );
116 allowed.extend(
117 route
118 .passthrough_headers
119 .iter()
120 .map(|name| name.to_ascii_lowercase()),
121 );
122 allowed.extend(
123 route
124 .default_headers
125 .iter()
126 .map(|(name, _)| name.to_ascii_lowercase()),
127 );
128
129 // Vertex AI Anthropic rawPredict endpoints do not accept the
130 // `anthropic-beta` header. Beta feature enablement for Vertex AI is
131 // controlled through Google Cloud, not HTTP headers. Strip it here so
132 // clients (e.g. Claude Code) that always send beta flags don't cause
133 // HTTP 400 errors from the Vertex AI backend.
134 let strip_anthropic_beta = is_vertex_anthropic_rawpredict_route(route);
135
136 headers
137 .iter()
138 .filter_map(|(name, value)| {
139 let name_lc = name.to_ascii_lowercase();
140 if should_strip_request_header(&name_lc) || !allowed.contains(&name_lc) {
141 return None;
142 }
143 if strip_anthropic_beta && name_lc == "anthropic-beta" {
144 return None;
145 }
146 Some((name.clone(), value.clone()))
147 })
148 .collect()
149}
150
151fn should_strip_request_header(name: &str) -> bool {
152 matches!(