{"record":{"id":"523507bb74cd56c8","repo":"LGUG2Z/komorebi","slug":"invalid-command","errorCode":null,"errorMessage":"Invalid command","messagePattern":"Invalid command","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"komorebi-bar/src/config.rs","lineNumber":499,"sourceCode":"                    messages.push(SocketMessage::MouseFollowsFocus(false));\n                    messages.push(config.message.clone());\n                    messages.push(SocketMessage::MouseFollowsFocus(mouse_follows_focus));\n                } else {\n                    messages.push(config.message.clone());\n                }\n\n                tracing::debug!(\"Sending messages: {messages:?}\");\n\n                if komorebi_client::send_batch(messages).is_err() {\n                    tracing::error!(\"could not send commands\");\n                }\n            }\n            MouseMessage::Command(cmd) => {\n                tracing::debug!(\"Executing command: {}\", cmd);\n\n                let cmd_no_env = cmd.replace_env();\n\n                if exec_powershell(cmd_no_env.to_str().expect(\"Invalid command\")).is_err() {\n                    tracing::error!(\"Failed to execute '{}'\", cmd);\n                }\n            }\n        };\n    }\n}\n\nimpl KomobarConfig {\n    pub fn read(path: &PathBuf) -> color_eyre::Result<Self> {\n        let content = std::fs::read_to_string(path)?;\n        let mut value: Self = match path.extension().unwrap().to_string_lossy().as_str() {\n            \"json\" => serde_json::from_str(&content)?,\n            _ => panic!(\"unsupported format\"),\n        };\n\n        if value.frame.is_none() {\n            value.frame = Some(FrameConfig {\n                inner_margin: Position {","sourceCodeStart":481,"sourceCodeEnd":517,"githubUrl":"https://github.com/LGUG2Z/komorebi/blob/e0709f02bfae4e503bf4640f58ee75ecbbfdbb97/komorebi-bar/src/config.rs#L481-L517","documentation":"In komorebi-bar, a mouse-triggered `MouseMessage::Command` is executed via `execute` in config.rs. The command is first converted with `replace_env()` into a `PathBuf`, then unwrapped to a `&str` with `.to_str().expect(\"Invalid command\")`. This expect panics when the command bytes are not valid UTF-8, so the library deliberately aborts rather than execute a garbled command string.","triggerScenarios":"Calling the bar's command-execution path (e.g. via komo's `komorebic` mouse bindings that send `MouseMessage::Command(cmd)`) where `cmd.replace_env()` yields a `PathBuf` whose contents are not valid UTF-8, so `to_str()` returns None and the expect fires.","commonSituations":"Config files saved with a non-UTF-8 encoding (e.g. UTF-16, common when edited in PowerShell's default editor), commands containing binary bytes pasted from the clipboard, or environment-variable substitution (`%VAR%`/`$Env:` expansion) that injects non-UTF-8 bytes into the command string.","solutions":["Re-save the config file (komorebi.json / bar config) as UTF-8 without BOM, then reload the bar.","Check the command string in the config for stray non-ASCII characters or pasted binary content and retype it plainly.","If an environment variable is expanded into the command, verify that variable's value is valid UTF-8 (`echo $Env:MYVAR | Format-Hex`).","As a library-side hardening, replace `.expect(\"Invalid command\")` with a graceful `.to_str().ok()` plus an error log instead of a panic."],"exampleFix":"// before\nif exec_powershell(cmd_no_env.to_str().expect(\"Invalid command\")).is_err() {\n    tracing::error!(\"Failed to execute '{}'\", cmd);\n}\n// after\nmatch cmd_no_env.to_str() {\n    Some(cmd_str) => {\n        if exec_powershell(cmd_str).is_err() {\n            tracing::error!(\"Failed to execute '{}'\", cmd);\n        }\n    }\n    None => tracing::error!(\"Invalid command (not valid UTF-8): {}\", cmd),\n}","handlingStrategy":"validation","validationCode":"// caller-side check before sending a MouseMessage::Command\nfn is_valid_command(cmd: &str) -> bool {\n    !cmd.trim().is_empty() && std::str::from_utf8(cmd.as_bytes()).is_ok()\n}","typeGuard":"fn command_to_str(cmd: &PathBuf) -> Option<&str> {\n    cmd.to_str() // returns None when the command bytes are not valid UTF-8\n}","tryCatchPattern":"// Rust: avoid the expect by handling the None case\nmatch cmd_no_env.to_str() {\n    Some(s) => { let _ = exec_powershell(s); }\n    None => tracing::error!(\"Invalid command (non-UTF-8): {}\", cmd),\n}","preventionTips":["Save all komorebi config files as UTF-8 (no BOM).","Retype pasted commands instead of copying from rich/binary sources.","Validate any env vars expanded into commands contain only text.","Keep command bindings in a linter-checked config to catch stray characters."],"tags":["rust","utf-8","panic","config","command-execution"],"backgroundTag":"invalid-argument-value","analyzedSha":"e0709f02bfae4e503bf4640f58ee75ecbbfdbb97","analyzedAt":"2026-09-06T07:08:05.107Z","contentChangedAt":"2026-09-06T07:08:05.107Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}