Skip to content

Commit 41afb1e

Browse files
committed
Validate spec_version during initialization
According to point 2 in the semver specification: "A normal version number MUST take the form X.Y.Z where X, Y, and Z are non-negative integers...". See: https://semver.org/#spec-item-2 Also, even though version strings like "2.0.0-rc.2" or "1.0.0-beta" are valid strings in semantic versioning format, in TUF we never needed to add letters for our specification number. That's why I validate that: spec_version is a . separated string and when split it has a length of 3 and that each of the three elements is a number. The modules under the tuf/api folder in TUF are an alternative TUF implementation. That's why they should use their own constant for SPECIFICATION_VERSION in tuf/metadata/api. This time, I used a list for the SPECIFICATION_VERSION constant in order to retrieve major and minor versions easier. I use the SPECIFICATION_VERSION to check that the given spec_version is supported against the tuf code spec version. Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
1 parent 15eb0d9 commit 41afb1e

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

tuf/api/metadata.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
# and currently, we are above 1000 lines by a small margin.
3838
# pylint: disable=C0302
3939

40+
# We aim to support SPECIFICATION_VERSION and require the input metadata
41+
# files to have the same major version (the first number) as ours.
42+
SPECIFICATION_VERSION = ["1", "0", "19"]
43+
4044

4145
class Metadata:
4246
"""A container for signed TUF metadata.
@@ -290,6 +294,16 @@ def __init__(
290294
expires: datetime,
291295
unrecognized_fields: Optional[Mapping[str, Any]] = None,
292296
) -> None:
297+
spec_list = spec_version.split(".")
298+
if (
299+
len(spec_list) != 3
300+
or not all(el.isdigit() for el in spec_list)
301+
or spec_list[0] != SPECIFICATION_VERSION[0]
302+
):
303+
raise ValueError(
304+
f"Unsupported spec_version, got {spec_list}, "
305+
f"supported {'.'.join(SPECIFICATION_VERSION)}"
306+
)
293307
self.spec_version = spec_version
294308
self.expires = expires
295309

0 commit comments

Comments
 (0)