Skip to content

fix: escape ampersand and question mark in ProxyCommand #480

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 7 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pluginUntilBuild=242.*
# that exists, ideally the most recent one, for example
# 233.15325-EAP-CANDIDATE-SNAPSHOT).
platformType=GW
platformVersion=233.15325-EAP-CANDIDATE-SNAPSHOT
platformVersion=233.15619-EAP-CANDIDATE-SNAPSHOT
instrumentationCompiler=242.19533-EAP-CANDIDATE-SNAPSHOT
# Gateway does not have open sources.
platformDownloadSources=true
Expand Down
12 changes: 9 additions & 3 deletions src/main/kotlin/com/coder/gateway/util/Escape.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,22 @@ package com.coder.gateway.util
/**
* Escape an argument to be used in the ProxyCommand of an SSH config.
*
* Escaping happens by surrounding with double quotes if the argument contains
* whitespace and escaping any existing double quotes regardless of whitespace.
* Escaping happens by:
* 1. Surrounding with double quotes if the argument contains whitespace, ?, or
* & (to handle query parameters in URLs) as these characters have special
* meaning in shells.
* 2. Always escaping existing double quotes.
*
* Double quotes does not preserve the literal values of $, `, \, *, @, and !
* (when history expansion is enabled); these are not currently handled.
*
* Throws if the argument is invalid.
*/
fun escape(s: String): String {
if (s.contains("\n")) {
throw Exception("argument cannot contain newlines")
}
if (s.contains(" ") || s.contains("\t")) {
if (s.contains(" ") || s.contains("\t") || s.contains("&") || s.contains("?")) {
return "\"" + s.replace("\"", "\\\"") + "\""
}
return s.replace("\"", "\\\"")
Expand Down
16 changes: 16 additions & 0 deletions src/test/fixtures/outputs/url.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# --- START CODER JETBRAINS test.coder.invalid
Host coder-jetbrains--url--test.coder.invalid
ProxyCommand /tmp/coder-gateway/test.coder.invalid/coder-linux-amd64 --global-config /tmp/coder-gateway/test.coder.invalid/config --url "https://test.coder.invalid?foo=bar&baz=qux" ssh --stdio --usage-app=jetbrains url
ConnectTimeout 0
StrictHostKeyChecking no
UserKnownHostsFile /dev/null
LogLevel ERROR
SetEnv CODER_SSH_SESSION_TYPE=JetBrains
Host coder-jetbrains--url--test.coder.invalid--bg
ProxyCommand /tmp/coder-gateway/test.coder.invalid/coder-linux-amd64 --global-config /tmp/coder-gateway/test.coder.invalid/config --url "https://test.coder.invalid?foo=bar&baz=qux" ssh --stdio --usage-app=disable url
ConnectTimeout 0
StrictHostKeyChecking no
UserKnownHostsFile /dev/null
LogLevel ERROR
SetEnv CODER_SSH_SESSION_TYPE=JetBrains
# --- END CODER JETBRAINS test.coder.invalid
10 changes: 9 additions & 1 deletion src/test/kotlin/com/coder/gateway/cli/CoderCLIManagerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ internal class CoderCLIManagerTest {
val extraConfig: String = "",
val env: Environment = Environment(),
val sshLogDirectory: Path? = null,
val url: URL? = null
)

@Test
Expand Down Expand Up @@ -390,6 +391,13 @@ internal class CoderCLIManagerTest {
"blank",
sshLogDirectory = tmpdir.resolve("ssh-logs"),
),
SSHTest(
listOf("url"),
input = null,
output = "url",
remove = "blank",
url = URL("https://test.coder.invalid?foo=bar&baz=qux"),
),
)

val newlineRe = "\r?\n".toRegex()
Expand All @@ -408,7 +416,7 @@ internal class CoderCLIManagerTest {
env = it.env,
)

val ccm = CoderCLIManager(URL("https://test.coder.invalid"), settings)
val ccm = CoderCLIManager(it.url ?: URL("https://test.coder.invalid"), settings)

// Input is the configuration that we start with, if any.
if (it.input != null) {
Expand Down
4 changes: 4 additions & 0 deletions src/test/kotlin/com/coder/gateway/util/EscapeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ internal class EscapeTest {
"""C:\echo "hello world"""" to """"C:\echo \"hello world\""""",
"""C:\"no"\"spaces"""" to """C:\\"no\"\\"spaces\"""",
""""C:\Program Files\HeaderCommand.exe" --flag""" to """"\"C:\Program Files\HeaderCommand.exe\" --flag"""",
"https://coder.com" to """https://coder.com""",
"https://coder.com/?question" to """"https://coder.com/?question"""",
"https://coder.com/&ampersand" to """"https://coder.com/&ampersand"""",
"https://coder.com/?with&both" to """"https://coder.com/?with&both"""",
)
tests.forEach {
assertEquals(it.value, escape(it.key))
Expand Down
Loading