Skip to content

Commit d2992ad

Browse files
committed
fix: Guard against overwriting unknown ~/.ssh/coder file
1 parent cd2b2f8 commit d2992ad

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".
@@ -133,6 +134,11 @@ func configSSH() *cobra.Command {
133134
if err != nil && !errors.Is(err, fs.ErrNotExist) {
134135
return xerrors.Errorf("read ssh config failed: %w", err)
135136
}
137+
if len(coderConfigRaw) > 0 {
138+
if !bytes.HasPrefix(coderConfigRaw, []byte(sshCoderConfigHeader)) {
139+
return xerrors.Errorf("unexpected content in %s: remove the file and rerun the command to continue", coderConfigFile)
140+
}
141+
}
136142

137143
// Keep track of changes we are making.
138144
var changes []string
@@ -192,6 +198,7 @@ func configSSH() *cobra.Command {
192198

193199
buf := &bytes.Buffer{}
194200
_, _ = buf.WriteString(sshCoderConfigHeader)
201+
_, _ = buf.WriteString(sshCoderConfigDocsHeader)
195202

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

0 commit comments

Comments
 (0)