Skip to content

Commit c1197e7

Browse files
committed
fix(config): address CodeRabbit review feedback
- Bumped Go version to 1.23.10 to fix GO-2025-3750 - Added Validate() method to Config struct - Added validation check to Load() after applying overrides - Added error logging for invalid numeric environment variables
1 parent fe037fc commit c1197e7

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module github.com/AOSSIE-Org/ThruBox-Server
22

3-
go 1.22
3+
go 1.23.10
44

55
require gopkg.in/yaml.v3 v3.0.1

internal/config/config.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package config
22

33
import (
44
"fmt"
5+
"log"
56
"os"
67
"strconv"
78

@@ -74,6 +75,9 @@ func Load(path string) (*Config, error) {
7475
if os.IsNotExist(err) {
7576
// No config file — use defaults + env overrides
7677
applyEnvOverrides(cfg)
78+
if err := cfg.Validate(); err != nil {
79+
return nil, fmt.Errorf("invalid default configuration: %w", err)
80+
}
7781
return cfg, nil
7882
}
7983
return nil, fmt.Errorf("reading config file: %w", err)
@@ -84,6 +88,9 @@ func Load(path string) (*Config, error) {
8488
}
8589

8690
applyEnvOverrides(cfg)
91+
if err := cfg.Validate(); err != nil {
92+
return nil, fmt.Errorf("invalid configuration: %w", err)
93+
}
8794
return cfg, nil
8895
}
8996

@@ -93,6 +100,8 @@ func applyEnvOverrides(cfg *Config) {
93100
if v := os.Getenv("RELAY_SERVER_PORT"); v != "" {
94101
if port, err := strconv.Atoi(v); err == nil {
95102
cfg.Server.Port = port
103+
} else {
104+
log.Printf("warning: invalid RELAY_SERVER_PORT=%q, using default", v)
96105
}
97106
}
98107

@@ -111,18 +120,24 @@ func applyEnvOverrides(cfg *Config) {
111120
if v := os.Getenv("RELAY_MESSAGES_TTL_DAYS"); v != "" {
112121
if days, err := strconv.Atoi(v); err == nil {
113122
cfg.Messages.TTLDays = days
123+
} else {
124+
log.Printf("warning: invalid RELAY_MESSAGES_TTL_DAYS=%q, using default", v)
114125
}
115126
}
116127

117128
if v := os.Getenv("RELAY_MESSAGES_MAX_PAYLOAD_SIZE"); v != "" {
118129
if size, err := strconv.Atoi(v); err == nil {
119130
cfg.Messages.MaxPayloadSize = size
131+
} else {
132+
log.Printf("warning: invalid RELAY_MESSAGES_MAX_PAYLOAD_SIZE=%q, using default", v)
120133
}
121134
}
122135

123136
if v := os.Getenv("RELAY_SECURITY_RATE_LIMIT"); v != "" {
124137
if limit, err := strconv.Atoi(v); err == nil {
125138
cfg.Security.RateLimit = limit
139+
} else {
140+
log.Printf("warning: invalid RELAY_SECURITY_RATE_LIMIT=%q, using default", v)
126141
}
127142
}
128143

@@ -131,6 +146,23 @@ func applyEnvOverrides(cfg *Config) {
131146
}
132147
}
133148

149+
// Validate checks the configuration values for validity.
150+
func (c *Config) Validate() error {
151+
if c.Server.Port < 1 || c.Server.Port > 65535 {
152+
return fmt.Errorf("invalid server port: %d", c.Server.Port)
153+
}
154+
if c.Messages.TTLDays < 0 {
155+
return fmt.Errorf("invalid ttl_days: %d", c.Messages.TTLDays)
156+
}
157+
if c.Messages.MaxPayloadSize < 0 {
158+
return fmt.Errorf("invalid max_payload_size: %d", c.Messages.MaxPayloadSize)
159+
}
160+
if c.Security.RateLimit < 0 {
161+
return fmt.Errorf("invalid rate_limit: %d", c.Security.RateLimit)
162+
}
163+
return nil
164+
}
165+
134166
// Addr returns the listen address as "host:port".
135167
func (c *Config) Addr() string {
136168
return fmt.Sprintf("%s:%d", c.Server.Host, c.Server.Port)

0 commit comments

Comments
 (0)