Fix data races, nil-record panics, scanner SSRF, log date layout, shutdown timeouts, and enable stricter linters (#54)

High (behavioral bugs):
- service.Geo: add RLock on lookups, handle Reload error, keep old DBs on failure
- models.GeoDB.Reload: open new DBs before closing old ones (no reader gap)
- router: guard nil geo records (404 instead of 500) in geo.go, generic.go, dns.go
- router/port_scanner: reject undetermined client IPs (nil->localhost dial)
- internal/httputils: fix access log date layout (02/Jan/2006, not literal Nov)
- server/quic: log.Printf instead of log.Fatal in request handler

Medium:
- server/*: 10s shutdown timeout; SIGTERM drains servers before closing geo DBs
- httputils: Clone() request headers instead of mutating the live map; strings.Builder
- setting/app: handle all os.Stat errors; fix "truster" typos; use errors.New;
  remove dead Token field
- cmd: errors.Is for sentinel checks; os.Stderr; log.Fatal instead of panic;
  pass http.Handler by value (not pointer-to-interface)
- resolver: validate config at startup (pre-parse RRs, validate IPs); check WriteMsg
  errors; Setup returns error

Low:
- .golangci.yaml: enable errcheck, govet, errorlint; fix all resulting findings
- server/tls: explicit TLSConfig{MinVersion: tls.VersionTLS12}
- router/dns: label-exact vhost suffix check, strip port; template.Must
- models: errors.Join instead of %s on []error
- service/geo_test: race-regression test (concurrent lookups during Reload)
- README: Go >= 1.25; fix latent test bug (store.Add->Set in dns_test.go)
This commit is contained in:
2026-07-19 12:09:52 +02:00
committed by GitHub
parent 210e8c7cdb
commit 49ffcadb0a
24 changed files with 273 additions and 143 deletions
+8 -13
View File
@@ -11,32 +11,27 @@ import (
"github.com/gin-gonic/gin"
)
// HeadersToSortedString shorts and dumps http.Header to a string separated by \n
// HeadersToSortedString sorts and dumps http.Header to a string separated by \n
func HeadersToSortedString(headers http.Header) string {
var output string
keys := make([]string, 0, len(headers))
for k := range headers {
keys = append(keys, k)
}
sort.Strings(keys)
var output strings.Builder
for _, k := range keys {
if len(headers[k]) > 1 {
for _, h := range headers[k] {
output += k + ": " + h + "\n"
}
} else {
output += k + ": " + headers[k][0] + "\n"
for _, h := range headers[k] {
output.WriteString(k + ": " + h + "\n")
}
}
return output
return output.String()
}
// GetHeadersWithoutTrustedHeaders return a http.Heade object with the original headers except trusted headers
// GetHeadersWithoutTrustedHeaders returns a copy of the request headers with the trusted headers removed
func GetHeadersWithoutTrustedHeaders(ctx *gin.Context) http.Header {
h := ctx.Request.Header
h := ctx.Request.Header.Clone()
for _, k := range []string{setting.App.TrustedHeader, setting.App.TrustedPortHeader} {
delete(h, textproto.CanonicalMIMEHeaderKey(k))
@@ -49,7 +44,7 @@ func GetHeadersWithoutTrustedHeaders(ctx *gin.Context) http.Header {
func GetLogFormatter(param gin.LogFormatterParams) string {
return fmt.Sprintf("%s - [%s] \"%s %s %s\" %d %d %d %s \"%s\" \"%s\" \"%s\"\n",
param.ClientIP,
param.TimeStamp.Format("02/Nov/2006:15:04:05 -0700"),
param.TimeStamp.Format("02/Jan/2006:15:04:05 -0700"),
param.Method,
param.Path,
param.Request.Proto,
+2 -2
View File
@@ -25,7 +25,7 @@ Header3: Three
}
func TestGetLogFormatter(t *testing.T) {
expected := "127.0.0.1 - [01/Nov/0001:00:00:00 +0000] \"GET / HTTP/1.1\" 200 100 1000 local \"golang test 1.0\" \"1.1.1.1, 2.2.2.2\" \"-\"\n"
expected := "127.0.0.1 - [15/Jul/2022:10:30:00 +0000] \"GET / HTTP/1.1\" 200 100 1000 local \"golang test 1.0\" \"1.1.1.1, 2.2.2.2\" \"-\"\n"
h := http.Header{}
h.Set("User-Agent", "golang test 1.0")
@@ -39,7 +39,7 @@ func TestGetLogFormatter(t *testing.T) {
p := gin.LogFormatterParams{
ClientIP: "127.0.0.1",
TimeStamp: time.Time{},
TimeStamp: time.Date(2022, time.July, 15, 10, 30, 0, 0, time.UTC),
Method: "GET",
Path: "/",
StatusCode: 200,
+9 -10
View File
@@ -13,9 +13,8 @@ import (
)
type geodbConf struct {
City string
ASN string
Token *string
City string
ASN string
}
type serverSettings struct {
ReadTimeout time.Duration
@@ -136,25 +135,25 @@ func Setup(args []string) (output string, err error) {
}
if (App.GeodbPath.City != "" && App.GeodbPath.ASN == "") || (App.GeodbPath.City == "" && App.GeodbPath.ASN != "") {
return "", fmt.Errorf("both --geoip2-city and --geoip2-asn are mandatory to enable geo information")
return "", errors.New("both --geoip2-city and --geoip2-asn are mandatory to enable geo information")
}
if App.TrustedPortHeader != "" && App.TrustedHeader == "" {
return "", fmt.Errorf("truster-header is mandatory when truster-port-header is set")
return "", errors.New("trusted-header is mandatory when trusted-port-header is set")
}
if (App.TLSAddress != "") && (App.TLSCrtPath == "" || App.TLSKeyPath == "") {
return "", fmt.Errorf("in order to use TLS, the -tls-crt and -tls-key flags are mandatory")
return "", errors.New("in order to use TLS, the -tls-crt and -tls-key flags are mandatory")
}
if App.EnableHTTP3 && App.TLSAddress == "" {
return "", fmt.Errorf("in order to use HTTP3, the -tls-bind is mandatory")
return "", errors.New("in order to use HTTP3, the -tls-bind is mandatory")
}
if App.TemplatePath != "" {
info, err := os.Stat(App.TemplatePath)
if os.IsNotExist(err) {
return "", fmt.Errorf("%s no such file or directory", App.TemplatePath)
if err != nil {
return "", fmt.Errorf("template path: %w", err)
}
if info.IsDir() {
return "", fmt.Errorf("%s must be a file", App.TemplatePath)
@@ -165,7 +164,7 @@ func Setup(args []string) (output string, err error) {
var err error
App.Resolver, err = readYAML(resolverConf)
if err != nil {
return "", fmt.Errorf("error reading resolver configuration %w", err)
return "", fmt.Errorf("reading resolver configuration: %w", err)
}
}