Repository navigation
Expand file tree
/
Copy pathcatchall.go
More file actions
61 lines (57 loc) · 2.25 KB
/
Copy pathcatchall.go
File metadata and controls
61 lines (57 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package http
import (
"context"
"net/http"
"strings"
"sync"
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/http/catchall"
"github.com/projectdiscovery/retryablehttp-go"
httpUtils "github.com/projectdiscovery/utils/http"
)
// catchAllCheck returns the lazy check for whether resp is the page the host
// serves for any missing path, or nil when calibration cannot judge this
// request. Fuzzed and raced requests target real endpoints on purpose, and
// only GET and HEAD are probed so that calibration never sends a body.
func (request *Request) catchAllCheck(client *retryablehttp.Client, generated *generatedRequest, resp catchall.Response, rateLimitKey string) func() bool {
detector := request.options.CatchAll
sent := generated.request
if detector == nil || client == nil || sent == nil || sent.URL == nil || generated.fuzzGeneratedRequest.Request != nil || request.Race {
return nil
}
if sent.Method != http.MethodGet && sent.Method != http.MethodHead {
return nil
}
// The response buffers are reused for the next response in the chain,
// and the check may run after that.
resp.Body = strings.Clone(resp.Body)
requestURL := *sent.URL.URL
header := sent.Header.Clone()
hostHeader := sent.Host
limit := int64(request.getEffectiveReadLimit())
probe := func(ctx context.Context, path string) (catchall.Response, error) {
if err := request.rateLimitTake(ctx, rateLimitKey); err != nil {
return catchall.Response{}, err
}
target := requestURL
target.Path, target.RawPath, target.RawQuery = path, "", ""
probeReq, err := retryablehttp.NewRequestWithContext(ctx, sent.Method, target.String(), nil)
if err != nil {
return catchall.Response{}, err
}
probeReq.Header = header.Clone()
probeReq.Host = hostHeader
probeResp, err := client.Do(probeReq)
if err != nil {
return catchall.Response{}, err
}
chain := httpUtils.NewResponseChain(probeResp, limit)
defer chain.Close()
if err := chain.Fill(); err != nil {
return catchall.Response{}, err
}
final := chain.Response()
return catchall.Response{StatusCode: final.StatusCode, Location: final.Header.Get("Location"), Body: chain.BodyString()}, nil
}
ctx := sent.Context()
return sync.OnceValue(func() bool { return detector.Matches(ctx, &requestURL, resp, probe) })
}