8000 fix: escape ampersand and question mark in ProxyCommand by kylecarbs · Pull Request #480 · coder/jetbrains-coder · GitHub
[go: up one dir, main page]

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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
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.
  • Loading branch information
code-asher committed Sep 13, 2024
commit ee23d22725f0d7118ede971fc5b471edb85d57dc
16 changes: 11 additions & 5 deletions src/main/kotlin/com/coder/gateway/util/Escape.kt
8000
Original file line number Diff line numberDiff line change
@@ -1,10 +1,18 @@
package com.coder.gateway.util

import com.intellij.icons.ExpUiIcons.Nodes.Exception

/**
* 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.
*/
Expand All @@ -13,9 +21,7 @@ fun escape(s: String): String {
throw Exception("argument cannot contain newlines")
}
if (s.contains(" ") || s.contains("\t") || s.contains("&") || s.contains("?")) {
// See https://github.com/coder/jetbrains-coder/issues/479
// Escape existing " and &
return "\"" + s.replace("\"", "\\\"").replace("&", "\\&").replace("?", "\\?") + "\""
return "\"" + s.replace("\"", "\\\"") + "\""
}
return s.replace("\"", "\\\"")
}
Expand Down
0