Skip to content
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

limit cname recursion #225

Merged
merged 2 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
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
19 changes: 18 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@ import (
sliceutil "github.com/projectdiscovery/utils/slice"
)

var ErrRetriesExceeded = errors.New("could not resolve, max retries exceeded")
var ()

var (
// DefaultMaxPerCNAMEFollows is the default number of times a CNAME can be followed within a trace
DefaultMaxPerCNAMEFollows = 32

// ErrRetriesExceeded is the error returned when the max retries are exceeded
ErrRetriesExceeded = errors.New("could not resolve, max retries exceeded")
)

var internalRangeCheckerInstance *internalRangeChecker

Expand Down Expand Up @@ -64,6 +72,10 @@ func NewWithOptions(options Options) (*Client, error) {
knownHosts, _ = hostsfile.ParseDefault()
}

if options.MaxPerCNAMEFollows == 0 {
options.MaxPerCNAMEFollows = DefaultMaxPerCNAMEFollows
}

httpClient := doh.NewHttpClientWithTimeout(options.Timeout)

client := Client{
Expand Down Expand Up @@ -480,6 +492,7 @@ func (c *Client) Trace(host string, requestType uint16, maxrecursion int) (*Trac
msg.SetQuestion(host, requestType)
servers := RootDNSServersIPv4
seenNS := make(map[string]struct{})
seenCName := make(map[string]int)
for i := 1; i < maxrecursion; i++ {
msg.SetQuestion(host, requestType)
dnsdatas, err := c.QueryParallel(host, requestType, servers)
Expand Down Expand Up @@ -542,6 +555,10 @@ func (c *Client) Trace(host string, requestType uint16, maxrecursion int) (*Trac

// follow cname if any
if nextCname != "" {
seenCName[nextCname]++
if seenCName[nextCname] > c.options.MaxPerCNAMEFollows {
break
}
host = nextCname
}
}
Expand Down
1 change: 1 addition & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Options struct {
LocalAddrIP net.IP
LocalAddrPort uint16
ConnectionPoolThreads int
MaxPerCNAMEFollows int
}

// Returns a net.Addr of a UDP or TCP type depending on whats required
Expand Down
Loading