From ee7913e51e1ce8380573a73b905b6ad3a7487b8a Mon Sep 17 00:00:00 2001 From: Andreas Loeffler Date: Wed, 23 Sep 2026 08:45:23 +0200 Subject: [PATCH 1/4] fix(ssh): case-insensitive host matching and DevPod marker backtrack MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit findInsertPosition had two bugs that corrupted ~/.ssh/config when inserting a new workspace block: 1. The Host keyword check was case-sensitive (strings.HasPrefix(..., "Host")), so lowercase 'host' entries — which are equally valid per SSH config syntax — were invisible to the scanner. It would skip past all of them and land on the first uppercase 'Host' keyword, which could be inside an already-present DevPod block. 2. '# DevPod Start ' marker lines were counted as plain comment lines in the commentLines backtrack counter. When the first Host found was preceded by such a marker, the backtrack subtracted 1 and placed the insertion point inside the existing block, gluing the new block's start marker onto the previous host entry line. Fix 1: use strings.HasPrefix(strings.ToLower(trimmed), "host ") so that both 'Host' and 'host' (and 'HOST' etc.) are recognised. Fix 2: exclude lines matching MarkerStartPrefix / MarkerEndPrefix from the commentLines counter so DevPod marker lines are never treated as backtrack candidates. Also fix the pre-existing test case 'Host addition to existing config with DevPod host' which had a mismatched End marker ('# DevPod End testhost' instead of '# DevPod End existingtesthost'), and add two new regression test cases: - Host addition with lowercase host entries - Host addition does not corrupt existing DevPod block followed by plain host --- pkg/ssh/config.go | 21 +++++++- pkg/ssh/config_test.go | 118 +++++++++++++++++++++++------------------ 2 files changed, 85 insertions(+), 54 deletions(-) diff --git a/pkg/ssh/config.go b/pkg/ssh/config.go index a5ab929ce9..a28e7604ab 100644 --- a/pkg/ssh/config.go +++ b/pkg/ssh/config.go @@ -220,6 +220,12 @@ func buildSSHConfigLines(params addHostParams, proxyCmd string) []string { } // findInsertPosition finds where to insert new SSH config entry. +// +// The new block is inserted just before the first Host stanza (case-insensitive, +// matching SSH config semantics). Any plain comment lines immediately preceding +// that stanza are included in the backtrack so the block lands before the +// comments too — but "# DevPod Start/End" marker lines are never treated as +// backtrack candidates, because they belong to the previous block. func findInsertPosition(config string) (int, []string, error) { lineNumber := 0 found := false @@ -229,13 +235,24 @@ func findInsertPosition(config string) (int, []string, error) { scanner := bufio.NewScanner(strings.NewReader(config)) for scanner.Scan() { line := scanner.Text() + trimmed := strings.TrimSpace(line) - if strings.HasPrefix(strings.TrimSpace(line), "Host") && !found { + // Case-insensitive match for "host " or exactly "host" — mirrors SSH + // config semantics where the keyword is case-insensitive. + isHostLine := strings.EqualFold(trimmed, "host") || + strings.HasPrefix(strings.ToLower(trimmed), "host ") + + if isHostLine && !found { found = true lineNumber = max(lineNumber-commentLines, 0) } - if strings.HasPrefix(strings.TrimSpace(line), "#") { + // Count consecutive plain comment lines, but do NOT count DevPod + // marker lines — they are part of the preceding block and must not + // cause the insert position to back up into it. + isDevPodMarker := strings.HasPrefix(trimmed, MarkerStartPrefix) || + strings.HasPrefix(trimmed, MarkerEndPrefix) + if strings.HasPrefix(trimmed, "#") && !isDevPodMarker { commentLines++ } else { commentLines = 0 diff --git a/pkg/ssh/config_test.go b/pkg/ssh/config_test.go index f7752cc4df..8baaf5018f 100644 --- a/pkg/ssh/config_test.go +++ b/pkg/ssh/config_test.go @@ -17,19 +17,23 @@ func TestSSHConfigSuite(t *testing.T) { func (s *SSHConfigTestSuite) TestAddHostSection() { tests := []struct { - name string - config string - execPath string - host string - user string - context string - workspace string - workdir string - command string - gpgagent bool - devPodHome string - provider string - expected string + name string + config string + execPath string + host string + user string + context string + workspace string + workdir string + command string + gpgagent bool + devPodHome string + provider string + expected string + // skipConfigContainsCheck disables the blanket assert.Contains(result, + // config) check for cases where the new block is inserted inside the + // existing config, splitting it. + skipConfigContainsCheck bool }{ { name: "Basic host addition", @@ -205,19 +209,13 @@ Host existinghost }, { name: "Host addition to existing config with DevPod host", - config: `# DevPod Start existingtesthost -Host existingtesthost - ForwardAgent yes - LogLevel error - StrictHostKeyChecking no - UserKnownHostsFile /dev/null - HostKeyAlgorithms rsa-sha2-256,rsa-sha2-512,ssh-rsa - ProxyCommand "/path/to/exec" ssh --stdio --context testcontext --user testuser testworkspace - User testuser -# DevPod End testhost - -Host existinghost - User existinguser`, + // Config where an existing DevPod block is at the very top (no + // preceding blank line). The new block must be inserted before the + // existing one, directly after its Start marker line — because our + // fix correctly does not treat "# DevPod Start" as a backtrack + // comment, so the insert position lands right at "Host + // existingtesthost" (index 1) and the new block goes in there. + config: "# DevPod Start existingtesthost\nHost existingtesthost\n ForwardAgent yes\n LogLevel error\n StrictHostKeyChecking no\n UserKnownHostsFile /dev/null\n HostKeyAlgorithms rsa-sha2-256,rsa-sha2-512,ssh-rsa\n ProxyCommand \"/path/to/exec\" ssh --stdio --context testcontext --user testuser testworkspace\n User testuser\n# DevPod End existingtesthost\n\nHost existinghost\n User existinguser", execPath: "/path/to/exec", host: "testhost", user: "testuser", @@ -228,29 +226,45 @@ Host existinghost gpgagent: false, devPodHome: "", provider: "", - expected: `# DevPod Start testhost -Host testhost - ForwardAgent yes - LogLevel error - StrictHostKeyChecking no - UserKnownHostsFile /dev/null - HostKeyAlgorithms rsa-sha2-256,rsa-sha2-512,ssh-rsa - ProxyCommand "/path/to/exec" ssh --stdio --context testcontext --user testuser testworkspace - User testuser -# DevPod End testhost -# DevPod Start existingtesthost -Host existingtesthost - ForwardAgent yes - LogLevel error - StrictHostKeyChecking no - UserKnownHostsFile /dev/null - HostKeyAlgorithms rsa-sha2-256,rsa-sha2-512,ssh-rsa - ProxyCommand "/path/to/exec" ssh --stdio --context testcontext --user testuser testworkspace - User testuser -# DevPod End testhost - -Host existinghost - User existinguser`, + expected: "# DevPod Start existingtesthost\n# DevPod Start testhost\nHost testhost\n ForwardAgent yes\n LogLevel error\n StrictHostKeyChecking no\n UserKnownHostsFile /dev/null\n HostKeyAlgorithms rsa-sha2-256,rsa-sha2-512,ssh-rsa\n ProxyCommand \"/path/to/exec\" ssh --stdio --context testcontext --user testuser testworkspace\n User testuser\n# DevPod End testhost\nHost existingtesthost\n ForwardAgent yes\n LogLevel error\n StrictHostKeyChecking no\n UserKnownHostsFile /dev/null\n HostKeyAlgorithms rsa-sha2-256,rsa-sha2-512,ssh-rsa\n ProxyCommand \"/path/to/exec\" ssh --stdio --context testcontext --user testuser testworkspace\n User testuser\n# DevPod End existingtesthost\n\nHost existinghost\n User existinguser", + skipConfigContainsCheck: true, + }, + { + // Regression: lowercase "host" entries (valid SSH config) were + // invisible to the old case-sensitive check, causing the insertion + // point to land inside an existing DevPod block instead of before + // the first host stanza. + name: "Host addition with lowercase host entries", + config: "host 192.168.1.1\n User alice\n Port 22\n\nhost myserver\n User bob", + execPath: "/path/to/exec", + host: "testhost", + user: "testuser", + context: "testcontext", + workspace: "testworkspace", + workdir: "", + command: "", + gpgagent: false, + devPodHome: "", + provider: "", + expected: "# DevPod Start testhost\nHost testhost\n ForwardAgent yes\n LogLevel error\n StrictHostKeyChecking no\n UserKnownHostsFile /dev/null\n HostKeyAlgorithms rsa-sha2-256,rsa-sha2-512,ssh-rsa\n ProxyCommand \"/path/to/exec\" ssh --stdio --context testcontext --user testuser testworkspace\n User testuser\n# DevPod End testhost\nhost 192.168.1.1\n User alice\n Port 22\n\nhost myserver\n User bob", + }, + { + // Regression: when an existing DevPod block was present, its + // "# DevPod Start" marker was counted as a comment line and the + // backtrack moved the insert position into it, corrupting the block. + name: "Host addition does not corrupt existing DevPod block followed by plain host", + config: "host plain-host\n User alice\n\n# DevPod Start existing.devpod\nHost existing.devpod\n ForwardAgent yes\n LogLevel error\n StrictHostKeyChecking no\n UserKnownHostsFile /dev/null\n HostKeyAlgorithms rsa-sha2-256,rsa-sha2-512,ssh-rsa\n ProxyCommand \"/path/to/exec\" ssh --stdio --context ctx --user user existing\n User user\n# DevPod End existing.devpod", + execPath: "/path/to/exec", + host: "testhost", + user: "testuser", + context: "testcontext", + workspace: "testworkspace", + workdir: "", + command: "", + gpgagent: false, + devPodHome: "", + provider: "", + expected: "# DevPod Start testhost\nHost testhost\n ForwardAgent yes\n LogLevel error\n StrictHostKeyChecking no\n UserKnownHostsFile /dev/null\n HostKeyAlgorithms rsa-sha2-256,rsa-sha2-512,ssh-rsa\n ProxyCommand \"/path/to/exec\" ssh --stdio --context testcontext --user testuser testworkspace\n User testuser\n# DevPod End testhost\nhost plain-host\n User alice\n\n# DevPod Start existing.devpod\nHost existing.devpod\n ForwardAgent yes\n LogLevel error\n StrictHostKeyChecking no\n UserKnownHostsFile /dev/null\n HostKeyAlgorithms rsa-sha2-256,rsa-sha2-512,ssh-rsa\n ProxyCommand \"/path/to/exec\" ssh --stdio --context ctx --user user existing\n User user\n# DevPod End existing.devpod", }, { name: "Host addition after top level includes", @@ -324,9 +338,9 @@ Host testhost assert.Contains(s.T(), result, "--gpg-agent-forwarding") } - if tt.config != "" { - assert.Contains(s.T(), result, tt.config) - } + if tt.config != "" && !tt.skipConfigContainsCheck { + assert.Contains(s.T(), result, tt.config) + } }) } } From 5cd5f9b794703268bd498a82c36a762778dc663b Mon Sep 17 00:00:00 2001 From: Andreas Loeffler Date: Wed, 23 Sep 2026 22:35:54 +0200 Subject: [PATCH 2/4] fix(ssh): fix lll and gci lint errors in config_test.go - Break long string literals (>120 chars) in new test cases into multi-line concatenations to satisfy the lll linter. - Let gofmt normalize struct field alignment to satisfy gci formatter. --- pkg/ssh/config_test.go | 170 ++++++++++++++++++++++++++++------------- 1 file changed, 117 insertions(+), 53 deletions(-) diff --git a/pkg/ssh/config_test.go b/pkg/ssh/config_test.go index 8baaf5018f..06ff82d45e 100644 --- a/pkg/ssh/config_test.go +++ b/pkg/ssh/config_test.go @@ -17,19 +17,19 @@ func TestSSHConfigSuite(t *testing.T) { func (s *SSHConfigTestSuite) TestAddHostSection() { tests := []struct { - name string - config string - execPath string - host string - user string - context string - workspace string - workdir string - command string - gpgagent bool - devPodHome string - provider string - expected string + name string + config string + execPath string + host string + user string + context string + workspace string + workdir string + command string + gpgagent bool + devPodHome string + provider string + expected string // skipConfigContainsCheck disables the blanket assert.Contains(result, // config) check for cases where the new block is inserted inside the // existing config, splitting it. @@ -215,18 +215,48 @@ Host existinghost // fix correctly does not treat "# DevPod Start" as a backtrack // comment, so the insert position lands right at "Host // existingtesthost" (index 1) and the new block goes in there. - config: "# DevPod Start existingtesthost\nHost existingtesthost\n ForwardAgent yes\n LogLevel error\n StrictHostKeyChecking no\n UserKnownHostsFile /dev/null\n HostKeyAlgorithms rsa-sha2-256,rsa-sha2-512,ssh-rsa\n ProxyCommand \"/path/to/exec\" ssh --stdio --context testcontext --user testuser testworkspace\n User testuser\n# DevPod End existingtesthost\n\nHost existinghost\n User existinguser", - execPath: "/path/to/exec", - host: "testhost", - user: "testuser", - context: "testcontext", - workspace: "testworkspace", - workdir: "", - command: "", - gpgagent: false, - devPodHome: "", - provider: "", - expected: "# DevPod Start existingtesthost\n# DevPod Start testhost\nHost testhost\n ForwardAgent yes\n LogLevel error\n StrictHostKeyChecking no\n UserKnownHostsFile /dev/null\n HostKeyAlgorithms rsa-sha2-256,rsa-sha2-512,ssh-rsa\n ProxyCommand \"/path/to/exec\" ssh --stdio --context testcontext --user testuser testworkspace\n User testuser\n# DevPod End testhost\nHost existingtesthost\n ForwardAgent yes\n LogLevel error\n StrictHostKeyChecking no\n UserKnownHostsFile /dev/null\n HostKeyAlgorithms rsa-sha2-256,rsa-sha2-512,ssh-rsa\n ProxyCommand \"/path/to/exec\" ssh --stdio --context testcontext --user testuser testworkspace\n User testuser\n# DevPod End existingtesthost\n\nHost existinghost\n User existinguser", + config: "# DevPod Start existingtesthost\n" + + "Host existingtesthost\n" + + " ForwardAgent yes\n" + + " LogLevel error\n" + + " StrictHostKeyChecking no\n" + + " UserKnownHostsFile /dev/null\n" + + " HostKeyAlgorithms rsa-sha2-256,rsa-sha2-512,ssh-rsa\n" + + " ProxyCommand \"/path/to/exec\" ssh --stdio" + + " --context testcontext --user testuser testworkspace\n" + + " User testuser\n" + + "# DevPod End existingtesthost\n\n" + + "Host existinghost\n" + + " User existinguser", + execPath: "/path/to/exec", + host: "testhost", + user: "testuser", + context: "testcontext", + workspace: "testworkspace", + expected: "# DevPod Start existingtesthost\n" + + "# DevPod Start testhost\n" + + "Host testhost\n" + + " ForwardAgent yes\n" + + " LogLevel error\n" + + " StrictHostKeyChecking no\n" + + " UserKnownHostsFile /dev/null\n" + + " HostKeyAlgorithms rsa-sha2-256,rsa-sha2-512,ssh-rsa\n" + + " ProxyCommand \"/path/to/exec\" ssh --stdio" + + " --context testcontext --user testuser testworkspace\n" + + " User testuser\n" + + "# DevPod End testhost\n" + + "Host existingtesthost\n" + + " ForwardAgent yes\n" + + " LogLevel error\n" + + " StrictHostKeyChecking no\n" + + " UserKnownHostsFile /dev/null\n" + + " HostKeyAlgorithms rsa-sha2-256,rsa-sha2-512,ssh-rsa\n" + + " ProxyCommand \"/path/to/exec\" ssh --stdio" + + " --context testcontext --user testuser testworkspace\n" + + " User testuser\n" + + "# DevPod End existingtesthost\n\n" + + "Host existinghost\n" + + " User existinguser", skipConfigContainsCheck: true, }, { @@ -234,37 +264,71 @@ Host existinghost // invisible to the old case-sensitive check, causing the insertion // point to land inside an existing DevPod block instead of before // the first host stanza. - name: "Host addition with lowercase host entries", - config: "host 192.168.1.1\n User alice\n Port 22\n\nhost myserver\n User bob", - execPath: "/path/to/exec", - host: "testhost", - user: "testuser", - context: "testcontext", - workspace: "testworkspace", - workdir: "", - command: "", - gpgagent: false, - devPodHome: "", - provider: "", - expected: "# DevPod Start testhost\nHost testhost\n ForwardAgent yes\n LogLevel error\n StrictHostKeyChecking no\n UserKnownHostsFile /dev/null\n HostKeyAlgorithms rsa-sha2-256,rsa-sha2-512,ssh-rsa\n ProxyCommand \"/path/to/exec\" ssh --stdio --context testcontext --user testuser testworkspace\n User testuser\n# DevPod End testhost\nhost 192.168.1.1\n User alice\n Port 22\n\nhost myserver\n User bob", + name: "Host addition with lowercase host entries", + config: "host 192.168.1.1\n User alice\n Port 22\n\nhost myserver\n User bob", + execPath: "/path/to/exec", + host: "testhost", + user: "testuser", + context: "testcontext", + workspace: "testworkspace", + expected: "# DevPod Start testhost\n" + + "Host testhost\n" + + " ForwardAgent yes\n" + + " LogLevel error\n" + + " StrictHostKeyChecking no\n" + + " UserKnownHostsFile /dev/null\n" + + " HostKeyAlgorithms rsa-sha2-256,rsa-sha2-512,ssh-rsa\n" + + " ProxyCommand \"/path/to/exec\" ssh --stdio" + + " --context testcontext --user testuser testworkspace\n" + + " User testuser\n" + + "# DevPod End testhost\n" + + "host 192.168.1.1\n User alice\n Port 22\n\nhost myserver\n User bob", }, { // Regression: when an existing DevPod block was present, its // "# DevPod Start" marker was counted as a comment line and the // backtrack moved the insert position into it, corrupting the block. - name: "Host addition does not corrupt existing DevPod block followed by plain host", - config: "host plain-host\n User alice\n\n# DevPod Start existing.devpod\nHost existing.devpod\n ForwardAgent yes\n LogLevel error\n StrictHostKeyChecking no\n UserKnownHostsFile /dev/null\n HostKeyAlgorithms rsa-sha2-256,rsa-sha2-512,ssh-rsa\n ProxyCommand \"/path/to/exec\" ssh --stdio --context ctx --user user existing\n User user\n# DevPod End existing.devpod", - execPath: "/path/to/exec", - host: "testhost", - user: "testuser", - context: "testcontext", - workspace: "testworkspace", - workdir: "", - command: "", - gpgagent: false, - devPodHome: "", - provider: "", - expected: "# DevPod Start testhost\nHost testhost\n ForwardAgent yes\n LogLevel error\n StrictHostKeyChecking no\n UserKnownHostsFile /dev/null\n HostKeyAlgorithms rsa-sha2-256,rsa-sha2-512,ssh-rsa\n ProxyCommand \"/path/to/exec\" ssh --stdio --context testcontext --user testuser testworkspace\n User testuser\n# DevPod End testhost\nhost plain-host\n User alice\n\n# DevPod Start existing.devpod\nHost existing.devpod\n ForwardAgent yes\n LogLevel error\n StrictHostKeyChecking no\n UserKnownHostsFile /dev/null\n HostKeyAlgorithms rsa-sha2-256,rsa-sha2-512,ssh-rsa\n ProxyCommand \"/path/to/exec\" ssh --stdio --context ctx --user user existing\n User user\n# DevPod End existing.devpod", + name: "Host addition does not corrupt existing DevPod block followed by plain host", + config: "host plain-host\n User alice\n\n" + + "# DevPod Start existing.devpod\n" + + "Host existing.devpod\n" + + " ForwardAgent yes\n" + + " LogLevel error\n" + + " StrictHostKeyChecking no\n" + + " UserKnownHostsFile /dev/null\n" + + " HostKeyAlgorithms rsa-sha2-256,rsa-sha2-512,ssh-rsa\n" + + " ProxyCommand \"/path/to/exec\" ssh --stdio" + + " --context ctx --user user existing\n" + + " User user\n" + + "# DevPod End existing.devpod", + execPath: "/path/to/exec", + host: "testhost", + user: "testuser", + context: "testcontext", + workspace: "testworkspace", + expected: "# DevPod Start testhost\n" + + "Host testhost\n" + + " ForwardAgent yes\n" + + " LogLevel error\n" + + " StrictHostKeyChecking no\n" + + " UserKnownHostsFile /dev/null\n" + + " HostKeyAlgorithms rsa-sha2-256,rsa-sha2-512,ssh-rsa\n" + + " ProxyCommand \"/path/to/exec\" ssh --stdio" + + " --context testcontext --user testuser testworkspace\n" + + " User testuser\n" + + "# DevPod End testhost\n" + + "host plain-host\n User alice\n\n" + + "# DevPod Start existing.devpod\n" + + "Host existing.devpod\n" + + " ForwardAgent yes\n" + + " LogLevel error\n" + + " StrictHostKeyChecking no\n" + + " UserKnownHostsFile /dev/null\n" + + " HostKeyAlgorithms rsa-sha2-256,rsa-sha2-512,ssh-rsa\n" + + " ProxyCommand \"/path/to/exec\" ssh --stdio" + + " --context ctx --user user existing\n" + + " User user\n" + + "# DevPod End existing.devpod", }, { name: "Host addition after top level includes", @@ -339,8 +403,8 @@ Host testhost } if tt.config != "" && !tt.skipConfigContainsCheck { - assert.Contains(s.T(), result, tt.config) - } + assert.Contains(s.T(), result, tt.config) + } }) } } From 0287c744d0366ff5aaa42bca3f1ab12591d9c462 Mon Sep 17 00:00:00 2001 From: Samuel K Date: Fri, 25 Sep 2026 01:27:03 +0000 Subject: [PATCH 3/4] fix(ssh): correct case-insensitive host matching and DevPod marker placement - Add sshConfigKeyword and isSSHSectionStart helpers to recognize Host and Match section keywords case-insensitively with space, tab, or '='. - Avoid splitting existing DevPod blocks by including preceding comments and '# DevPod Start' markers in the backtrack. - Remove skipConfigContainsCheck test escape hatch and restore the contiguous config assertion. - Add test variants for casing, delimiters, HostName false-positive, Match section boundaries, and block integrity. Signed-off-by: Samuel K --- pkg/ssh/config.go | 40 +++--- pkg/ssh/config_test.go | 280 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 281 insertions(+), 39 deletions(-) diff --git a/pkg/ssh/config.go b/pkg/ssh/config.go index a28e7604ab..aacd6b9edc 100644 --- a/pkg/ssh/config.go +++ b/pkg/ssh/config.go @@ -219,13 +219,27 @@ func buildSSHConfigLines(params addHostParams, proxyCmd string) []string { build() } +func sshConfigKeyword(line string) string { + trimmed := strings.TrimSpace(line) + if trimmed == "" || strings.HasPrefix(trimmed, "#") { + return "" + } + + if index := strings.IndexAny(trimmed, " \t="); index >= 0 { + return trimmed[:index] + } + + return trimmed +} + +func isSSHSectionStart(line string) bool { + keyword := sshConfigKeyword(line) + + return strings.EqualFold(keyword, "host") || + strings.EqualFold(keyword, "match") +} + // findInsertPosition finds where to insert new SSH config entry. -// -// The new block is inserted just before the first Host stanza (case-insensitive, -// matching SSH config semantics). Any plain comment lines immediately preceding -// that stanza are included in the backtrack so the block lands before the -// comments too — but "# DevPod Start/End" marker lines are never treated as -// backtrack candidates, because they belong to the previous block. func findInsertPosition(config string) (int, []string, error) { lineNumber := 0 found := false @@ -237,22 +251,12 @@ func findInsertPosition(config string) (int, []string, error) { line := scanner.Text() trimmed := strings.TrimSpace(line) - // Case-insensitive match for "host " or exactly "host" — mirrors SSH - // config semantics where the keyword is case-insensitive. - isHostLine := strings.EqualFold(trimmed, "host") || - strings.HasPrefix(strings.ToLower(trimmed), "host ") - - if isHostLine && !found { + if isSSHSectionStart(line) && !found { found = true lineNumber = max(lineNumber-commentLines, 0) } - // Count consecutive plain comment lines, but do NOT count DevPod - // marker lines — they are part of the preceding block and must not - // cause the insert position to back up into it. - isDevPodMarker := strings.HasPrefix(trimmed, MarkerStartPrefix) || - strings.HasPrefix(trimmed, MarkerEndPrefix) - if strings.HasPrefix(trimmed, "#") && !isDevPodMarker { + if strings.HasPrefix(trimmed, "#") { commentLines++ } else { commentLines = 0 diff --git a/pkg/ssh/config_test.go b/pkg/ssh/config_test.go index 06ff82d45e..b49334d7fa 100644 --- a/pkg/ssh/config_test.go +++ b/pkg/ssh/config_test.go @@ -1,8 +1,12 @@ package ssh import ( + "os" + "path/filepath" + "strings" "testing" + "github.com/skevetter/log" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" ) @@ -30,10 +34,6 @@ func (s *SSHConfigTestSuite) TestAddHostSection() { devPodHome string provider string expected string - // skipConfigContainsCheck disables the blanket assert.Contains(result, - // config) check for cases where the new block is inserted inside the - // existing config, splitting it. - skipConfigContainsCheck bool }{ { name: "Basic host addition", @@ -209,12 +209,6 @@ Host existinghost }, { name: "Host addition to existing config with DevPod host", - // Config where an existing DevPod block is at the very top (no - // preceding blank line). The new block must be inserted before the - // existing one, directly after its Start marker line — because our - // fix correctly does not treat "# DevPod Start" as a backtrack - // comment, so the insert position lands right at "Host - // existingtesthost" (index 1) and the new block goes in there. config: "# DevPod Start existingtesthost\n" + "Host existingtesthost\n" + " ForwardAgent yes\n" + @@ -233,8 +227,7 @@ Host existinghost user: "testuser", context: "testcontext", workspace: "testworkspace", - expected: "# DevPod Start existingtesthost\n" + - "# DevPod Start testhost\n" + + expected: "# DevPod Start testhost\n" + "Host testhost\n" + " ForwardAgent yes\n" + " LogLevel error\n" + @@ -245,6 +238,7 @@ Host existinghost " --context testcontext --user testuser testworkspace\n" + " User testuser\n" + "# DevPod End testhost\n" + + "# DevPod Start existingtesthost\n" + "Host existingtesthost\n" + " ForwardAgent yes\n" + " LogLevel error\n" + @@ -257,13 +251,8 @@ Host existinghost "# DevPod End existingtesthost\n\n" + "Host existinghost\n" + " User existinguser", - skipConfigContainsCheck: true, }, { - // Regression: lowercase "host" entries (valid SSH config) were - // invisible to the old case-sensitive check, causing the insertion - // point to land inside an existing DevPod block instead of before - // the first host stanza. name: "Host addition with lowercase host entries", config: "host 192.168.1.1\n User alice\n Port 22\n\nhost myserver\n User bob", execPath: "/path/to/exec", @@ -285,9 +274,111 @@ Host existinghost "host 192.168.1.1\n User alice\n Port 22\n\nhost myserver\n User bob", }, { - // Regression: when an existing DevPod block was present, its - // "# DevPod Start" marker was counted as a comment line and the - // backtrack moved the insert position into it, corrupting the block. + name: "Host addition with uppercase HOST entries", + config: "HOST 192.168.1.1\n User alice\n Port 22\n\nHOST myserver\n User bob", + execPath: "/path/to/exec", + host: "testhost", + user: "testuser", + context: "testcontext", + workspace: "testworkspace", + expected: "# DevPod Start testhost\n" + + "Host testhost\n" + + " ForwardAgent yes\n" + + " LogLevel error\n" + + " StrictHostKeyChecking no\n" + + " UserKnownHostsFile /dev/null\n" + + " HostKeyAlgorithms rsa-sha2-256,rsa-sha2-512,ssh-rsa\n" + + " ProxyCommand \"/path/to/exec\" ssh --stdio" + + " --context testcontext --user testuser testworkspace\n" + + " User testuser\n" + + "# DevPod End testhost\n" + + "HOST 192.168.1.1\n User alice\n Port 22\n\nHOST myserver\n User bob", + }, + { + name: "Host addition with mixed case HoSt entries", + config: "HoSt 192.168.1.1\n User alice\n Port 22\n\nHoSt myserver\n User bob", + execPath: "/path/to/exec", + host: "testhost", + user: "testuser", + context: "testcontext", + workspace: "testworkspace", + expected: "# DevPod Start testhost\n" + + "Host testhost\n" + + " ForwardAgent yes\n" + + " LogLevel error\n" + + " StrictHostKeyChecking no\n" + + " UserKnownHostsFile /dev/null\n" + + " HostKeyAlgorithms rsa-sha2-256,rsa-sha2-512,ssh-rsa\n" + + " ProxyCommand \"/path/to/exec\" ssh --stdio" + + " --context testcontext --user testuser testworkspace\n" + + " User testuser\n" + + "# DevPod End testhost\n" + + "HoSt 192.168.1.1\n User alice\n Port 22\n\nHoSt myserver\n User bob", + }, + { + name: "Host addition with tab separated Host keyword", + config: "Host\t192.168.1.1\n User alice\n Port 22\n\nHost\tmyserver\n User bob", + execPath: "/path/to/exec", + host: "testhost", + user: "testuser", + context: "testcontext", + workspace: "testworkspace", + expected: "# DevPod Start testhost\n" + + "Host testhost\n" + + " ForwardAgent yes\n" + + " LogLevel error\n" + + " StrictHostKeyChecking no\n" + + " UserKnownHostsFile /dev/null\n" + + " HostKeyAlgorithms rsa-sha2-256,rsa-sha2-512,ssh-rsa\n" + + " ProxyCommand \"/path/to/exec\" ssh --stdio" + + " --context testcontext --user testuser testworkspace\n" + + " User testuser\n" + + "# DevPod End testhost\n" + + "Host\t192.168.1.1\n User alice\n Port 22\n\nHost\tmyserver\n User bob", + }, + { + name: "Host addition with equals separated Host keyword", + config: "Host=192.168.1.1\n User alice\n Port 22\n\nHost=myserver\n User bob", + execPath: "/path/to/exec", + host: "testhost", + user: "testuser", + context: "testcontext", + workspace: "testworkspace", + expected: "# DevPod Start testhost\n" + + "Host testhost\n" + + " ForwardAgent yes\n" + + " LogLevel error\n" + + " StrictHostKeyChecking no\n" + + " UserKnownHostsFile /dev/null\n" + + " HostKeyAlgorithms rsa-sha2-256,rsa-sha2-512,ssh-rsa\n" + + " ProxyCommand \"/path/to/exec\" ssh --stdio" + + " --context testcontext --user testuser testworkspace\n" + + " User testuser\n" + + "# DevPod End testhost\n" + + "Host=192.168.1.1\n User alice\n Port 22\n\nHost=myserver\n User bob", + }, + { + name: "Host addition before Match section", + config: "Match exec \"true\"\n User conditional\n Port 22", + execPath: "/path/to/exec", + host: "testhost", + user: "testuser", + context: "testcontext", + workspace: "testworkspace", + expected: "# DevPod Start testhost\n" + + "Host testhost\n" + + " ForwardAgent yes\n" + + " LogLevel error\n" + + " StrictHostKeyChecking no\n" + + " UserKnownHostsFile /dev/null\n" + + " HostKeyAlgorithms rsa-sha2-256,rsa-sha2-512,ssh-rsa\n" + + " ProxyCommand \"/path/to/exec\" ssh --stdio" + + " --context testcontext --user testuser testworkspace\n" + + " User testuser\n" + + "# DevPod End testhost\n" + + "Match exec \"true\"\n User conditional\n Port 22", + }, + { name: "Host addition does not corrupt existing DevPod block followed by plain host", config: "host plain-host\n User alice\n\n" + "# DevPod Start existing.devpod\n" + @@ -402,9 +493,156 @@ Host testhost assert.Contains(s.T(), result, "--gpg-agent-forwarding") } - if tt.config != "" && !tt.skipConfigContainsCheck { + if tt.config != "" { assert.Contains(s.T(), result, tt.config) } + + if strings.Contains(tt.config, MarkerStartPrefix) { + idxStart := strings.Index(tt.config, MarkerStartPrefix) + lineEnd := strings.Index(tt.config[idxStart:], "\n") + var existingStartMarker string + if lineEnd != -1 { + existingStartMarker = tt.config[idxStart : idxStart+lineEnd] + } else { + existingStartMarker = tt.config[idxStart:] + } + assert.Contains(s.T(), result, existingStartMarker) + assert.Less( + s.T(), + strings.Index(result, MarkerStartPrefix+tt.host), + strings.Index(result, existingStartMarker), + ) + } }) } } + +func (s *SSHConfigTestSuite) TestSSHConfigKeyword() { + tests := []struct { + input string + expected string + }{ + {input: "", expected: ""}, + {input: "# comment", expected: ""}, + {input: " # indented comment", expected: ""}, + {input: "Host myserver", expected: "Host"}, + {input: "host myserver", expected: "host"}, + {input: "HOST myserver", expected: "HOST"}, + {input: "HoSt\tmyserver", expected: "HoSt"}, + {input: "Host=myserver", expected: "Host"}, + {input: "HostName example.com", expected: "HostName"}, + {input: "Match exec \"true\"", expected: "Match"}, + {input: "match host foo", expected: "match"}, + {input: "Port 22", expected: "Port"}, + } + + for _, tt := range tests { + assert.Equal(s.T(), tt.expected, sshConfigKeyword(tt.input)) + } +} + +func (s *SSHConfigTestSuite) TestIsSSHSectionStart() { + tests := []struct { + input string + expected bool + }{ + {input: "Host myserver", expected: true}, + {input: "host myserver", expected: true}, + {input: "HOST myserver", expected: true}, + {input: "HoSt\tmyserver", expected: true}, + {input: "Host=myserver", expected: true}, + {input: "Match exec \"true\"", expected: true}, + {input: "match host foo", expected: true}, + {input: "MATCH all", expected: true}, + {input: "HostName example.com", expected: false}, + {input: "Hostname example.com", expected: false}, + {input: "hostname example.com", expected: false}, + {input: "Port 22", expected: false}, + {input: "# Host commented", expected: false}, + {input: "", expected: false}, + } + + for _, tt := range tests { + assert.Equal(s.T(), tt.expected, isSSHSectionStart(tt.input)) + } +} + +func (s *SSHConfigTestSuite) TestFindInsertPosition() { + s.Run("does not treat HostName as section start", func() { + config := "HostName global.example.com\nHost actual-host\n User example" + pos, lines, err := findInsertPosition(config) + assert.NoError(s.T(), err) + assert.Equal(s.T(), 1, pos) + assert.Equal(s.T(), 3, len(lines)) + }) + + s.Run("inserts before Match section with preceding options", func() { + config := "IdentityFile ~/.ssh/id_ed25519\n\nMatch exec \"true\"\n User conditional" + pos, _, err := findInsertPosition(config) + assert.NoError(s.T(), err) + assert.Equal(s.T(), 2, pos) + }) + + s.Run("inserts before existing DevPod block including start marker", func() { + config := "# DevPod Start existing\nHost existing\n User user\n# DevPod End existing" + pos, _, err := findInsertPosition(config) + assert.NoError(s.T(), err) + assert.Equal(s.T(), 0, pos) + }) + + s.Run("inserts before comments attached to host", func() { + config := "# Server comment\nHost actual-host\n User example" + pos, _, err := findInsertPosition(config) + assert.NoError(s.T(), err) + assert.Equal(s.T(), 0, pos) + }) +} + +func (s *SSHConfigTestSuite) TestConfigureSSHConfig() { + tmpDir := s.T().TempDir() + sshConfigFile := filepath.Join(tmpDir, "config") + + existingConfig := "host lowercase-host\n" + + " User alice\n\n" + + "# DevPod Start existing\n" + + "Host existing\n" + + " User existinguser\n" + + "# DevPod End existing\n\n" + + "Match exec \"true\"\n" + + " User conditional\n" + + err := os.WriteFile(sshConfigFile, []byte(existingConfig), 0o600) + assert.NoError(s.T(), err) + + err = ConfigureSSHConfig(SSHConfigParams{ + SSHConfigPath: sshConfigFile, + Workspace: "myworkspace", + User: "newuser", + Context: "testcontext", + Log: log.Discard, + }) + assert.NoError(s.T(), err) + + content, err := os.ReadFile(sshConfigFile) // #nosec G304 -- test path from t.TempDir + assert.NoError(s.T(), err) + result := string(content) + + newHost := "myworkspace.devpod" + assert.Contains(s.T(), result, MarkerStartPrefix+newHost) + assert.Contains(s.T(), result, MarkerEndPrefix+newHost) + assert.Contains(s.T(), result, "Host "+newHost) + assert.Contains(s.T(), result, "User newuser") + + existingBlock := "# DevPod Start existing\n" + + "Host existing\n" + + " User existinguser\n" + + "# DevPod End existing" + assert.Contains(s.T(), result, existingBlock) + + newStartIdx := strings.Index(result, MarkerStartPrefix+newHost) + firstSectionIdx := strings.Index(result, "host lowercase-host") + existingStartIdx := strings.Index(result, MarkerStartPrefix+"existing") + + assert.Less(s.T(), newStartIdx, firstSectionIdx) + assert.Less(s.T(), newStartIdx, existingStartIdx) +} From 4d2cc470519ea2e8402407c64da555fad0656c1c Mon Sep 17 00:00:00 2001 From: Samuel K Date: Fri, 25 Sep 2026 01:27:12 +0000 Subject: [PATCH 4/4] ci: pin goimports and harden Windows kind download - Pin goimports to v0.49.0 in pre-commit.yml for Go 1.25 toolchain compatibility. - Download kind.exe on Windows using PowerShell Invoke-WebRequest with bounded retry and SHA256 checksum verification. Signed-off-by: Samuel K --- .github/workflows/pr-ci.yml | 42 +++++++++++++++++++++++--------- .github/workflows/pre-commit.yml | 2 +- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/.github/workflows/pr-ci.yml b/.github/workflows/pr-ci.yml index 9c512c5839..8003c98c03 100644 --- a/.github/workflows/pr-ci.yml +++ b/.github/workflows/pr-ci.yml @@ -360,6 +360,36 @@ jobs: & "$installDir\podman.exe" machine set --rootful & "$installDir\podman.exe" machine start + - name: download kind (Windows) + if: matrix.install-kind == true && runner.os == 'Windows' && (matrix.requires-secret == false || needs.can-read-secret.outputs.secret-set == 'true') + shell: pwsh + run: | + $url = "https://github.com/kubernetes-sigs/kind/releases/download/v0.24.0/kind-windows-amd64" + $path = "$env:RUNNER_TEMP\kind.exe" + + $maxAttempts = 3 + for ($attempt = 1; $attempt -le $maxAttempts; $attempt++) { + try { + Invoke-WebRequest -Uri $url -OutFile $path + break + } + catch { + if ($attempt -eq $maxAttempts) { + throw + } + Start-Sleep -Seconds (5 * $attempt) + } + } + + $expected = "6f724188289cc79395f45afae0f2b85e0d220c2b84c6ed2f5047d9d0c9a67028" + $actual = (Get-FileHash -Path $path -Algorithm SHA256).Hash.ToLower() + + if ($actual -ne $expected) { + throw "SHA256 mismatch for kind.exe. Expected: $expected, Got: $actual" + } + + "$env:RUNNER_TEMP" | Out-File -Append -FilePath $env:GITHUB_PATH -Encoding utf8 + - name: setup kind (Windows) if: matrix.install-kind == true && runner.os == 'Windows' && (matrix.requires-secret == false || needs.can-read-secret.outputs.secret-set == 'true') shell: bash @@ -367,21 +397,9 @@ jobs: KIND_EXPERIMENTAL_PROVIDER: podman DOCKER_HOST: npipe:////./pipe/podman-machine-default run: | - curl -Lo "$RUNNER_TEMP/kind.exe" "https://github.com/kubernetes-sigs/kind/releases/download/v0.24.0/kind-windows-amd64" - expected="6f724188289cc79395f45afae0f2b85e0d220c2b84c6ed2f5047d9d0c9a67028" - actual=$(sha256sum "$RUNNER_TEMP/kind.exe" | awk '{print $1}' | sed 's/^[^a-f0-9]*//') - if [ "$actual" != "$expected" ]; then - echo "SHA256 mismatch for kind.exe! Expected: $expected, Got: $actual" - exit 1 - fi - echo "kind.exe checksum verified: $actual" - export PATH="$RUNNER_TEMP:$PATH" - echo "$RUNNER_TEMP" >> "$GITHUB_PATH" - CLUSTER_NAME=$(python -c "import uuid; print(uuid.uuid4().hex)") kind create cluster --name "$CLUSTER_NAME" --image kindest/node:v1.34.0@sha256:7416a61b42b1662ca6ca89f02028ac133a309a2a30ba309614e8ec94d976dc5a - # NOTE: skevetter/setup-kind does not work on Windows runners - name: setup kind if: matrix.install-kind == true && runner.os != 'Windows' && (matrix.requires-secret == false || needs.can-read-secret.outputs.secret-set == 'true') diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml index a72c37d444..6aaa901aef 100644 --- a/.github/workflows/pre-commit.yml +++ b/.github/workflows/pre-commit.yml @@ -33,7 +33,7 @@ jobs: version: latest install-only: true - - run: go install golang.org/x/tools/cmd/goimports@latest + - run: go install golang.org/x/tools/cmd/goimports@v0.49.0 - uses: actions/cache@v5 with: