Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metadata API: validate root role names #1630

Merged
merged 3 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions tests/repository_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
from typing import Dict, Iterator, List, Optional, Tuple
from urllib import parse

from tuf.api.metadata import TOP_LEVEL_ROLE_NAMES
from tuf.api.serialization.json import JSONSerializer
from tuf.exceptions import FetcherHTTPError
from tuf.api.metadata import (
Expand Down Expand Up @@ -152,10 +153,11 @@ def _initialize(self):
timestamp = Timestamp(1, SPEC_VER, self.safe_expiry, snapshot_meta)
self.md_timestamp = Metadata(timestamp, OrderedDict())

root = Root(1, SPEC_VER, self.safe_expiry, {}, {}, True)
for role in ["root", "timestamp", "snapshot", "targets"]:
roles = {role_name: Role([], 1) for role_name in TOP_LEVEL_ROLE_NAMES}
root = Root(1, SPEC_VER, self.safe_expiry, {}, roles, True)

for role in TOP_LEVEL_ROLE_NAMES:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that sets are unordered. If the original order, i.e. ["root", "timestamp", "snapshot", "targets"], needs to be guaranteed, you can't use TOP_LEVEL_ROLE_NAMES here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good note. I don't think the order matters here, am I right @jku?

Also, I decided to use a set, instead of a list for TOP_LEVEL_ROLE_NAMES because of this check:
if set(roles) != TOP_LEVEL_ROLE_NAMES: in tuf/api/metadata.py in Root.init() will result as false even if roles have the same content as TOP_LEVEL_ROLE_NAMES. The reason is that one is a list and the other is a set.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think using a set makes sense. If it turns out that there are many use cases that require the same particular order of the top-level role names, we can still add another global that is a list, or make this one an ordered set.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WFM

key, signer = self.create_key()
root.roles[role] = Role([], 1)
root.add_key(role, key)
# store the private key
if role not in self.signers:
Expand Down
65 changes: 61 additions & 4 deletions tests/test_metadata_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,23 +142,38 @@ def test_role_serialization(self, test_case_data: str):
"keyid1" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"}}, \
"keyid2" : {"keytype": "ed25519", "scheme": "ed25519", "keyval": {"public": "bar"}}}, \
"roles": { \
"root": {"keyids": ["keyid1"], "threshold": 1}, \
"timestamp": {"keyids": ["keyid2"], "threshold": 1}, \
"targets": {"keyids": ["keyid1"], "threshold": 1}, \
"snapshot": {"keyids": ["keyid2"], "threshold": 1}} \
}',
"no consistent_snapshot": '{ "_type": "root", "spec_version": "1.0.0", "version": 1, \
"expires": "2030-01-01T00:00:00Z", \
"keys": {"keyid" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"} }}, \
"roles": { "targets": {"keyids": ["keyid"], "threshold": 3} } \
"roles": { \
"root": {"keyids": ["keyid"], "threshold": 1}, \
"timestamp": {"keyids": ["keyid"], "threshold": 1}, \
"targets": {"keyids": ["keyid"], "threshold": 1}, \
"snapshot": {"keyids": ["keyid"], "threshold": 1}} \
}',
"empty keys and roles": '{"_type": "root", "spec_version": "1.0.0", "version": 1, \
"empty keys": '{"_type": "root", "spec_version": "1.0.0", "version": 1, \
"expires": "2030-01-01T00:00:00Z", "consistent_snapshot": false, \
"keys": {}, \
"roles": {} \
"roles": { \
"root": {"keyids": [], "threshold": 1}, \
"timestamp": {"keyids": [], "threshold": 1}, \
"targets": {"keyids": [], "threshold": 1}, \
"snapshot": {"keyids": [], "threshold": 1}} \
}',
"unrecognized field": '{"_type": "root", "spec_version": "1.0.0", "version": 1, \
"expires": "2030-01-01T00:00:00Z", "consistent_snapshot": false, \
"keys": {"keyid" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"}}}, \
"roles": { "targets": {"keyids": ["keyid"], "threshold": 3}}, \
"roles": { \
"root": {"keyids": ["keyid"], "threshold": 1}, \
"timestamp": {"keyids": ["keyid"], "threshold": 1}, \
"targets": {"keyids": ["keyid"], "threshold": 1}, \
"snapshot": {"keyids": ["keyid"], "threshold": 1} \
}, \
"foo": "bar"}',
}

Expand All @@ -169,6 +184,48 @@ def test_root_serialization(self, test_case_data: str):
self.assertDictEqual(case_dict, root.to_dict())


invalid_roots: utils.DataSet = {
"invalid role name": '{"_type": "root", "spec_version": "1.0.0", "version": 1, \
"expires": "2030-01-01T00:00:00Z", "consistent_snapshot": false, \
"keys": { \
"keyid1" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"}}, \
"keyid2" : {"keytype": "ed25519", "scheme": "ed25519", "keyval": {"public": "bar"}}}, \
"roles": { \
"bar": {"keyids": ["keyid1"], "threshold": 1}, \
"timestamp": {"keyids": ["keyid2"], "threshold": 1}, \
"targets": {"keyids": ["keyid1"], "threshold": 1}, \
"snapshot": {"keyids": ["keyid2"], "threshold": 1}} \
}',
"missing root role": '{"_type": "root", "spec_version": "1.0.0", "version": 1, \
"expires": "2030-01-01T00:00:00Z", "consistent_snapshot": false, \
"keys": { \
"keyid1" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"}}, \
"keyid2" : {"keytype": "ed25519", "scheme": "ed25519", "keyval": {"public": "bar"}}}, \
"roles": { \
"timestamp": {"keyids": ["keyid2"], "threshold": 1}, \
"targets": {"keyids": ["keyid1"], "threshold": 1}, \
"snapshot": {"keyids": ["keyid2"], "threshold": 1}} \
}',
"one additional role": '{"_type": "root", "spec_version": "1.0.0", "version": 1, \
"expires": "2030-01-01T00:00:00Z", "consistent_snapshot": false, \
"keys": { \
"keyid1" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"}}, \
"keyid2" : {"keytype": "ed25519", "scheme": "ed25519", "keyval": {"public": "bar"}}}, \
"roles": { \
"root": {"keyids": ["keyid1"], "threshold": 1}, \
"timestamp": {"keyids": ["keyid2"], "threshold": 1}, \
"targets": {"keyids": ["keyid1"], "threshold": 1}, \
"snapshot": {"keyids": ["keyid2"], "threshold": 1}, \
"foo": {"keyids": ["keyid2"], "threshold": 1}} \
}',
}

@utils.run_sub_tests_with_dataset(invalid_roots)
def test_invalid_root_serialization(self, test_case_data: Dict[str, str]):
case_dict = json.loads(test_case_data)
with self.assertRaises(ValueError):
Root.from_dict(copy.deepcopy(case_dict))

invalid_metafiles: utils.DataSet = {
"wrong length type": '{"version": 1, "length": "a", "hashes": {"sha256" : "abc"}}',
"length 0": '{"version": 1, "length": 0, "hashes": {"sha256" : "abc"}}',
Expand Down
4 changes: 4 additions & 0 deletions tuf/api/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
# We aim to support SPECIFICATION_VERSION and require the input metadata
# files to have the same major version (the first number) as ours.
SPECIFICATION_VERSION = ["1", "0", "19"]
TOP_LEVEL_ROLE_NAMES = {"root", "timestamp", "snapshot", "targets"}

# T is a Generic type constraint for Metadata.signed
T = TypeVar("T", "Root", "Timestamp", "Snapshot", "Targets")
Expand Down Expand Up @@ -728,6 +729,9 @@ def __init__(
super().__init__(version, spec_version, expires, unrecognized_fields)
self.consistent_snapshot = consistent_snapshot
self.keys = keys
if set(roles) != TOP_LEVEL_ROLE_NAMES:
raise ValueError("Role names must be the top-level metadata roles")

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be helpful to include the received, i.e. roles and expected, i.e. TOP_LEVEL_ROLE_NAMES value in the error message. I can't remember, do we have a general recommendation for error messages in that regard?

Copy link
Member

@jku jku Oct 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd avoid any complexity. We're preparing for one of two options:

  • deserialization: error message won't be useful
  • construction of new Root: this is code that we hopefully write once in a repository component and that's it

complex error message would have very limited value in both cases

self.roles = roles

@classmethod
Expand Down