aetherlight: TickMinutes owns its bound at decode, replay's clock clause goes
TickMinutes was serde(transparent): a stored zero-minute tick (or a span above MAX_ROUTE_COST, which submit cannot build) decoded, and replay's clock clause alone judged it. The type now decodes through TryFrom<u32> (1..=MAX_ROUTE_COST), so a command, a routine period or a stored tick outside the bound cannot be read. With that the clock clause in verify_history is unreachable (only TimeAdvanced writes the clock, and a tick's span is at least one minute), so it is deleted rather than defended; so is the period clause in verify_state_shape. Tests: bounds at decode, and an export whose tick span is rewritten to 0 or over the bound does not open. Addresses clock-clause-not-redundant. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
415ac9208b
commit
9c8ab5ec85
2 changed files with 79 additions and 50 deletions
|
|
@ -26,14 +26,33 @@ impl FictionalMinutes {
|
|||
}
|
||||
}
|
||||
|
||||
/// The span one `AdvanceTime` names. Constructor-checked `1..=MAX_ROUTE_COST`
|
||||
/// (one year of minutes), reusing pass 2's constant as a span bound rather than
|
||||
/// an edge bound: an unbounded span lets one command jump the clock past every
|
||||
/// due date in the world and makes the size of a commit a caller-chosen number.
|
||||
/// The span one `AdvanceTime` names, `1..=MAX_ROUTE_COST` (one year of
|
||||
/// minutes), reusing pass 2's constant as a span bound rather than an edge
|
||||
/// bound: an unbounded span lets one command jump the clock past every due date
|
||||
/// in the world and makes the size of a commit a caller-chosen number.
|
||||
///
|
||||
/// The type owns its bound at every door: `new` and decoding both go through
|
||||
/// it, so a command, a routine's period or a stored commit that names a span
|
||||
/// outside the bound cannot be read at all, and no reducer or replay clause
|
||||
/// re-checks it.
|
||||
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
#[serde(transparent)]
|
||||
#[serde(try_from = "u32", into = "u32")]
|
||||
pub struct TickMinutes(u32);
|
||||
|
||||
impl TryFrom<u32> for TickMinutes {
|
||||
type Error = &'static str;
|
||||
|
||||
fn try_from(minutes: u32) -> Result<Self, Self::Error> {
|
||||
Self::new(minutes).ok_or("a tick span is 1..=525600 minutes")
|
||||
}
|
||||
}
|
||||
|
||||
impl From<TickMinutes> for u32 {
|
||||
fn from(span: TickMinutes) -> u32 {
|
||||
span.0
|
||||
}
|
||||
}
|
||||
|
||||
impl TickMinutes {
|
||||
pub fn new(minutes: u32) -> Option<Self> {
|
||||
(1..=MAX_ROUTE_COST)
|
||||
|
|
|
|||
|
|
@ -170,7 +170,6 @@ pub(super) fn verify_history(commits: &[WorldCommit]) -> Result<WorldState, Jour
|
|||
"commit chain is not contiguous or verifiable".into(),
|
||||
));
|
||||
}
|
||||
let previous_now = replay.header.now;
|
||||
// Authority is re-decided here for free: `apply_committed_command`
|
||||
// re-runs `reduce` against the pre-commit state and requires effect
|
||||
// equality, then `apply_effect`, and both of those call
|
||||
|
|
@ -180,14 +179,10 @@ pub(super) fn verify_history(commits: &[WorldCommit]) -> Result<WorldState, Jour
|
|||
// third owner of one decision.
|
||||
replay.begin();
|
||||
apply_committed_command(&mut replay, commit)?;
|
||||
// Two chain checks one state cannot make: the clock never runs
|
||||
// backwards, and it advances only on a tick.
|
||||
let advanced = matches!(commit.effect, WorldEffect::TimeAdvanced { .. });
|
||||
if replay.header.now < previous_now || (replay.header.now > previous_now) != advanced {
|
||||
return Err(JournalError::Corrupt(
|
||||
"the world clock moved without a time-advanced commit, or ran backwards".into(),
|
||||
));
|
||||
}
|
||||
// The clock needs no chain check of its own: only `TimeAdvanced` writes
|
||||
// it, `apply_effect` re-derives `to` from a `TickMinutes` that cannot
|
||||
// be read outside `1..=MAX_ROUTE_COST`, so every tick moves it forward
|
||||
// and nothing else moves it.
|
||||
replay.revision = commit.resulting_revision;
|
||||
let accumulator = replay.folded().map_err(kernel_error)?;
|
||||
replay.state_digest = accumulator.state_digest(replay.revision);
|
||||
|
|
@ -407,9 +402,6 @@ pub(super) fn verify_state_shape(state: &WorldState) -> Result<(), JournalError>
|
|||
for (subject_id, held) in &state.commitments {
|
||||
let commitment_is_canonical = |commitment: &super::Commitment| {
|
||||
(commitment.kind == super::CommitmentKind::Routine) == commitment.period.is_some()
|
||||
&& commitment.period.is_none_or(|period| {
|
||||
(1..=super::patch::MAX_ROUTE_COST).contains(&period.minutes())
|
||||
})
|
||||
&& (commitment.kind == super::CommitmentKind::Routine
|
||||
|| commitment.checks.is_empty())
|
||||
&& match commitment.counterparty {
|
||||
|
|
@ -961,6 +953,38 @@ mod tests {
|
|||
));
|
||||
}
|
||||
|
||||
/// A stored tick whose span is zero or above the bound is not a world: the
|
||||
/// export does not decode. This is the door the clock needs no check behind:
|
||||
/// a zero-minute tick would be a commit that moves nothing, and an oversize
|
||||
/// one a commit no `submit` could have built.
|
||||
#[test]
|
||||
fn a_stored_tick_with_a_span_outside_the_bound_does_not_open() {
|
||||
let kernel = clock_history();
|
||||
let honest = export_of(&kernel.journal.commits);
|
||||
// MessagePack names the span `minutes` and encodes the honest ones
|
||||
// (1, 2, 30) as a single positive fixint byte, so each is rewritten in
|
||||
// place, in the command and again in the effect.
|
||||
let key = b"\xa7minutes";
|
||||
let at: Vec<usize> = (0..honest.len() - key.len())
|
||||
.filter(|&at| &honest[at..at + key.len()] == key)
|
||||
.collect();
|
||||
assert!(at.len() >= 2, "a tick carries its span in the command and its effect");
|
||||
for forged_span in [vec![0x00], [&[0xce][..], &(crate::patch::MAX_ROUTE_COST + 1).to_be_bytes()].concat()] {
|
||||
let mut bytes = honest.clone();
|
||||
for &start in at.iter().rev() {
|
||||
let value = start + key.len();
|
||||
assert!(honest[value] < 0x80, "an honest span is a fixint");
|
||||
bytes.splice(value..=value, forged_span.iter().copied());
|
||||
}
|
||||
let error = WorldJournal::open(&bytes).err().expect("a forged span opened");
|
||||
assert!(
|
||||
error.to_string().contains("could not decode the export")
|
||||
&& error.to_string().contains("tick span"),
|
||||
"{forged_span:?}: {error}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// The replay-side future check reads only the subjects the transaction
|
||||
/// touched, and a key a subject already held is not newly born.
|
||||
#[test]
|
||||
|
|
@ -2243,40 +2267,26 @@ mod custody_tests {
|
|||
let _ = subject;
|
||||
}
|
||||
|
||||
/// `TickMinutes` is `#[serde(transparent)]`, so a hand-written store row
|
||||
/// can carry a period of zero — a routine that re-arms on the same minute
|
||||
/// forever — without ever passing through `TickMinutes::new`. The shape
|
||||
/// check now enforces the same `1..=MAX_ROUTE_COST` span bound the
|
||||
/// constructor enforces, so the forged row is refused directly.
|
||||
/// `TickMinutes` owns its bound at decode, so a span outside
|
||||
/// `1..=MAX_ROUTE_COST` cannot be read in any position: a routine's period,
|
||||
/// a command, or a tick's own effect.
|
||||
#[test]
|
||||
fn soul_a_zero_period_is_refused_by_the_shape_check() {
|
||||
let (kernel, subject) = ticking_world();
|
||||
|
||||
// The forgery an in-crate caller cannot write: `TickMinutes::new`
|
||||
// refuses zero, and the field is private, so the only door is serde.
|
||||
assert!(crate::TickMinutes::new(0).is_none());
|
||||
let zero: crate::TickMinutes =
|
||||
rmp_serde::from_slice(&rmp_serde::to_vec(&0u32).unwrap()).expect("a transparent span");
|
||||
|
||||
let mut forged = kernel.state.clone();
|
||||
let key = *forged.commitments[&subject]
|
||||
.iter()
|
||||
.find(|(_, commitment)| commitment.period.is_some())
|
||||
.expect("the routine")
|
||||
.0;
|
||||
forged
|
||||
.commitments
|
||||
.get_mut(&subject)
|
||||
.unwrap()
|
||||
.get_mut(&key)
|
||||
.unwrap()
|
||||
.period = Some(zero);
|
||||
|
||||
// The shape check refuses the forged span bound directly.
|
||||
assert!(
|
||||
matches!(verify_state_shape(&forged), Err(JournalError::Corrupt(_))),
|
||||
"verify_state_shape admitted a period outside 1..=MAX_ROUTE_COST"
|
||||
);
|
||||
fn a_span_outside_the_bound_cannot_be_decoded() {
|
||||
for minutes in [0u32, crate::patch::MAX_ROUTE_COST + 1, u32::MAX] {
|
||||
let raw = rmp_serde::to_vec(&minutes).unwrap();
|
||||
assert!(
|
||||
rmp_serde::from_slice::<crate::TickMinutes>(&raw).is_err(),
|
||||
"{minutes} decoded as a span"
|
||||
);
|
||||
assert!(serde_json::from_value::<crate::TickMinutes>(minutes.into()).is_err());
|
||||
}
|
||||
for minutes in [1u32, crate::patch::MAX_ROUTE_COST] {
|
||||
let raw = rmp_serde::to_vec(&minutes).unwrap();
|
||||
assert_eq!(
|
||||
rmp_serde::from_slice::<crate::TickMinutes>(&raw).unwrap().minutes(),
|
||||
minutes
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue