@@ -139,7 +139,7 @@ func Test_sshConfigSplitOnCoderSection(t *testing.T) {
139
139
// This test tries to mimic the behavior of OpenSSH
140
140
// when executing e.g. a ProxyCommand.
141
141
// nolint:tparallel
142
- func Test_sshConfigExecEscape (t * testing.T ) {
142
+ func Test_sshConfigProxyCommandEscape (t * testing.T ) {
143
143
t .Parallel ()
144
144
145
145
tests := []struct {
@@ -186,6 +186,60 @@ func Test_sshConfigExecEscape(t *testing.T) {
186
186
}
187
187
}
188
188
189
+ // This test tries to mimic the behavior of OpenSSH
190
+ // when executing e.g. a match exec command.
191
+ // nolint:tparallel
192
+ func Test_sshConfigMatchExecEscape (t * testing.T ) {
193
+ t .Parallel ()
194
+
195
+ tests := []struct {
196
+ name string
197
+ path string
198
+ wantErr bool
199
+ }{
200
+ {"no spaces" , "simple" , false },
201
+ {"spaces" , "path with spaces" , false },
202
+ {"quotes fails" , "path with \" quotes\" " , true },
203
+ {"backslashes" , "path with \\ backslashes" , false },
204
+ {"tabs" , "path with \t tabs" , false },
205
+ {"newline fails" , "path with \n newline" , true },
206
+ }
207
+ // nolint:paralleltest // Fixes a flake
208
+ for _ , tt := range tests {
209
+ tt := tt
210
+ t .Run (tt .name , func (t * testing.T ) {
211
+ cmd := "/bin/sh"
212
+ arg := "-c"
213
+ contents := []byte ("#!/bin/sh\n echo yay\n " )
214
+ if runtime .GOOS == "windows" {
215
+ cmd = "cmd.exe"
216
+ arg = "/c"
217
+ contents = []byte ("echo yay\n " )
218
+ }
219
+
220
+ dir := filepath .Join (t .TempDir (), tt .path )
221
+ err := os .MkdirAll (dir , 0o755 )
222
+ require .NoError (t , err )
223
+ bin := filepath .Join (dir , "coder.bat" ) // Windows will treat it as batch, Linux doesn't care
224
+
225
+ err = os .WriteFile (bin , contents , 0o755 ) //nolint:gosec
226
+ require .NoError (t , err )
227
+
228
+ escaped , err := sshConfigMatchExecEscape (bin )
229
+ if tt .wantErr {
230
+ require .Error (t , err )
231
+ return
232
+ }
233
+ require .NoError (t , err )
234
+
235
+ b , err := exec .Command (cmd , arg , escaped ).CombinedOutput () //nolint:gosec
236
+ require .NoError (t , err )
237
+ got := strings .TrimSpace (string (b ))
238
+ require .Equal (t , "yay" , got )
239
+ })
240
+ }
241
+ }
242
+
189
243
func Test_sshConfigExecEscapeSeparatorForce (t * testing.T ) {
190
244
t .Parallel ()
191
245
0 commit comments