Skip to content

Commit

Permalink
Do not void valid metadata mixed with deprecated metadata (#8164)
Browse files Browse the repository at this point in the history
  • Loading branch information
qstokkink authored Sep 23, 2024
2 parents 2515d9c + f0fec82 commit 0aef503
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 7 deletions.
91 changes: 85 additions & 6 deletions src/tribler/core/database/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,17 @@
NULL_KEY = b'\x00' * 64

# Metadata types. Should have been an enum, but in Python its unwieldy.
TYPELESS = 100
CHANNEL_NODE = 200
METADATA_NODE = 210
COLLECTION_NODE = 220
JSON_NODE = 230
CHANNEL_DESCRIPTION = 231
BINARY_NODE = 240
CHANNEL_THUMBNAIL = 241
REGULAR_TORRENT = 300
CHANNEL_TORRENT = 400
DELETED = 500
SNIPPET = 600


Expand Down Expand Up @@ -67,13 +75,14 @@ def read_payload_with_offset(data: bytes, offset: int = 0) -> tuple[TorrentMetad
"""
# First we have to determine the actual payload type
metadata_type = struct.unpack_from('>H', data, offset=offset)[0]
payload_class = METADATA_TYPE_TO_PAYLOAD_CLASS.get(metadata_type)
if payload_class is not None:
payload, offset = default_serializer.unpack_serializable(payload_class, data, offset=offset)
payload.signature = data[offset: offset + 64]
return payload, offset + 64

if metadata_type != REGULAR_TORRENT:
raise UnknownBlobTypeException(metadata_type)

payload, offset = default_serializer.unpack_serializable(TorrentMetadataPayload, data, offset=offset)
payload.signature = data[offset: offset + 64]
return payload, offset + 64
# Unknown metadata type, raise exception
raise UnknownBlobTypeException


@vp_compile
Expand Down Expand Up @@ -162,6 +171,46 @@ class ChannelNodePayload(SignedPayload):
timestamp: int


@vp_compile
class MetadataNodePayload(ChannelNodePayload):
"""
Deprecated, do not use.
"""

names = [*ChannelNodePayload.names, "title", "tags"]
format_list = [*ChannelNodePayload.format_list, "varlenIutf8", "varlenIutf8"]


@vp_compile
class JsonNodePayload(ChannelNodePayload):
"""
Deprecated, do not use.
"""

names = [*ChannelNodePayload.names, "json_text"]
format_list = [*ChannelNodePayload.format_list, "varlenIutf8"]


@vp_compile
class BinaryNodePayload(ChannelNodePayload):
"""
Deprecated, do not use.
"""

names = [*ChannelNodePayload.names, "binary_data", "data_type"]
format_list = [*ChannelNodePayload.format_list, "varlenI", "varlenIutf8"]


@vp_compile
class CollectionNodePayload(MetadataNodePayload):
"""
Deprecated, do not use.
"""

names = [*MetadataNodePayload.names, "num_entries"]
format_list = [*MetadataNodePayload.format_list, "Q"]


@vp_compile
class TorrentMetadataPayload(ChannelNodePayload):
"""
Expand Down Expand Up @@ -201,6 +250,36 @@ def get_magnet(self) -> str:
+ (f"&tr={self.tracker_info}" if self.tracker_info else ""))


@vp_compile
class ChannelMetadataPayload(TorrentMetadataPayload):
"""
Deprecated, do not use.
"""

names = [*TorrentMetadataPayload.names, "num_entries", "start_timestamp"]
format_list = [*TorrentMetadataPayload.format_list, "Q", "Q"]


@vp_compile
class DeletedMetadataPayload(SignedPayload):
"""
Deprecated, do not use.
"""

names = [*SignedPayload.names, "delete_signature"]
format_list = [*SignedPayload.format_list, "64s"]


METADATA_TYPE_TO_PAYLOAD_CLASS = {
REGULAR_TORRENT: TorrentMetadataPayload,
CHANNEL_TORRENT: ChannelMetadataPayload,
COLLECTION_NODE: CollectionNodePayload,
CHANNEL_THUMBNAIL: BinaryNodePayload,
CHANNEL_DESCRIPTION: JsonNodePayload,
DELETED: DeletedMetadataPayload,
}


@vp_compile
class HealthItemsPayload(VariablePayload):
"""
Expand Down
3 changes: 2 additions & 1 deletion src/tribler/core/database/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,8 @@ def process_squashed_mdblob(self, chunk_data: bytes, external_thread: bool = Fal
payload_list = []
while offset < len(chunk_data):
payload, offset = read_payload_with_offset(chunk_data, offset)
if payload:
if payload and isinstance(payload, TorrentMetadataPayload):
# Silently ignore deprecated payloads
payload_list.append(payload)

if health_info and len(health_info) == len(payload_list):
Expand Down

0 comments on commit 0aef503

Please sign in to comment.