Skip to content

Commit

Permalink
Fix nil pointer exception in group delete (#1211)
Browse files Browse the repository at this point in the history
Fix group delete panic

In case if in the db the DNSSettings is null then can cause panic in delete group function
because this field is pointer and it was not checked. Because of in the future implementation
this variable will be filled in any case then make no sense to keep the pointer type.

Fix DNSSettings copy function
  • Loading branch information
pappz authored Oct 11, 2023
1 parent 659110f commit b8599f6
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 40 deletions.
21 changes: 8 additions & 13 deletions management/server/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ type Account struct {
Policies []*Policy
Routes map[string]*route.Route
NameServerGroups map[string]*nbdns.NameServerGroup
DNSSettings *DNSSettings
DNSSettings DNSSettings
// Settings is a dictionary of Account settings
Settings *Settings
}
Expand Down Expand Up @@ -513,13 +513,11 @@ func (a *Account) getUserGroups(userID string) ([]string, error) {
func (a *Account) getPeerDNSManagementStatus(peerID string) bool {
peerGroups := a.getPeerGroups(peerID)
enabled := true
if a.DNSSettings != nil {
for _, groupID := range a.DNSSettings.DisabledManagementGroups {
_, found := peerGroups[groupID]
if found {
enabled = false
break
}
for _, groupID := range a.DNSSettings.DisabledManagementGroups {
_, found := peerGroups[groupID]
if found {
enabled = false
break
}
}
return enabled
Expand Down Expand Up @@ -606,10 +604,7 @@ func (a *Account) Copy() *Account {
nsGroups[id] = nsGroup.Copy()
}

var dnsSettings *DNSSettings
if a.DNSSettings != nil {
dnsSettings = a.DNSSettings.Copy()
}
dnsSettings := a.DNSSettings.Copy()

var settings *Settings
if a.Settings != nil {
Expand Down Expand Up @@ -1618,7 +1613,7 @@ func newAccountWithId(accountID, userID, domain string) *Account {
setupKeys := map[string]*SetupKey{}
nameServersGroups := make(map[string]*nbdns.NameServerGroup)
users[userID] = NewAdminUser(userID)
dnsSettings := &DNSSettings{
dnsSettings := DNSSettings{
DisabledManagementGroups: make([]string, 0),
}
log.Debugf("created new account %s", accountID)
Expand Down
2 changes: 1 addition & 1 deletion management/server/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1374,7 +1374,7 @@ func TestAccount_Copy(t *testing.T) {
NameServers: []nbdns.NameServer{},
},
},
DNSSettings: &DNSSettings{DisabledManagementGroups: []string{}},
DNSSettings: DNSSettings{DisabledManagementGroups: []string{}},
Settings: &Settings{},
}
err := hasNilField(account)
Expand Down
30 changes: 7 additions & 23 deletions management/server/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,11 @@ type DNSSettings struct {
}

// Copy returns a copy of the DNS settings
func (d *DNSSettings) Copy() *DNSSettings {
settings := &DNSSettings{
DisabledManagementGroups: make([]string, 0),
func (d DNSSettings) Copy() DNSSettings {
settings := DNSSettings{
DisabledManagementGroups: make([]string, len(d.DisabledManagementGroups)),
}

if d == nil {
return settings
}

if d.DisabledManagementGroups != nil && len(d.DisabledManagementGroups) > 0 {
settings.DisabledManagementGroups = d.DisabledManagementGroups[:]
}

copy(settings.DisabledManagementGroups, d.DisabledManagementGroups)
return settings
}

Expand All @@ -58,12 +50,8 @@ func (am *DefaultAccountManager) GetDNSSettings(accountID string, userID string)
if !user.IsAdmin() {
return nil, status.Errorf(status.PermissionDenied, "only admins are allowed to view DNS settings")
}

if account.DNSSettings == nil {
return &DNSSettings{}, nil
}

return account.DNSSettings.Copy(), nil
dnsSettings := account.DNSSettings.Copy()
return &dnsSettings, nil
}

// SaveDNSSettings validates a user role and updates the account's DNS settings
Expand Down Expand Up @@ -96,11 +84,7 @@ func (am *DefaultAccountManager) SaveDNSSettings(accountID string, userID string
}
}

oldSettings := &DNSSettings{}
if account.DNSSettings != nil {
oldSettings = account.DNSSettings.Copy()
}

oldSettings := account.DNSSettings.Copy()
account.DNSSettings = dnsSettingsToSave.Copy()

account.Network.IncSerial()
Expand Down
2 changes: 1 addition & 1 deletion management/server/dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestGetDNSSettings(t *testing.T) {
t.Fatal("DNS settings for new accounts shouldn't return nil")
}

account.DNSSettings = &DNSSettings{
account.DNSSettings = DNSSettings{
DisabledManagementGroups: []string{group1ID},
}

Expand Down
4 changes: 2 additions & 2 deletions management/server/http/dns_settings_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const (
testDNSSettingsUserID = "test_user"
)

var baseExistingDNSSettings = &server.DNSSettings{
var baseExistingDNSSettings = server.DNSSettings{
DisabledManagementGroups: []string{testDNSSettingsExistingGroup},
}

Expand All @@ -43,7 +43,7 @@ func initDNSSettingsTestData() *DNSSettingsHandler {
return &DNSSettingsHandler{
accountManager: &mock_server.MockAccountManager{
GetDNSSettingsFunc: func(accountID string, userID string) (*server.DNSSettings, error) {
return testingDNSSettingsAccount.DNSSettings, nil
return &testingDNSSettingsAccount.DNSSettings, nil
},
SaveDNSSettingsFunc: func(accountID string, userID string, dnsSettingsToSave *server.DNSSettings) error {
if dnsSettingsToSave != nil {
Expand Down

0 comments on commit b8599f6

Please sign in to comment.