Skip to content

Commit 1ceaa48

Browse files
committed
Escape slashes in header command
1 parent 3704482 commit 1ceaa48

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

src/main/kotlin/com/coder/gateway/sdk/CoderCLIManager.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,13 +195,15 @@ class CoderCLIManager @JvmOverloads constructor(
195195
}
196196
}
197197

198+
var escapeRegex = """(["\\])""".toRegex()
199+
198200
/**
199201
* Escape a command argument by wrapping it in double quotes and escaping
200-
* any double quotes in the argument. For example, echo "test" becomes
201-
* "echo \"test\"".
202+
* any slashes and double quotes in the argument. For example, echo "te\st"
203+
* becomes "echo \"te\\st\"".
202204
*/
203205
private fun escape(s: String): String {
204-
return "\"" + s.replace("\"", "\\\"") + "\""
206+
return "\"" + s.replace(escapeRegex, """\\$1""") + "\""
205207
}
206208

207209
/**
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# --- START CODER JETBRAINS test.coder.invalid
2+
Host coder-jetbrains--header--test.coder.invalid
3+
HostName coder.header
4+
ProxyCommand "/tmp/coder-gateway/test.coder.invalid/coder-linux-amd64" --global-config "/tmp/coder-gateway/test.coder.invalid/config" --header-command "C:\\Program Files\\My Header Command\\\"also has quotes\"\\HeaderCommand.exe" ssh --stdio header
5+
ConnectTimeout 0
6+
StrictHostKeyChecking no
7+
UserKnownHostsFile /dev/null
8+
LogLevel ERROR
9+
SetEnv CODER_SSH_SESSION_TYPE=JetBrains
10+
# --- END CODER JETBRAINS test.coder.invalid

src/test/groovy/CoderCLIManagerTest.groovy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@ class CoderCLIManagerTest extends Specification {
424424
["foo-bar"] | "no-related-blocks" | "append-no-related-blocks" | "no-related-blocks" | null
425425
["foo-bar"] | "no-newline" | "append-no-newline" | "no-blocks" | null
426426
["header"] | null | "header-command" | "blank" | "my-header-command \"test\""
427+
["header"] | null | "header-command-windows" | "blank" | $/C:\Program Files\My Header Command\"also has quotes"\HeaderCommand.exe/$
427428
}
428429

429430
def "fails if config is malformed"() {

0 commit comments

Comments
 (0)