gosec govulncheck runs (#1)

* gosec govulncheck runs

* Fix flaky TestRateLimiter_Matrix test

The test was failing due to two issues:
1. Test name generation used invalid character conversion (string(rune('0'+limit)))
   which produced non-printable characters for limits >= 10
2. Using 10ms windows with 100 requests caused race conditions - early requests
   would expire before all 100 were made, allowing the 101st request

Changed to use struct-based test cases with proper fmt.Sprintf naming and
a consistent 1-second window that won't expire during rapid test execution.
This commit is contained in:
2025-12-09 01:07:16 +00:00
committed by GitHub
parent 27d5011ab1
commit 29263dc8a2
17 changed files with 80 additions and 215 deletions
+4 -71
View File
@@ -167,7 +167,7 @@ func (m *Manager) watchLoop() {
func (m *Manager) Stop() {
close(m.stopCh)
if m.watcher != nil {
m.watcher.Close()
_ = m.watcher.Close()
}
}
@@ -338,76 +338,6 @@ func (c *Config) DeleteHost(alias string) bool {
return false
}
// UpdateHost updates an existing host by alias.
func (c *Config) UpdateHost(oldAlias, domain, ip, newAlias, groupName string) error {
// Find the host
var foundGroup int = -1
var foundHost int = -1
for i := range c.Groups {
for j := range c.Groups[i].Hosts {
if c.Groups[i].Hosts[j].Alias == oldAlias {
foundGroup = i
foundHost = j
break
}
}
if foundHost >= 0 {
break
}
}
if foundHost < 0 {
return fmt.Errorf("alias not found: %s", oldAlias)
}
// Check for duplicate alias if alias is changing
if oldAlias != newAlias {
if existing, _ := c.FindHostByAlias(newAlias); existing != nil {
return fmt.Errorf("alias already exists: %s", newAlias)
}
}
// Get current enabled state
enabled := c.Groups[foundGroup].Hosts[foundHost].Enabled
// If group is changing, move to new group
if c.Groups[foundGroup].Name != groupName {
// Remove from old group
c.Groups[foundGroup].Hosts = append(c.Groups[foundGroup].Hosts[:foundHost], c.Groups[foundGroup].Hosts[foundHost+1:]...)
// Add to new group
host := Host{
Domain: domain,
IP: ip,
Alias: newAlias,
Enabled: enabled,
}
// Find or create target group
found := false
for i := range c.Groups {
if c.Groups[i].Name == groupName {
c.Groups[i].Hosts = append(c.Groups[i].Hosts, host)
found = true
break
}
}
if !found {
c.Groups = append(c.Groups, Group{
Name: groupName,
Hosts: []Host{host},
})
}
} else {
// Update in place
c.Groups[foundGroup].Hosts[foundHost].Domain = domain
c.Groups[foundGroup].Hosts[foundHost].IP = ip
c.Groups[foundGroup].Hosts[foundHost].Alias = newAlias
}
return nil
}
// ApplyPreset applies a preset to the configuration.
func (c *Config) ApplyPreset(name string) error {
preset := c.FindPreset(name)
@@ -482,6 +412,7 @@ func (m *Manager) Save() error {
return fmt.Errorf("failed to marshal config: %w", err)
}
// #nosec G306 -- config file should be world-readable
if err := os.WriteFile(m.path, data, 0644); err != nil {
return fmt.Errorf("failed to write config: %w", err)
}
@@ -492,6 +423,7 @@ func (m *Manager) Save() error {
// CreateDefault creates a default configuration file.
func CreateDefault(path string) error {
dir := filepath.Dir(path)
// #nosec G301 -- config directory should be world-readable
if err := os.MkdirAll(dir, 0755); err != nil {
return fmt.Errorf("failed to create config directory: %w", err)
}
@@ -533,6 +465,7 @@ func CreateDefault(path string) error {
return fmt.Errorf("failed to marshal default config: %w", err)
}
// #nosec G306 -- config file should be world-readable
if err := os.WriteFile(path, data, 0644); err != nil {
return fmt.Errorf("failed to write default config: %w", err)
}
-71
View File
@@ -432,77 +432,6 @@ func TestConfig_DeleteHost(t *testing.T) {
})
}
func TestConfig_UpdateHost(t *testing.T) {
t.Run("update in same group", func(t *testing.T) {
cfg := &Config{
Groups: []Group{
{Name: "dev", Hosts: []Host{{Domain: "old.com", IP: "127.0.0.1", Alias: "test"}}},
},
}
err := cfg.UpdateHost("test", "new.com", "192.168.1.1", "test", "dev")
require.NoError(t, err)
assert.Equal(t, "new.com", cfg.Groups[0].Hosts[0].Domain)
assert.Equal(t, "192.168.1.1", cfg.Groups[0].Hosts[0].IP)
})
t.Run("move to different group", func(t *testing.T) {
cfg := &Config{
Groups: []Group{
{Name: "source", Hosts: []Host{{Domain: "a.com", IP: "127.0.0.1", Alias: "test"}}},
{Name: "target", Hosts: []Host{}},
},
}
err := cfg.UpdateHost("test", "a.com", "127.0.0.1", "test", "target")
require.NoError(t, err)
assert.Len(t, cfg.Groups[0].Hosts, 0)
assert.Len(t, cfg.Groups[1].Hosts, 1)
})
t.Run("move to new group", func(t *testing.T) {
cfg := &Config{
Groups: []Group{
{Name: "source", Hosts: []Host{{Domain: "a.com", IP: "127.0.0.1", Alias: "test"}}},
},
}
err := cfg.UpdateHost("test", "a.com", "127.0.0.1", "test", "newgroup")
require.NoError(t, err)
assert.Len(t, cfg.Groups, 2)
assert.Equal(t, "newgroup", cfg.Groups[1].Name)
})
t.Run("change alias", func(t *testing.T) {
cfg := &Config{
Groups: []Group{
{Name: "dev", Hosts: []Host{{Domain: "a.com", IP: "127.0.0.1", Alias: "old"}}},
},
}
err := cfg.UpdateHost("old", "a.com", "127.0.0.1", "new", "dev")
require.NoError(t, err)
assert.Equal(t, "new", cfg.Groups[0].Hosts[0].Alias)
})
t.Run("alias conflict error", func(t *testing.T) {
cfg := &Config{
Groups: []Group{
{Name: "dev", Hosts: []Host{
{Domain: "a.com", Alias: "a"},
{Domain: "b.com", Alias: "b"},
}},
},
}
err := cfg.UpdateHost("a", "a.com", "127.0.0.1", "b", "dev")
assert.Error(t, err)
assert.Contains(t, err.Error(), "alias already exists")
})
t.Run("host not found error", func(t *testing.T) {
cfg := &Config{Groups: []Group{}}
err := cfg.UpdateHost("nonexistent", "a.com", "127.0.0.1", "new", "dev")
assert.Error(t, err)
assert.Contains(t, err.Error(), "alias not found")
})
}
func TestConfig_AddPreset(t *testing.T) {
t.Run("add new preset", func(t *testing.T) {
cfg := &Config{Presets: []Preset{}}