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
Update tests for escaped paths
  • Loading branch information
code-asher committed Sep 22, 2023
commit 9b8bdb77182ba800eee69ee85d142134e568b79b
33 changes: 17 additions & 16 deletions src/main/kotlin/com/coder/gateway/sdk/CoderCLIManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -195,22 +195,6 @@ class CoderCLIManager @JvmOverloads constructor(
}
}

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

/**
* Escape a command argument by wrapping it in double quotes and escaping
* any slashes and double quotes in the argument. For example, echo "te\st"
* becomes "echo \"te\\st\"".
*
* Throws if the argument is invalid.
*/
private fun escape(s: String): String {
if (s.contains("\n")) {
throw Exception("argument cannot contain newlines")
}
return "\"" + s.replace(escapeRegex, """\\$1""") + "\""
}

/**
* Given an existing SSH config modify it to add or remove the config for
* this deployment and return the modified config or null if it does not
Expand Down Expand Up @@ -522,6 +506,23 @@ class CoderCLIManager @JvmOverloads constructor(
// working binary and the binary directory does not.
return if (cliMatches == null && dataCLIMatches != null) dataCLI else cli
}

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

/**
* Escape a command argument by wrapping it in double quotes and escaping
* any slashes and double quotes in the argument. For example, echo "te\st"
* becomes "echo \"te\\st\"".
*
* Throws if the argument is invalid.
*/
@JvmStatic
fun escape(s: String): String {
if (s.contains("\n")) {
throw Exception("argument cannot contain newlines")
}
return "\"" + s.replace(escapeRegex, """\\$1""") + "\""
}
}
}

Expand Down
15 changes: 13 additions & 2 deletions src/test/groovy/CoderCLIManagerTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,17 @@ class CoderCLIManagerTest extends Specification {
Path.of("/tmp/coder-gateway-test/localappdata/coder-gateway") == dataDir()
}

def "escapes arguments"() {
expect:
CoderCLIManager.escape(str) == expected

where:
str | expected
$/C:\"quote after slash"/$ | $/"C:\\\"quote after slash\""/$
$/C:\echo "hello world"/$ | $/"C:\\echo \"hello world\""/$
$/"C:\Program Files\HeaderCommand.exe" --flag/$ | $/"\"C:\\Program Files\\HeaderCommand.exe\" --flag"/$
}

def "configures an SSH file"() {
given:
def sshConfigPath = tmpdir.resolve(input + "_to_" + output + ".conf")
Expand All @@ -394,8 +405,8 @@ class CoderCLIManagerTest extends Specification {

def expectedConf = Path.of("src/test/fixtures/outputs/").resolve(output + ".conf").toFile().text
.replaceAll("\\r?\\n", System.lineSeparator())
.replace("/tmp/coder-gateway/test.coder.invalid/config", coderConfigPath.toString())
.replace("/tmp/coder-gateway/test.coder.invalid/coder-linux-amd64", ccm.localBinaryPath.toString())
.replace("\"/tmp/coder-gateway/test.coder.invalid/config\"", CoderCLIManager.escape(coderConfigPath.toString()))
.replace("\"/tmp/coder-gateway/test.coder.invalid/coder-linux-amd64\"", CoderCLIManager.escape(ccm.localBinaryPath.toString()))

when:
ccm.configSsh(workspaces.collect { DataGen.workspace(it) }, headerCommand)
Expand Down