From e4e2e1f3e01e2c2561a3c473a2d800dd88c42ed6 Mon Sep 17 00:00:00 2001 From: Stephanie Ahart Date: Fri, 6 Nov 2026 16:51:11 -0800 Subject: [PATCH] BRE2-1286: validate SSH certificate authority entries --- pkg/cmd/enablessh/enablessh.go | 18 ++++++++++++ pkg/cmd/enablessh/enablessh_test.go | 43 +++++++++++++++++++++++++++-- 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/pkg/cmd/enablessh/enablessh.go b/pkg/cmd/enablessh/enablessh.go index 5ba47a6e..309f3816 100644 --- a/pkg/cmd/enablessh/enablessh.go +++ b/pkg/cmd/enablessh/enablessh.go @@ -4,11 +4,13 @@ package enablessh import ( "context" "fmt" + "golang.org/x/crypto/ssh" "os" "os/exec" "os/user" "path/filepath" "strings" + "unicode" nodev1 "buf.build/gen/go/brevdev/devplane/protocolbuffers/go/devplaneapi/v1" "connectrpc.com/connect" @@ -325,6 +327,22 @@ func installCertAuthority(osUser *user.User, caPublicKey, nodeID, linuxUser stri 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)) diff --git a/pkg/cmd/enablessh/enablessh_test.go b/pkg/cmd/enablessh/enablessh_test.go index a4b6f7da..0d78274b 100644 --- a/pkg/cmd/enablessh/enablessh_test.go +++ b/pkg/cmd/enablessh/enablessh_test.go @@ -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" ) @@ -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) } @@ -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{ @@ -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) + } + }) + } +}