Skip to content

Commit ac29e6d

Browse files
committed
Solve Gosec G115
1 parent 069b9b0 commit ac29e6d

3 files changed

Lines changed: 14 additions & 4 deletions

File tree

cmd/socket-proxy/checksocketconnection.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func checkSocketAvailability(socketPath string) error {
2525
}
2626

2727
// startSocketWatchdog starts a watchdog that checks the socket availability every n seconds.
28-
func startSocketWatchdog(socketPath string, interval uint, stopOnWatchdog bool, exitChan chan int) {
28+
func startSocketWatchdog(socketPath string, interval int64, stopOnWatchdog bool, exitChan chan int) {
2929
ticker := time.NewTicker(time.Duration(interval) * time.Second)
3030
defer ticker.Stop()
3131

cmd/socket-proxy/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func main() {
141141

142142
// start the watchdog if configured
143143
if cfg.WatchdogInterval > 0 {
144-
go startSocketWatchdog(cfg.SocketPath, cfg.WatchdogInterval, cfg.StopOnWatchdog, internalQuit)
144+
go startSocketWatchdog(cfg.SocketPath, int64(cfg.WatchdogInterval), cfg.StopOnWatchdog, internalQuit) // #nosec G115 - we validated the integer size in config.go
145145
slog.Debug("watchdog running")
146146
}
147147

@@ -161,7 +161,7 @@ func main() {
161161
exitCode = value
162162
}
163163
// Try to shut down gracefully
164-
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(cfg.ShutdownGraceTime)*time.Second)
164+
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(int64(cfg.ShutdownGraceTime))*time.Second) // #nosec G115 - we validated the integer size in config.go
165165
defer cancel()
166166
if err := srv.Shutdown(ctx); err != nil {
167167
slog.Warn("timeout stopping server", "error", err)

internal/config/config.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"flag"
66
"fmt"
77
"log/slog"
8+
"math"
89
"net"
910
"net/http"
1011
"os"
@@ -139,9 +140,15 @@ func InitConfig() (*Config, error) {
139140
flag.StringVar(&logLevel, "loglevel", defaultLogLevel, "set log level: DEBUG, INFO, WARN, ERROR")
140141
flag.UintVar(&proxyPort, "proxyport", defaultProxyPort, "tcp port to listen on")
141142
flag.UintVar(&cfg.ShutdownGraceTime, "shutdowngracetime", defaultShutdownGraceTime, "maximum time in seconds to wait for the server to shut down gracefully")
143+
if cfg.ShutdownGraceTime > math.MaxInt64 {
144+
return nil, fmt.Errorf("shutdowngracetime has to be smaller than %i", math.MaxInt64) // this maximum value has no practical significance
145+
}
142146
flag.StringVar(&cfg.SocketPath, "socketpath", defaultSocketPath, "unix socket path to connect to")
143147
flag.BoolVar(&cfg.StopOnWatchdog, "stoponwatchdog", defaultStopOnWatchdog, "stop the program when the socket gets unavailable (otherwise log only)")
144148
flag.UintVar(&cfg.WatchdogInterval, "watchdoginterval", defaultWatchdogInterval, "watchdog interval in seconds (0 to disable)")
149+
if cfg.WatchdogInterval > math.MaxInt64 {
150+
return nil, fmt.Errorf("watchdoginterval has to be smaller than %i", math.MaxInt64) // this maximum value has no practical significance
151+
}
145152
flag.StringVar(&cfg.ProxySocketEndpoint, "proxysocketendpoint", defaultProxySocketEndpoint, "unix socket endpoint (if set, used instead of the TCP listener)")
146153
flag.UintVar(&endpointFileMode, "proxysocketendpointfilemode", defaultProxySocketEndpointFileMode, "set the file mode of the unix socket endpoint")
147154
for i := range mr {
@@ -172,7 +179,10 @@ func InitConfig() (*Config, error) {
172179
return nil, errors.New("invalid log level " + logLevel + ": Supported levels are DEBUG, INFO, WARN, ERROR")
173180
}
174181

175-
cfg.ProxySocketEndpointFileMode = os.FileMode(endpointFileMode)
182+
if endpointFileMode > 0o777 {
183+
return nil, errors.New("file mode has to be between 0 and 0o777")
184+
}
185+
cfg.ProxySocketEndpointFileMode = os.FileMode(uint32(endpointFileMode))
176186

177187
// compile regexes for allowed requests
178188
cfg.AllowedRequests = make(map[string]*regexp.Regexp)

0 commit comments

Comments
 (0)