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
This commit is contained in:
har7an 2022-10-13 08:17:54 +00:00 committed by GitHub
parent e07dfcde78
commit 8d56def4fc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 4 deletions

View file

@ -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)

View file

@ -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() {
@ -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(