Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions pkg/cmd/enablessh/enablessh.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import (
"context"
"fmt"
"golang.org/x/crypto/ssh"

Check failure on line 7 in pkg/cmd/enablessh/enablessh.go

View workflow job for this annotation

GitHub Actions / ci (ubuntu-22.04)

File is not properly formatted (gofumpt)
"os"
"os/exec"
"os/user"
"path/filepath"
"strings"
"unicode"

nodev1 "buf.build/gen/go/brevdev/devplane/protocolbuffers/go/devplaneapi/v1"
"connectrpc.com/connect"
Expand Down Expand Up @@ -320,11 +322,27 @@
return "", fmt.Errorf("open SSH port: %w", err)
}

func installCertAuthority(osUser *user.User, caPublicKey, nodeID, linuxUser string) error {

Check failure on line 325 in pkg/cmd/enablessh/enablessh.go

View workflow job for this annotation

GitHub Actions / ci (ubuntu-22.04)

cyclomatic complexity 25 of func `installCertAuthority` is high (> 16) (gocyclo)
if caPublicKey == "" {

Check failure on line 326 in pkg/cmd/enablessh/enablessh.go

View workflow job for this annotation

GitHub Actions / ci (ubuntu-22.04)

cyclomatic complexity 25 of func `installCertAuthority` is high (> 16) (gocyclo)
return fmt.Errorf("certificate authority public key is required")
}

caPublicKey = strings.TrimSpace(caPublicKey)
if strings.ContainsFunc(caPublicKey, unicode.IsControl) {
return fmt.Errorf("certificate authority public key must be a single line without control characters")
}
key, _, options, rest, err := ssh.ParseAuthorizedKey([]byte(caPublicKey))
if err != nil || len(options) != 0 || len(rest) != 0 {
return fmt.Errorf("certificate authority public key must be one SSH public key without options")
}
for _, principalPart := range []string{nodeID, linuxUser} {
if principalPart == "" || strings.ContainsFunc(principalPart, func(r rune) bool {
return !(r >= 'a' && r <= 'z' || r >= 'A' && r <= 'Z' || r >= '0' && r <= '9' || r == '_' || r == '-' || r == '.')
}) {
return fmt.Errorf("node ID and Linux user must contain only letters, digits, underscores, hyphens, or periods")
}
}
caPublicKey = strings.TrimSpace(string(ssh.MarshalAuthorizedKey(key)))
principal := fmt.Sprintf("brev:v1:vm:%s:login:%s", nodeID, linuxUser)
entry := fmt.Sprintf("cert-authority,principals=\"%s\" %s", principal, strings.TrimSpace(caPublicKey))

Expand Down
43 changes: 40 additions & 3 deletions pkg/cmd/enablessh/enablessh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ func Test_NewCmdEnableSSHExposesNonInteractiveFlags(t *testing.T) {

func Test_installCertAuthority(t *testing.T) {
const (
caKey = "ssh-ed25519 AAAAC3Nz dummyCA"
caKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
node = "unode_abc"
luser = "ubuntu"
)
Expand All @@ -564,7 +564,7 @@ func Test_installCertAuthority(t *testing.T) {
if err := installCertAuthority(u, caKey, node, luser); err != nil {
t.Fatalf("installCertAuthority: %v", err)
}
want := `cert-authority,principals="brev:v1:vm:unode_abc:login:ubuntu" ssh-ed25519 AAAAC3Nz dummyCA`
want := `cert-authority,principals="brev:v1:vm:unode_abc:login:ubuntu" ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA`
if result := readAuthorizedKeys(t, u); !strings.Contains(result, want) {
t.Errorf("expected cert-authority line not found:\n%s", result)
}
Expand Down Expand Up @@ -647,7 +647,7 @@ func Test_resolveLegacySSHPortNonInteractiveUsesProvidedPort(t *testing.T) {
}

func Test_enableSSHNonInteractiveUsesProvidedInputs(t *testing.T) {
const caKey = "ssh-ed25519 AAAAC3Nz dummyCA"
const caKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
svc := &fakeNodeService{
getNodeFn: func(_ *nodev1.GetNodeRequest) (*nodev1.GetNodeResponse, error) {
return &nodev1.GetNodeResponse{ExternalNode: &nodev1.ExternalNode{Ports: []*nodev1.Port{
Expand Down Expand Up @@ -753,3 +753,40 @@ func Test_enableSSH_LegacyNodeFallsBackToKeys(t *testing.T) {
}
}
}

func Test_installCertAuthorityRejectsInjectedEntries(t *testing.T) {
const key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
cases := []struct{ name, key, node, user string }{
{"second key", key + "\n" + key, "unode_abc", "ubuntu"},
{"carriage return", key + "\rcomment", "unode_abc", "ubuntu"},
{"invalid key", "ssh-ed25519 invalid", "unode_abc", "ubuntu"},
{"key options", "no-pty " + key, "unode_abc", "ubuntu"},
{"quote in node", key, `node",command="id`, "ubuntu"},
{"newline in node", key, "node\nattacker", "ubuntu"},
{"wildcard node", key, "*", "ubuntu"},
{"principal list", key, "unode_abc", "ubuntu,root"},
{"wildcard user", key, "unode_abc", "*"},
{"empty node", key, "", "ubuntu"},
{"empty user", key, "unode_abc", ""},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
u := tempUser(t)
sshDir := filepath.Join(u.HomeDir, ".ssh")
if err := os.MkdirAll(sshDir, 0o700); err != nil {
t.Fatal(err)
}
path := filepath.Join(sshDir, "authorized_keys")
original := "existing key\n"
if err := os.WriteFile(path, []byte(original), 0o600); err != nil {
t.Fatal(err)
}
if err := installCertAuthority(u, tc.key, tc.node, tc.user); err == nil {
t.Fatal("expected validation error")
}
if got := readAuthorizedKeys(t, u); got != original {
t.Fatalf("authorized_keys changed: %q", got)
}
})
}
}
Loading