Skip to content

Commit

Permalink
Merge pull request #225 from projectdiscovery/bugfix-recursive-cname
Browse files Browse the repository at this point in the history
limit cname recursion
  • Loading branch information
Mzack9999 authored Sep 2, 2024
2 parents 86a5a5a + c262211 commit e9a62fb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
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

0 comments on commit e9a62fb

Please sign in to comment.