Skip to content

Commit ee23d22

Browse files
committed
Rely on quotes for escaping
Also add some notes on characters double quotes does not handle, in case we need to do something about them in the future.
1 parent 209077c commit ee23d22

File tree

1 file changed

+11
-5
lines changed
  • src/main/kotlin/com/coder/gateway/util

1 file changed

+11
-5
lines changed

src/main/kotlin/com/coder/gateway/util/Escape.kt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
package com.coder.gateway.util
22

3+
import com.intellij.icons.ExpUiIcons.Nodes.Exception
4+
35
/**
46
* Escape an argument to be used in the ProxyCommand of an SSH config.
57
*
6-
* Escaping happens by surrounding with double quotes if the argument contains
7-
* whitespace and escaping any existing double quotes regardless of whitespace.
8+
* Escaping happens by:
9+
* 1. Surrounding with double quotes if the argument contains whitespace, ?, or
10+
* & (to handle query parameters in URLs) as these characters have special
11+
* meaning in shells.
12+
* 2. Always escaping existing double quotes.
13+
*
14+
* Double quotes does not preserve the literal values of $, `, \, *, @, and !
15+
* (when history expansion is enabled); these are not currently handled.
816
*
917
* Throws if the argument is invalid.
1018
*/
@@ -13,9 +21,7 @@ fun escape(s: String): String {
1321
throw Exception("argument cannot contain newlines")
1422
}
1523
if (s.contains(" ") || s.contains("\t") || s.contains("&") || s.contains("?")) {
16-
// See https://github.com/coder/jetbrains-coder/issues/479
17-
// Escape existing " and &
18-
return "\"" + s.replace("\"", "\\\"").replace("&", "\\&").replace("?", "\\?") + "\""
24+
return "\"" + s.replace("\"", "\\\"") + "\""
1925
}
2026
return s.replace("\"", "\\\"")
2127
}

0 commit comments

Comments
 (0)