-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
421 lines (377 loc) · 13.1 KB
/
http.go
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package main
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"strings"
"github.com/julienschmidt/httprouter"
"github.com/projectdiscovery/httpx/internal/testutils"
fileutil "github.com/projectdiscovery/utils/file"
)
var httpTestcases = map[string]testutils.TestCase{
"Standard HTTP GET Request": &standardHttpGet{},
"Standard HTTPS GET Request": &standardHttpGet{tls: true},
"Raw HTTP GET Request": &standardHttpGet{unsafe: true},
"Raw request with non standard rfc path via stdin": &standardHttpGet{unsafe: true, stdinPath: "/%invalid"},
"Raw request with non standard rfc path via cli flag": &standardHttpGet{unsafe: true, path: "/%invalid"},
"Regression test for: https://github.com/projectdiscovery/httpx/issues/363": &issue363{}, // infinite redirect
"Regression test for: https://github.com/projectdiscovery/httpx/issues/276": &issue276{}, // full path with port in output
"Regression test for: https://github.com/projectdiscovery/httpx/issues/277": &issue277{}, // scheme://host:port via stdin
"Regression test for: https://github.com/projectdiscovery/httpx/issues/303": &issue303{}, // misconfigured gzip header with uncompressed body
"Regression test for: https://github.com/projectdiscovery/httpx/issues/400": &issue400{}, // post operation with body
"Regression test for: https://github.com/projectdiscovery/httpx/issues/414": &issue414{}, // stream mode with path
"Regression test for unwanted chars": &titleUnwantedChars{}, // new line scanning with title flag, Regression test for: https://github.com/projectdiscovery/httpx/issues/433
"Request URI to existing file - https://github.com/projectdiscovery/httpx/issues/480": &issue480{}, // request uri pointing to existing file
"Standard HTTP GET Request with match response time": &standardHttpGet{mrt: true, inputValue: "\"<10s\""},
"Standard HTTP GET Request with filter response time": &standardHttpGet{frt: true, inputValue: "\">3ms\""},
"Multiple Custom Header": &customHeader{inputData: []string{"-debug-req", "-H", "'user-agent: test'", "-H", "'foo: bar'"}, expectedOutput: []string{"User-Agent: test", "Foo: bar"}},
"Output Match Condition": &outputMatchCondition{inputData: []string{"-silent", "-mdc", "\"status_code == 200\""}},
"Output Filter Condition": &outputFilterCondition{inputData: []string{"-silent", "-fdc", "\"status_code == 400\""}},
"Output All": &outputAll{},
}
type standardHttpGet struct {
tls bool
unsafe bool
mrt bool
frt bool
inputValue string
stdinPath string
path string
expectedOutput string
}
func (h *standardHttpGet) Execute() error {
router := httprouter.New()
router.GET("/", httprouter.Handle(func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprintf(w, "This is a test")
r.Close = true
}))
var ts *httptest.Server
if h.tls {
ts = httptest.NewTLSServer(router)
} else {
ts = httptest.NewServer(router)
}
defer ts.Close()
var extra []string
if h.unsafe {
extra = append(extra, "-unsafe")
}
if h.path != "" {
extra = append(extra, "-path", "\""+h.path+"\"")
}
if h.mrt {
extra = append(extra, "-mrt", h.inputValue)
}
if h.frt {
extra = append(extra, "-frt", h.inputValue)
}
URL := ts.URL
if h.stdinPath != "" {
URL += h.stdinPath
}
results, err := testutils.RunHttpxAndGetResults(URL, debug, extra...)
if err != nil {
return err
}
if len(results) != 1 {
return errIncorrectResultsCount(results)
}
if h.expectedOutput != "" && !strings.EqualFold(results[0], h.expectedOutput) {
return errIncorrectResult(h.expectedOutput, results[0])
}
return nil
}
type issue276 struct{}
func (h *issue276) Execute() error {
var ts *httptest.Server
router := httprouter.New()
router.GET("/redirect", httprouter.Handle(func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
w.Header().Add("Location", ts.URL+"/redirect")
w.WriteHeader(302)
fmt.Fprintf(w, "<html><body><title>Object moved</title></body></html>")
}))
ts = httptest.NewServer(router)
defer ts.Close()
results, err := testutils.RunHttpxAndGetResults(ts.URL+"/redirect", debug, "-status-code", "-title", "-no-color")
if err != nil {
return err
}
if len(results) != 1 {
return errIncorrectResultsCount(results)
}
// check if we have all the items on the cli
// full url with port
// status code
// title
expected := ts.URL + "/redirect" + " [302] [Object moved]"
if !strings.EqualFold(results[0], expected) {
return errIncorrectResult(expected, results[0])
}
return nil
}
type issue277 struct{}
func (h *issue277) Execute() error {
var ts *httptest.Server
router := httprouter.New()
router.GET("/hpp", httprouter.Handle(func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
if p.ByName("pp") == `%22%3E%3Ch1%3Easdasd%3C%2Fh1%3E` {
w.WriteHeader(http.StatusOK)
}
}))
ts = httptest.NewServer(router)
defer ts.Close()
uripath := "/hpp/?pp=%22%3E%3Ch1%3Easdasd%3C%2Fh1%3E"
results, err := testutils.RunHttpxAndGetResults(ts.URL+uripath, debug)
if err != nil {
return err
}
if len(results) != 1 {
return errIncorrectResultsCount(results)
}
// check if we have all the items on the cli
// full url with port
// status code
// title
expected := ts.URL + uripath
if !strings.EqualFold(results[0], expected) {
return errIncorrectResult(expected, results[0])
}
return nil
}
type issue303 struct{}
func (h *issue303) Execute() error {
var ts *httptest.Server
router := httprouter.New()
router.GET("/hpp", httprouter.Handle(func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
// mimic a misconfigured web server behavior declaring gzip body
w.Header().Add("Content-Encoding", "gzip")
// but sending it uncompressed
fmt.Fprint(w, "<html><body>This is a test</body></html>")
}))
ts = httptest.NewServer(router)
defer ts.Close()
results, err := testutils.RunHttpxAndGetResults(ts.URL, debug)
if err != nil {
return err
}
if len(results) != 1 {
return errIncorrectResultsCount(results)
}
// check if we have all the items on the cli
// full url with port
expected := ts.URL
if !strings.EqualFold(results[0], expected) {
return errIncorrectResult(expected, results[0])
}
return nil
}
type issue363 struct{}
func (h *issue363) Execute() error {
var ts *httptest.Server
router := httprouter.New()
router.GET("/redirect", httprouter.Handle(func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
w.Header().Add("Location", ts.URL+"/redirect")
w.WriteHeader(302)
fmt.Fprintf(w, "<html><body><title>Object moved</title></body></html>")
}))
ts = httptest.NewServer(router)
defer ts.Close()
results, err := testutils.RunHttpxAndGetResults(ts.URL+"/redirect", debug, "-no-color", "-follow-redirects")
if err != nil {
return err
}
if len(results) != 1 {
return errIncorrectResultsCount(results)
}
return nil
}
type issue400 struct{}
func (h *issue400) Execute() error {
var ts *httptest.Server
router := httprouter.New()
router.POST("/receive", httprouter.Handle(func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
w.Header().Add("Content-Type", "application/json")
data, _ := io.ReadAll(r.Body)
fmt.Fprintf(w, "data received %s", data)
}))
ts = httptest.NewServer(router)
defer ts.Close()
results, err := testutils.RunHttpxAndGetResults(ts.URL+"/receive", debug, "-body 'a=b'", "-x POST", "-status-code")
if err != nil {
return err
}
if len(results) != 1 {
return errIncorrectResultsCount(results)
}
return nil
}
type issue414 struct{}
func (h *issue414) Execute() error {
var ts *httptest.Server
uripath := "/path"
router := httprouter.New()
router.POST(uripath, httprouter.Handle(func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
w.Header().Add("Content-Type", "application/json")
data, _ := io.ReadAll(r.Body)
fmt.Fprintf(w, "data received %s", data)
}))
ts = httptest.NewServer(router)
defer ts.Close()
results, err := testutils.RunHttpxAndGetResults(ts.URL, debug, fmt.Sprintf("-path '%s'", uripath))
if err != nil {
return err
}
if len(results) != 1 {
return errIncorrectResultsCount(results)
}
expected := ts.URL + uripath
if !strings.EqualFold(results[0], expected) {
return errIncorrectResult(expected, results[0])
}
return nil
}
type titleUnwantedChars struct{}
func (h *titleUnwantedChars) Execute() error {
var ts *httptest.Server
router := httprouter.New()
uriPath := "/index"
router.GET(uriPath, httprouter.Handle(func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
htmlResponse := "<html><head><title>\v\fProject\n\r Discovery\n - Httpx\t></title></head><body>test data</body></html>"
fmt.Fprint(w, htmlResponse)
}))
ts = httptest.NewServer(router)
defer ts.Close()
results, err := testutils.RunHttpxAndGetResults(fmt.Sprint(ts.URL, uriPath), debug, "-title", "-no-color")
if err != nil {
return err
}
if strings.Contains(results[0], "\n") {
return errIncorrectResultsCount(results)
}
if strings.Contains(results[0], "\r") {
return errIncorrectResultsCount(results)
}
return nil
}
type issue480 struct{}
func (h *issue480) Execute() error {
var ts *httptest.Server
router := httprouter.New()
uriPath := "////////////////../../../../../../../../etc/passwd"
router.GET(uriPath, httprouter.Handle(func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
htmlResponse := "<html><body>ok from uri</body></html>"
fmt.Fprint(w, htmlResponse)
}))
ts = httptest.NewServer(router)
defer ts.Close()
results, err := testutils.RunHttpxAndGetResults(ts.URL, debug, "-path", "////////////////../../../../../../../../etc/passwd")
if err != nil {
return err
}
if !strings.Contains(results[0], uriPath) {
return errIncorrectResultsCount(results)
}
return nil
}
type customHeader struct {
inputData []string
expectedOutput []string
}
func (h *customHeader) Execute() error {
var ts *httptest.Server
router := httprouter.New()
router.GET("/", httprouter.Handle(func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
w.Header().Add("Content-Type", "application/json")
fmt.Fprint(w, `{"status": "ok"}`)
}))
ts = httptest.NewServer(router)
defer ts.Close()
results, err := testutils.RunHttpxAndGetCombinedResults(ts.URL, true, h.inputData...)
if err != nil {
return err
}
for _, expected := range h.expectedOutput {
if !strings.Contains(results, expected) {
return errIncorrectResult(expected, results)
}
}
return nil
}
type outputMatchCondition struct {
inputData []string
}
func (h *outputMatchCondition) Execute() error {
var ts *httptest.Server
router := httprouter.New()
router.GET("/", httprouter.Handle(func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(200)
fmt.Fprint(w, `{"status": "ok"}`)
}))
ts = httptest.NewServer(router)
defer ts.Close()
results, err := testutils.RunHttpxAndGetResults(ts.URL, false, h.inputData...)
if err != nil {
return err
}
if len(results) != 1 {
return errIncorrectResultsCount(results)
}
return nil
}
type outputFilterCondition struct {
inputData []string
}
func (h *outputFilterCondition) Execute() error {
var ts *httptest.Server
router := httprouter.New()
router.GET("/", httprouter.Handle(func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(200)
fmt.Fprint(w, `{"status": "ok"}`)
}))
ts = httptest.NewServer(router)
defer ts.Close()
results, err := testutils.RunHttpxAndGetResults(ts.URL, false, h.inputData...)
if err != nil {
return err
}
if len(results) != 1 {
return errIncorrectResultsCount(results)
}
return nil
}
type outputAll struct {
}
func (h *outputAll) Execute() error {
var ts *httptest.Server
router := httprouter.New()
router.GET("/", httprouter.Handle(func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(200)
fmt.Fprint(w, `{"status": "ok"}`)
}))
ts = httptest.NewServer(router)
defer ts.Close()
fileName := "test_output_all"
_, hErr := testutils.RunHttpxAndGetResults(ts.URL, false, []string{"-o", fileName, "-oa"}...)
if hErr != nil {
return hErr
}
expectedFiles := []string{fileName, fileName + ".json", fileName + ".csv"}
var actualFiles []string
for _, file := range expectedFiles {
if fileutil.FileExists(file) {
actualFiles = append(actualFiles, file)
}
}
if len(actualFiles) != 3 {
return errIncorrectResultsCount(actualFiles)
}
for _, file := range actualFiles {
_ = os.Remove(file)
}
return nil
}