Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Redact environment variables from language server spawn errors (#44783)
Peter Tripp413f4ea49c99
diff --git a/crates/project/src/lsp_store.rs b/crates/project/src/lsp_store.rsindex 6696ec8c4c2..5093b6977a1 100644--- a/crates/project/src/lsp_store.rs+++ b/crates/project/src/lsp_store.rs@@ -128,6 +128,7 @@ use util::{ConnectionResult, ResultExt as _, debug_panic, defer, maybe, merge_json_value_into,paths::{PathStyle, SanitizedPath},post_inc,+ redact::redact_command,rel_path::RelPath,};@@ -577,9 +578,12 @@ impl LocalLspStore {},},);- log::error!("Failed to start language server {server_name:?}: {err:?}");+ log::error!(+ "Failed to start language server {server_name:?}: {}",+ redact_command(&format!("{err:?}"))+ );if !log.is_empty() {- log::error!("server stderr: {log}");+ log::error!("server stderr: {}", redact_command(&log));}None}diff --git a/crates/util/src/redact.rs b/crates/util/src/redact.rsindex 6b297dfb58b..ad11f7618b1 100644--- a/crates/util/src/redact.rs+++ b/crates/util/src/redact.rs@@ -1,3 +1,9 @@+use std::sync::LazyLock;++static REDACT_REGEX: LazyLock<regex::Regex> = LazyLock::new(|| {+ regex::Regex::new(r#"([A-Z_][A-Z0-9_]*)=("(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'|\S+)"#).unwrap()+});+/// Whether a given environment variable name should have its value redactedpub fn should_redact(env_var_name: &str) -> bool {const REDACTED_SUFFIXES: &[&str] = &[@@ -13,3 +19,31 @@ pub fn should_redact(env_var_name: &str) -> bool {.iter().any(|suffix| env_var_name.ends_with(suffix))}++/// Redact a string which could include a command with environment variables+pub fn redact_command(command: &str) -> String {+ REDACT_REGEX+ .replace_all(command, |caps: ®ex::Captures| {+ let var_name = &caps[1];+ let value = &caps[2];+ if should_redact(var_name) {+ format!(r#"{}="[REDACTED]""#, var_name)+ } else {+ format!("{}={}", var_name, value)+ }+ })+ .to_string()+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn test_redact_string_with_multiple_env_vars() {+ let input = r#"failed to spawn command cd "/code/something" && ANTHROPIC_API_KEY="sk-ant-api03-WOOOO" COMMAND_MODE="unix2003" GEMINI_API_KEY="AIGEMINIFACE" HOME="/Users/foo""#;+ let result = redact_command(input);+ let expected = r#"failed to spawn command cd "/code/something" && ANTHROPIC_API_KEY="[REDACTED]" COMMAND_MODE="unix2003" GEMINI_API_KEY="[REDACTED]" HOME="/Users/foo""#;+ assert_eq!(result, expected);+ }+}