Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(utils): validate session name #2607

Merged
merged 5 commits into from
Sep 16, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion zellij-utils/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,26 @@ use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use url::Url;

fn validate_session(name: &str) -> Result<String, String> {
#[cfg(unix)]
{
let mut socket_path = crate::consts::ZELLIJ_SOCK_DIR.clone();
let available_length = 108_usize
.saturating_sub(socket_path.as_os_str().len())
.saturating_sub(1);
socket_path.push(name);
if socket_path.as_os_str().len() >= 108 {
Copy link

@silicakes silicakes Jul 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider extracting 108 into some constant just because it's being reused here and under available_length

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is good idea. @deepsghimire could you please update this? Or if you're busy can I work on this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, yes you can work on this.

// socket path must be less than 108 bytes
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 {
Expand All @@ -25,7 +45,7 @@ pub struct CliArgs {
pub server: Option<PathBuf>,

/// 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<String>,

/// Name of a predefined layout inside the layout directory or the path to a layout file
Expand Down