Skip to content

fix: escape variables in the header command #279

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 2 commits into from
May 28, 2024
Merged
Changes from all commits
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
10 changes: 9 additions & 1 deletion src/remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -606,12 +606,20 @@ export class Remote {
}

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

// Add headers from the header command.
let headerArg = ""
const headerCommand = getHeaderCommand(vscode.workspace.getConfiguration())
if (typeof headerCommand === "string" && headerCommand.trim().length > 0) {
headerArg = ` --header-command ${escape(headerCommand)}`
headerArg = ` --header-command ${escapeSubcommand(headerCommand)}`
}
let logArg = ""
if (hasCoderLogs) {
Expand Down