Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4fe4aca

Browse files
authoredMay 28, 2024··
fix: escape variables in the header command (#279)
1 parent 8963512 commit 4fe4aca

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed
 

‎src/remote.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,12 +589,20 @@ export class Remote {
589589
}
590590

591591
const escape = (str: string): string => `"${str.replace(/"/g, '\\"')}"`
592+
// Escape a command line to be executed by the Coder binary, so ssh doesn't substitute variables.
593+
const escapeSubcommand: (str: string) => string =
594+
os.platform() === "win32"
595+
? // On Windows variables are %VAR%, and we need to use double quotes.
596+
(str) => escape(str).replace(/%/g, "%%")
597+
: // On *nix we can use single quotes to escape $VARS.
598+
// Note single quotes cannot be escaped inside single quotes.
599+
(str) => `'${str.replace(/'/g, "'\\''")}'`
592600

593601
// Add headers from the header command.
594602
let headerArg = ""
595603
const headerCommand = getHeaderCommand(vscode.workspace.getConfiguration())
596604
if (typeof headerCommand === "string" && headerCommand.trim().length > 0) {
597-
headerArg = ` --header-command ${escape(headerCommand)}`
605+
headerArg = ` --header-command ${escapeSubcommand(headerCommand)}`
598606
}
599607
let logArg = ""
600608
if (hasCoderLogs) {

0 commit comments

Comments
 (0)
Please sign in to comment.