From 8d56def4fc3441b7231eda8c2ca944445af43920 Mon Sep 17 00:00:00 2001 From: har7an <99636919+har7an@users.noreply.github.com> Date: Thu, 13 Oct 2022 08:17:54 +0000 Subject: [PATCH] zellij/commands: Prevent recursive sessions (#1766) * zellij/commands: Prevent recursive sessions with session names specified in layout files. A "recursive session" is created when, while running inside some zellij session, a user attempts to spawn zellij and make it attach to that same session. When attaching via CLI (`zellij attach`) we explicitly check for this condition and error out when needed. However, besides `zellij attach` it is also possible to declare the session to attach to in layout files like so: ```yaml session: name: "foo" ``` This takes a different codepath when starting zellij, and hence bypases the checks we already have in place to avoid recursive sessions. Hence, we implement the check in the other codepath, too, and prevent recursive sessions from happening for good. Fixes: #1735 * changelog: fix recursive zellij sessions --- CHANGELOG.md | 1 + src/commands.rs | 21 +++++++++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 92a30e5c..50e0f2d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) * fix: error on mixed nodes in layouts (https://github.com/zellij-org/zellij/pull/1791) * fix: error on duplicate pane_template / tab_template definitions in layouts (https://github.com/zellij-org/zellij/pull/1792) * fix: accept session-name through the cli properly (https://github.com/zellij-org/zellij/pull/1793) +* fix: Prevent recursive sessions from layout files (https://github.com/zellij-org/zellij/pull/1766) ## [0.31.4] - 2022-09-09 * Terminal compatibility: improve vttest compliance (https://github.com/zellij-org/zellij/pull/1671) diff --git a/src/commands.rs b/src/commands.rs index b4e2e1da..1acaf12f 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -275,10 +275,10 @@ fn attach_with_session_name( ) -> ClientInfo { match &session_name { Some(session) if create => { - if !session_exists(session).unwrap() { - ClientInfo::New(session_name.unwrap()) - } else { + if session_exists(session).unwrap() { ClientInfo::Attach(session_name.unwrap(), config_options) + } else { + ClientInfo::New(session_name.unwrap()) } }, Some(prefix) => match match_session_name(prefix).unwrap() { @@ -351,7 +351,7 @@ pub(crate) fn start_client(opts: CliArgs) { if let Ok(val) = std::env::var(envs::SESSION_NAME_ENV_KEY) { if val == *client.get_session_name() { - eprintln!("You are trying to attach to the current session(\"{}\"). Zellij does not support nesting a session in itself.", val); + eprintln!("You are trying to attach to the current session (\"{}\"). Zellij does not support nesting a session in itself.", val); process::exit(1); } } @@ -391,6 +391,19 @@ pub(crate) fn start_client(opts: CliArgs) { ); } else { if let Some(session_name) = config_options.session_name.as_ref() { + if let Ok(val) = envs::get_session_name() { + // This prevents the same type of recursion as above, only that here we + // don't get the command to "attach", but to start a new session instead. + // This occurs for example when declaring the session name inside a layout + // file and then, from within this session, trying to open a new zellij + // session with the same layout. This causes an infinite recursion in the + // `zellij_server::terminal_bytes::listen` task, flooding the server and + // clients with infinite `Render` requests. + if *session_name == val { + eprintln!("You are trying to attach to the current session (\"{}\"). Zellij does not support nesting a session in itself.", session_name); + process::exit(1); + } + } match config_options.attach_to_session { Some(true) => { let client = attach_with_session_name(