|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +use super::*; |
| 5 | +use crate::l7::correlation::{HttpOutcome, RequestCorrelation, scope_connection}; |
| 6 | +use crate::l7::jsonrpc::parse_jsonrpc_body_with_options; |
| 7 | +use crate::l7::relay::L7EvalContext; |
| 8 | +use crate::test_support::request_tracing::{RequestTracing, span_attribute}; |
| 9 | +use opentelemetry_sdk::trace::Sampler; |
| 10 | +use tracing::instrument::WithSubscriber as _; |
| 11 | + |
| 12 | +#[tokio::test] |
| 13 | +async fn inspection_metadata_is_bounded_and_does_not_expose_parser_input() { |
| 14 | + let _lock = openshell_otel_test_support::tracing_test_lock().await; |
| 15 | + let capture = RequestTracing::new(Some(Sampler::AlwaysOn)); |
| 16 | + let options = JsonRpcInspectionMode::JsonRpc.into(); |
| 17 | + let mut messages = (0..70) |
| 18 | + .map(|id| { |
| 19 | + let mut message = serde_json::json!({ |
| 20 | + "jsonrpc": "2.0", "method": "DO-NOT-RECORD-METHOD", |
| 21 | + "params": { "secret": "DO-NOT-RECORD-PAYLOAD" } |
| 22 | + }); |
| 23 | + if id % 2 == 0 { |
| 24 | + message["id"] = id.into(); |
| 25 | + } |
| 26 | + message |
| 27 | + }) |
| 28 | + .collect::<Vec<_>>(); |
| 29 | + messages.push(serde_json::json!({"jsonrpc":"2.0", "id":123, "result":"DO-NOT-RECORD-RESULT"})); |
| 30 | + let body = serde_json::to_vec(&messages).unwrap(); |
| 31 | + scope_connection(async { |
| 32 | + let correlation = RequestCorrelation::new_http( |
| 33 | + "POST", |
| 34 | + b"POST /rpc HTTP/1.1\r\nHost: example.test\r\n\r\n", |
| 35 | + &L7EvalContext::default(), |
| 36 | + ); |
| 37 | + let info = parse_jsonrpc_body_with_options(&body, options); |
| 38 | + assert_eq!(info.calls.len(), 70); |
| 39 | + assert_eq!(info.calls[0].method, "DO-NOT-RECORD-METHOD"); |
| 40 | + assert!(info.has_response); |
| 41 | + let malformed = parse_jsonrpc_body_with_options( |
| 42 | + br#"{"DO-NOT-RECORD-KEY":1,"DO-NOT-RECORD-KEY":2}"#, |
| 43 | + options, |
| 44 | + ); |
| 45 | + assert_eq!( |
| 46 | + malformed.error.as_ref().unwrap().kind(), |
| 47 | + JsonRpcInspectionErrorKind::InvalidJson |
| 48 | + ); |
| 49 | + correlation.record_outcome(HttpOutcome::Allowed); |
| 50 | + }) |
| 51 | + .with_subscriber(capture.subscriber()) |
| 52 | + .await; |
| 53 | + |
| 54 | + // The public parser is also used without an inspected request. |
| 55 | + tracing::subscriber::with_default(capture.subscriber(), || { |
| 56 | + assert_eq!( |
| 57 | + parse_jsonrpc_body_with_options(&body, options).calls.len(), |
| 58 | + 70 |
| 59 | + ); |
| 60 | + }); |
| 61 | + let spans = capture.spans(); |
| 62 | + let inspections = spans |
| 63 | + .iter() |
| 64 | + .filter(|span| span.name == "openshell.mcp.inspect") |
| 65 | + .collect::<Vec<_>>(); |
| 66 | + assert_eq!(inspections.len(), 2); |
| 67 | + let batch = inspections |
| 68 | + .iter() |
| 69 | + .find(|span| { |
| 70 | + span_attribute(span, "openshell.mcp.inspection.shape") |
| 71 | + .is_some_and(|value| value.as_str() == "batch") |
| 72 | + }) |
| 73 | + .unwrap(); |
| 74 | + for (key, value) in [ |
| 75 | + ("openshell.mcp.request_count", 32_i64), |
| 76 | + ("openshell.mcp.notification_count", 32_i64), |
| 77 | + ] { |
| 78 | + assert_eq!( |
| 79 | + span_attribute(batch, key), |
| 80 | + Some(&opentelemetry::Value::I64(value)) |
| 81 | + ); |
| 82 | + } |
| 83 | + assert_eq!( |
| 84 | + span_attribute(batch, "openshell.mcp.has_response"), |
| 85 | + Some(&opentelemetry::Value::Bool(true)) |
| 86 | + ); |
| 87 | + assert_eq!( |
| 88 | + span_attribute(batch, "openshell.mcp.counts_truncated"), |
| 89 | + Some(&opentelemetry::Value::Bool(true)) |
| 90 | + ); |
| 91 | + let rejected = inspections |
| 92 | + .iter() |
| 93 | + .find(|span| { |
| 94 | + span_attribute(span, "openshell.mcp.inspection.outcome") |
| 95 | + .is_some_and(|value| value.as_str() == "rejected") |
| 96 | + }) |
| 97 | + .unwrap(); |
| 98 | + assert_eq!( |
| 99 | + span_attribute(rejected, "openshell.mcp.inspection.failure_kind") |
| 100 | + .unwrap() |
| 101 | + .as_str(), |
| 102 | + "invalid_json" |
| 103 | + ); |
| 104 | + assert!(matches!( |
| 105 | + rejected.status, |
| 106 | + opentelemetry::trace::Status::Error { .. } |
| 107 | + )); |
| 108 | + assert!(!format!("{spans:?}").contains("DO-NOT-RECORD")); |
| 109 | + assert!(capture.events.json_events().is_empty()); |
| 110 | + capture.shutdown(); |
| 111 | +} |
0 commit comments