From f1bef8f0d150df38352379a2bce5666838cb0438 Mon Sep 17 00:00:00 2001 From: deepsghimire <70006817+deepsghimire@users.noreply.github.com> Date: Sat, 16 Sep 2023 12:00:35 +0545 Subject: [PATCH] fix(utils): validate session name (#2607) * fix(utils): validate session name * cargo fmt * refactor: assign constant values to variables * refactor: move operations unrealted to the condition --------- Co-authored-by: Jae-Heon Ji --- zellij-utils/src/cli.rs | 26 +++++++++++++++++++++++++- zellij-utils/src/consts.rs | 2 ++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/zellij-utils/src/cli.rs b/zellij-utils/src/cli.rs index f8c59f5430..323834b370 100644 --- a/zellij-utils/src/cli.rs +++ b/zellij-utils/src/cli.rs @@ -9,6 +9,30 @@ use serde::{Deserialize, Serialize}; use std::path::PathBuf; use url::Url; +fn validate_session(name: &str) -> Result { + #[cfg(unix)] + { + use crate::consts::ZELLIJ_SOCK_MAX_LENGTH; + + let mut socket_path = crate::consts::ZELLIJ_SOCK_DIR.clone(); + socket_path.push(name); + + if socket_path.as_os_str().len() >= ZELLIJ_SOCK_MAX_LENGTH { + // socket path must be less than 108 bytes + let available_length = ZELLIJ_SOCK_MAX_LENGTH + .saturating_sub(socket_path.as_os_str().len()) + .saturating_sub(1); + + return Err(format!( + "session name must be less than {} characters", + available_length + )); + }; + }; + + Ok(name.to_owned()) +} + #[derive(Parser, Default, Debug, Clone, Serialize, Deserialize)] #[clap(version, name = "zellij")] pub struct CliArgs { @@ -25,7 +49,7 @@ pub struct CliArgs { pub server: Option, /// Specify name of a new session - #[clap(long, short, overrides_with = "session", value_parser)] + #[clap(long, short, overrides_with = "session", value_parser = validate_session)] pub session: Option, /// Name of a predefined layout inside the layout directory or the path to a layout file diff --git a/zellij-utils/src/consts.rs b/zellij-utils/src/consts.rs index 43990b4baf..9091052bb1 100644 --- a/zellij-utils/src/consts.rs +++ b/zellij-utils/src/consts.rs @@ -112,6 +112,8 @@ mod unix_only { use nix::unistd::Uid; use std::env::temp_dir; + pub const ZELLIJ_SOCK_MAX_LENGTH: usize = 108; + lazy_static! { static ref UID: Uid = Uid::current(); pub static ref ZELLIJ_TMP_DIR: PathBuf = temp_dir().join(format!("zellij-{}", *UID));