Skip to content

Commit 2159b88

Browse files
committed
Raise if none of paths and path_hash_prefixes is set
The specification does not state clearly what is the behaviour when none of delegation's "paths" and "path_hash_prefixes" is set. See #1497. Until this issue is clarified, copy current Updater which raises an error in such case. Signed-off-by: Teodora Sechkova <[email protected]>
1 parent 4e6e816 commit 2159b88

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

tests/test_metadata_serialization.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,9 @@ def test_snapshot_serialization(self, test_case_data: str):
242242
"no path attribute":
243243
'{"keyids": ["keyid"], "name": "a", "terminating": false, \
244244
"path_hash_prefixes": ["h1", "h2"], "threshold": 99}',
245-
"no hash or path prefix":
246-
'{"keyids": ["keyid"], "name": "a", "terminating": true, "threshold": 3}',
247245
"unrecognized field":
248-
'{"keyids": ["keyid"], "name": "a", "terminating": true, "threshold": 3, "foo": "bar"}',
246+
'{"keyids": ["keyid"], "name": "a", "paths": ["fn1", "fn2"], \
247+
"terminating": true, "threshold": 3, "foo": "bar"}',
249248
}
250249

251250
@run_sub_tests_with_dataset(valid_delegated_roles)
@@ -257,10 +256,10 @@ def test_delegated_role_serialization(self, test_case_data: str):
257256

258257
valid_delegations: DataSet = {
259258
"all": '{"keys": {"keyid" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"}}}, \
260-
"roles": [ {"keyids": ["keyid"], "name": "a", "terminating": true, "threshold": 3} ]}',
259+
"roles": [ {"keyids": ["keyid"], "name": "a", "paths": ["fn1", "fn2"], "terminating": true, "threshold": 3} ]}',
261260
"unrecognized field":
262261
'{"keys": {"keyid" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"}}}, \
263-
"roles": [ {"keyids": ["keyid"], "name": "a", "terminating": true, "threshold": 3} ], \
262+
"roles": [ {"keyids": ["keyid"], "name": "a", "paths": ["fn1", "fn2"], "terminating": true, "threshold": 3} ], \
264263
"foo": "bar"}',
265264
}
266265

@@ -305,13 +304,13 @@ def test_targetfile_serialization(self, test_case_data: str):
305304
"targets": { "file.txt": {"length": 12, "hashes": {"sha256" : "abc"} } }, \
306305
"delegations": {"keys": {"keyid" : {"keytype": "rsa", \
307306
"scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"} }}, \
308-
"roles": [ {"keyids": ["keyid"], "name": "a", "terminating": true, "threshold": 3} ]} \
307+
"roles": [ {"keyids": ["keyid"], "name": "a", "paths": ["fn1", "fn2"], "terminating": true, "threshold": 3} ]} \
309308
}',
310309
"empty targets": '{"_type": "targets", "spec_version": "1.0.0", "version": 1, "expires": "2030-01-01T00:00:00Z", \
311310
"targets": {}, \
312311
"delegations": {"keys": {"keyid" : {"keytype": "rsa", \
313312
"scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"} }}, \
314-
"roles": [ {"keyids": ["keyid"], "name": "a", "terminating": true, "threshold": 3} ]} \
313+
"roles": [ {"keyids": ["keyid"], "name": "a", "paths": ["fn1", "fn2"], "terminating": true, "threshold": 3} ]} \
315314
}',
316315
"no delegations": '{"_type": "targets", "spec_version": "1.0.0", "version": 1, "expires": "2030-01-01T00:00:00Z", \
317316
"targets": { "file.txt": {"length": 12, "hashes": {"sha256" : "abc"} } } \

tuf/api/metadata.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -962,12 +962,12 @@ def update(self, rolename: str, role_info: MetaFile) -> None:
962962
class DelegatedRole(Role):
963963
"""A container with information about a delegated role.
964964
965-
A delegation can happen in three ways:
966-
- paths is None and path_hash_prefixes is None: delegates all targets
965+
A delegation can happen in two ways:
967966
- paths is set: delegates targets matching any path pattern in paths
968967
- path_hash_prefixes is set: delegates targets whose target path hash
969968
starts with any of the prefixes in path_hash_prefixes
970-
paths and path_hash_prefixes are mutually exclusive: both cannot be set.
969+
paths and path_hash_prefixes are mutually exclusive: both cannot be set,
970+
at least one of them must be set.
971971
972972
Attributes:
973973
name: A string giving the name of the delegated role.
@@ -996,6 +996,13 @@ def __init__(
996996
"Only one of the attributes 'paths' and"
997997
"'path_hash_prefixes' can be set!"
998998
)
999+
1000+
if paths is None and path_hash_prefixes is None:
1001+
raise ValueError(
1002+
"At least one of the attributes 'paths' and"
1003+
"'path_hash_prefixes' must be set!"
1004+
)
1005+
9991006
self.paths = paths
10001007
self.path_hash_prefixes = path_hash_prefixes
10011008

0 commit comments

Comments
 (0)