Repository navigation
Expand file tree
/
Copy pathutils_test.go
More file actions
128 lines (112 loc) · 4.95 KB
/
Copy pathutils_test.go
File metadata and controls
128 lines (112 loc) · 4.95 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package http
import (
"bytes"
"context"
"net/http"
"strings"
"testing"
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/http/raw"
"github.com/projectdiscovery/nuclei/v3/pkg/utils/httpbody"
"github.com/projectdiscovery/retryablehttp-go"
urlutil "github.com/projectdiscovery/utils/url"
"github.com/stretchr/testify/require"
)
func TestGetHTTPProjectCacheScope_SeparatesSchemeAndPort(t *testing.T) {
dumped := requestDump{data: []byte("GET / HTTP/1.1\r\nHost: example.com\r\n\r\n")}
httpScoped := getHTTPProjectCacheScope(dumped, nil, "http", "example.com:80")
httpsScoped := getHTTPProjectCacheScope(dumped, nil, "https", "example.com:443")
require.NotEqual(t, httpScoped, httpsScoped)
require.True(t, bytes.HasSuffix(httpScoped, dumped.data))
require.True(t, bytes.HasSuffix(httpsScoped, dumped.data))
}
func TestDumpRequestMatchesRetryableHTTPDump(t *testing.T) {
tests := []struct {
name string
method string
url string
body string
headers map[string]string
host string
}{
{name: "get", method: http.MethodGet, url: "http://example.com/path?a=1"},
{name: "head", method: http.MethodHead, url: "http://example.com/"},
{name: "post body", method: http.MethodPost, url: "http://example.com/submit", body: "a=1&b=2", headers: map[string]string{"Content-Type": "application/x-www-form-urlencoded"}},
{name: "https", method: http.MethodPost, url: "https://example.com:8443/x", body: "{}"},
{name: "custom host", method: http.MethodGet, url: "http://127.0.0.1/", host: "virtual.example"},
{name: "headers", method: http.MethodPut, url: "http://example.com/", body: "data", headers: map[string]string{"Authorization": "Basic dXNlcjpwYXNz", "X-Custom": "1", "User-Agent": "nuclei"}},
{name: "accept encoding", method: http.MethodGet, url: "http://example.com/", headers: map[string]string{"Accept-Encoding": "br"}},
{name: "range", method: http.MethodGet, url: "http://example.com/", headers: map[string]string{"Range": "bytes=0-10"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
build := func(withHTTPBody bool) *retryablehttp.Request {
urlx, err := urlutil.ParseAbsoluteURL(tt.url, true)
require.NoError(t, err)
var body interface{}
if !withHTTPBody && tt.body != "" {
body = tt.body
}
req, err := retryablehttp.NewRequestFromURLWithContext(context.Background(), tt.method, urlx, body)
require.NoError(t, err)
if withHTTPBody && tt.body != "" {
httpbody.SetString(req, tt.body)
}
for k, v := range tt.headers {
req.Header.Set(k, v)
}
if tt.host != "" {
req.Host = tt.host
}
return req
}
expected, err := build(false).Dump()
require.NoError(t, err)
actual, err := dumpRequest(build(true), 0)
require.NoError(t, err)
require.Equal(t, string(expected), string(actual.data))
require.Zero(t, actual.truncated)
})
}
}
func TestDumpRequestTruncatesLargeBody(t *testing.T) {
body := strings.Repeat("A", 1024*1024)
req, err := retryablehttp.NewRequest(http.MethodPost, "http://example.com/", nil)
require.NoError(t, err)
httpbody.SetString(req, body)
dumped, err := dumpRequest(req, 100)
require.NoError(t, err)
require.Equal(t, len(body)-100, dumped.truncated)
headers, rest, found := strings.Cut(string(dumped.data), "\r\n\r\n")
require.True(t, found)
require.Contains(t, headers, "Content-Length: 1048576", "headers must keep the real length")
require.Equal(t, strings.Repeat("A", 100)+"\n...[truncated 1048476 bytes]", rest)
full, err := dumpRequest(req, 0)
require.NoError(t, err)
require.Zero(t, full.truncated)
require.True(t, strings.HasSuffix(string(full.data), body))
capped := capDumpBody(full.data, 100)
_, rest, found = strings.Cut(string(capped), "\r\n\r\n")
require.True(t, found)
require.Equal(t, strings.Repeat("A", 100)+"\n...[truncated 1048476 bytes]", rest)
require.Equal(t, full.data, capDumpBody(full.data, 0))
require.Equal(t, full.data, capDumpBody(full.data, len(body)))
}
func TestDumpRawRequestTruncatesLargeBody(t *testing.T) {
generated := &generatedRequest{rawRequest: &raw.Request{
Method: http.MethodPost,
Path: "/",
Headers: map[string]string{"Host": "example.com", "Content-Length": "5000"},
Data: strings.Repeat("B", 5000),
}}
dumped, err := dump(generated, "http://example.com/", 1000)
require.NoError(t, err)
require.Equal(t, 4000, dumped.truncated)
require.Contains(t, string(dumped.data), "Content-Length: 5000")
require.True(t, strings.HasSuffix(string(dumped.data), strings.Repeat("B", 1000)+"\n...[truncated 4000 bytes]"))
}
func TestGetHTTPProjectCacheScope_HashesTruncatedBody(t *testing.T) {
dumped := requestDump{data: []byte("POST / HTTP/1.1\r\nHost: example.com\r\n\r\nAAAA\n...[truncated 4 bytes]"), truncated: 4}
first := getHTTPProjectCacheScope(dumped, []byte("AAAABBBB"), "http", "example.com")
second := getHTTPProjectCacheScope(dumped, []byte("AAAACCCC"), "http", "example.com")
require.NotEqual(t, first, second, "bodies differing after the cap must get different keys")
}