Redact command environment variables from log output (#32985)

Before/After (linebreaks added for readability)
```log 
# before
INFO  [project::context_server_store::extension]
loaded command for context server mcp-server-github:
Command { 
  command: "/Users/peter/Library/Application Support/Zed/extensions/work/mcp-server-github/github-mcp-server-v0.5.0/github-mcp-server", 
  args: ["stdio"], 
  env: [("GITHUB_PERSONAL_ACCESS_TOKEN", "gho_WOOOOOOOOOOOOOOO")] 
}

#after
INFO  [project::context_server_store::extension]
loaded command for context server mcp-server-github:
Command {
  command: "/Users/peter/Library/Application Support/Zed/extensions/work/mcp-server-github/github-mcp-server-v0.5.0/github-mcp-server",
  args: ["stdio"],
  env: [("GITHUB_PERSONAL_ACCESS_TOKEN", "[REDACTED]")]
}
```

Release Notes:

- Redact sensitive environment variables from MCP logs
This commit is contained in:
Peter Tripp 2025-06-21 11:19:23 -04:00 committed by GitHub
parent 76e3136369
commit a713c66a9d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 45 additions and 2 deletions

View file

@ -5,6 +5,8 @@ mod slash_command;
use std::ops::Range;
use util::redact::should_redact;
pub use context_server::*;
pub use dap::*;
pub use lsp::*;
@ -14,7 +16,6 @@ pub use slash_command::*;
pub type EnvVars = Vec<(String, String)>;
/// A command.
#[derive(Debug)]
pub struct Command {
/// The command to execute.
pub command: String,
@ -24,6 +25,22 @@ pub struct Command {
pub env: EnvVars,
}
impl std::fmt::Debug for Command {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let filtered_env = self
.env
.iter()
.map(|(k, v)| (k, if should_redact(k) { "[REDACTED]" } else { v }))
.collect::<Vec<_>>();
f.debug_struct("Command")
.field("command", &self.command)
.field("args", &self.args)
.field("env", &filtered_env)
.finish()
}
}
/// A label containing some code.
#[derive(Debug, Clone)]
pub struct CodeLabel {