Skip to content

Add header command setting #303

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 11 commits into from
Sep 22, 2023
Prev Previous commit
Next Next commit
Escape slashes in header command
  • Loading branch information
code-asher committed Sep 22, 2023
commit 1ceaa48708e6d60cbdd11bb98bb280d52d4df266
8 changes: 5 additions & 3 deletions src/main/kotlin/com/coder/gateway/sdk/CoderCLIManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,15 @@ class CoderCLIManager @JvmOverloads constructor(
}
}

var escapeRegex = """(["\\])""".toRegex()

/**
* Escape a command argument by wrapping it in double quotes and escaping
* any double quotes in the argument. For example, echo "test" becomes
* "echo \"test\"".
* any slashes and double quotes in the argument. For example, echo "te\st"
* becomes "echo \"te\\st\"".
*/
private fun escape(s: String): String {
return "\"" + s.replace("\"", "\\\"") + "\""
return "\"" + s.replace(escapeRegex, """\\$1""") + "\""
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/test/fixtures/outputs/header-command-windows.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# --- START CODER JETBRAINS test.coder.invalid
Host coder-jetbrains--header--test.coder.invalid
HostName coder.header
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
ConnectTimeout 0
StrictHostKeyChecking no
UserKnownHostsFile /dev/null
LogLevel ERROR
SetEnv CODER_SSH_SESSION_TYPE=JetBrains
# --- END CODER JETBRAINS test.coder.invalid
1 change: 1 addition & 0 deletions src/test/groovy/CoderCLIManagerTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ class CoderCLIManagerTest extends Specification {
["foo-bar"] | "no-related-blocks" | "append-no-related-blocks" | "no-related-blocks" | null
["foo-bar"] | "no-newline" | "append-no-newline" | "no-blocks" | null
["header"] | null | "header-command" | "blank" | "my-header-command \"test\""
["header"] | null | "header-command-windows" | "blank" | $/C:\Program Files\My Header Command\"also has quotes"\HeaderCommand.exe/$
}

def "fails if config is malformed"() {
Expand Down