Check for short packets in PGPPacket.from_bytes
authorW. Trevor King <wking@tremily.us>
Thu, 19 Dec 2013 04:44:30 +0000 (20:44 -0800)
committerW. Trevor King <wking@tremily.us>
Thu, 19 Dec 2013 04:44:30 +0000 (20:44 -0800)
When the data is longer than expected, the packet may just be part of
a stream.

gpg-migrate.py

index 905f8b64aacb63bf7189069d1d23dabd3c6d4625..00e672c8a5fce7d0bc92743e3c281861508d330d 100755 (executable)
@@ -48,6 +48,9 @@ class PGPPacket (dict):
             offset += length_bytes
             self['length'] = _struct.unpack(length_format, length_data)[0]
         packet = data[offset:offset + self['length']]
+        if len(packet) < self['length']:
+            raise ValueError('packet too short ({} < {})'.format(
+                len(packet), self['length']))
         offset += self['length']
         return offset