Switchyard lets LLM applications route traffic across models and providers while preserving native OpenAI and Anthropic API compatibility - enabling flexible model selection, benchmarking, and cost/performance optimization.
Rust
fix(relay): redact configured provider keys from plugin outputs (#738)
Bryan Bednarski6926d1fabe78
diff --git a/crates/switchyard-nemo-relay-plugin/README.md b/crates/switchyard-nemo-relay-plugin/README.mdindex 981db02fc..d889afada 100644--- a/crates/switchyard-nemo-relay-plugin/README.md+++ b/crates/switchyard-nemo-relay-plugin/README.md@@ -271,6 +271,29 @@ String values longer than 64 bytes are omitted and should be stable, non-sensiti## Failure policy+### Provider credential redaction++The plugin replaces configured `api_key_env` credentials with `[REDACTED]` in+buffered responses and each translated stream event before returning them to+Relay. It also redacts returned error strings, routing mark data and metadata,+metric attributes and metadata, and plugin telemetry-emission diagnostics.+The intended upstream still receives the original credential. Upstream response+headers are not returned through the plugin's JSON execution intercepts.++The plugin and standalone server share the credential replacement helpers.+Redaction runs after translation, including on preserved provider fields and+JSON member names. It does not buffer the response stream or change cancellation.+Each event is handled independently: a credential split across events or separate+JSON strings is **not** reconstructed or redacted as a whole. String values and error+text are checked for both raw credentials and their JSON-escaped forms, including+one embedded JSON serialization layer such as serialized tool arguments. Further+repeated escaping and other encodings or transformations are outside this policy.+Preventing reconstruction across stream events requires a separate stateful policy+for each logical text or tool argument field; per-event redaction does not provide+that guarantee.++### Execution failures+`switchyard-llm-client` owns provider retry and route-candidate fallbackbehavior. The plugin does not maintain a separate trusted-default target orrerun routing after an execution failure. Failures outside the shared runner,diff --git a/crates/switchyard-nemo-relay-plugin/src/lib.rs b/crates/switchyard-nemo-relay-plugin/src/lib.rsindex 853a2e81b..944294ff0 100644--- a/crates/switchyard-nemo-relay-plugin/src/lib.rs+++ b/crates/switchyard-nemo-relay-plugin/src/lib.rs@@ -77,7 +77,7 @@ fn register_buffered(}let decoded = runtime.decode_request(inbound, request, false)?;let execution = runtime.execute_buffered(inbound, decoded).await;- emit_events(&plugin_runtime, execution.events);+ emit_events(&plugin_runtime, runtime.redactor(), execution.events);execution.result}},@@ -105,14 +105,17 @@ fn register_stream(}let decoded = runtime.decode_request(inbound, request, true)?;let stream_plugin_runtime = plugin_runtime.clone();+ let stream_runtime = Arc::clone(&runtime);let execution = runtime.execute_stream(inbound,decoded,- Arc::new(move |event| emit_event(&stream_plugin_runtime, event)),+ Arc::new(move |event| {+ emit_event(&stream_plugin_runtime, stream_runtime.redactor(), event)+ }),).await;- emit_events(&plugin_runtime, execution.events);+ emit_events(&plugin_runtime, runtime.redactor(), execution.events);execution.result}},diff --git a/crates/switchyard-nemo-relay-plugin/src/redaction_tests.rs b/crates/switchyard-nemo-relay-plugin/src/redaction_tests.rsnew file mode 100644index 000000000..e25e0ad7d--- /dev/null+++ b/crates/switchyard-nemo-relay-plugin/src/redaction_tests.rs@@ -0,0 +1,212 @@+// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.+// SPDX-License-Identifier: Apache-2.0++//! Exercises the configured credential through the real runner and loopback HTTP client.++use std::process::Command;++use wiremock::matchers::{header, method, path};+use wiremock::{Mock, MockServer, ResponseTemplate};++use super::*;++const KEY_ENV: &str = "SWITCHYARD_RELAY_REDACTION_TEST_KEY";+const KEY: &str = "qa-only-synthetic-relay-provider-key";++// A child process owns the environment so parallel tests never race on set_var.+#[test]+fn configured_provider_credentials_are_redacted() {+ const CHILD: &str = "SWITCHYARD_RELAY_REDACTION_TEST_CHILD";+ if std::env::var_os(CHILD).is_none() {+ let output = Command::new(std::env::current_exe().unwrap())+ .args([+ "--exact",+ "runtime::redaction_tests::configured_provider_credentials_are_redacted",+ "--nocapture",+ ])+ .env(CHILD, "1")+ .env(KEY_ENV, KEY)+ .output()+ .unwrap();+ assert!(+ output.status.success(),+ "{}\n{}",+ String::from_utf8_lossy(&output.stdout),+ String::from_utf8_lossy(&output.stderr)+ );+ return;+ }+ tokio::runtime::Runtime::new().unwrap().block_on(async {+ for format in [+ WireFormat::OpenAiChat,+ WireFormat::OpenAiResponses,+ WireFormat::AnthropicMessages,+ ] {+ for streaming in [false, true] {+ reflected_response(format, streaming, false).await;+ reflected_response(format, streaming, true).await;+ }+ }+ });+}++async fn reflected_response(format: WireFormat, streaming: bool, fail: bool) {+ let server = MockServer::start().await;+ let reflected = format!("ordinary diagnostics; Bearer {KEY}");+ let body = if streaming {+ let chunk = json!({+ "id": "chatcmpl-redaction", "object": "chat.completion.chunk",+ "choices": [{"index": 0, "delta": {"role": "assistant", "content": reflected}, "finish_reason": null}]+ });+ if fail {+ format!(+ "data: {chunk}\n\nevent: error\ndata: {}\n\n",+ json!({"error": {"message": reflected, "type": "provider_error"}})+ )+ } else {+ format!(+ "data: {chunk}\n\ndata: {}\n\ndata: [DONE]\n\n",+ json!({"id": "chatcmpl-redaction", "choices": [{"index": 0, "delta": {}, "finish_reason": "stop"}]})+ )+ }+ } else if fail {+ json!({"error": {"message": reflected, "type": "provider_error"}}).to_string()+ } else {+ json!({+ "id": "chatcmpl-redaction", "object": "chat.completion", "model": "target/model",+ "choices": [{"index": 0, "message": {"role": "assistant", "content": reflected}, "finish_reason": "stop"}]+ }).to_string()+ };+ Mock::given(method("POST"))+ .and(path("/v1/chat/completions"))+ .and(header("authorization", format!("Bearer {KEY}")))+ .respond_with(+ ResponseTemplate::new(if fail && !streaming { 400 } else { 200 })+ .insert_header("x-request-id", KEY)+ .insert_header(+ "content-type",+ if streaming {+ "text/event-stream"+ } else {+ "application/json"+ },+ )+ .set_body_string(body),+ )+ .expect(1)+ .mount(&server)+ .await;+ let deployment = json!({+ "schema_version": 1,+ "llm_clients": {"target": {"format": "openai_chat", "base_url": format!("{}/v1", server.uri()), "api_key_env": KEY_ENV, "max_retries": 0}},+ "targets": {"default": {"id": "target/model", "llm_client": "target"}},+ "routes": {"default": {"id": "switchyard/default", "type": "passthrough", "target": "default"}}+ });+ let runtime = SwitchyardRuntime::new(SwitchyardConfig {+ priority: 0,+ switchyard_config_path: None,+ switchyard_config: Some(deployment.as_object().unwrap().clone()),+ })+ .unwrap();+ let mut llm_request = switchyard_protocol::text_request(+ Some("switchyard/default".into()),+ "reflect the synthetic credential",+ );+ llm_request.stream = streaming;+ let request = Request {+ llm_request,+ raw_request: None,+ metadata: Some(Metadata {+ session_id: Some(KEY.into()),+ ..Default::default()+ }),+ };+ let captured = Arc::new(Mutex::new(Vec::new()));+ let output = if streaming {+ let observed = Arc::clone(&captured);+ let execution = runtime+ .execute_stream(+ format,+ request,+ Arc::new(move |event| {+ observed.lock().unwrap().push(event);+ }),+ )+ .await;+ assert!(+ captured.lock().unwrap().is_empty(),+ "stream must remain unpolled"+ );+ captured.lock().unwrap().extend(execution.events);+ let events = execution.result.unwrap().collect::<Vec<_>>().await;+ assert_eq!(events.iter().any(Result::is_err), fail);+ format!("{events:?}")+ } else {+ let execution = runtime.execute_buffered(format, request).await;+ captured.lock().unwrap().extend(execution.events);+ assert_eq!(execution.result.is_err(), fail);+ format!("{:?}", execution.result)+ };+ assert!(+ !output.contains(KEY),+ "{format:?}, streaming={streaming}, fail={fail}: {output}"+ );+ if !fail || streaming {+ assert!(output.contains("ordinary diagnostics"), "{output}");+ assert!(output.contains("[REDACTED]"), "{output}");+ }+ let telemetry = format!("{:?}", captured.lock().unwrap());+ assert!(!telemetry.contains(KEY), "{telemetry}");+ assert!(telemetry.contains("[REDACTED]"), "{telemetry}");+}++#[test]+fn routing_data_and_metric_attributes_are_redacted() {+ let redactor = ProviderKeyRedactor::new(&[KEY.into()]);+ let events = [+ RoutingEvent::Mark(RoutingMark {+ name: "switchyard.routing.decision".into(),+ data: json!({"evidence": {"source": KEY}, "selected_model": KEY}),+ metadata: json!({"session_id": KEY}),+ severity: Some(LogSeverity::Info),+ }),+ counter_metric(+ "switchyard.routing.requests",+ "Requests managed by Switchyard routing.",+ json!({"target_model": KEY, KEY: [KEY, "ordinary"]}),+ json!({"session_id": KEY}),+ ),+ ];+ for event in events {+ let sanitized = sanitize_event(&redactor, event);+ let output = format!("{sanitized:?}");+ assert!(!output.contains(KEY), "{output}");+ assert!(output.contains("[REDACTED]"), "{output}");+ }+}++#[test]+fn client_visible_errors_preserve_only_nonsecret_diagnostics() {+ let runtime = SwitchyardRuntime {+ runner: Runner::new(Vec::new()),+ translation: TranslationEngine::default(),+ redactor: Arc::new(ProviderKeyRedactor::new(&[KEY.into()])),+ };+ let execution: Execution<()> = runtime.sanitize_execution(Execution {+ result: Err(format!("ordinary transport diagnostics: {KEY}")),+ events: Vec::new(),+ });+ assert_eq!(+ execution.result.unwrap_err(),+ "ordinary transport diagnostics: [REDACTED]"+ );+ let request = RelayRequest {+ headers: Map::new(),+ content: json!({"model": "switchyard/default", "messages": [{"role": KEY, "content": "hello"}]}),+ };+ let error = runtime+ .decode_request(WireFormat::OpenAiChat, request, false)+ .err()+ .expect("an invalid role should fail decoding");+ assert!(!error.contains(KEY), "{error}");+}diff --git a/crates/switchyard-nemo-relay-plugin/src/runtime.rs b/crates/switchyard-nemo-relay-plugin/src/runtime.rsindex c29935170..95794f0d8 100644--- a/crates/switchyard-nemo-relay-plugin/src/runtime.rs+++ b/crates/switchyard-nemo-relay-plugin/src/runtime.rs@@ -16,12 +16,18 @@ use switchyard_protocol::{LlmClientError, LlmResponse, LlmResponseChunk, LlmStreamError, Metadata, ProviderExtensions,Request, Response, Usage, WireFormat,};-use switchyard_runner::{Route, RouteErrorSummary, Runner, stream_error_summary};+use switchyard_runner::{+ ProviderKeyRedactor, Route, RouteErrorSummary, Runner, stream_error_summary,+};use switchyard_translation::{TranslationEngine, encode_stream_with_extensions};use crate::config::SwitchyardConfig;use crate::translation;+#[cfg(test)]+#[path = "redaction_tests.rs"]+mod redaction_tests;+const ROUTING_MARK_SCHEMA_VERSION: &str = "1";const MAX_EVIDENCE_STRING_BYTES: usize = 64;@@ -79,16 +85,24 @@ struct RoutedResponse {pub(crate) struct SwitchyardRuntime {runner: Runner,translation: TranslationEngine,+ redactor: Arc<ProviderKeyRedactor>,}impl SwitchyardRuntime {pub(crate) fn new(config: SwitchyardConfig) -> Result<Self, String> {+ let runner = config.load_runner()?;+ let redactor = Arc::new(ProviderKeyRedactor::new(runner.provider_api_keys()));Ok(Self {- runner: config.load_runner()?,+ runner,translation: TranslationEngine::default(),+ redactor,})}+ pub(crate) fn redactor(&self) -> &ProviderKeyRedactor {+ &self.redactor+ }+pub(crate) fn manages_model(&self, model: &str) -> bool {self.runner.route(model).is_some()}@@ -99,7 +113,8 @@ impl SwitchyardRuntime {request: RelayRequest,streaming: bool,) -> Result<Request, String> {- let mut llm_request = translation::decode_request(&self.translation, inbound, &request)?;+ let mut llm_request = translation::decode_request(&self.translation, inbound, &request)+ .map_err(|error| self.redactor.text(error))?;llm_request.stream = streaming;let headers = string_headers(&request.headers);let mut metadata = Metadata::from_headers(&headers);@@ -143,7 +158,10 @@ impl SwitchyardRuntime {if finalization_failed {self.error_mark(&mut events, "response_finalization", None);}- Execution { result, events }+ self.sanitize_execution(Execution {+ result: result.map(|value| self.redactor.value(value)),+ events,+ })}pub(crate) async fn execute_stream(@@ -152,6 +170,10 @@ impl SwitchyardRuntime {request: Request,emit_event: RoutingEventEmitter,) -> Execution<ReturnedEventStream> {+ let redactor = Arc::clone(&self.redactor);+ let emit_event: RoutingEventEmitter = Arc::new(move |event| {+ emit_event(sanitize_event(&redactor, event));+ });let request_extensions = request.llm_request.extensions.clone();let Execution { result, mut events } = self.execute(inbound, request).await;let (result, finalization_failed) = match result {@@ -179,7 +201,27 @@ impl SwitchyardRuntime {if finalization_failed {self.error_mark(&mut events, "response_finalization", None);}- Execution { result, events }+ let redactor = Arc::clone(&self.redactor);+ self.sanitize_execution(Execution {+ result: result.map(|stream| {+ Box::pin(stream.map(move |item| {+ item.map(|value| redactor.value(value))+ .map_err(|error| redactor.text(error))+ })) as ReturnedEventStream+ }),+ events,+ })+ }++ fn sanitize_execution<T>(&self, execution: Execution<T>) -> Execution<T> {+ Execution {+ result: execution.result.map_err(|error| self.redactor.text(error)),+ events: execution+ .events+ .into_iter()+ .map(|event| sanitize_event(&self.redactor, event))+ .collect(),+ }}async fn execute(&self, inbound: WireFormat, request: Request) -> Execution<RoutedResponse> {@@ -406,13 +448,21 @@ fn evidence_for_mark(evidence: Option<Json>) -> Option<Json> {(!evidence.is_empty()).then_some(Json::Object(evidence))}-pub(crate) fn emit_events(runtime: &PluginRuntime, events: Vec<RoutingEvent>) {+pub(crate) fn emit_events(+ runtime: &PluginRuntime,+ redactor: &ProviderKeyRedactor,+ events: Vec<RoutingEvent>,+) {for event in events {- emit_event(runtime, event);+ emit_event(runtime, redactor, event);}}-pub(crate) fn emit_event(runtime: &PluginRuntime, event: RoutingEvent) {+pub(crate) fn emit_event(+ runtime: &PluginRuntime,+ redactor: &ProviderKeyRedactor,+ event: RoutingEvent,+) {let result = match event {RoutingEvent::Mark(mark) => {let data_schema = mark.data_schema();@@ -431,8 +481,31 @@ pub(crate) fn emit_event(runtime: &PluginRuntime, event: RoutingEvent) {.map_err(|error| ("routing metric", metric.name, error)),};if let Err((kind, name, error)) = result {- eprintln!("Switchyard could not emit {kind} {name:?}: {error}");+ let message = redactor.text(format!(+ "Switchyard could not emit {kind} {name:?}: {error}"+ ));+ eprintln!("{message}");+ }+}++// Sanitize both synchronous marks and metrics emitted while a stream is polled.+fn sanitize_event(redactor: &ProviderKeyRedactor, mut event: RoutingEvent) -> RoutingEvent {+ match &mut event {+ RoutingEvent::Mark(mark) => {+ mark.data = redactor.value(std::mem::take(&mut mark.data));+ mark.metadata = redactor.value(std::mem::take(&mut mark.metadata));+ }+ RoutingEvent::Metric(metric) => {+ metric.metadata = redactor.value(std::mem::take(&mut metric.metadata));+ for measurement in &mut metric.measurements {+ measurement.attributes = measurement+ .attributes+ .take()+ .map(|value| redactor.value(value));+ }+ }}+ event}fn take_observations(observations: &Mutex<Vec<RunObservation>>) -> Vec<RunObservation> {@@ -833,6 +906,7 @@ mod tests {SwitchyardRuntime {runner: Runner::new(vec![(ModelId::from(model), route)]),translation: TranslationEngine::default(),+ redactor: Arc::default(),}}diff --git a/crates/switchyard-runner/src/lib.rs b/crates/switchyard-runner/src/lib.rsindex 175eb0204..a488b31a7 100644--- a/crates/switchyard-runner/src/lib.rs+++ b/crates/switchyard-runner/src/lib.rs@@ -6,6 +6,7 @@mod algorithm;mod config;mod failure;+mod provider_key_redactor;mod route;mod runner;@@ -16,6 +17,7 @@ pub use algorithm::{pub use failure::{RouteErrorKind, RouteErrorPhase, RouteErrorSummary, stream_error_summary};// Re-exported because `Route::new` takes it, so a host wiring routes does not need a libsy dep.pub use libsy::RuntimeModels;+pub use provider_key_redactor::ProviderKeyRedactor;pub use route::{AuxiliaryTarget, CallerAuthKind, ModelCapabilities, Route, RunOutput, RunnerError,};diff --git a/crates/switchyard-runner/src/provider_key_redactor.rs b/crates/switchyard-runner/src/provider_key_redactor.rsnew file mode 100644index 000000000..7c16bff85--- /dev/null+++ b/crates/switchyard-runner/src/provider_key_redactor.rs@@ -0,0 +1,211 @@+// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.+// SPDX-License-Identifier: Apache-2.0++//! Shared output-boundary redaction for configured provider credentials.++use serde_json::Value;++/// Replaces configured provider keys without changing the credentials sent upstream.+///+/// Apply this after response translation, including to preserved provider fields.+/// Each call is independent: keys split across stream events or separate JSON strings+/// are not reconstructed. This is exact substring matching, not a general secret detector.+#[derive(Default)]+pub struct ProviderKeyRedactor {+ text: Vec<String>,+ json: Vec<String>,+}++impl ProviderKeyRedactor {+ /// Registers nonempty keys, matching longer keys before their prefixes.+ pub fn new(keys: &[String]) -> Self {+ let mut raw: Vec<String> = keys.iter().filter(|key| !key.is_empty()).cloned().collect();+ raw.sort_by(|a, b| b.len().cmp(&a.len()).then_with(|| a.cmp(b)));+ raw.dedup();+ let mut json: Vec<String> = raw+ .iter()+ .map(|key| {+ let encoded = Value::String(key.clone()).to_string();+ encoded[1..encoded.len() - 1].to_string()+ })+ .collect();+ json.sort_by_key(|key| std::cmp::Reverse(key.len()));+ // Match escaped forms before any raw-key suffix they contain. Otherwise a+ // leading backslash can survive replacement and invalidate embedded JSON.+ let mut text = raw;+ text.extend(json.iter().cloned());+ text.sort_by(|a, b| b.len().cmp(&a.len()).then_with(|| a.cmp(b)));+ text.dedup();+ Self { text, json }+ }++ /// Returns whether there are no configured secrets to redact.+ pub fn is_empty(&self) -> bool {+ self.text.is_empty()+ }++ /// Redacts JSON-escaped keys in serialized JSON, preserving the server's wire behavior.+ pub fn json(&self, value: String) -> String {+ replace(value, &self.json)+ }++ /// Redacts raw and JSON-escaped keys in text, including headers and error messages.+ pub fn text(&self, value: String) -> String {+ replace(value, &self.text)+ }++ /// Redacts raw and JSON-escaped keys in string values and member names throughout JSON.+ pub fn value(&self, value: Value) -> Value {+ if self.is_empty() {+ return value;+ }+ match value {+ Value::String(text) => Value::String(self.text(text)),+ Value::Array(values) => {+ Value::Array(values.into_iter().map(|value| self.value(value)).collect())+ }+ Value::Object(fields) => Value::Object(+ fields+ .into_iter()+ .map(|(name, value)| (self.text(name), self.value(value)))+ .collect(),+ ),+ value => value,+ }+ }+}++fn replace(mut value: String, secrets: &[String]) -> String {+ for secret in secrets {+ if value.contains(secret) {+ value = value.replace(secret, "[REDACTED]");+ }+ }+ value+}++#[cfg(test)]+mod tests {+ use serde_json::json;++ use super::*;++ #[test]+ fn escaped_credentials_are_redacted_in_names_and_nested_values() {+ let key = "synthetic-\"provider\\key-雪";+ let redactor = ProviderKeyRedactor::new(&[key.into()]);+ let value = json!({+ "error": {"debug": format!("Bearer {key}"), key: [key, "ordinary diagnostics"]},+ "count": 3, "enabled": true, "missing": null,+ });+ let sanitized = redactor.value(value.clone());+ assert_eq!(sanitized["error"]["debug"], "Bearer [REDACTED]");+ assert_eq!(+ sanitized["error"]["[REDACTED]"],+ json!(["[REDACTED]", "ordinary diagnostics"])+ );+ assert_eq!(sanitized["count"], 3);+ assert_eq!(sanitized["enabled"], true);+ assert!(sanitized["missing"].is_null());+ let wire: Value = serde_json::from_str(&redactor.json(value.to_string())).unwrap();+ assert_eq!(wire, sanitized);+ assert_eq!(redactor.text(format!("Bearer {key}")), "Bearer [REDACTED]");+ }++ #[test]+ fn tool_arguments_and_preserved_extensions_are_redacted() {+ let redactor = ProviderKeyRedactor::new(&["qa-token".into()]);+ let value = json!({+ "arguments": json!({"credential": "qa-token", "query": "ordinary text"}).to_string(),+ "provider_extension": {"debug": ["Bearer qa-token"]},+ });+ let sanitized = redactor.value(value.clone());+ let arguments: Value =+ serde_json::from_str(sanitized["arguments"].as_str().unwrap()).unwrap();+ assert_eq!(+ arguments,+ json!({"credential": "[REDACTED]", "query": "ordinary text"})+ );+ assert_eq!(+ sanitized["provider_extension"]["debug"],+ json!(["Bearer [REDACTED]"])+ );+ assert_eq!(+ serde_json::from_str::<Value>(&redactor.json(value.to_string())).unwrap(),+ sanitized+ );+ }++ #[test]+ fn json_escaped_credentials_are_redacted_in_error_text() {+ for key in [r#"provider-"key"#, r"provider-\key", r#"\provider-"key"#] {+ let redactor = ProviderKeyRedactor::new(&[key.into()]);+ let detail = json!({"credential": key, "message": "ordinary diagnostics"});+ let text = format!("raw: {key}; provider error: {detail}");+ let expected = format!(+ "raw: [REDACTED]; provider error: {}",+ json!({"credential": "[REDACTED]", "message": "ordinary diagnostics"})+ );+ assert_eq!(redactor.text(text), expected);+ }+ }++ #[test]+ fn nested_json_strings_cannot_recover_escaped_credentials() {+ for key in [r#"provider-"key"#, r"provider-\key", r#"\provider-"key"#] {+ let redactor = ProviderKeyRedactor::new(&[key.into()]);+ let arguments = json!({key: {"credential": key, "query": "ordinary text"}});+ let response = json!({"result": {"arguments": arguments.to_string()}});+ // Decode both layers, as a client consuming serialized tool arguments would.+ let sanitized = redactor.value(response);+ let decoded: Value =+ serde_json::from_str(sanitized["result"]["arguments"].as_str().unwrap()).unwrap();+ assert_eq!(+ decoded,+ json!({"[REDACTED]": {+ "credential": "[REDACTED]", "query": "ordinary text"+ }})+ );+ }+ }++ #[test]+ fn escaped_form_is_replaced_before_an_overlapping_raw_key() {+ let key = r"\provider-key";+ let redactor = ProviderKeyRedactor::new(&[key.into()]);+ let text = json!({"credential": key}).to_string();+ // Replacing the raw suffix first would leave a stray escape in the JSON string.+ let sanitized = redactor.text(text);+ assert_eq!(+ serde_json::from_str::<Value>(&sanitized).unwrap(),+ json!({"credential": "[REDACTED]"})+ );+ }++ #[test]+ fn empty_duplicate_and_overlapping_keys_preserve_nonsecret_content() {+ let redactor = ProviderKeyRedactor::new(&[+ "".into(),+ "qa-token".into(),+ "qa-token-extra".into(),+ "qa-token".into(),+ ]);+ assert_eq!(+ redactor.text("keep qa-token-extra / qa-token".into()),+ "keep [REDACTED] / [REDACTED]"+ );+ assert!(ProviderKeyRedactor::new(&["".into()]).is_empty());+ let value = json!({"choices": [{"delta": {"content": "ordinary text"}}]});+ assert_eq!(redactor.value(value.clone()), value);+ assert_eq!(ProviderKeyRedactor::default().value(value.clone()), value);+ }++ #[test]+ fn independent_events_do_not_reassemble_split_credentials() {+ let redactor = ProviderKeyRedactor::new(&["qa-token".into()]);+ let events = [json!({"delta": "qa-"}), json!({"delta": "token"})];+ for event in events {+ assert_eq!(redactor.value(event.clone()), event);+ }+ }+}diff --git a/crates/switchyard-runner/src/runner.rs b/crates/switchyard-runner/src/runner.rsindex 64b442ae2..249e31271 100644--- a/crates/switchyard-runner/src/runner.rs+++ b/crates/switchyard-runner/src/runner.rs@@ -66,14 +66,14 @@ impl Runner {}}- /// Registers deployment-owned API keys for server response redaction.+ /// Registers deployment-owned API keys for serving-surface output redaction./// TOML loading registers these automatically; programmatic hosts must supply them.pub fn with_provider_api_keys(mut self, keys: Vec<String>) -> Self {self.provider_api_keys = keys;self}- /// Returns deployment-owned secrets for the server's response redactor.+ /// Returns deployment-owned secrets for serving-surface output redactors.pub fn provider_api_keys(&self) -> &[String] {&self.provider_api_keys}diff --git a/crates/switchyard-server/src/redaction.rs b/crates/switchyard-server/src/redaction.rsindex cff0df75b..e069c33cd 100644--- a/crates/switchyard-server/src/redaction.rs+++ b/crates/switchyard-server/src/redaction.rs@@ -11,45 +11,7 @@ use axum::response::Response;use crate::{DEFAULT_MAX_REQUEST_BODY_BYTES, ServerState};-#[derive(Default)]-pub(crate) struct Redactor {- raw: Vec<String>,- json: Vec<String>,-}--impl Redactor {- pub(crate) fn new(keys: &[String]) -> Self {- let mut raw: Vec<String> = keys.iter().filter(|key| !key.is_empty()).cloned().collect();- raw.sort_by(|a, b| b.len().cmp(&a.len()).then_with(|| a.cmp(b)));- raw.dedup();- let mut json = Vec::with_capacity(raw.len());- for key in &raw {- let Ok(encoded) = serde_json::to_string(key) else {- unreachable!("serde_json::to_string on a String cannot fail");- };- json.push(encoded[1..encoded.len() - 1].to_string());- }- json.sort_by_key(|key| std::cmp::Reverse(key.len()));- Self { raw, json }- }-- pub(crate) fn json(&self, value: String) -> String {- replace(value, &self.json)- }-- pub(crate) fn text(&self, value: String) -> String {- replace(value, &self.raw)- }-}--fn replace(mut value: String, secrets: &[String]) -> String {- for secret in secrets {- if value.contains(secret) {- value = value.replace(secret, "[REDACTED]");- }- }- value-}+pub(crate) use switchyard_runner::ProviderKeyRedactor as Redactor;pub(crate) async fn redact_response(State(state): State<ServerState>,@@ -58,7 +20,7 @@ pub(crate) async fn redact_response() -> Response {let mut response = next.run(request).await;let redactor = &state.redactor;- if redactor.raw.is_empty() {+ if redactor.is_empty() {return response;}let is_json = response.headers().get(CONTENT_TYPE).is_some_and(|value| {