Skip to content

Commit d7838b5

Browse files
committed
fix: Guard against overwriting unknown ~/.ssh/coder file
1 parent 8d0f72c commit d7838b5

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

cli/configssh.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ import (
2929
const (
3030
sshDefaultConfigFileName = "~/.ssh/config"
3131
sshDefaultCoderConfigFileName = "~/.ssh/coder"
32-
sshCoderConfigHeader = `# This file is managed by coder. DO NOT EDIT.
32+
sshCoderConfigHeader = "# This file is managed by coder. DO NOT EDIT."
33+
sshCoderConfigDocsHeader = `
3334
#
3435
# You should not hand-edit this file, all changes will be lost upon workspace
3536
# creation, deletion or when running "coder config-ssh".
@@ -131,6 +132,11 @@ func configSSH() *cobra.Command {
131132
if err != nil && !errors.Is(err, fs.ErrNotExist) {
132133
return xerrors.Errorf("read ssh config failed: %w", err)
133134
}
135+
if len(coderConfigRaw) > 0 {
136+
if !bytes.HasPrefix(coderConfigRaw, []byte(sshCoderConfigHeader)) {
137+
return xerrors.Errorf("unexpected content in %s: remove the file and rerun the command to continue", coderConfigFile)
138+
}
139+
}
134140

135141
// Keep track of changes we are making.
136142
var changes []string
@@ -190,6 +196,7 @@ func configSSH() *cobra.Command {
190196

191197
buf := &bytes.Buffer{}
192198
_, _ = buf.WriteString(sshCoderConfigHeader)
199+
_, _ = buf.WriteString(sshCoderConfigDocsHeader)
193200

194201
// Store the provided flags as part of the
195202
// config for future (re)use.

0 commit comments

Comments
 (0)