Skip to content

bug: config-ssh writes bad Match exec clause on Windows #18199

Closed

Description

@spikecurtis

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

coder config-ssh writes a config stanza like

# ------------START-CODER-----------
# This section is managed by coder. DO NOT EDIT.
#
# You should not hand-edit this section unless you are removing it, all
# changes will be lost when running "coder config-ssh".
#
Host coder.*
        ConnectTimeout=0
        StrictHostKeyChecking=no
        UserKnownHostsFile=/dev/null
        LogLevel ERROR
        ProxyCommand "C:\Program Files\Coder\bin\coder.exe" --global-config C:\Users\micha\AppData\Roaming\coderv2 ssh --stdio --ssh-host-prefix coder. %h

Host *.coder
        ConnectTimeout=0
        StrictHostKeyChecking=no
        UserKnownHostsFile=/dev/null
        LogLevel ERROR

Match host *.coder !exec ""C:\Program Files\Coder\bin\coder.exe" connect exists %h"
        ProxyCommand "C:\Program Files\Coder\bin\coder.exe" --global-config C:\Users\micha\AppData\Roaming\coderv2 ssh --stdio --hostname-suffix coder %h
# ------------END-CODER------------

This results in a failed ssh invocation

C:\Users\micha\Downloads> ssh net-integration.coder
Missing Match criteria for exec
C:\\Users\\micha/.ssh/config line 66: Bad Match condition
C:\\Users\\micha/.ssh/config: terminating, 1 bad configuration options

Relevant Log Output

Expected Behavior

ssh should connect

Steps to Reproduce

  1. Install Coder on Windows to a path with a space in it like C:\Program Files\Coder\
  2. run coder config-ssh
  3. run ssh <workspace>

Environment

  • Host OS: Windows 11
  • Coder version: 2.22.0

Additional Context

No response

Activity

  1. self-assigned this
    on Jun 3, 2025
  2. added
    s1
    on Jun 3, 2025
  3. added theissue type on Jun 3, 2025
  4. added
    regression
    on Jun 4, 2025
  5. spikecurtis commented on Jun 5, 2025

    @spikecurtis
    CollaboratorAuthor

    The news is pretty bad. OpenSSH parses the Match line with a very simple tokenizer that accepts "-enclosed strings for the exec command, and has no supported escape sequences for ". This means we cannot include " within the command to execute.

    To make matters worse, on Windows, OpenSSH passes the string directly to cmd.exe for execution, and as far as I can tell, the only supported way to call a path that has spaces in it is to surround it with ".

    We can't actually include " directly, but here is a horrible workaround:

    Match host *.coder !exec "for /f %%a in ('powershell.exe -Command [char]34') do @cmd.exe /c %%aC:\Program Files\Coder\bin\coder.exe%%a connect exists %h"
    	ProxyCommand "C:\Program Files\Coder\bin\coder.exe" --global-config C:\Users\micha\AppData\Roaming\coderv2 ssh --stdio --hostname-suffix coder %h
    

    The key insight here is to store the character " in a variable (%a in this case, but the % itself needs to be escaped, so it becomes %%a), and then use that variable to construct the double-quoted path: %%aC:\Program Files\Coder\bin\coder.exe%%a connect exists %h. How do we generate a single " character without actually using that character? I couldn't find any command in cmd.exe to do it, but powershell.exe can convert ASCII to characters like this: [char]34 (where 34 is the code point for ").

  6. added a commit that references this issue on Jun 6, 2025
    d47a53d
  7. added a commit that references this issue on Jul 7, 2025
    b01a101
  8. added a commit that references this issue on Jul 8, 2025
    049feec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

regressions1

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions