Skip to content

fix: check if OpenSSH supports SetEnv #82

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 1 commit into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
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
9 changes: 6 additions & 3 deletions src/remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import prettyBytes from "pretty-bytes"
import * as semver from "semver"
import * as vscode from "vscode"
import * as ws from "ws"
import { SSHConfig, defaultSSHConfigResponse, mergeSSHConfigValues } from "./sshConfig"
import { SSHConfig, SSHValues, defaultSSHConfigResponse, mergeSSHConfigValues } from "./sshConfig"
import { sshSupportsSetEnv } from "./sshSupport"
import { Storage } from "./storage"

export class Remote {
Expand Down Expand Up @@ -509,7 +510,7 @@ export class Remote {
}

const escape = (str: string): string => `"${str.replace(/"/g, '\\"')}"`
const sshValues = {
const sshValues: SSHValues = {
Host: `${Remote.Prefix}*`,
ProxyCommand: `${escape(binaryPath)} vscodessh --network-info-dir ${escape(
this.storage.getNetworkInfoPath(),
Expand All @@ -520,9 +521,11 @@ export class Remote {
StrictHostKeyChecking: "no",
UserKnownHostsFile: "/dev/null",
LogLevel: "ERROR",
}
if (sshSupportsSetEnv()) {
// This allows for tracking the number of extension
// users connected to workspaces!
SetEnv: "CODER_SSH_SESSION_TYPE=vscode",
sshValues.SetEnv = " CODER_SSH_SESSION_TYPE=vscode"
}

await sshConfig.update(sshValues, sshConfigOverrides)
Expand Down
6 changes: 3 additions & 3 deletions src/sshConfig.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { SSHConfigResponse } from "coder/site/src/api/typesGenerated"
import { writeFile, readFile } from "fs/promises"
import { readFile, writeFile } from "fs/promises"
import { ensureDir } from "fs-extra"
import path from "path"

Expand All @@ -9,13 +8,14 @@ interface Block {
raw: string
}

interface SSHValues {
export interface SSHValues {
Host: string
ProxyCommand: string
ConnectTimeout: string
StrictHostKeyChecking: string
UserKnownHostsFile: string
LogLevel: string
SetEnv?: string
}

// Interface for the file system to make it easier to test
Expand Down
18 changes: 18 additions & 0 deletions src/sshSupport.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { it, expect } from "vitest"
import { sshSupportsSetEnv, sshVersionSupportsSetEnv } from "./sshSupport"

const supports = {
"OpenSSH_8.9p1 Ubuntu-3ubuntu0.1, OpenSSL 3.0.2 15 Mar 2022": true,
"OpenSSH_7.6p1 Ubuntu-4ubuntu0.7, OpenSSL 1.0.2n 7 Dec 2017": false,
"OpenSSH_7.4p1, OpenSSL 1.0.2k-fips 26 Jan 2017": false,
}

Object.entries(supports).forEach(([version, expected]) => {
it(version, () => {
expect(sshVersionSupportsSetEnv(version)).toBe(expected)
})
})

it("current shell supports ssh", () => {
expect(sshSupportsSetEnv()).toBeTruthy()
})
36 changes: 36 additions & 0 deletions src/sshSupport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as childProcess from "child_process"

export function sshSupportsSetEnv(): boolean {
try {
// Run `ssh -V` to get the version string.
const spawned = childProcess.spawnSync("ssh", ["-V"])
// The version string outputs to stderr.
return sshVersionSupportsSetEnv(spawned.stderr.toString().trim())
} catch (error) {
return false
}
}

// sshVersionSupportsSetEnv ensures that the version string from the SSH
// command line supports the `SetEnv` directive.
//
// It was introduced in SSH 7.8 and not all versions support it.
export function sshVersionSupportsSetEnv(sshVersionString: string): boolean {
const match = sshVersionString.match(/OpenSSH_([\d.]+)[^,]*/)
if (match && match[1]) {
const installedVersion = match[1]
const parts = installedVersion.split(".")
if (parts.length < 2) {
return false
}
// 7.8 is the first version that supports SetEnv
if (Number.parseInt(parts[0], 10) < 7) {
return false
}
if (Number.parseInt(parts[1], 10) < 8) {
return false
}
return true
}
return false
}