Enforce that the periods in a consensus lifetime are nonempty.

To be valid, a lifetime must have valid_after < fresh_until <
valid_until.
This commit is contained in:
Nick Mathewson 2020-12-04 15:00:06 -05:00
parent 402d501020
commit 6ddf55f799
3 changed files with 17 additions and 7 deletions

View File

@ -764,7 +764,8 @@ mod test {
now.into(),
(now + one_hour).into(),
(now + one_hour * 2).into(),
),
)
.unwrap(),
[0xAB; 32],
[0xBC; 32],
);

View File

@ -87,11 +87,15 @@ impl Lifetime {
valid_after: time::SystemTime,
fresh_until: time::SystemTime,
valid_until: time::SystemTime,
) -> Self {
Lifetime {
valid_after,
fresh_until,
valid_until,
) -> Result<Self> {
if valid_after < fresh_until && fresh_until < valid_until {
Ok(Lifetime {
valid_after,
fresh_until,
valid_until,
})
} else {
Err(Error::InvalidLifetime)
}
}
/// Return time when this consensus first becomes valid.
@ -792,7 +796,7 @@ impl CommonHeader {
.args_as_str()
.parse::<ISO8601TimeSp>()?
.into();
let lifetime = Lifetime::new(valid_after, fresh_until, valid_until);
let lifetime = Lifetime::new(valid_after, fresh_until, valid_until)?;
let client_versions = sec
.maybe(CLIENT_VERSIONS)

View File

@ -265,6 +265,9 @@ pub enum Error {
/// Items not sorted as expected
#[error("Incorrect sort order{0}")]
WrongSortOrder(Pos),
/// A consensus lifetime was ill-formed.
#[error("Invalid consensus lifetime")]
InvalidLifetime,
}
impl Error {
@ -302,6 +305,7 @@ impl Error {
WrongStartingToken(_, p) => Some(p),
WrongEndingToken(_, p) => Some(p),
WrongSortOrder(p) => Some(p),
InvalidLifetime => None,
}
}
@ -341,6 +345,7 @@ impl Error {
WrongStartingToken(_, p) => Some(p),
WrongEndingToken(_, p) => Some(p),
WrongSortOrder(p) => Some(p),
InvalidLifetime => None,
};
*pos.unwrap_or(&Pos::Unknown)
}