Skip to content

Make argument quotation compatible with cygwin/msys2/gitbash on Windows #97

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

Merged
merged 1 commit into from
Apr 21, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
strategy:
matrix:
go-version: [1.23.x, 1.24.x]
platform: [ubuntu-latest, macos-latest]
platform: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.platform }}
steps:
- uses: actions/checkout@v4
Expand All @@ -20,5 +20,6 @@ jobs:
with:
go-version: ${{ matrix.go-version }}
- run: make
- run: sudo make install
- if: "${{ matrix.platform != 'windows-latest' }}"
run: sudo make install
- run: go test -covermode=atomic -race -v ./...
16 changes: 14 additions & 2 deletions pkg/reversesshfs/reversesshfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (rsf *ReverseSSHFS) Prepare() error {
sshArgs = append(sshArgs, "-p", strconv.Itoa(rsf.Port))
}
sshArgs = append(sshArgs, rsf.Host, "--")
sshArgs = append(sshArgs, "mkdir", "-p", strconv.Quote(rsf.RemotePath))
sshArgs = append(sshArgs, "mkdir", "-p", addQuotes(rsf.RemotePath))
sshCmd := exec.Command(sshBinary, sshArgs...)
logrus.Debugf("executing ssh for preparing sshfs: %s %v", sshCmd.Path, sshCmd.Args)
out, err := sshCmd.CombinedOutput()
Expand Down Expand Up @@ -141,7 +141,7 @@ func (rsf *ReverseSSHFS) Start() error {
sshArgs = append(sshArgs, "-p", strconv.Itoa(rsf.Port))
}
sshArgs = append(sshArgs, rsf.Host, "--")
sshArgs = append(sshArgs, "sshfs", strconv.Quote(":"+rsf.LocalPath), strconv.Quote(rsf.RemotePath), "-o", "slave")
sshArgs = append(sshArgs, "sshfs", addQuotes(":"+rsf.LocalPath), addQuotes(rsf.RemotePath), "-o", "slave")
if rsf.Readonly {
sshArgs = append(sshArgs, "-o", "ro")
}
Expand Down Expand Up @@ -252,6 +252,18 @@ func (rsf *ReverseSSHFS) Start() error {
return nil
}

func addQuotes(input string) string {
input = strconv.Quote(input)
// exec.Command on Windows would escape wrapping double quotes in a way, which is not compatible
// with passing arguments into bash shell over ssh, replacing with single quotes as a workaround.
if runtime.GOOS == "windows" {
Copy link
Member

Choose a reason for hiding this comment

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

Could you add a comment to explain the reason?

Also, can we have a unit test and/or an integration test?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will try to sketch the unit test. It will take some time, I will ping you, when it is done.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I managed to get a reproducer with a command line example. The command is extracted using recording processes started on the host.

"C:\Program Files\Git\usr\bin\ssh.exe" -i /c/Users/User/.local/share/containers/podman/machine/machine -p 60251 core@localhost -- mkdir -p \"/c/Users/runneradmin\"
bash: -c: line 1: unexpected EOF while looking for matching `"'

I don't think this can be reasonably covered by integration test - this would recover spinning VM just for that test.

And the method in question is not unit testable as it actually executes the resulting external commands within itself.

It would be only possible to cover private addQuotes with a unit test, which is actually a single standard library call on all platforms other than Windows, but I will add it and add the comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added basic unit test and also extended test matrix to include Windows.

input = strings.TrimPrefix(input, "\"")
input = strings.TrimSuffix(input, "\"")
return fmt.Sprintf(`'%s'`, input)
}
return input
}

func (rsf *ReverseSSHFS) waitForRemoteReady() error {
scriptName := "wait-for-remote-ready"
scriptTemplate := `#!/bin/sh
Expand Down
35 changes: 35 additions & 0 deletions pkg/reversesshfs/reversesshfs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package reversesshfs

import (
"runtime"
"testing"
)

func TestAddQuotes(t *testing.T) {
type testCase struct {
input string
expected string
}
var testCases []testCase
if runtime.GOOS != "windows" {
testCases = []testCase{
{
input: "/user/test/path",
expected: "\"/user/test/path\"",
},
}
} else {
testCases = []testCase{
{
input: "/user/test/path",
expected: "'/user/test/path'",
},
}
}
for i, tc := range testCases {
got := addQuotes(tc.input)
if got != tc.expected {
t.Errorf("#%d: expected %q, got %q", i, tc.expected, got)
}
}
}