Skip to content

Commit f0f30d9

Browse files
committed
Treat spec_version 1.0.0 as valid
In #377, @lukesteensen noticed that we created metadata with the wrong spec version. We used `1.0`, but the proper form is `1.0.0`. Unfortunately landing this fix will be non-trivial, since old versions of rust-tuf will error out if the spec version is not `1.0`. As a stopgap, this patch changes rust-tuf to allow either a spec version of `1.0` or `1.0.0` so that we can switch to the proper schem once all the old clients have upgraded, or we come up with another way to gracefully perform this migration.
1 parent 6229fc6 commit f0f30d9

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

tuf/src/interchange/cjson/shims.rs

+36-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ use crate::Result;
1010

1111
const SPEC_VERSION: &str = "1.0";
1212

13+
// Ensure the given spec version matches our spec version.
14+
//
15+
// We also need to handle the literal "1.0" here, despite that fact that it is not a valid version
16+
// according to the SemVer spec, because it is already baked into some of the old roots.
17+
fn valid_spec_version(other: &str) -> bool {
18+
matches!(other, "1.0" | "1.0.0")
19+
}
20+
1321
fn parse_datetime(ts: &str) -> Result<DateTime<Utc>> {
1422
Utc.datetime_from_str(ts, "%FT%TZ")
1523
.map_err(|e| Error::Encoding(format!("Can't parse DateTime: {:?}", e)))
@@ -70,7 +78,7 @@ impl RootMetadata {
7078
)));
7179
}
7280

73-
if self.spec_version != SPEC_VERSION {
81+
if !valid_spec_version(&self.spec_version) {
7482
return Err(Error::Encoding(format!(
7583
"Unknown spec version {}",
7684
self.spec_version
@@ -184,7 +192,7 @@ impl TimestampMetadata {
184192
)));
185193
}
186194

187-
if self.spec_version != SPEC_VERSION {
195+
if !valid_spec_version(&self.spec_version) {
188196
return Err(Error::Encoding(format!(
189197
"Unknown spec version {}",
190198
self.spec_version
@@ -233,7 +241,7 @@ impl SnapshotMetadata {
233241
)));
234242
}
235243

236-
if self.spec_version != SPEC_VERSION {
244+
if !valid_spec_version(&self.spec_version) {
237245
return Err(Error::Encoding(format!(
238246
"Unknown spec version {}",
239247
self.spec_version
@@ -299,7 +307,7 @@ impl TargetsMetadata {
299307
)));
300308
}
301309

302-
if self.spec_version != SPEC_VERSION {
310+
if !valid_spec_version(&self.spec_version) {
303311
return Err(Error::Encoding(format!(
304312
"Unknown spec version {}",
305313
self.spec_version
@@ -570,3 +578,27 @@ mod deserialize_reject_duplicates {
570578
})
571579
}
572580
}
581+
582+
#[cfg(test)]
583+
mod test {
584+
use super::*;
585+
586+
#[test]
587+
fn spec_version_validation() {
588+
let valid_spec_versions = ["1.0.0", "1.0"];
589+
590+
for version in valid_spec_versions {
591+
assert!(valid_spec_version(version), "{:?} should be valid", version);
592+
}
593+
594+
let invalid_spec_versions = ["1.0.1", "1.1.0", "2.0.0", "3.0"];
595+
596+
for version in invalid_spec_versions {
597+
assert!(
598+
!valid_spec_version(version),
599+
"{:?} should be invalid",
600+
version
601+
);
602+
}
603+
}
604+
}

0 commit comments

Comments
 (0)