From 3735c2d5f5248259482b9045c34fb7a8a3892352 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20P=C3=A9rez=20de=20Algaba?= <124347725+jperezdealgaba@users.noreply.github.com> Date: Thu, 10 Sep 2026 04:54:40 +0200 Subject: [PATCH] [Security][Rust Frontend] Normalize HTTP method labels in metrics (#56058) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bugen Zhao Co-authored-by: OpenAI Codex Signed-off-by: Juan Pérez de Algaba Signed-off-by: Bugen Zhao --- rust/src/server/src/middleware/metrics.rs | 52 +++++++++++++++++++- rust/src/server/src/routes/tests.rs | 58 +++++++++++++++++++++++ 2 files changed, 109 insertions(+), 1 deletion(-) diff --git a/rust/src/server/src/middleware/metrics.rs b/rust/src/server/src/middleware/metrics.rs index e45ae5f8e4..1e6a79a556 100644 --- a/rust/src/server/src/middleware/metrics.rs +++ b/rust/src/server/src/middleware/metrics.rs @@ -4,6 +4,7 @@ use std::time::Instant; use axum::extract::{MatchedPath, Request}; +use axum::http::Method; use axum::middleware::Next; use axum::response::Response; use vllm_metrics::{HttpHandlerLabels, HttpRequestLabels, METRICS}; @@ -29,10 +30,29 @@ const EXCLUDED_HANDLERS: &[&str] = &[ "/is_sleeping", ]; +/// Map the request method to a bounded Prometheus label. +/// +/// HTTP parsers accept arbitrary method tokens. Recording the raw token as a +/// label would let unique values create unbounded time series. +pub(crate) fn http_metrics_method(method: &Method) -> &str { + match *method { + Method::GET + | Method::POST + | Method::PUT + | Method::PATCH + | Method::DELETE + | Method::HEAD + | Method::OPTIONS + | Method::CONNECT + | Method::TRACE => method.as_str(), + _ => "other", + } +} + /// Record API-server HTTP metrics with Python-compatible /// (`PrometheusFastApiInstrumentator` style) family names and labels. pub async fn track_http_metrics(req: Request, next: Next) -> Response { - let method = req.method().as_str().to_string(); + let method = http_metrics_method(req.method()).to_string(); let handler = req .extensions() .get::() @@ -80,3 +100,33 @@ fn status_group(status: u16) -> &'static str { _ => "unknown", } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn known_methods_keep_their_names() { + for (method, expected) in [ + (Method::GET, "GET"), + (Method::POST, "POST"), + (Method::PUT, "PUT"), + (Method::PATCH, "PATCH"), + (Method::DELETE, "DELETE"), + (Method::HEAD, "HEAD"), + (Method::OPTIONS, "OPTIONS"), + (Method::CONNECT, "CONNECT"), + (Method::TRACE, "TRACE"), + ] { + assert_eq!(http_metrics_method(&method), expected); + } + } + + #[test] + fn unknown_methods_collapse_to_other() { + let method = Method::from_bytes(b"XVULNCARD000001").expect("valid token"); + assert_eq!(http_metrics_method(&method), "other"); + let other = Method::from_bytes(b"XVULNCARD000002").expect("valid token"); + assert_eq!(http_metrics_method(&other), "other"); + } +} diff --git a/rust/src/server/src/routes/tests.rs b/rust/src/server/src/routes/tests.rs index a04c3f2f11..210ae044a0 100644 --- a/rust/src/server/src/routes/tests.rs +++ b/rust/src/server/src/routes/tests.rs @@ -3115,6 +3115,64 @@ async fn http_metrics_group_error_statuses() { ); } +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +#[serial] +async fn http_metrics_collapse_unknown_methods() { + let mut app = test_app().await; + let before = METRICS.render().unwrap(); + + for (method, path) in [ + ("XVULNCARD000001", "/tokenize"), + ("XVULNCARD000002", "/tokenize"), + ("XVULNCARD000003", "/detokenize"), + ] { + let response = app + .call( + Request::builder() + .method(method) + .uri(path) + .body(Body::empty()) + .expect("build request"), + ) + .await + .expect("call app"); + assert_eq!(response.status(), StatusCode::METHOD_NOT_ALLOWED); + } + + let after = METRICS.render().unwrap(); + assert!( + !after.contains("XVULNCARD"), + "raw method tokens must not appear in metrics: {after}" + ); + assert_eq!( + metric_delta( + &before, + &after, + "http_requests_total", + Some("method=\"other\",status=\"4xx\",handler=\"/tokenize\""), + ), + 2.0 + ); + assert_eq!( + metric_delta( + &before, + &after, + "http_requests_total", + Some("method=\"other\",status=\"4xx\",handler=\"/detokenize\""), + ), + 1.0 + ); + assert_eq!( + metric_delta( + &before, + &after, + "http_request_duration_seconds_count", + Some("method=\"other\",handler=\"/tokenize\""), + ), + 2.0 + ); +} + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[serial] async fn load_endpoint_tracks_chat_stream_lifecycle() {