Skip to content

Commit 641507a

Browse files
authored
Merge pull request #357 from tomato42/heartbeat-padding-collection
fixups to heartbeat support
2 parents d82944f + 59b549f commit 641507a

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

tlslite/messages.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2279,6 +2279,7 @@ def parse(self, p):
22792279
"""
22802280
self.message_type = p.get(1)
22812281
self.payload = p.getVarBytes(2)
2282+
self.padding = p.getFixBytes(p.getRemainingLength())
22822283
return self
22832284

22842285
def write(self):

tlslite/tlsrecordlayer.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -845,20 +845,24 @@ def _getMsg(self, expectedType, secondaryType=None, constructorType=None):
845845
"Received heartbeat_request to "
846846
"peer_not_allowed_to_send mode"):
847847
yield result
848-
else:
849-
heartbeat_response = heartbeat_message.\
850-
create_response()
848+
if len(heartbeat_message.padding) < 16:
849+
# per RFC, silently ignore if the message
850+
# is malformed
851+
continue
852+
heartbeat_response = heartbeat_message.\
853+
create_response()
851854
for result in self._sendMsg(
852855
heartbeat_response):
853856
yield result
854857
# If we received heartbeat response, then we
855858
# check, if its payload is same as payload of
856859
# request we sent
857860
elif heartbeat_message.message_type == \
858-
HeartbeatMessageType.heartbeat_response:
861+
HeartbeatMessageType.heartbeat_response \
862+
and self.heartbeat_response_callback:
859863
self.heartbeat_response_callback(
860864
heartbeat_message)
861-
except socket.error:
865+
except (socket.error, SyntaxError):
862866
pass
863867
continue
864868

unit_tests/test_tlslite_messages.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3615,13 +3615,14 @@ def test_parse(self):
36153615
b'\x01' + # request
36163616
b'\x00\x01' + # payload length
36173617
b'\x00' + # payload
3618-
b'\x00' # padding
3618+
b'\xff' # padding
36193619
))
36203620

36213621
heartbeat_msg = heartbeat_msg.parse(parser)
36223622

36233623
self.assertEqual(heartbeat_msg.message_type, 1)
36243624
self.assertEqual(heartbeat_msg.payload, b'\x00')
3625+
self.assertEqual(heartbeat_msg.padding, b'\xff')
36253626

36263627
def test_parse_with_missing_data(self):
36273628
heartbeat_msg = Heartbeat()

0 commit comments

Comments
 (0)