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,