Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 11, 16:00 UTC
use math/rand/v2 instead of math/rand (#7413)
WeidiDeng committed
Feb 12, 2026 at 00:15 UTC
47f3e8f8dc5f27632599aa2ee9a00ec7a6fba903
10 files changed
+23
-23
caddytest/integration/listener_test.go
+2
-2
@@ -3,7 +3,7 @@ package integration
3
import (
4
"bytes"
5
"fmt"
6
- "math/rand"
6
+ "math/rand/v2"
7
"net"
8
"net/http"
9
"strings"
@@ -54,7 +54,7 @@ func TestHTTPRedirectWrapperWithLargeUpload(t *testing.T) {
54
const uploadSize = (1024 * 1024) + 1 // 1 MB + 1 byte
55
// 1 more than an MB
56
body := make([]byte, uploadSize)
57
- rand.New(rand.NewSource(0)).Read(body)
57
+ rand.NewChaCha8([32]byte{}).Read(body)
58
59
tester := setupListenerWrapperTest(t, func(writer http.ResponseWriter, request *http.Request) {
60
buf := new(bytes.Buffer)
modules/caddyhttp/caddyauth/basicauth.go
+2
-2
@@ -19,7 +19,7 @@ import (
19
"encoding/hex"
20
"encoding/json"
21
"fmt"
22
- weakrand "math/rand"
22
+ weakrand "math/rand/v2"
23
"net/http"
24
"strings"
25
"sync"
@@ -244,7 +244,7 @@ func (c *Cache) makeRoom() {
244
// strategy; generating random numbers is cheap and
245
// ensures a much better distribution.
246
//nolint:gosec
247
- rnd := weakrand.Intn(len(c.cache))
247
+ rnd := weakrand.IntN(len(c.cache))
248
i := 0
249
for key := range c.cache {
250
if i == rnd {
modules/caddyhttp/errors.go
+2
-2
@@ -17,7 +17,7 @@ package caddyhttp
17
import (
18
"errors"
19
"fmt"
20
- weakrand "math/rand"
20
+ weakrand "math/rand/v2"
21
"path"
22
"runtime"
23
"strings"
@@ -98,7 +98,7 @@ func randString(n int, sameCase bool) string {
98
b := make([]byte, n)
99
for i := range b {
100
//nolint:gosec
101
- b[i] = dict[weakrand.Int63()%int64(len(dict))]
101
+ b[i] = dict[weakrand.IntN(len(dict))]
102
}
103
return string(b)
104
}
modules/caddyhttp/fileserver/staticfiles.go
+2
-2
@@ -20,7 +20,7 @@ import (
20
"fmt"
21
"io"
22
"io/fs"
23
- weakrand "math/rand"
23
+ weakrand "math/rand/v2"
24
"mime"
25
"net/http"
26
"os"
@@ -601,7 +601,7 @@ func (fsrv *FileServer) openFile(fileSystem fs.FS, filename string, w http.Respo
601
// maybe the server is under load and ran out of file descriptors?
602
// have client wait arbitrary seconds to help prevent a stampede
603
//nolint:gosec
604
- backoff := weakrand.Intn(maxBackoff-minBackoff) + minBackoff
604
+ backoff := weakrand.IntN(maxBackoff-minBackoff) + minBackoff
605
w.Header().Set("Retry-After", strconv.Itoa(backoff))
606
if c := fsrv.logger.Check(zapcore.DebugLevel, "retry after backoff"); c != nil {
607
c.Write(zap.String("filename", filename), zap.Int("backoff", backoff), zap.Error(err))
modules/caddyhttp/reverseproxy/fastcgi/client_test.go
+2
-2
@@ -27,7 +27,7 @@ import (
27
"fmt"
28
"io"
29
"log"
30
- "math/rand"
30
+ "math/rand/v2"
31
"net"
32
"net/http"
33
"net/http/fcgi"
@@ -197,7 +197,7 @@ func generateRandFile(size int) (p string, m string) {
197
h := md5.New()
198
for i := 0; i < size/16; i++ {
199
buf := make([]byte, 16)
200
- binary.PutVarint(buf, rand.Int63())
200
+ binary.PutVarint(buf, rand.Int64())
201
if _, err := fo.Write(buf); err != nil {
202
log.Printf("[ERROR] failed to write buffer: %v\n", err)
203
}
modules/caddyhttp/reverseproxy/httptransport.go
+2
-2
@@ -21,7 +21,7 @@ import (
21
"encoding/base64"
22
"encoding/json"
23
"fmt"
24
- weakrand "math/rand"
24
+ weakrand "math/rand/v2"
25
"net"
26
"net/http"
27
"net/url"
@@ -266,7 +266,7 @@ func (h *HTTPTransport) NewTransport(caddyCtx caddy.Context) (*http.Transport, e
266
PreferGo: true,
267
Dial: func(ctx context.Context, _, _ string) (net.Conn, error) {
268
//nolint:gosec
269
- addr := h.Resolver.netAddrs[weakrand.Intn(len(h.Resolver.netAddrs))]
269
+ addr := h.Resolver.netAddrs[weakrand.IntN(len(h.Resolver.netAddrs))]
270
return d.DialContext(ctx, addr.Network, addr.JoinHostPort(0))
271
},
272
}
modules/caddyhttp/reverseproxy/selectionpolicies.go
+5
-5
@@ -20,7 +20,7 @@ import (
20
"encoding/hex"
21
"encoding/json"
22
"fmt"
23
- weakrand "math/rand"
23
+ weakrand "math/rand/v2"
24
"net"
25
"net/http"
26
"strconv"
@@ -225,7 +225,7 @@ func (r RandomChoiceSelection) Select(pool UpstreamPool, _ *http.Request, _ http
225
if !upstream.Available() {
226
continue
227
}
228
- j := weakrand.Intn(i + 1) //nolint:gosec
228
+ j := weakrand.IntN(i + 1) //nolint:gosec
229
if j < k {
230
choices[j] = upstream
231
}
@@ -274,7 +274,7 @@ func (LeastConnSelection) Select(pool UpstreamPool, _ *http.Request, _ http.Resp
274
// sample: https://en.wikipedia.org/wiki/Reservoir_sampling
275
if numReqs == leastReqs {
276
count++
277
- if count == 1 || (weakrand.Int()%count) == 0 { //nolint:gosec
277
+ if count == 1 || weakrand.IntN(count) == 0 { //nolint:gosec
278
bestHost = host
279
}
280
}
@@ -788,7 +788,7 @@ func selectRandomHost(pool []*Upstream) *Upstream {
788
// upstream will always be chosen if there is at
789
// least one available
790
count++
791
- if (weakrand.Int() % count) == 0 { //nolint:gosec
791
+ if weakrand.IntN(count) == 0 { //nolint:gosec
792
randomHost = upstream
793
}
794
}
@@ -827,7 +827,7 @@ func leastRequests(upstreams []*Upstream) *Upstream {
827
if len(best) == 1 {
828
return best[0]
829
}
830
- return best[weakrand.Intn(len(best))] //nolint:gosec
830
+ return best[weakrand.IntN(len(best))] //nolint:gosec
831
}
832
833
// hostByHashing returns an available host from pool based on a hashable string s.
modules/caddyhttp/reverseproxy/streaming.go
+1
-1
@@ -24,7 +24,7 @@ import (
24
"errors"
25
"fmt"
26
"io"
27
- weakrand "math/rand"
27
+ weakrand "math/rand/v2"
28
"mime"
29
"net/http"
30
"sync"
modules/caddyhttp/reverseproxy/upstreams.go
+3
-3
@@ -4,7 +4,7 @@ import (
4
"context"
5
"encoding/json"
6
"fmt"
7
- weakrand "math/rand"
7
+ weakrand "math/rand/v2"
8
"net"
9
"net/http"
10
"strconv"
@@ -107,7 +107,7 @@ func (su *SRVUpstreams) Provision(ctx caddy.Context) error {
107
PreferGo: true,
108
Dial: func(ctx context.Context, _, _ string) (net.Conn, error) {
109
//nolint:gosec
110
- addr := su.Resolver.netAddrs[weakrand.Intn(len(su.Resolver.netAddrs))]
110
+ addr := su.Resolver.netAddrs[weakrand.IntN(len(su.Resolver.netAddrs))]
111
return d.DialContext(ctx, addr.Network, addr.JoinHostPort(0))
112
},
113
}
@@ -330,7 +330,7 @@ func (au *AUpstreams) Provision(ctx caddy.Context) error {
330
PreferGo: true,
331
Dial: func(ctx context.Context, _, _ string) (net.Conn, error) {
332
//nolint:gosec
333
- addr := au.Resolver.netAddrs[weakrand.Intn(len(au.Resolver.netAddrs))]
333
+ addr := au.Resolver.netAddrs[weakrand.IntN(len(au.Resolver.netAddrs))]
334
return d.DialContext(ctx, addr.Network, addr.JoinHostPort(0))
335
},
336
}
modules/caddypki/acmeserver/acmeserver.go
+2
-2
@@ -17,7 +17,7 @@ package acmeserver
17
import (
18
"context"
19
"fmt"
20
- weakrand "math/rand"
20
+ weakrand "math/rand/v2"
21
"net"
22
"net/http"
23
"os"
@@ -307,7 +307,7 @@ func (ash Handler) makeClient() (acme.Client, error) {
307
PreferGo: true,
308
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
309
//nolint:gosec
310
- addr := ash.resolvers[weakrand.Intn(len(ash.resolvers))]
310
+ addr := ash.resolvers[weakrand.IntN(len(ash.resolvers))]
311
return dialer.DialContext(ctx, addr.Network, addr.JoinHostPort(0))
312
},
313
}