mirror of
https://github.com/coredns/coredns.git
synced 2026-01-05 00:11:29 -05:00
chore(lint): enable gosec (#7792)
Enable "gosec" linter.
Exclude:
- All G115 (integer overflow) findings, to be fixed separately.
Add targeted gosec annotations for:
- non-crypto math/rand usage
- md5 used only for file change detection
- G114 ("net/http serve with no timeout settings"), to be fixed
separately.
Other findings fixed.
Signed-off-by: Ville Vesilehto <ville@vesilehto.fi>
This commit is contained in:
@@ -11,6 +11,7 @@ linters:
|
|||||||
- copyloopvar
|
- copyloopvar
|
||||||
- durationcheck
|
- durationcheck
|
||||||
- godoclint
|
- godoclint
|
||||||
|
- gosec
|
||||||
- govet
|
- govet
|
||||||
- ineffassign
|
- ineffassign
|
||||||
- intrange
|
- intrange
|
||||||
@@ -39,7 +40,11 @@ linters:
|
|||||||
- path: _test\.go
|
- path: _test\.go
|
||||||
linters:
|
linters:
|
||||||
- perfsprint
|
- perfsprint
|
||||||
|
- gosec
|
||||||
settings:
|
settings:
|
||||||
|
gosec:
|
||||||
|
excludes:
|
||||||
|
- G115
|
||||||
govet:
|
govet:
|
||||||
enable:
|
enable:
|
||||||
- nilness
|
- nilness
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ func (c Chaos) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (
|
|||||||
default:
|
default:
|
||||||
return plugin.NextOrFailure(c.Name(), c.Next, ctx, w, r)
|
return plugin.NextOrFailure(c.Name(), c.Next, ctx, w, r)
|
||||||
case "authors.bind.":
|
case "authors.bind.":
|
||||||
rnd := rand.New(rand.NewSource(time.Now().Unix()))
|
rnd := rand.New(rand.NewSource(time.Now().Unix())) // #nosec G404 -- non-cryptographic randomness for shuffling authors.
|
||||||
|
|
||||||
for _, i := range rnd.Perm(len(c.Authors)) {
|
for _, i := range rnd.Perm(len(c.Authors)) {
|
||||||
m.Answer = append(m.Answer, &dns.TXT{Hdr: hdr, Txt: []string{c.Authors[i]}})
|
m.Answer = append(m.Answer, &dns.TXT{Hdr: hdr, Txt: []string{c.Authors[i]}})
|
||||||
|
|||||||
@@ -70,6 +70,7 @@ func (d *dio) dial() error {
|
|||||||
|
|
||||||
if d.proto == "tls" {
|
if d.proto == "tls" {
|
||||||
config := &tls.Config{
|
config := &tls.Config{
|
||||||
|
// #nosec G402 -- optional, user-configurable escape hatch for environments that cannot validate certs.
|
||||||
InsecureSkipVerify: d.skipVerify,
|
InsecureSkipVerify: d.skipVerify,
|
||||||
}
|
}
|
||||||
dialer := &net.Dialer{
|
dialer := &net.Dialer{
|
||||||
|
|||||||
@@ -195,7 +195,7 @@ Restart:
|
|||||||
|
|
||||||
// jitter returns a random duration between [0,n) * time.Millisecond
|
// jitter returns a random duration between [0,n) * time.Millisecond
|
||||||
func jitter(n int) time.Duration {
|
func jitter(n int) time.Duration {
|
||||||
r := rand.Intn(n)
|
r := rand.Intn(n) // #nosec G404 -- non-cryptographic jitter to spread transfer attempts.
|
||||||
return time.Duration(r) * time.Millisecond
|
return time.Duration(r) * time.Millisecond
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -63,6 +63,7 @@ func (h *health) OnStartup() error {
|
|||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
ctx, h.stop = context.WithCancel(ctx)
|
ctx, h.stop = context.WithCancel(ctx)
|
||||||
|
|
||||||
|
// #nosec G114 -- TODO
|
||||||
go func() { http.Serve(h.ln, h.mux) }()
|
go func() { http.Serve(h.ln, h.mux) }()
|
||||||
go func() { h.overloaded(ctx) }()
|
go func() { h.overloaded(ctx) }()
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package loadbalance
|
|||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/md5"
|
"crypto/md5" // #nosec G501 -- used only as a checksum for file change detection (not for security).
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@@ -52,7 +52,7 @@ type randomUint struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *randomUint) randInit() {
|
func (r *randomUint) randInit() {
|
||||||
r.rn = rand.New(rand.NewSource(time.Now().UnixNano()))
|
r.rn = rand.New(rand.NewSource(time.Now().UnixNano())) // #nosec G404 -- non-cryptographic randomness for load balancing.
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *randomUint) randUint(limit uint) uint {
|
func (r *randomUint) randUint(limit uint) uint {
|
||||||
@@ -245,7 +245,7 @@ func (w *weightedRR) updateWeights() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
md5sum := md5.Sum(bytes)
|
md5sum := md5.Sum(bytes) // #nosec G401 -- used only as a checksum for file change detection (not for security).
|
||||||
if md5sum == w.md5sum {
|
if md5sum == w.md5sum {
|
||||||
// file contents has not changed
|
// file contents has not changed
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ type Rand struct {
|
|||||||
|
|
||||||
// New returns a new Rand from seed.
|
// New returns a new Rand from seed.
|
||||||
func New(seed int64) *Rand {
|
func New(seed int64) *Rand {
|
||||||
return &Rand{r: rand.New(rand.NewSource(seed))}
|
return &Rand{r: rand.New(rand.NewSource(seed))} // #nosec G404 -- non-cryptographic RNG by design (load balancing only).
|
||||||
}
|
}
|
||||||
|
|
||||||
// Int returns a non-negative pseudo-random int from the Source in Rand.r.
|
// Int returns a non-negative pseudo-random int from the Source in Rand.r.
|
||||||
|
|||||||
@@ -95,7 +95,11 @@ func NewTLSConfig(certPath, keyPath, caPath string) (*tls.Config, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
tlsConfig := &tls.Config{Certificates: []tls.Certificate{cert}, RootCAs: roots}
|
// #nosec G402 -- MinVersion and MaxVersion are set in setTLSDefaults
|
||||||
|
tlsConfig := &tls.Config{
|
||||||
|
Certificates: []tls.Certificate{cert},
|
||||||
|
RootCAs: roots,
|
||||||
|
}
|
||||||
setTLSDefaults(tlsConfig)
|
setTLSDefaults(tlsConfig)
|
||||||
|
|
||||||
return tlsConfig, nil
|
return tlsConfig, nil
|
||||||
@@ -109,7 +113,10 @@ func NewTLSClientConfig(caPath string) (*tls.Config, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
tlsConfig := &tls.Config{RootCAs: roots}
|
// #nosec G402 -- MinVersion and MaxVersion are set in setTLSDefaults
|
||||||
|
tlsConfig := &tls.Config{
|
||||||
|
RootCAs: roots,
|
||||||
|
}
|
||||||
setTLSDefaults(tlsConfig)
|
setTLSDefaults(tlsConfig)
|
||||||
|
|
||||||
return tlsConfig, nil
|
return tlsConfig, nil
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ func (h *handler) Startup() error {
|
|||||||
runtime.SetBlockProfileRate(h.rateBloc)
|
runtime.SetBlockProfileRate(h.rateBloc)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
|
// #nosec G114 -- TODO
|
||||||
http.Serve(h.ln, h.mux)
|
http.Serve(h.ln, h.mux)
|
||||||
}()
|
}()
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -61,6 +61,7 @@ func (rd *ready) onStartup() error {
|
|||||||
io.WriteString(w, notReadyPlugins)
|
io.WriteString(w, notReadyPlugins)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// #nosec G114 -- TODO
|
||||||
go func() { http.Serve(rd.ln, rd.mux) }()
|
go func() { http.Serve(rd.ln, rd.mux) }()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ func setup(c *caddy.Controller) error {
|
|||||||
j = i / 2
|
j = i / 2
|
||||||
}
|
}
|
||||||
|
|
||||||
jitter := time.Duration(rand.Int63n(j.Nanoseconds()) - (j.Nanoseconds() / 2))
|
jitter := time.Duration(rand.Int63n(j.Nanoseconds()) - (j.Nanoseconds() / 2)) // #nosec G404 -- non-cryptographic jitter.
|
||||||
i = i + jitter
|
i = i + jitter
|
||||||
|
|
||||||
// prepare info for next onInstanceStartup event
|
// prepare info for next onInstanceStartup event
|
||||||
|
|||||||
@@ -62,8 +62,8 @@ func parse(c *caddy.Controller) (*Sign, error) {
|
|||||||
signers[i] = &Signer{
|
signers[i] = &Signer{
|
||||||
dbfile: dbfile,
|
dbfile: dbfile,
|
||||||
origin: origins[i],
|
origin: origins[i],
|
||||||
jitterIncep: time.Duration(float32(durationInceptionJitter) * rand.Float32()),
|
jitterIncep: time.Duration(float32(durationInceptionJitter) * rand.Float32()), // #nosec G404 -- non-cryptographic jitter.
|
||||||
jitterExpir: time.Duration(float32(durationExpirationDayJitter) * rand.Float32()),
|
jitterExpir: time.Duration(float32(durationExpirationDayJitter) * rand.Float32()), // #nosec G404 -- non-cryptographic jitter.
|
||||||
directory: "/var/lib/coredns",
|
directory: "/var/lib/coredns",
|
||||||
stop: make(chan struct{}),
|
stop: make(chan struct{}),
|
||||||
signedfile: fmt.Sprintf("db.%ssigned", origins[i]), // origins[i] is a fqdn, so it ends with a dot, hence %ssigned.
|
signedfile: fmt.Sprintf("db.%ssigned", origins[i]), // origins[i] is a fqdn, so it ends with a dot, hence %ssigned.
|
||||||
|
|||||||
@@ -12,7 +12,10 @@ func TempFile(dir, content string) (string, func(), error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
if err := os.WriteFile(f.Name(), []byte(content), 0644); err != nil {
|
if err := f.Close(); err != nil {
|
||||||
|
return "", nil, err
|
||||||
|
}
|
||||||
|
if err := os.WriteFile(f.Name(), []byte(content), 0600); err != nil {
|
||||||
return "", nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
rmFunc := func() { os.Remove(f.Name()) }
|
rmFunc := func() { os.Remove(f.Name()) }
|
||||||
@@ -43,7 +46,7 @@ xGbtCkhVk2VQ+BiCWnjYXJ6ZMzabP7wiOFDP9Pvr2ik22PRItsW/TLfHFXM1jDmc
|
|||||||
I1rs/VUGKzcJGVIWbHrgjP68CTStGAvKgbsTqw7aLXTSqtPw88N9XVSyRg==
|
I1rs/VUGKzcJGVIWbHrgjP68CTStGAvKgbsTqw7aLXTSqtPw88N9XVSyRg==
|
||||||
-----END CERTIFICATE-----`
|
-----END CERTIFICATE-----`
|
||||||
path := filepath.Join(tempDir, "ca.pem")
|
path := filepath.Join(tempDir, "ca.pem")
|
||||||
if err := os.WriteFile(path, []byte(data), 0644); err != nil {
|
if err := os.WriteFile(path, []byte(data), 0600); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
data = `-----BEGIN CERTIFICATE-----
|
data = `-----BEGIN CERTIFICATE-----
|
||||||
@@ -64,10 +67,11 @@ zhDEPP4FhY+Sz+y1yWirphl7A1aZwhXVPcfWIGqpQ3jzNwUeocbH27kuLh+U4hQo
|
|||||||
qeg10RdFnw==
|
qeg10RdFnw==
|
||||||
-----END CERTIFICATE-----`
|
-----END CERTIFICATE-----`
|
||||||
path = filepath.Join(tempDir, "cert.pem")
|
path = filepath.Join(tempDir, "cert.pem")
|
||||||
if err := os.WriteFile(path, []byte(data), 0644); err != nil {
|
if err := os.WriteFile(path, []byte(data), 0600); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//nolint:gosec // Test fixture private key.
|
||||||
data = `-----BEGIN RSA PRIVATE KEY-----
|
data = `-----BEGIN RSA PRIVATE KEY-----
|
||||||
MIIEpgIBAAKCAQEAxPBrvAIWiIJp383ndpRF+OuZ74pHsVLTJ/lSv05H+gzcGhL2
|
MIIEpgIBAAKCAQEAxPBrvAIWiIJp383ndpRF+OuZ74pHsVLTJ/lSv05H+gzcGhL2
|
||||||
y1i7kWXOvfmgvlPq3kZzZ7LvyZSz8KzTumyeNR0ofnlsOklJ0bvNb2Zc3J4vAh58
|
y1i7kWXOvfmgvlPq3kZzZ7LvyZSz8KzTumyeNR0ofnlsOklJ0bvNb2Zc3J4vAh58
|
||||||
@@ -96,7 +100,7 @@ E/WObVJXDnBdViu0L9abE9iaTToBVri4cmlDlZagLuKVR+TFTCN/DSlVZTDkqkLI
|
|||||||
8chzqtkH6b2b2R73hyRysWjsomys34ma3mEEPTX/aXeAF2MSZ/EWT9yL
|
8chzqtkH6b2b2R73hyRysWjsomys34ma3mEEPTX/aXeAF2MSZ/EWT9yL
|
||||||
-----END RSA PRIVATE KEY-----`
|
-----END RSA PRIVATE KEY-----`
|
||||||
path = filepath.Join(tempDir, "key.pem")
|
path = filepath.Join(tempDir, "key.pem")
|
||||||
if err := os.WriteFile(path, []byte(data), 0644); err != nil {
|
if err := os.WriteFile(path, []byte(data), 0600); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user