Skip to content

Commit b9c7634

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

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-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
/**

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)