@@ -82,7 +82,7 @@ const UNSAFE_CHARS_REGEX = /[\r\n\0]/;
8282const DRIVE_OR_UNC_PATH_REGEX = / ^ ( [ a - z ] : [ \\ / ] | [ \\ / ] { 2 } [ ^ \\ / ] ) / i;
8383
8484/** The drive of a forward-slashed Windows path, such as `C:/`. */
85- const DRIVE_PREFIX_REGEX = / ^ ( [ a - z ] ) : \/ / i;
85+ const FORWARD_SLASH_DRIVE_REGEX = / ^ ( [ a - z ] ) : \/ / i;
8686
8787/** The drive of an MSYS path, such as `/h` or `/h/dir`. */
8888const MSYS_DRIVE_REGEX = / ^ \/ ( [ a - z ] ) (? = \/ | $ ) / i;
@@ -111,14 +111,25 @@ const INCLUDE_MARKERS: BlockMarkers = {
111111} ;
112112
113113/**
114- * The config Git for Windows ssh reads without `-F` when its home is set by
115- * the environment: `HOME`, else `HOMEDRIVE` and `HOMEPATH`, which a domain
116- * home folder sets (the `env` scheme of its `db_home` default). An MSYS `HOME`
117- * like `/h` is `H:\`, where Git mounts drives. Undefined when Git falls back to
118- * the profile, off Windows, and for MSYS homes like `/home/jane` under Git's
119- * install directory. May be the profile's config under another spelling.
114+ * The config Git for Windows ssh reads without `-F`. Undefined off Windows,
115+ * when Git uses the profile, and for an MSYS home such as `/home/jane`. May
116+ * name the profile's config.
120117 */
121- export function getGitSshConfigPath ( ) : string | undefined {
118+ export function getGitForWindowsSshConfigPath ( ) : string | undefined {
119+ const home = getGitForWindowsHome ( ) ;
120+ return home ? path . win32 . join ( home , ".ssh" , "config" ) : undefined ;
121+ }
122+
123+ /**
124+ * Git for Windows ssh's home as a Windows path:
125+ * - `HOME`, where an MSYS drive like `/h` is `H:\`.
126+ * - Else `HOMEDRIVE` + `HOMEPATH`, which Windows sets at logon: the profile
127+ * by default, a network drive such as `H:\` for a domain home folder.
128+ *
129+ * Undefined off Windows, when neither is set (Git uses the profile), and for
130+ * an MSYS home such as `/home/jane`.
131+ */
132+ function getGitForWindowsHome ( ) : string | undefined {
122133 const { HOME , HOMEDRIVE , HOMEPATH } = process . env ;
123134 const home =
124135 HOME || ( HOMEDRIVE && HOMEPATH ? HOMEDRIVE + HOMEPATH : undefined ) ;
@@ -129,15 +140,35 @@ export function getGitSshConfigPath(): string | undefined {
129140 MSYS_DRIVE_REGEX ,
130141 ( _ , drive : string ) => `${ drive . toUpperCase ( ) } :\\` ,
131142 ) ;
132- return DRIVE_OR_UNC_PATH_REGEX . test ( windowsHome )
133- ? path . win32 . join ( windowsHome , ".ssh" , "config" )
143+ return DRIVE_OR_UNC_PATH_REGEX . test ( windowsHome ) ? windowsHome : undefined ;
144+ }
145+
146+ /**
147+ * Whether every Windows ssh build expands `~` to the profile. Not when `HOME`
148+ * is set, even to the profile, since Windows OpenSSH misreads a non-ASCII one.
149+ */
150+ function windowsSshHomeIsProfile ( ) : boolean {
151+ if ( process . env . HOME ) {
152+ return false ;
153+ }
154+ const gitHome = getGitForWindowsHome ( ) ;
155+ const key = ( dir : string ) => path . win32 . resolve ( dir ) . toLowerCase ( ) ;
156+ return gitHome === undefined || key ( gitHome ) === key ( os . homedir ( ) ) ;
157+ }
158+
159+ /** `dir` relative to the home directory, or undefined when outside it. */
160+ function homeRelativePath ( dir : string ) : string | undefined {
161+ const pathApi = process . platform === "win32" ? path . win32 : path . posix ;
162+ const relative = pathApi . relative ( os . homedir ( ) , dir ) ;
163+ return relative && ! relative . startsWith ( ".." ) && ! pathApi . isAbsolute ( relative )
164+ ? relative
134165 : undefined ;
135166}
136167
137168/** `C:/Users/...` as MSYS builds such as Git for Windows spell it: `/c/Users/...`. */
138169function toMsysPath ( dir : string ) : string {
139170 return dir . replace (
140- DRIVE_PREFIX_REGEX ,
171+ FORWARD_SLASH_DRIVE_REGEX ,
141172 ( _ , drive : string ) => `/${ drive . toLowerCase ( ) } /` ,
142173 ) ;
143174}
@@ -400,7 +431,11 @@ export class SshConfig {
400431 this . logger . debug ( "Loaded SSH config" , this . filePath ) ;
401432 }
402433
403- /** Regenerate the whole per-deployment file; last-writer-wins. */
434+ /**
435+ * Regenerate the whole per-deployment file; last-writer-wins.
436+ * @throws {Error } on invalid values or overrides, a `*.conf` entry that is
437+ * not a regular file, or a failed `stat`, write or rename.
438+ */
404439 async update ( values : SshValues , overrides ?: Record < string , string > ) {
405440 const block = this . renderDeploymentBlock ( values , overrides ) ;
406441 this . raw = `${ CODER_SSH_CONFIG_HEADER } \n\n${ block } ` ;
@@ -411,6 +446,9 @@ export class SshConfig {
411446 * Keep the shared include first so our options win, removing superseded
412447 * blocks. Read-modify-write with no locking, like the CLI's config-ssh;
413448 * when the include is already in place nothing is written.
449+ * @throws {SshConfigBadFormat } on malformed Coder block markers.
450+ * @throws {Error } on an unrepresentable include path, or a failed read,
451+ * `stat`, write or rename.
414452 */
415453 async updateInclude ( includeDir : string , safeHostname : string ) {
416454 const block = this . renderIncludeBlock ( this . includeArguments ( includeDir ) ) ;
@@ -489,22 +527,32 @@ export class SshConfig {
489527 }
490528
491529 /**
492- * `~/...` under the home directory off Windows. Windows gets the absolute
493- * path, since each build expands `~` from a different home: `/C:/...` for
494- * Windows OpenSSH (before 8.0 it reads no other absolute spelling) and
495- * `/c/...` for Git for Windows. Each build skips the one it cannot match.
530+ * How the Include line spells `includeDir`:
531+ * - `~/...` under the home directory; on Windows only when every build's
532+ * `~` is the profile.
533+ * - Else absolute. A Windows drive path gets `/C:/...` (Windows OpenSSH;
534+ * before 8.0 its only absolute form) and `/c/...` (Git for Windows), and
535+ * each build skips the other. A UNC path gets `//server/...` (untested).
536+ *
537+ * Windows OpenSSH reads `/c/...` as `\c\...` on the current drive, which
538+ * any local user can create, so the default setup keeps `~/...`.
496539 */
497540 private includeArguments ( includeDir : string ) : string [ ] {
498- if ( process . platform === "win32" ) {
499- const dir = includeDir . replaceAll ( "\\" , "/" ) ;
500- return DRIVE_PREFIX_REGEX . test ( dir )
501- ? [ `/${ dir } ` , toMsysPath ( dir ) ]
502- : [ dir ] ;
541+ const isWindows = process . platform === "win32" ;
542+ const relative =
543+ ! isWindows || windowsSshHomeIsProfile ( )
544+ ? homeRelativePath ( includeDir )
545+ : undefined ;
546+ if ( relative ) {
547+ return [ `~/${ relative } ` ] ;
548+ }
549+ if ( ! isWindows ) {
550+ return [ includeDir ] ;
503551 }
504- const relative = path . relative ( os . homedir ( ) , includeDir ) ;
505- return relative && ! relative . startsWith ( ".." ) && ! path . isAbsolute ( relative )
506- ? [ `~ /${ relative } ` ]
507- : [ includeDir ] ;
552+ const dir = includeDir . replaceAll ( "\\" , "/" ) ;
553+ return FORWARD_SLASH_DRIVE_REGEX . test ( dir )
554+ ? [ `/${ dir } ` , toMsysPath ( dir ) ]
555+ : [ dir ] ;
508556 }
509557
510558 private escapeIncludePath ( argument : string ) : string {
@@ -535,7 +583,8 @@ export class SshConfig {
535583 rest = this . removeRange ( rest , block ) ;
536584 }
537585 }
538- return [ includeBlock , rest ] . filter ( Boolean ) . join ( "\n\n" ) ;
586+ // Trim like removeRange, so a second call is a no-op.
587+ return [ includeBlock , rest . trimStart ( ) ] . filter ( Boolean ) . join ( "\n\n" ) ;
539588 }
540589
541590 private removeRange ( raw : string , range : Block ) : string {
0 commit comments