Skip to content

Commit fa2268d

Browse files
author
Jussi Kukkonen
authored
Merge pull request #1449 from MVrachev/key-validation
Metadata API: Add Key attributes types validation
2 parents 25e5f30 + a9dc24a commit fa2268d

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

tests/test_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ def test_key_class(self):
382382
Key.from_dict("id", test_key_dict)
383383
# Test creating a Key instance with wrong keyval format.
384384
key_dict["keyval"] = {}
385-
with self.assertRaises(ValueError):
385+
with self.assertRaises(KeyError):
386386
Key.from_dict("id", key_dict)
387387

388388

tuf/api/metadata.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,8 @@ def bump_version(self) -> None:
394394

395395
class Key:
396396
"""A container class representing the public portion of a Key.
397+
Please note that "Key" instances are not semanticly validated during
398+
initialization. We consider this as responsibility of securesystemslib.
397399
398400
Attributes:
399401
keyid: An identifier string that must uniquely identify a key within
@@ -416,8 +418,9 @@ def __init__(
416418
keyval: Dict[str, str],
417419
unrecognized_fields: Optional[Mapping[str, Any]] = None,
418420
) -> None:
419-
if not keyval.get("public"):
420-
raise ValueError("keyval doesn't follow the specification format!")
421+
val = keyval["public"]
422+
if not all(isinstance(at, str) for at in [keyid, keytype, scheme, val]):
423+
raise ValueError("Unexpected Key attributes types!")
421424
self.keyid = keyid
422425
self.keytype = keytype
423426
self.scheme = scheme

0 commit comments

Comments
 (0)