diff --git a/config/src/config.rs b/config/src/config.rs index b4fd69add..feded0907 100644 --- a/config/src/config.rs +++ b/config/src/config.rs @@ -842,6 +842,7 @@ impl Config { p.display() ) })?; + cfg.check_consistency()?; // Compute but discard the key bindings here so that we raise any // problems earlier than we use them. @@ -914,6 +915,44 @@ impl Config { Ok(config) } + /// Check for logical conflicts in the config + pub fn check_consistency(&self) -> anyhow::Result<()> { + self.check_domain_consistency()?; + Ok(()) + } + + fn check_domain_consistency(&self) -> anyhow::Result<()> { + let mut domains = HashMap::new(); + + let mut check_domain = |name: &str, kind: &str| { + if let Some(exists) = domains.get(name) { + anyhow::bail!( + "{kind} with name \"{name}\" conflicts with \ + another existing {exists} with the same name" + ); + } + domains.insert(name.to_string(), kind.to_string()); + Ok(()) + }; + + for d in &self.unix_domains { + check_domain(&d.name, "unix domain")?; + } + for d in &self.ssh_domains { + check_domain(&d.name, "ssh domain")?; + } + for d in &self.exec_domains { + check_domain(&d.name, "exec domain")?; + } + for d in &self.wsl_domains { + check_domain(&d.name, "wsl domain")?; + } + for d in &self.tls_clients { + check_domain(&d.name, "tls domain")?; + } + Ok(()) + } + pub fn default_config() -> Self { Self::default().compute_extra_defaults(None) } @@ -1656,16 +1695,16 @@ impl Default for ImePreeditRendering { } } -fn validate_row_or_col(value: u16) -> Result<(), String> { - if value < 1 { +fn validate_row_or_col(value: &u16) -> Result<(), String> { + if *value < 1 { Err("initial_cols and initial_rows must be non-zero".to_string()) } else { Ok(()) } } -fn validate_line_height(value: f64) -> Result<(), String> { - if value <= 0.0 { +fn validate_line_height(value: &f64) -> Result<(), String> { + if *value <= 0.0 { Err(format!( "Illegal value {value} for line_height; it must be positive and greater than zero!" )) @@ -1673,3 +1712,15 @@ fn validate_line_height(value: f64) -> Result<(), String> { Ok(()) } } + +pub(crate) fn validate_domain_name(name: &str) -> Result<(), String> { + if name == "local" { + Err(format!( + "\"{name}\" is a built-in domain and cannot be redefined" + )) + } else if name == "" { + Err("the empty string is an invalid domain name".to_string()) + } else { + Ok(()) + } +} diff --git a/config/src/exec_domain.rs b/config/src/exec_domain.rs index 081b17581..ec93057bc 100644 --- a/config/src/exec_domain.rs +++ b/config/src/exec_domain.rs @@ -1,3 +1,4 @@ +use crate::config::validate_domain_name; use luahelper::impl_lua_conversion_dynamic; use wezterm_dynamic::{FromDynamic, ToDynamic, Value}; @@ -10,6 +11,7 @@ impl_lua_conversion_dynamic!(ValueOrFunc); #[derive(Debug, Clone, FromDynamic, ToDynamic)] pub struct ExecDomain { + #[dynamic(validate = "validate_domain_name")] pub name: String, pub fixup_command: String, pub label: Option, diff --git a/config/src/lib.rs b/config/src/lib.rs index 7752f765b..6165d78e4 100644 --- a/config/src/lib.rs +++ b/config/src/lib.rs @@ -284,6 +284,8 @@ fn default_config_with_overrides_applied() -> anyhow::Result { // problems earlier than we use them. let _ = cfg.key_bindings(); + cfg.check_consistency()?; + Ok(cfg) } diff --git a/config/src/ssh.rs b/config/src/ssh.rs index 08ad3ac30..fc0581fc1 100644 --- a/config/src/ssh.rs +++ b/config/src/ssh.rs @@ -1,3 +1,4 @@ +use crate::config::validate_domain_name; use crate::*; use std::fmt::Display; use std::str::FromStr; @@ -49,6 +50,7 @@ impl Default for Shell { pub struct SshDomain { /// The name of this specific domain. Must be unique amongst /// all types of domain in the configuration file. + #[dynamic(validate = "validate_domain_name")] pub name: String, /// identifies the host:port pair of the remote server. diff --git a/config/src/tls.rs b/config/src/tls.rs index 4e42a883a..e2b9cb359 100644 --- a/config/src/tls.rs +++ b/config/src/tls.rs @@ -1,3 +1,4 @@ +use crate::config::validate_domain_name; use crate::*; use wezterm_dynamic::{FromDynamic, ToDynamic}; @@ -29,6 +30,7 @@ pub struct TlsDomainServer { pub struct TlsDomainClient { /// The name of this specific domain. Must be unique amongst /// all types of domain in the configuration file. + #[dynamic(validate = "validate_domain_name")] pub name: String, /// If set, use ssh to connect, start the server, and obtain diff --git a/config/src/unix.rs b/config/src/unix.rs index 50152e44e..afbafbf58 100644 --- a/config/src/unix.rs +++ b/config/src/unix.rs @@ -1,3 +1,4 @@ +use crate::config::validate_domain_name; use crate::*; use std::path::PathBuf; use wezterm_dynamic::{FromDynamic, ToDynamic}; @@ -8,6 +9,7 @@ use wezterm_dynamic::{FromDynamic, ToDynamic}; pub struct UnixDomain { /// The name of this specific domain. Must be unique amongst /// all types of domain in the configuration file. + #[dynamic(validate = "validate_domain_name")] pub name: String, /// The path to the socket. If unspecified, a resonable default @@ -104,6 +106,7 @@ impl UnixDomain { pub fn default_unix_domains() -> Vec { vec![UnixDomain { + name: "unix".to_string(), read_timeout: default_read_timeout(), write_timeout: default_read_timeout(), ..Default::default() diff --git a/config/src/wsl.rs b/config/src/wsl.rs index 3fc98f09b..574a6fdcb 100644 --- a/config/src/wsl.rs +++ b/config/src/wsl.rs @@ -1,3 +1,4 @@ +use crate::config::validate_domain_name; use crate::*; use luahelper::impl_lua_conversion_dynamic; use std::collections::HashMap; @@ -5,6 +6,7 @@ use wezterm_dynamic::{FromDynamic, ToDynamic}; #[derive(Default, Debug, Clone, FromDynamic, ToDynamic)] pub struct WslDomain { + #[dynamic(validate = "validate_domain_name")] pub name: String, pub distribution: Option, pub username: Option, diff --git a/docs/changelog.md b/docs/changelog.md index 1288bbc4b..5175ceab4 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -73,9 +73,11 @@ As features stabilize some brief notes about them will accumulate here. * CharSelect panic when pressing enter when no matches were found [#2580](https://github.com/wez/wezterm/issues/2580) * Panic when setting `initial_rows` or `initial_cols` to `0` - [2593](https://github.com/wez/wezterm/issues/2593) + [#2593](https://github.com/wez/wezterm/issues/2593) * X11: Crash on systems using DRI2 based Intel graphics - [2559](https://github.com/wez/wezterm/issues/2559) + [#2559](https://github.com/wez/wezterm/issues/2559) +* Missing validation of conflicting domain names + [#2618](https://github.com/wez/wezterm/issues/2618) #### Changed * Removed Last Resort fallback font diff --git a/wezterm-dynamic/derive/src/attr.rs b/wezterm-dynamic/derive/src/attr.rs index f801cbf8e..ea65d451c 100644 --- a/wezterm-dynamic/derive/src/attr.rs +++ b/wezterm-dynamic/derive/src/attr.rs @@ -112,7 +112,7 @@ impl<'a> FieldInfo<'a> { }; let validate_value = if let Some(validator) = &self.validate { quote!( - #validator(value).map_err(|msg| { + #validator(&value).map_err(|msg| { wezterm_dynamic::Error::ErrorInField{ type_name: #struct_name, field_name: #name,