Repository navigation
Expand file tree
/
Copy pathserver_conformance_test.go
More file actions
398 lines (305 loc) · 11.5 KB
/
Copy pathserver_conformance_test.go
File metadata and controls
398 lines (305 loc) · 11.5 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
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
package http2
import (
"bufio"
"crypto/tls"
"encoding/binary"
"testing"
"time"
"github.com/valyala/fasthttp"
)
// h2spec covers the server against the spec text. These fill in the cases it
// does not reach, using the raw client from dos_test.go.
// writeRaw emits a frame byte for byte, so a test can send frame types the
// library's own types cannot express.
func (a *attacker) writeRaw(kind, flags byte, stream uint32, payload []byte) error {
var header [9]byte
header[0] = byte(len(payload) >> 16)
header[1] = byte(len(payload) >> 8)
header[2] = byte(len(payload))
header[3] = kind
header[4] = flags
binary.BigEndian.PutUint32(header[5:], stream)
if _, err := a.bw.Write(header[:]); err != nil {
return err
}
if _, err := a.bw.Write(payload); err != nil {
return err
}
return a.bw.Flush()
}
// writeData sends a DATA frame on a stream.
func (a *attacker) writeData(id uint32, endStream bool, body []byte) error {
fr := AcquireFrameHeader()
fr.SetStream(id)
data := AcquireFrame(FrameData).(*Data)
data.SetPadding(false)
data.SetEndStream(endStream)
data.SetData(body)
fr.SetBody(data)
return a.write(fr)
}
// TestServerIgnoresUnknownFrameType covers RFC 7540 4.1 and 5.5: a frame of an
// unknown type is discarded. Extension frames such as ALTSVC, ORIGIN and
// PRIORITY_UPDATE are the ordinary reason a peer sends one, so rejecting them
// breaks connections that are behaving correctly.
func TestServerIgnoresUnknownFrameType(t *testing.T) {
addr, handled := newAttackServer(t, ServerConfig{PingInterval: -1})
a := dialAttacker(t, addr)
d := a.drain()
if err := a.writeRaw(0x1f, 0, 0, []byte("extension payload")); err != nil {
t.Fatalf("writing the extension frame: %v", err)
}
if err := a.writeHeaders(1, true, true, requestFields(addr)); err != nil {
t.Fatalf("writing the request: %v", err)
}
a.flush()
deadline := time.Now().Add(5 * time.Second)
for handled.Load() == 0 && time.Now().Before(deadline) {
time.Sleep(10 * time.Millisecond)
}
if handled.Load() == 0 {
t.Error("the request never reached the handler after an extension frame")
}
if d.gotGoAway.Load() {
t.Errorf("server sent GOAWAY %s for an unknown frame type, want it discarded",
ErrorCode(d.goaway.Load()))
}
}
// TestServerRejectsExtensionFrameInHeaderBlock covers the exception in RFC 7540
// 6.10: nothing may be interleaved between a HEADERS frame and the
// CONTINUATION frames that complete it, extension frames included.
func TestServerRejectsExtensionFrameInHeaderBlock(t *testing.T) {
addr, handled := newAttackServer(t, ServerConfig{PingInterval: -1})
a := dialAttacker(t, addr)
d := a.drain()
// HEADERS without END_HEADERS opens a header block.
if err := a.writeHeaders(1, false, false, requestFields(addr)); err != nil {
t.Fatalf("writing the request: %v", err)
}
a.flush()
if err := a.writeRaw(0x1f, 0, 0, []byte("extension payload")); err != nil {
t.Fatalf("writing the extension frame: %v", err)
}
if !d.wait(5 * time.Second) {
t.Error("server kept the connection open after an extension frame inside a header block")
}
if !d.gotGoAway.Load() {
t.Error("no GOAWAY for an extension frame inside a header block")
} else if code := ErrorCode(d.goaway.Load()); code != ProtocolError {
t.Errorf("goaway code = %s, want %s", code, ProtocolError)
}
if n := handled.Load(); n != 0 {
t.Errorf("handler ran %d times for a header block that was never completed", n)
}
}
// TestServerAcceptsTableSizeUpdateInTrailers covers RFC 7541 4.2: a dynamic
// table size update belongs at the start of the first header block after the
// size changed, which is not necessarily the first block on the stream. The
// server used to accept one only in a stream's opening block, so a peer whose
// SETTINGS arrived while its request headers were already in flight had its
// connection killed with COMPRESSION_ERROR when it put the update in the
// trailers. net/http's client does exactly that, intermittently.
func TestServerAcceptsTableSizeUpdateInTrailers(t *testing.T) {
addr, handled := newAttackServer(t, ServerConfig{PingInterval: -1})
a := dialAttacker(t, addr)
d := a.drain()
// Request headers, body, then a trailer block that opens with a dynamic
// table size update.
if err := a.writeHeaders(1, false, true, requestFields(addr)); err != nil {
t.Fatalf("writing the request: %v", err)
}
a.flush()
var block []byte
// 001xxxxx with a 5-bit integer: set the table size to 4096.
block = appendInt(append(block, 0x20), 5, 4096)
hf := AcquireHeaderField()
hf.Set("x-checksum", "abc123")
block = a.enc.AppendHeader(block, hf, false)
ReleaseHeaderField(hf)
if err := a.writeRaw(byte(FrameHeaders), byte(FlagEndStream|FlagEndHeaders), 1, block); err != nil {
t.Fatalf("writing the trailers: %v", err)
}
deadline := time.Now().Add(5 * time.Second)
for handled.Load() == 0 && time.Now().Before(deadline) {
time.Sleep(10 * time.Millisecond)
}
if handled.Load() == 0 {
t.Error("the request never reached the handler")
}
if d.gotGoAway.Load() {
t.Errorf("server sent GOAWAY %s for a table size update in the trailers",
ErrorCode(d.goaway.Load()))
}
}
// TestServerDoesNotCancelAnOpenRequest covers a request that is still being
// sent when the connection's request timer first fires. The timer was created
// already expired, and with no read timeout configured every open stream looked
// overdue, so a request whose body had not finished arriving was answered with
// RST_STREAM(CANCEL) and nothing in the logs to explain it.
//
// Any request with a body is exposed: the stream stays open between the headers
// and the last DATA frame, which is exactly the window the stale tick lands in.
func TestServerDoesNotCancelAnOpenRequest(t *testing.T) {
const conns = 20
addr, _ := newAttackServer(t, ServerConfig{PingInterval: -1})
for i := 0; i < conns; i++ {
func() {
a := dialAttacker(t, addr)
defer func() { _ = a.c.Close() }()
// Headers without END_STREAM: the stream is open and waiting for a
// body from here on.
if err := a.writeHeaders(1, false, true, requestFields(addr)); err != nil {
t.Fatalf("connection %d: request: %v", i, err)
}
a.flush()
// Long enough for the timer that was armed when the connection
// started to have fired.
time.Sleep(20 * time.Millisecond)
if err := a.writeData(1, true, []byte("body")); err != nil {
t.Fatalf("connection %d: body: %v", i, err)
}
a.flush()
_ = a.c.SetReadDeadline(time.Now().Add(10 * time.Second))
for {
fr, err := ReadFrameFrom(a.br)
if err != nil {
t.Fatalf("connection %d: reading the answer: %v", i, err)
}
kind, stream := fr.Type(), fr.Stream()
var code ErrorCode
if kind == FrameResetStream {
code = fr.Body().(*RstStream).Code()
}
ReleaseFrameHeader(fr)
if kind == FrameResetStream && stream == 1 {
t.Fatalf("connection %d: server reset a request that was still arriving, with %s", i, code)
}
if kind == FrameHeaders && stream == 1 {
return
}
}
}()
}
}
// TestServerWaitsForEndHeaders covers RFC 7540 6.2 and 6.10: a HEADERS frame
// that sets END_STREAM but not END_HEADERS half-closes the stream while its
// header block is still arriving in CONTINUATION frames. The request is not
// complete until END_HEADERS, and answering at END_STREAM hands the handler a
// request whose headers are only half decoded.
func TestServerWaitsForEndHeaders(t *testing.T) {
type seen struct {
late string
ok bool
}
got := make(chan seen, 4)
addr := newConcurrencyServer(t, ServerConfig{PingInterval: -1},
func(ctx *fasthttp.RequestCtx) {
got <- seen{late: string(ctx.Request.Header.Peek("x-late")), ok: true}
ctx.SetBodyString("ok")
})
a := dialAttacker(t, addr)
// END_STREAM here, END_HEADERS only on the CONTINUATION that follows.
if err := a.writeHeaders(1, true, false, requestFields(addr)); err != nil {
t.Fatal(err)
}
a.flush()
// Give the server every chance to answer early, which is the bug.
time.Sleep(100 * time.Millisecond)
select {
case s := <-got:
t.Fatalf("the handler ran before END_HEADERS, seeing x-late=%q", s.late)
default:
}
if err := a.writeContinuationFields(1, true, []hdr{{"x-late", "present"}}); err != nil {
t.Fatal(err)
}
a.flush()
select {
case s := <-got:
if s.late != "present" {
t.Errorf("handler saw x-late=%q, want %q: the header block was not "+
"fully decoded before the request was dispatched", s.late, "present")
}
case <-time.After(5 * time.Second):
t.Fatal("the handler never ran")
}
}
// writeSettingsWith sends a SETTINGS frame carrying st.
func (a *attacker) writeSettingsWith(st *Settings) error {
fr := AcquireFrameHeader()
frSt := AcquireFrame(FrameSettings).(*Settings)
st.CopyTo(frSt)
fr.SetBody(frSt)
if err := a.write(fr); err != nil {
return err
}
a.flush()
return nil
}
// TestServerRejectsFrameOverItsOwnMaxFrameSize covers RFC 7540 4.2: a frame
// larger than the SETTINGS_MAX_FRAME_SIZE the receiver advertised is an error.
// The limit is the receiver's own, not the sender's: a peer that says it can
// receive 1 MiB frames has said nothing about what it may send.
func TestServerRejectsFrameOverItsOwnMaxFrameSize(t *testing.T) {
addr, _ := newAttackServer(t, ServerConfig{PingInterval: -1})
a := dialAttacker(t, addr)
d := a.drain()
// We can receive 1 MiB frames. The server still only accepts 16 KiB ones.
st := &Settings{}
st.Reset()
st.SetMaxFrameSize(1 << 20)
if err := a.writeSettingsWith(st); err != nil {
t.Fatalf("writing the settings: %v", err)
}
// Give the server time to apply them, so the test fails on the size check
// rather than on a race with it.
time.Sleep(100 * time.Millisecond)
if err := a.writeRaw(byte(FrameData), 0, 1, make([]byte, 256*1024)); err != nil &&
!isClosedConn(err) {
t.Fatalf("writing the oversized frame: %v", err)
}
if !d.wait(5 * time.Second) {
t.Error("server kept the connection open after an oversized frame")
}
if !d.gotGoAway.Load() {
t.Fatal("no GOAWAY for a frame over the advertised maximum size")
}
if code := ErrorCode(d.goaway.Load()); code != FrameSizeError {
t.Errorf("goaway code = %s, want %s", code, FrameSizeError)
}
}
// TestServerRejectsOversizedFrameBeforeSettings covers the same limit before
// the peer has sent any SETTINGS. Until then the server has nothing to go on
// but its own advertised value, and a connection with no limit at all lets one
// frame allocate up to the 16 MiB the length field can express.
func TestServerRejectsOversizedFrameBeforeSettings(t *testing.T) {
addr, _ := newAttackServer(t, ServerConfig{PingInterval: -1})
c, err := tls.Dial("tcp", addr, &tls.Config{
InsecureSkipVerify: true,
NextProtos: []string{H2TLSProto},
})
if err != nil {
t.Fatalf("dial: %v", err)
}
t.Cleanup(func() { _ = c.Close() })
if _, err := c.Write(http2Preface); err != nil {
t.Fatalf("writing the preface: %v", err)
}
a := &attacker{t: t, c: c, br: bufio.NewReader(c), bw: bufio.NewWriter(c), addr: addr}
d := a.drain()
// No SETTINGS of our own, straight to a frame the server never said it
// would accept.
if err := a.writeRaw(byte(FrameData), 0, 1, make([]byte, 256*1024)); err != nil &&
!isClosedConn(err) {
t.Fatalf("writing the oversized frame: %v", err)
}
if !d.wait(5 * time.Second) {
t.Error("server kept the connection open after an oversized frame")
}
if !d.gotGoAway.Load() {
t.Fatal("no GOAWAY for a frame over the advertised maximum size")
}
if code := ErrorCode(d.goaway.Load()); code != FrameSizeError {
t.Errorf("goaway code = %s, want %s", code, FrameSizeError)
}
}