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

Failed to start SSH connection if the returned SSH stdout first line contains carriage return character (\r) #11970

Open
2 tasks done
songpola opened this issue Nov 26, 2024 · 0 comments
Labels

Comments

@songpola
Copy link

songpola commented Nov 26, 2024

Describe the bug

When starting an SSH connection, the SSHMaster::startCommand checks the first line of the SSH process output to ensure it matches the string "started". However, the comparison fails due to an extra carriage return character (\r) in the output, causing the program to incorrectly throw an error even when the SSH connection starts successfully.

Steps To Reproduce

  1. Install NixOS-WSL
  2. Symlink ssh to ssh.exe (/mnt/c/Windows/System32/OpenSSH/ssh.exe)
    • Example: ln -s /mnt/c/Windows/System32/OpenSSH/ssh.exe ~/ssh
  3. Prepend the symlinked to PATH
  4. Run nix copy --to ssh://<user>@<host> nixpkgs#hello -vvv to copy hello package as an example
    (Replace <user> and <host> with your remote user and host, respectively)
  5. Error: error: failed to start SSH connection

Example

> nix copy --to ssh://[email protected] nixpkgs#hello -vvv
...
'\\wsl.localhost\NixOS\home\nixos'
CMD.EXE was started with the above path as the current directory.
UNC paths are not supported.  Defaulting to Windows directory.
SSH stdout first line: started
error: failed to start SSH connection to '[email protected]'

Expected behavior

The SSHMaster::startCommand should correctly identify the "started" message from the SSH process output, ignoring any extra carriage return characters, and proceed without throwing an error.

The comparison should succeed if the output is "started" with or without trailing whitespace or carriage return characters.

Metadata

❯ nix-env --version
nix-env (Nix) 2.24.8

Additional context

This is because the ssh.exe on Windows returns with the carriage return characters (\r) at the end.

So the real output is:

Reply     ASCII:  115 116 97 114 116 101 100 13
                  s   t   a  r   t   e   d   \r

Expected  ASCII:  115 116 97 114 116 101 100
                  s   t   a  r   t   e   d

Which is why the check (reply != "started") returns true, and the error occurs.


My goal is to use ssh.exe on Windows to forward the authentication process with 1Password SSH WSL.

I tried symlinking the ssh to /mnt/c/Windows/System32/OpenSSH/ssh.exe using a derivation and set the ssh.package NixOS options to it.

My ssh-wrapper package:

pkgs.symlinkJoin {
  name = "ssh-wrapper";
  paths = [ pkgs.openssh ];
  postBuild = ''
    # Rename ssh to ssh-wsl
    mv $out/bin/ssh $out/bin/ssh-wsl

    # ssh:
    # - WSL: $out/bin/ssh-wsl (${pkgs.openssh}/bin/ssh)
    # - Windows: /mnt/c/Windows/System32/OpenSSH/ssh.exe
    ln -s /mnt/c/Windows/System32/OpenSSH/ssh.exe $out/bin/ssh
  '';
}
programs.ssh.package = pkgs.songpola.ssh-wrapper;

Other programs, such as git, do work as intended though. And after I tried trimming the reply variable (e.g. reply = trim(reply);) before checking it (if (reply != "started") {). IT WORKS!

Checklist


Add 👍 to issues you find important.

@songpola songpola added the bug label Nov 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant