-
Notifications
You must be signed in to change notification settings - Fork 0
/
slow_test.go
102 lines (79 loc) · 2.67 KB
/
slow_test.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
package turtle
import (
"io"
"net/http"
"testing"
"time"
"github.com/b4fun/turtle/internal/testhttp"
"github.com/stretchr/testify/assert"
)
func Test_SlowBodyReadRequest_defaults(t *testing.T) {
v := SlowBodyReadRequest{}
assert.NoError(t, v.defaults())
assert.Equal(t, http.MethodPost, v.Method)
assert.NotNil(t, v.randn)
}
func Test_SlowBodyReadRequest_vulnerable(t *testing.T) {
t.Parallel()
counter := testhttp.NewConnStateCounters()
testCtx, stopTest := getTestContext()
defer stopTest()
serverUrl, serverStopped := testhttp.CreateTestServer(testCtx, t, func(s *http.Server) {
s.ReadTimeout = 0 // hung forever
s.ConnState = counter.ServerConnState
s.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Body == nil {
return
}
defer r.Body.Close()
_, _ = io.Copy(io.Discard, r.Body)
})
})
target := getTestTarget(t, *serverUrl)
attack := SlowBodyReadRequest{Target: target}
assert.NoError(t, attack.Run(testCtx))
stopTest()
<-serverStopped
t.Log(counter.String())
conns := counter.GetConns()
assert.NotEmpty(t, conns, "should have at least one connection")
assert.Len(t, conns, target.Connections, "should not create more connections")
for _, conn := range conns {
timeline := counter.GetConnStateTimeline(conn)
assert.GreaterOrEqual(t, len(timeline), 1, "should have at least one state")
// a typical state transition is: new - (due to server close) -> active -> closed
assert.Equal(t, http.StateNew, timeline[0])
}
}
func Test_SlowBodyReadRequest_invulnerable(t *testing.T) {
t.Parallel()
counter := testhttp.NewConnStateCounters()
testCtx, stopTest := getTestContext()
defer stopTest()
serverUrl, serverStopped := testhttp.CreateTestServer(testCtx, t, func(s *http.Server) {
s.ReadTimeout = 1 * time.Second
s.ConnState = counter.ServerConnState
s.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Body == nil {
return
}
defer r.Body.Close()
_, _ = io.Copy(io.Discard, r.Body)
})
})
target := getTestTarget(t, *serverUrl)
attack := SlowBodyReadRequest{Target: target}
assert.NoError(t, attack.Run(testCtx))
stopTest()
<-serverStopped
t.Log(counter.String())
conns := counter.GetConns()
assert.NotEmpty(t, conns, "should have at least one connection")
assert.Greater(t, len(conns), target.Connections, "should create more connections")
for _, conn := range conns {
timeline := counter.GetConnStateTimeline(conn)
assert.GreaterOrEqual(t, len(timeline), 1, "should have at least one state")
// a typical state transition is: new - (read body) -> active - (read body timeout close) -> closed
assert.Equal(t, http.StateNew, timeline[0])
}
}