@@ -175,86 +175,77 @@ function toMsysPath(dir: string): string {
175175const CODER_SSH_CONFIG_HEADER = `# Coder workspace hosts. Do not edit; the Coder extension rewrites this file
176176# on every connection. Override options with the "coder.sshConfig" setting.` ;
177177
178- /**
179- * SSH options a deployment may not set, mirroring the server's validation of
180- * --ssh-config-options (codersdk.ValidateSSHConfigOption).
181- */
182- const DENIED_DEPLOYMENT_KEYS : ReadonlySet < Lowercase < string > > = new Set ( [
183- // Structural directives that escape Coder's managed block.
184- "host" ,
185- "match" ,
186- "include" ,
187- // Directives that run an attacker-supplied command string.
188- "proxycommand" ,
189- "localcommand" ,
190- "permitlocalcommand" ,
191- "remotecommand" ,
192- "knownhostscommand" ,
193- // Directives that load an attacker-controlled shared library.
194- "pkcs11provider" ,
195- "securitykeyprovider" ,
196- "smartcarddevice" ,
197- // Directives that execute a command for X11 authentication.
198- "xauthlocation" ,
199- // Conflicts with Coder's managed ProxyCommand.
200- "proxyjump" ,
178+ /** SSH options a deployment may set; the rest are ignored. */
179+ const ALLOWED_DEPLOYMENT_KEYS : ReadonlySet < Lowercase < string > > = new Set ( [
180+ "addressfamily" ,
181+ "batchmode" ,
182+ "bindaddress" ,
183+ "bindinterface" ,
184+ "casignaturealgorithms" ,
185+ "challengeresponseauthentication" ,
186+ "channeltimeout" ,
187+ "checkhostip" ,
188+ "ciphers" ,
189+ "clearallforwardings" ,
190+ "compression" ,
191+ "connectionattempts" ,
192+ "connecttimeout" ,
193+ "controlmaster" ,
194+ "controlpersist" ,
195+ "escapechar" ,
196+ "exitonforwardfailure" ,
197+ "fingerprinthash" ,
198+ "hashknownhosts" ,
199+ "hostbasedacceptedalgorithms" ,
200+ "hostkeyalgorithms" ,
201+ "identitiesonly" ,
202+ "ipqos" ,
203+ "kbdinteractiveauthentication" ,
204+ "kbdinteractivedevices" ,
205+ "kexalgorithms" ,
206+ "loglevel" ,
207+ "logverbose" ,
208+ "macs" ,
209+ "nohostauthenticationforlocalhost" ,
210+ "numberofpasswordprompts" ,
211+ "obscurekeystroketiming" ,
212+ "passwordauthentication" ,
213+ "port" ,
214+ "preferredauthentications" ,
215+ "pubkeyacceptedalgorithms" ,
216+ "pubkeyacceptedkeytypes" ,
217+ "pubkeyauthentication" ,
218+ "rekeylimit" ,
219+ "requesttty" ,
220+ "requiredrsasize" ,
221+ "serveralivecountmax" ,
222+ "serveraliveinterval" ,
223+ "syslogfacility" ,
224+ "tcpkeepalive" ,
225+ "verifyhostkeydns" ,
226+ "visualhostkey" ,
201227] ) ;
202228
203229/**
204- * Options that send local environment variables (directly or through `${VAR}`
205- * expansion) or local sockets to the workspace. Ignored rather than rejected,
206- * since the server accepts them.
207- */
208- const IGNORED_DEPLOYMENT_KEYS : ReadonlySet < Lowercase < string > > = new Set ( [
209- "sendenv" ,
210- "setenv" ,
211- "user" ,
212- "remoteforward" ,
213- ] ) ;
214-
215- /**
216- * Connection-critical options Coder always writes itself, so the error must
217- * not suggest overriding them in coder.sshConfig.
218- */
219- const PINNED_KEYS : ReadonlySet < Lowercase < string > > = new Set ( [
220- "proxycommand" ,
221- "userknownhostsfile" ,
222- "stricthostkeychecking" ,
223- ] ) ;
224-
225- /**
226- * Validate SSH options sent by the Coder deployment, removing the ignored ones.
227- * Keys in `userOverrides` are exempt from the deny list: the user's value wins
228- * the merge, so the deployment's value is never written.
229- * @throws {Error } when an option is malformed or denied.
230+ * Validate SSH options sent by the Coder deployment, removing the ones outside
231+ * the allow list or repeated in another case.
232+ * @throws {Error } when an option is malformed.
230233 */
231234export function validateDeploymentSshOptions (
232235 options : Record < string , unknown > ,
233- userOverrides : Record < string , string > ,
234- ) : { options : Record < string , string > ; ignored : string [ ] } {
236+ ) : {
237+ options : Record < string , string > ;
238+ ignored : string [ ] ;
239+ } {
235240 validateSshConfigOptions ( options ) ;
236- const overridden = new Set ( Object . keys ( userOverrides ) . map ( lowercase ) ) ;
237- const denied = Object . keys ( options ) . filter ( ( key ) => {
238- const lower = lowercase ( key ) ;
239- return DENIED_DEPLOYMENT_KEYS . has ( lower ) && ! overridden . has ( lower ) ;
241+ const lowerKeys = Object . keys ( options ) . map ( lowercase ) ;
242+ const ignored = Object . keys ( options ) . filter ( ( _key , i ) => {
243+ const lower = lowerKeys [ i ] ;
244+ return (
245+ ! ALLOWED_DEPLOYMENT_KEYS . has ( lower ) ||
246+ lowerKeys . indexOf ( lower ) !== lowerKeys . lastIndexOf ( lower )
247+ ) ;
240248 } ) ;
241- if ( denied . length > 0 ) {
242- const quote = ( keys : string [ ] ) =>
243- keys . map ( ( key ) => JSON . stringify ( key ) ) . join ( ", " ) ;
244- const pinned = denied . filter ( ( key ) => PINNED_KEYS . has ( lowercase ( key ) ) ) ;
245- const overridable = denied . filter ( ( key ) => ! pinned . includes ( key ) ) ;
246- let message = `The Coder deployment tried to set SSH options that could run code or change how Coder connects: ${ quote ( denied ) } .` ;
247- if ( overridable . length > 0 ) {
248- message += ` To allow ${ quote ( overridable ) } , set the option yourself in the "coder.sshConfig" setting, which overrides the deployment.` ;
249- }
250- if ( pinned . length > 0 ) {
251- message += ` Coder manages ${ quote ( pinned ) } , which cannot be overridden.` ;
252- }
253- throw new Error ( message ) ;
254- }
255- const ignored = Object . keys ( options ) . filter ( ( key ) =>
256- IGNORED_DEPLOYMENT_KEYS . has ( lowercase ( key ) ) ,
257- ) ;
258249 return {
259250 options : Object . fromEntries (
260251 Object . entries ( options ) . filter ( ( [ key ] ) => ! ignored . includes ( key ) ) ,
@@ -363,6 +354,7 @@ export function mergeSshConfigValues(
363354 Object . keys ( overrides ) . forEach ( ( key ) => {
364355 caseInsensitiveOverrides [ key . toLowerCase ( ) ] = key ;
365356 } ) ;
357+ const overridden = new Set ( Object . keys ( caseInsensitiveOverrides ) ) ;
366358
367359 Object . keys ( config ) . forEach ( ( key ) => {
368360 const lower = key . toLowerCase ( ) ;
@@ -384,6 +376,9 @@ export function mergeSshConfigValues(
384376
385377 return ;
386378 }
379+ if ( overridden . has ( lower ) ) {
380+ return ;
381+ }
387382 if ( config [ key ] !== "" ) {
388383 merged [ key ] = config [ key ] ;
389384 }
0 commit comments