Skip to content

fix: use crypto/rand instead of math/rand #291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 23, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions internal/dns/dns.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Blink Labs Software
// Copyright 2025 Blink Labs Software
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
Expand All @@ -9,7 +9,8 @@ package dns
import (
"fmt"
"log/slog"
"math/rand"
"crypto/rand"
"math/big"
"net"
"os"
"strings"
Expand Down Expand Up @@ -326,7 +327,11 @@ func randomNameserverAddress(nameservers map[string][]net.IP) net.IP {
tmpNameservers = append(tmpNameservers, addresses...)
}
if len(tmpNameservers) > 0 {
tmpNameserver := tmpNameservers[rand.Intn(len(tmpNameservers))]
n, err := rand.Int(rand.Reader, big.NewInt(int64(len(tmpNameservers))))
if err != nil {
return nil
}
tmpNameserver := tmpNameservers[n.Int64()]
return tmpNameserver
}
return nil
Expand Down Expand Up @@ -511,12 +516,20 @@ func randomNameserver(nameservers map[string][]net.IP) (string, string) {
mapKeys = append(mapKeys, k)
}
if len(mapKeys) > 0 {
randNsName := mapKeys[rand.Intn(len(mapKeys))]
n, err := rand.Int(rand.Reader, big.NewInt(int64(len(mapKeys))))
if err != nil {
return "", ""
}
randNsName := mapKeys[n.Int64()]
randNsAddresses := nameservers[randNsName]
if randNsAddresses == nil {
return "", ""
}
randNsAddress := randNsAddresses[rand.Intn(len(randNsAddresses))].String()
n, err = rand.Int(rand.Reader, big.NewInt(int64(len(randNsAddresses))))
if err != nil {
return "", ""
}
randNsAddress := randNsAddresses[n.Int64()].String()
return randNsName, randNsAddress
}
return "", ""
Expand All @@ -531,9 +544,11 @@ func createQuery(recordName string, recordType uint16) *dns.Msg {

func randomFallbackServer() string {
cfg := config.GetConfig()
return cfg.Dns.FallbackServers[rand.Intn(
len(cfg.Dns.FallbackServers),
)]
n, err := rand.Int(rand.Reader, big.NewInt(int64(len(cfg.Dns.FallbackServers))))
if err != nil {
return ""
}
return cfg.Dns.FallbackServers[n.Int64()]
}

func formatMessageAnswerSection(section []dns.RR) string {
Expand Down