Debian Codegraph @

testing/web-platform/tests/tools/third_party/h2/test/test_header_indexing.py

raw
1 # -*- coding: utf-8 -*-
2 """
3 test_header_indexing.py
4 ~~~~~~~~~~~~~~~~~~~~~~~
5
6 This module contains tests that use HPACK header tuples that provide additional
7 metadata to the hpack module about how to encode the headers.
8 """
9 import pytest
10
11 from hpack import HeaderTuple, NeverIndexedHeaderTuple
12
13 import h2.config
14 import h2.connection
15
16
17 def assert_header_blocks_actually_equal(block_a, block_b):
18 """
19 Asserts that two header bocks are really, truly equal, down to the types
20 of their tuples. Doesn't return anything.
21 """
22 assert len(block_a) == len(block_b)
23
24 for a, b in zip(block_a, block_b):
25 assert a == b
26 assert a.__class__ is b.__class__
27
28
29 class TestHeaderIndexing(object):
30 """
31 Test that Hyper-h2 can correctly handle never indexed header fields using
32 the appropriate hpack data structures.
33 """
34 example_request_headers = [
35 HeaderTuple(u':authority', u'example.com'),
36 HeaderTuple(u':path', u'/'),
37 HeaderTuple(u':scheme', u'https'),
38 HeaderTuple(u':method', u'GET'),
39 ]
40 bytes_example_request_headers = [
41 HeaderTuple(b':authority', b'example.com'),
42 HeaderTuple(b':path', b'/'),
43 HeaderTuple(b':scheme', b'https'),
44 HeaderTuple(b':method', b'GET'),
45 ]
46
47 extended_request_headers = [
48 HeaderTuple(u':authority', u'example.com'),
49 HeaderTuple(u':path', u'/'),
50 HeaderTuple(u':scheme', u'https'),
51 HeaderTuple(u':method', u'GET'),
52 NeverIndexedHeaderTuple(u'authorization', u'realpassword'),
53 ]
54 bytes_extended_request_headers = [
55 HeaderTuple(b':authority', b'example.com'),
56 HeaderTuple(b':path', b'/'),
57 HeaderTuple(b':scheme', b'https'),
58 HeaderTuple(b':method', b'GET'),
59 NeverIndexedHeaderTuple(b'authorization', b'realpassword'),
60 ]
61
62 example_response_headers = [
63 HeaderTuple(u':status', u'200'),
64 HeaderTuple(u'server', u'fake-serv/0.1.0')
65 ]
66 bytes_example_response_headers = [
67 HeaderTuple(b':status', b'200'),
68 HeaderTuple(b'server', b'fake-serv/0.1.0')
69 ]
70
71 extended_response_headers = [
72 HeaderTuple(u':status', u'200'),
73 HeaderTuple(u'server', u'fake-serv/0.1.0'),
74 NeverIndexedHeaderTuple(u'secure', u'you-bet'),
75 ]
76 bytes_extended_response_headers = [
77 HeaderTuple(b':status', b'200'),
78 HeaderTuple(b'server', b'fake-serv/0.1.0'),
79 NeverIndexedHeaderTuple(b'secure', b'you-bet'),
80 ]
81
82 server_config = h2.config.H2Configuration(client_side=False)
83
84 @pytest.mark.parametrize(
85 'headers', (
86 example_request_headers,
87 bytes_example_request_headers,
88 extended_request_headers,
89 bytes_extended_request_headers,
90 )
91 )
92 def test_sending_header_tuples(self, headers, frame_factory):
93 """
94 Providing HeaderTuple and HeaderTuple subclasses preserves the metadata
95 about indexing.
96 """
97 c = h2.connection.H2Connection()
98 c.initiate_connection()
99
100 # Clear the data, then send headers.
101 c.clear_outbound_data_buffer()
102 c.send_headers(1, headers)
103
104 f = frame_factory.build_headers_frame(headers=headers)
105 assert c.data_to_send() == f.serialize()
106
107 @pytest.mark.parametrize(
108 'headers', (
109 example_request_headers,
110 bytes_example_request_headers,
111 extended_request_headers,
112 bytes_extended_request_headers,
113 )
114 )
115 def test_header_tuples_in_pushes(self, headers, frame_factory):
116 """
117 Providing HeaderTuple and HeaderTuple subclasses to push promises
118 preserves metadata about indexing.
119 """
120 c = h2.connection.H2Connection(config=self.server_config)
121 c.receive_data(frame_factory.preamble())
122
123 # We can use normal headers for the request.
124 f = frame_factory.build_headers_frame(
125 self.example_request_headers
126 )
127 c.receive_data(f.serialize())
128
129 frame_factory.refresh_encoder()
130 expected_frame = frame_factory.build_push_promise_frame(
131 stream_id=1,
132 promised_stream_id=2,
133 headers=headers,
134 flags=['END_HEADERS'],
135 )
136
137 c.clear_outbound_data_buffer()
138 c.push_stream(
139 stream_id=1,
140 promised_stream_id=2,
141 request_headers=headers
142 )
143
144 assert c.data_to_send() == expected_frame.serialize()
145
146 @pytest.mark.parametrize(
147 'headers,encoding', (
148 (example_request_headers, 'utf-8'),
149 (bytes_example_request_headers, None),
150 (extended_request_headers, 'utf-8'),
151 (bytes_extended_request_headers, None),
152 )
153 )
154 def test_header_tuples_are_decoded_request(self,
155 headers,
156 encoding,
157 frame_factory):
158 """
159 The indexing status of the header is preserved when emitting
160 RequestReceived events.
161 """
162 config = h2.config.H2Configuration(
163 client_side=False, header_encoding=encoding
164 )
165 c = h2.connection.H2Connection(config=config)
166 c.receive_data(frame_factory.preamble())
167
168 f = frame_factory.build_headers_frame(headers)
169 data = f.serialize()
170 events = c.receive_data(data)
171
172 assert len(events) == 1
173 event = events[0]
174
175 assert isinstance(event, h2.events.RequestReceived)
176 assert_header_blocks_actually_equal(headers, event.headers)
177
178 @pytest.mark.parametrize(
179 'headers,encoding', (
180 (example_response_headers, 'utf-8'),
181 (bytes_example_response_headers, None),
182 (extended_response_headers, 'utf-8'),
183 (bytes_extended_response_headers, None),
184 )
185 )
186 def test_header_tuples_are_decoded_response(self,
187 headers,
188 encoding,
189 frame_factory):
190 """
191 The indexing status of the header is preserved when emitting
192 ResponseReceived events.
193 """
194 config = h2.config.H2Configuration(
195 header_encoding=encoding
196 )
197 c = h2.connection.H2Connection(config=config)
198 c.initiate_connection()
199 c.send_headers(stream_id=1, headers=self.example_request_headers)
200
201 f = frame_factory.build_headers_frame(headers)
202 data = f.serialize()
203 events = c.receive_data(data)
204
205 assert len(events) == 1
206 event = events[0]
207
208 assert isinstance(event, h2.events.ResponseReceived)
209 assert_header_blocks_actually_equal(headers, event.headers)
210
211 @pytest.mark.parametrize(
212 'headers,encoding', (
213 (example_response_headers, 'utf-8'),
214 (bytes_example_response_headers, None),
215 (extended_response_headers, 'utf-8'),
216 (bytes_extended_response_headers, None),
217 )
218 )
219 def test_header_tuples_are_decoded_info_response(self,
220 headers,
221 encoding,
222 frame_factory):
223 """
224 The indexing status of the header is preserved when emitting
225 InformationalResponseReceived events.
226 """
227 # Manipulate the headers to send 100 Continue. We need to copy the list
228 # to avoid breaking the example headers.
229 headers = headers[:]
230 if encoding:
231 headers[0] = HeaderTuple(u':status', u'100')
232 else:
233 headers[0] = HeaderTuple(b':status', b'100')
234
235 config = h2.config.H2Configuration(
236 header_encoding=encoding
237 )
238 c = h2.connection.H2Connection(config=config)
239 c.initiate_connection()
240 c.send_headers(stream_id=1, headers=self.example_request_headers)
241
242 f = frame_factory.build_headers_frame(headers)
243 data = f.serialize()
244 events = c.receive_data(data)
245
246 assert len(events) == 1
247 event = events[0]
248
249 assert isinstance(event, h2.events.InformationalResponseReceived)
250 assert_header_blocks_actually_equal(headers, event.headers)
251
252 @pytest.mark.parametrize(
253 'headers,encoding', (
254 (example_response_headers, 'utf-8'),
255 (bytes_example_response_headers, None),
256 (extended_response_headers, 'utf-8'),
257 (bytes_extended_response_headers, None),
258 )
259 )
260 def test_header_tuples_are_decoded_trailers(self,
261 headers,
262 encoding,
263 frame_factory):
264 """
265 The indexing status of the header is preserved when emitting
266 TrailersReceived events.
267 """
268 # Manipulate the headers to remove the status, which shouldn't be in
269 # the trailers. We need to copy the list to avoid breaking the example
270 # headers.
271 headers = headers[1:]
272
273 config = h2.config.H2Configuration(
274 header_encoding=encoding
275 )
276 c = h2.connection.H2Connection(config=config)
277 c.initiate_connection()
278 c.send_headers(stream_id=1, headers=self.example_request_headers)
279 f = frame_factory.build_headers_frame(self.example_response_headers)
280 data = f.serialize()
281 c.receive_data(data)
282
283 f = frame_factory.build_headers_frame(headers, flags=['END_STREAM'])
284 data = f.serialize()
285 events = c.receive_data(data)
286
287 assert len(events) == 2
288 event = events[0]
289
290 assert isinstance(event, h2.events.TrailersReceived)
291 assert_header_blocks_actually_equal(headers, event.headers)
292
293 @pytest.mark.parametrize(
294 'headers,encoding', (
295 (example_request_headers, 'utf-8'),
296 (bytes_example_request_headers, None),
297 (extended_request_headers, 'utf-8'),
298 (bytes_extended_request_headers, None),
299 )
300 )
301 def test_header_tuples_are_decoded_push_promise(self,
302 headers,
303 encoding,
304 frame_factory):
305 """
306 The indexing status of the header is preserved when emitting
307 PushedStreamReceived events.
308 """
309 config = h2.config.H2Configuration(
310 header_encoding=encoding
311 )
312 c = h2.connection.H2Connection(config=config)
313 c.initiate_connection()
314 c.send_headers(stream_id=1, headers=self.example_request_headers)
315
316 f = frame_factory.build_push_promise_frame(
317 stream_id=1,
318 promised_stream_id=2,
319 headers=headers,
320 flags=['END_HEADERS'],
321 )
322 data = f.serialize()
323 events = c.receive_data(data)
324
325 assert len(events) == 1
326 event = events[0]
327
328 assert isinstance(event, h2.events.PushedStreamReceived)
329 assert_header_blocks_actually_equal(headers, event.headers)
330
331
332 class TestSecureHeaders(object):
333 """
334 Certain headers should always be transformed to their never-indexed form.
335 """
336 example_request_headers = [
337 (u':authority', u'example.com'),
338 (u':path', u'/'),
339 (u':scheme', u'https'),
340 (u':method', u'GET'),
341 ]
342 bytes_example_request_headers = [
343 (b':authority', b'example.com'),
344 (b':path', b'/'),
345 (b':scheme', b'https'),
346 (b':method', b'GET'),
347 ]
348 possible_auth_headers = [
349 (u'authorization', u'test'),
350 (u'Authorization', u'test'),
351 (u'authorization', u'really long test'),
352 HeaderTuple(u'authorization', u'test'),
353 HeaderTuple(u'Authorization', u'test'),
354 HeaderTuple(u'authorization', u'really long test'),
355 NeverIndexedHeaderTuple(u'authorization', u'test'),
356 NeverIndexedHeaderTuple(u'Authorization', u'test'),
357 NeverIndexedHeaderTuple(u'authorization', u'really long test'),
358 (b'authorization', b'test'),
359 (b'Authorization', b'test'),
360 (b'authorization', b'really long test'),
361 HeaderTuple(b'authorization', b'test'),
362 HeaderTuple(b'Authorization', b'test'),
363 HeaderTuple(b'authorization', b'really long test'),
364 NeverIndexedHeaderTuple(b'authorization', b'test'),
365 NeverIndexedHeaderTuple(b'Authorization', b'test'),
366 NeverIndexedHeaderTuple(b'authorization', b'really long test'),
367 (u'proxy-authorization', u'test'),
368 (u'Proxy-Authorization', u'test'),
369 (u'proxy-authorization', u'really long test'),
370 HeaderTuple(u'proxy-authorization', u'test'),
371 HeaderTuple(u'Proxy-Authorization', u'test'),
372 HeaderTuple(u'proxy-authorization', u'really long test'),
373 NeverIndexedHeaderTuple(u'proxy-authorization', u'test'),
374 NeverIndexedHeaderTuple(u'Proxy-Authorization', u'test'),
375 NeverIndexedHeaderTuple(u'proxy-authorization', u'really long test'),
376 (b'proxy-authorization', b'test'),
377 (b'Proxy-Authorization', b'test'),
378 (b'proxy-authorization', b'really long test'),
379 HeaderTuple(b'proxy-authorization', b'test'),
380 HeaderTuple(b'Proxy-Authorization', b'test'),
381 HeaderTuple(b'proxy-authorization', b'really long test'),
382 NeverIndexedHeaderTuple(b'proxy-authorization', b'test'),
383 NeverIndexedHeaderTuple(b'Proxy-Authorization', b'test'),
384 NeverIndexedHeaderTuple(b'proxy-authorization', b'really long test'),
385 ]
386 secured_cookie_headers = [
387 (u'cookie', u'short'),
388 (u'Cookie', u'short'),
389 (u'cookie', u'nineteen byte cooki'),
390 HeaderTuple(u'cookie', u'short'),
391 HeaderTuple(u'Cookie', u'short'),
392 HeaderTuple(u'cookie', u'nineteen byte cooki'),
393 NeverIndexedHeaderTuple(u'cookie', u'short'),
394 NeverIndexedHeaderTuple(u'Cookie', u'short'),
395 NeverIndexedHeaderTuple(u'cookie', u'nineteen byte cooki'),
396 NeverIndexedHeaderTuple(u'cookie', u'longer manually secured cookie'),
397 (b'cookie', b'short'),
398 (b'Cookie', b'short'),
399 (b'cookie', b'nineteen byte cooki'),
400 HeaderTuple(b'cookie', b'short'),
401 HeaderTuple(b'Cookie', b'short'),
402 HeaderTuple(b'cookie', b'nineteen byte cooki'),
403 NeverIndexedHeaderTuple(b'cookie', b'short'),
404 NeverIndexedHeaderTuple(b'Cookie', b'short'),
405 NeverIndexedHeaderTuple(b'cookie', b'nineteen byte cooki'),
406 NeverIndexedHeaderTuple(b'cookie', b'longer manually secured cookie'),
407 ]
408 unsecured_cookie_headers = [
409 (u'cookie', u'twenty byte cookie!!'),
410 (u'Cookie', u'twenty byte cookie!!'),
411 (u'cookie', u'substantially longer than 20 byte cookie'),
412 HeaderTuple(u'cookie', u'twenty byte cookie!!'),
413 HeaderTuple(u'cookie', u'twenty byte cookie!!'),
414 HeaderTuple(u'Cookie', u'twenty byte cookie!!'),
415 (b'cookie', b'twenty byte cookie!!'),
416 (b'Cookie', b'twenty byte cookie!!'),
417 (b'cookie', b'substantially longer than 20 byte cookie'),
418 HeaderTuple(b'cookie', b'twenty byte cookie!!'),
419 HeaderTuple(b'cookie', b'twenty byte cookie!!'),
420 HeaderTuple(b'Cookie', b'twenty byte cookie!!'),
421 ]
422
423 server_config = h2.config.H2Configuration(client_side=False)
424
425 @pytest.mark.parametrize(
426 'headers', (example_request_headers, bytes_example_request_headers)
427 )
428 @pytest.mark.parametrize('auth_header', possible_auth_headers)
429 def test_authorization_headers_never_indexed(self,
430 headers,
431 auth_header,
432 frame_factory):
433 """
434 Authorization and Proxy-Authorization headers are always forced to be
435 never-indexed, regardless of their form.
436 """
437 # Regardless of what we send, we expect it to be never indexed.
438 send_headers = headers + [auth_header]
439 expected_headers = headers + [
440 NeverIndexedHeaderTuple(auth_header[0].lower(), auth_header[1])
441 ]
442
443 c = h2.connection.H2Connection()
444 c.initiate_connection()
445
446 # Clear the data, then send headers.
447 c.clear_outbound_data_buffer()
448 c.send_headers(1, send_headers)
449
450 f = frame_factory.build_headers_frame(headers=expected_headers)
451 assert c.data_to_send() == f.serialize()
452
453 @pytest.mark.parametrize(
454 'headers', (example_request_headers, bytes_example_request_headers)
455 )
456 @pytest.mark.parametrize('auth_header', possible_auth_headers)
457 def test_authorization_headers_never_indexed_push(self,
458 headers,
459 auth_header,
460 frame_factory):
461 """
462 Authorization and Proxy-Authorization headers are always forced to be
463 never-indexed, regardless of their form, when pushed by a server.
464 """
465 # Regardless of what we send, we expect it to be never indexed.
466 send_headers = headers + [auth_header]
467 expected_headers = headers + [
468 NeverIndexedHeaderTuple(auth_header[0].lower(), auth_header[1])
469 ]
470
471 c = h2.connection.H2Connection(config=self.server_config)
472 c.receive_data(frame_factory.preamble())
473
474 # We can use normal headers for the request.
475 f = frame_factory.build_headers_frame(
476 self.example_request_headers
477 )
478 c.receive_data(f.serialize())
479
480 frame_factory.refresh_encoder()
481 expected_frame = frame_factory.build_push_promise_frame(
482 stream_id=1,
483 promised_stream_id=2,
484 headers=expected_headers,
485 flags=['END_HEADERS'],
486 )
487
488 c.clear_outbound_data_buffer()
489 c.push_stream(
490 stream_id=1,
491 promised_stream_id=2,
492 request_headers=send_headers
493 )
494
495 assert c.data_to_send() == expected_frame.serialize()
496
497 @pytest.mark.parametrize(
498 'headers', (example_request_headers, bytes_example_request_headers)
499 )
500 @pytest.mark.parametrize('cookie_header', secured_cookie_headers)
501 def test_short_cookie_headers_never_indexed(self,
502 headers,
503 cookie_header,
504 frame_factory):
505 """
506 Short cookie headers, and cookies provided as NeverIndexedHeaderTuple,
507 are never indexed.
508 """
509 # Regardless of what we send, we expect it to be never indexed.
510 send_headers = headers + [cookie_header]
511 expected_headers = headers + [
512 NeverIndexedHeaderTuple(cookie_header[0].lower(), cookie_header[1])
513 ]
514
515 c = h2.connection.H2Connection()
516 c.initiate_connection()
517
518 # Clear the data, then send headers.
519 c.clear_outbound_data_buffer()
520 c.send_headers(1, send_headers)
521
522 f = frame_factory.build_headers_frame(headers=expected_headers)
523 assert c.data_to_send() == f.serialize()
524
525 @pytest.mark.parametrize(
526 'headers', (example_request_headers, bytes_example_request_headers)
527 )
528 @pytest.mark.parametrize('cookie_header', secured_cookie_headers)
529 def test_short_cookie_headers_never_indexed_push(self,
530 headers,
531 cookie_header,
532 frame_factory):
533 """
534 Short cookie headers, and cookies provided as NeverIndexedHeaderTuple,
535 are never indexed when pushed by servers.
536 """
537 # Regardless of what we send, we expect it to be never indexed.
538 send_headers = headers + [cookie_header]
539 expected_headers = headers + [
540 NeverIndexedHeaderTuple(cookie_header[0].lower(), cookie_header[1])
541 ]
542
543 c = h2.connection.H2Connection(config=self.server_config)
544 c.receive_data(frame_factory.preamble())
545
546 # We can use normal headers for the request.
547 f = frame_factory.build_headers_frame(
548 self.example_request_headers
549 )
550 c.receive_data(f.serialize())
551
552 frame_factory.refresh_encoder()
553 expected_frame = frame_factory.build_push_promise_frame(
554 stream_id=1,
555 promised_stream_id=2,
556 headers=expected_headers,
557 flags=['END_HEADERS'],
558 )
559
560 c.clear_outbound_data_buffer()
561 c.push_stream(
562 stream_id=1,
563 promised_stream_id=2,
564 request_headers=send_headers
565 )
566
567 assert c.data_to_send() == expected_frame.serialize()
568
569 @pytest.mark.parametrize(
570 'headers', (example_request_headers, bytes_example_request_headers)
571 )
572 @pytest.mark.parametrize('cookie_header', unsecured_cookie_headers)
573 def test_long_cookie_headers_can_be_indexed(self,
574 headers,
575 cookie_header,
576 frame_factory):
577 """
578 Longer cookie headers can be indexed.
579 """
580 # Regardless of what we send, we expect it to be indexed.
581 send_headers = headers + [cookie_header]
582 expected_headers = headers + [
583 HeaderTuple(cookie_header[0].lower(), cookie_header[1])
584 ]
585
586 c = h2.connection.H2Connection()
587 c.initiate_connection()
588
589 # Clear the data, then send headers.
590 c.clear_outbound_data_buffer()
591 c.send_headers(1, send_headers)
592
593 f = frame_factory.build_headers_frame(headers=expected_headers)
594 assert c.data_to_send() == f.serialize()
595
596 @pytest.mark.parametrize(
597 'headers', (example_request_headers, bytes_example_request_headers)
598 )
599 @pytest.mark.parametrize('cookie_header', unsecured_cookie_headers)
600 def test_long_cookie_headers_can_be_indexed_push(self,
601 headers,
602 cookie_header,
603 frame_factory):
604 """
605 Longer cookie headers can be indexed.
606 """
607 # Regardless of what we send, we expect it to be never indexed.
608 send_headers = headers + [cookie_header]
609 expected_headers = headers + [
610 HeaderTuple(cookie_header[0].lower(), cookie_header[1])
611 ]
612
613 c = h2.connection.H2Connection(config=self.server_config)
614 c.receive_data(frame_factory.preamble())
615
616 # We can use normal headers for the request.
617 f = frame_factory.build_headers_frame(
618 self.example_request_headers
619 )
620 c.receive_data(f.serialize())
621
622 frame_factory.refresh_encoder()
623 expected_frame = frame_factory.build_push_promise_frame(
624 stream_id=1,
625 promised_stream_id=2,
626 headers=expected_headers,
627 flags=['END_HEADERS'],
628 )
629
630 c.clear_outbound_data_buffer()
631 c.push_stream(
632 stream_id=1,
633 promised_stream_id=2,
634 request_headers=send_headers
635 )
636
637 assert c.data_to_send() == expected_frame.serialize()