Skip to content

Commit df9f3df

Browse files
author
Martin Vrachev
committed
Metadata API: preserve Role.keyids order
We made Role.keyids a set because the keyids are supposed to be unique and this still makes sense. However, the data should also preserve order (when deserialized and serialized) and currently, it does not. This is fairly serious since writing signed data potentially modifies the data (making the signature invalid). The simplest solution (as proposed by Teodora) is to sort the set during serialization and that would ensure the order of the items. Signed-off-by: Martin Vrachev <[email protected]>
1 parent 885fcac commit df9f3df

2 files changed

Lines changed: 2 additions & 1 deletion

File tree

tests/test_metadata_serialization.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ def test_invalid_role_serialization(self, test_case_data: Dict[str, str]):
141141

142142
valid_roles: DataSet = {
143143
"all": '{"keyids": ["keyid"], "threshold": 3}',
144+
"many keyids": '{"keyids": ["a", "b", "c", "d", "e"], "threshold": 1}',
144145
"unrecognized field": '{"keyids": ["keyid"], "threshold": 3, "foo": "bar"}',
145146
}
146147

tuf/api/metadata.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ def from_dict(cls, role_dict: Dict[str, Any]) -> "Role":
554554
def to_dict(self) -> Dict[str, Any]:
555555
"""Returns the dictionary representation of self."""
556556
return {
557-
"keyids": list(self.keyids),
557+
"keyids": sorted(self.keyids),
558558
"threshold": self.threshold,
559559
**self.unrecognized_fields,
560560
}

0 commit comments

Comments
 (0)