Skip to content

Commit ae685e9

Browse files
authored
fix(ssh): resolve the SSH config with ssh -G and forward the session type with SendEnv (#1197)
The extension resolved the Coder host's SSH options with a parser that ignores Include, Match and negated Host lines, so a RemoteCommand set through any of them went undetected. The host is now resolved with `ssh -G`, using the ssh executable the active Remote-SSH extension is configured with, and falls back to the parser if ssh fails or never read the Coder config. - Run `ssh -G` with `-F` only when Remote-SSH passes it, so it reads the same files as the connection. Windows batch wrappers are never run through a shell, and a missing configured ssh is logged rather than replaced by one from PATH. - Set `PermitLocalCommand no` on workspace hosts, so a `LocalCommand` elsewhere in the user's config never runs for workspaces. - Forward `CODER_SSH_SESSION_TYPE` with `SendEnv` from the environment ssh inherits, which works on every OpenSSH version, and drop the `ssh -V` check. - Ignore the deployment's `SendEnv`, `SetEnv`, `User` and `RemoteForward`, which can send local environment variables (directly or through `${VAR}` expansion) or local sockets to the workspace. They are logged and listed in a warning that "Don't Show Again" hides per deployment until a new option appears. - Write the user's own options from `coder.sshConfig` and `coder config-ssh -o` as given. - On Devin 3.12.32 and later, write Coder hosts into the file set in `remote.devinSSH.configFile`, which Devin passes to ssh. Closes VSC-52 Refs VSC-8
1 parent 67a9429 commit ae685e9

19 files changed

Lines changed: 602 additions & 263 deletions

‎CHANGELOG.md‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,25 @@
55
from published versions since it shows up in the VS Code extension changelog
66
tab and is confusing to users. Add it back between releases if needed. -->
77

8+
## Unreleased
9+
10+
### Security
11+
12+
- Workspace hosts now set `PermitLocalCommand no`, so a `LocalCommand` in your
13+
SSH config no longer runs for workspaces. Add `PermitLocalCommand yes` to
14+
`coder.sshConfig` to allow it.
15+
- The deployment's SSH config options can no longer send your local
16+
environment variables or sockets to the workspace: `SendEnv`, `SetEnv`,
17+
`User` and `RemoteForward` from the deployment are ignored, with a warning. Set them in `coder.sshConfig` instead.
18+
19+
### Fixed
20+
21+
- `RemoteCommand` set through an SSH config `Include` or `Match` block is now
22+
detected.
23+
- The session type now reaches the workspace with every OpenSSH version.
24+
- On Devin 3.12.32 and later, workspace hosts go into the file set in
25+
`remote.devinSSH.configFile`.
26+
827
## [v1.18.2](https://github.com/coder/vscode-coder/releases/tag/v1.18.2) (Pre-release) 2026-11-20
928

1029
### Added

‎docs/ARCHITECTURE.md‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,11 @@ connection:
6666
Host coder-cursor.dev.coder.com--*
6767
ConnectTimeout 0
6868
LogLevel ERROR
69+
PermitLocalCommand no
6970
ProxyCommand /tmp/coder --global-config /home/kyle/.config/Cursor/User/globalStorage/coder.coder-remote/dev.coder.com ssh --stdio --usage-app=cursor --network-info-dir /home/kyle/.config/Cursor/User/globalStorage/coder.coder-remote/net --ssh-host-prefix coder-cursor.dev.coder.com-- %h
71+
SendEnv CODER_SSH_SESSION_TYPE
7072
ServerAliveCountMax 3
7173
ServerAliveInterval 10
72-
SetEnv CODER_SSH_SESSION_TYPE=cursor
7374
StrictHostKeyChecking no
7475
UserKnownHostsFile /dev/null
7576
```
@@ -78,16 +79,23 @@ Options merge from three sources, highest priority first: the
7879
`coder.sshConfig` setting, `--ssh-option` flags from a `coder config-ssh`
7980
block, and the deployment's SSH config. Only the deployment's options are
8081
deny-listed (`DENIED_DEPLOYMENT_KEYS` in `src/remote/sshConfig.ts`), since the
81-
other two are the user's own. `SetEnv` is written only when the local ssh
82-
supports it, and `--usage-app` only when both the CLI and the deployment
83-
accept custom session app names; otherwise the session reports as `vscode`.
82+
other two are the user's own. Deployment options that send local environment
83+
variables or sockets to the workspace are ignored with a warning
84+
(`IGNORED_DEPLOYMENT_KEYS`). The user's
85+
`SetEnv` and `SendEnv` values accumulate across sources. The session app goes
86+
to the CLI as `--usage-app` and to the agent as `CODER_SSH_SESSION_TYPE`, set in the
87+
environment ssh inherits and forwarded with `SendEnv`. It is the editor's URI
88+
scheme when both the CLI and the deployment accept custom session app names,
89+
and `vscode` otherwise.
90+
91+
After writing, `ssh -G` resolves the host with the Remote - SSH extension's ssh
92+
executable, falling back to a built-in parser, to detect a `RemoteCommand`.
8493

8594
Which main file gains the include depends on the Remote - SSH extension.
8695
Microsoft's and Cursor's pass `remote.SSH.configFile` to ssh with `-F`, and
8796
VSCodium's parses the file itself instead of running ssh, so all three connect
88-
through it. Antigravity and Windsurf/Devin renamed the setting but spawn ssh
89-
without `-F`, so ssh reads `~/.ssh/config` regardless; we ignore the renamed
90-
setting there rather than add the include where the connection never looks.
97+
through it. Devin passes `remote.devinSSH.configFile` from 3.12.32. Antigravity
98+
spawns ssh without `-F`, so we ignore its renamed setting.
9199

92100
If any step fails, we show an error message. Once the error message is closed
93101
we close the remote so the Remote - SSH connection does not continue to

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
"title": "Coder",
6262
"properties": {
6363
"coder.sshConfig": {
64-
"markdownDescription": "These values will be included in the ssh config file. Eg: `'ConnectTimeout=10'` will set the timeout to 10 seconds. Any values included here will override anything provided by default or by the deployment. To unset a value that is written by default, set the value to the empty string, Eg: `'ConnectTimeout='` will unset it.\n\nNote: `SetEnv` values are accumulated across multiple entries and cannot be unset.",
64+
"markdownDescription": "These values will be included in the ssh config file. Eg: `'ConnectTimeout=10'` will set the timeout to 10 seconds. Any values included here will override anything provided by default or by the deployment. To unset a value that is written by default, set the value to the empty string, Eg: `'ConnectTimeout='` will unset it.\n\nNote: `SetEnv` and `SendEnv` values are accumulated across multiple entries and cannot be unset.",
6565
"type": "array",
6666
"items": {
6767
"title": "SSH Config Value",

‎src/remote/environment.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import type {
1111

1212
import type { WatchedSetting } from "../configWatcher";
1313

14+
export const SESSION_TYPE_ENV = "CODER_SSH_SESSION_TYPE";
15+
1416
type Environment = Record<string, string | undefined>;
1517
type SshProxyEnvironment = Partial<
1618
Record<"HTTP_PROXY" | "HTTPS_PROXY" | "NO_PROXY", string>
@@ -49,8 +51,9 @@ export const SSH_PROXY_SETTINGS: ReadonlyArray<
4951
/**
5052
* Apply the SSH environment that the spawned `coder ssh` ProxyCommand inherits.
5153
* Includes the proxy config (HTTP_PROXY/HTTPS_PROXY/NO_PROXY), read by the coder
52-
* CLI like any Go HTTP client, and the session ID via CODER_TRACE_SESSION_ID so
53-
* the CLI reuses the plugin's session ID instead of generating its own. Applied
54+
* CLI like any Go HTTP client, the session ID via CODER_TRACE_SESSION_ID so
55+
* the CLI reuses the plugin's session ID instead of generating its own, and
56+
* CODER_SSH_SESSION_TYPE, which ssh forwards to the agent through `SendEnv`. Applied
5457
* via both process.env (ssh spawned as a child, `remote.SSH.useLocalServer=true`)
5558
* and the terminal env collection (ssh spawned in a terminal,
5659
* `useLocalServer=false`, which can't see process.env), since the mode isn't
@@ -63,11 +66,13 @@ export function applySshEnvironment(
6366
GlobalEnvironmentVariableCollection,
6467
"persistent" | "replace" | "clear"
6568
>,
69+
sessionApp: string,
6670
env: Environment = process.env,
6771
): { dispose(): void } {
6872
const values: Environment = {
6973
...getSshProxyEnvironment(cfg),
7074
CODER_TRACE_SESSION_ID: sessionId,
75+
[SESSION_TYPE_ENV]: sessionApp,
7176
};
7277
const restoreEnv = applyEnvironment(values, env);
7378

‎src/remote/remote.ts‎

Lines changed: 67 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ import { CoderApi } from "../api/coderApi";
1616
import { HttpStatusCode } from "../api/httpStatusCode";
1717
import { extractAgents } from "../api/models";
1818
import { version as cliVersion } from "../cli/cliExec";
19-
import { expandPath } from "../common/fs";
20-
import { escapeCommandArg, escapeProxyCommandPath } from "../common/strings";
19+
import {
20+
escapeCommandArg,
21+
escapeProxyCommandPath,
22+
lowercase,
23+
} from "../common/strings";
2124
import {
2225
CONFIG_CHANGE_DEBOUNCE_MS,
2326
watchConfigurationChanges,
@@ -55,7 +58,11 @@ import {
5558
parseRemoteAuthority,
5659
sshHostOf,
5760
} from "./authority";
58-
import { applySshEnvironment, SSH_PROXY_SETTINGS } from "./environment";
61+
import {
62+
applySshEnvironment,
63+
SESSION_TYPE_ENV,
64+
SSH_PROXY_SETTINGS,
65+
} from "./environment";
5966
import { migrateAuthToSecretsStorage } from "./migration";
6067
import {
6168
SshConfig,
@@ -65,12 +72,12 @@ import {
6572
parseSshConfig,
6673
validateDeploymentSshOptions,
6774
} from "./sshConfig";
68-
import { getRemoteSshConfigFile } from "./sshExtension";
75+
import { getRemoteSshConfigFile, getRemoteSshPath } from "./sshExtension";
6976
import { applySettingOverrides, buildSshOverrides } from "./sshOverrides";
7077
import { SshProcessMonitor } from "./sshProcess";
7178
import {
7279
computeSshProperties,
73-
sshSupportsSetEnv,
80+
resolveSshProperties,
7481
type SshProperties,
7582
} from "./sshSupport";
7683
import { createManagedPermissions } from "./windowsAcl";
@@ -264,12 +271,6 @@ export class Remote {
264271
const { args, parts, workspaceName, baseUrl, token, disposables } = context;
265272

266273
try {
267-
disposables.push(
268-
applySshEnvironment(
269-
vscode.workspace.getConfiguration(),
270-
this.extensionContext.environmentVariableCollection,
271-
),
272-
);
273274
// Create OAuth session manager for this remote deployment
274275
const remoteOAuthManager = OAuthSessionManager.create(
275276
{ url: baseUrl, safeHostname: parts.safeHostname },
@@ -325,6 +326,13 @@ export class Remote {
325326
safeHostname: parts.safeHostname,
326327
}),
327328
);
329+
disposables.push(
330+
applySshEnvironment(
331+
vscode.workspace.getConfiguration(),
332+
this.extensionContext.environmentVariableCollection,
333+
sessionApp,
334+
),
335+
);
328336

329337
// Write token to keyring or file
330338
if (baseUrl && token !== undefined) {
@@ -884,11 +892,6 @@ export class Remote {
884892
return ["--log-dir", escapeProxyCommandPath(logDir), "-v"];
885893
}
886894

887-
private getMainSshConfigPath(): string {
888-
const configured = getRemoteSshConfigFile();
889-
return expandPath(configured || path.join("~", ".ssh", "config"));
890-
}
891-
892895
// updateSSHConfig updates the SSH configuration with a wildcard that handles
893896
// all Coder entries.
894897
private async updateSSHConfig({
@@ -901,8 +904,13 @@ export class Remote {
901904
}: SshConfigOptions): Promise<SshProperties> {
902905
// Taken from the authority, so a legacy host keeps working.
903906
const { hostPrefix, safeHostname, sshHost } = parts;
907+
// -F skips /etc/ssh/ssh_config, so use it only when Remote-SSH does.
908+
const configFile = getRemoteSshConfigFile();
904909
// One file per (host prefix, deployment); the user's config gains one shared include.
905-
const sshConfig = new SshConfig(this.getMainSshConfigPath(), this.logger);
910+
const sshConfig = new SshConfig(
911+
configFile ?? path.join(os.homedir(), ".ssh", "config"),
912+
this.logger,
913+
);
906914
await sshConfig.load();
907915
// Never loaded: update() regenerates it without reading the old content.
908916
const coderConfig = new SshConfig(
@@ -925,10 +933,15 @@ export class Remote {
925933
let deploymentSshConfig = {};
926934
try {
927935
const deploymentConfig = await workspaceClient.getDeploymentSSHConfig();
928-
deploymentSshConfig = validateDeploymentSshOptions(
936+
const { options, ignored } = validateDeploymentSshOptions(
929937
deploymentConfig.ssh_config_options,
930938
{ ...configSshOptions, ...userConfig },
931939
);
940+
deploymentSshConfig = options;
941+
if (ignored.length > 0) {
942+
this.logger.warn("Ignoring deployment SSH options", ignored);
943+
this.warnIgnoredSshOptions(safeHostname, ignored);
944+
}
932945
} catch (error) {
933946
if (!isAxiosError(error)) {
934947
throw error;
@@ -969,13 +982,12 @@ export class Remote {
969982
ConnectTimeout: "0",
970983
StrictHostKeyChecking: "no",
971984
UserKnownHostsFile: "/dev/null",
985+
PermitLocalCommand: "no",
972986
LogLevel: "ERROR",
973987
ServerAliveInterval: "10",
974988
ServerAliveCountMax: "3",
989+
SendEnv: SESSION_TYPE_ENV,
975990
};
976-
if (sshSupportsSetEnv()) {
977-
sshValues.SetEnv = `CODER_SSH_SESSION_TYPE=${sessionApp}`;
978-
}
979991

980992
// Write our file before including it, so the include never dangles.
981993
await coderConfig.update(sshValues, sshConfigOverrides);
@@ -984,11 +996,40 @@ export class Remote {
984996
safeHostname,
985997
);
986998

987-
// Mirror SSH's parse order; RemoteCommand can come from the user's config.
988-
return computeSshProperties(
989-
parts.sshHost,
990-
`${coderConfig.getRaw()}\n${sshConfig.getRaw()}`,
991-
);
999+
try {
1000+
return await resolveSshProperties(
1001+
getRemoteSshPath(),
1002+
parts.sshHost,
1003+
configFile,
1004+
);
1005+
} catch (error) {
1006+
this.logger.warn("ssh -G failed, parsing the SSH config instead", error);
1007+
return computeSshProperties(
1008+
parts.sshHost,
1009+
`${coderConfig.getRaw()}\n${sshConfig.getRaw()}`,
1010+
);
1011+
}
1012+
}
1013+
1014+
/** Stays hidden while every ignored option was already dismissed. */
1015+
private warnIgnoredSshOptions(safeHostname: string, ignored: string[]): void {
1016+
const memento = this.serviceContainer.getMementoManager();
1017+
const dismissed = new Set(memento.getDismissedSshOptions(safeHostname));
1018+
const keys = ignored.map(lowercase);
1019+
if (keys.every((key) => dismissed.has(key))) {
1020+
return;
1021+
}
1022+
const dontShow = "Don't Show Again";
1023+
void vscode.window
1024+
.showWarningMessage(
1025+
`Ignored SSH options from the Coder deployment that could expose your machine: ${ignored.join(", ")}. To use them, add them to the "coder.sshConfig" setting.`,
1026+
dontShow,
1027+
)
1028+
.then((choice) =>
1029+
choice === dontShow
1030+
? memento.addDismissedSshOptions(safeHostname, keys)
1031+
: undefined,
1032+
);
9921033
}
9931034

9941035
private watchSettings(

0 commit comments

Comments
 (0)