From 6589ce08b237434063739012c72d338e69e11c26 Mon Sep 17 00:00:00 2001 From: Martin Hoffmann Date: Mon, 20 Jan 2025 14:19:55 +0100 Subject: [PATCH] Reformatting. --- src/rrdp.rs | 260 ++++++++++++++++++++------------------- src/xml/decode.rs | 121 +++++++++--------- tests/rrdp-resilience.rs | 11 +- 3 files changed, 206 insertions(+), 186 deletions(-) diff --git a/src/rrdp.rs b/src/rrdp.rs index a440be2..6a42879 100644 --- a/src/rrdp.rs +++ b/src/rrdp.rs @@ -217,108 +217,111 @@ impl NotificationFile { delta_limit: Option, ) -> Result { let mut reader = Reader::new(reader); - let mut session_id = None; let mut serial = None; - let mut outer = reader.start_with_limit(|element| { - if element.name() != NOTIFICATION { - return Err(XmlError::Malformed) - } - - element.attributes(|name, value| { - match name { - b"version" => { - if value.ascii_into::()? != 1 { - return Err(XmlError::Malformed) - } - Ok(()) - } - b"session_id" => { - session_id = Some(value.ascii_into()?); - Ok(()) - } - b"serial" => { - serial = Some(value.ascii_into()?); - Ok(()) - } - _ => Err(XmlError::Malformed) + let mut outer = reader.start_with_limit( + |element| { + if element.name() != NOTIFICATION { + return Err(XmlError::Malformed) } - }) - }, 100_000_000)?; - - let mut snapshot = None; - let mut deltas = Ok(vec![]); - - while let Some(mut content) = outer.take_opt_element_with_limit(&mut reader, - |element| { - match element.name() { - SNAPSHOT => { - if snapshot.is_some() { - return Err(XmlError::Malformed) - } - let mut uri = None; - let mut hash = None; - element.attributes(|name, value| match name { - b"uri" => { - uri = Some(value.ascii_into()?); + element.attributes(|name, value| { + match name { + b"version" => { + if value.ascii_into::()? != 1 { + return Err(XmlError::Malformed) + } Ok(()) } - b"hash" => { - hash = Some(value.ascii_into()?); + b"session_id" => { + session_id = Some(value.ascii_into()?); Ok(()) } - _ => Err(XmlError::Malformed) - })?; - match (uri, hash) { - (Some(uri), Some(hash)) => { - snapshot = Some(UriAndHash::new(uri, hash)); - Ok(()) - } - _ => Err(XmlError::Malformed) - } - } - DELTA => { - let mut serial = None; - let mut uri = None; - let mut hash = None; - element.attributes(|name, value| match name { b"serial" => { serial = Some(value.ascii_into()?); Ok(()) } - b"uri" => { - uri = Some(value.ascii_into()?); - Ok(()) - } - b"hash" => { - hash = Some(value.ascii_into()?); - Ok(()) - } _ => Err(XmlError::Malformed) - })?; - let (serial, uri, hash) = match (serial, uri, hash) { - (Some(serial), Some(uri), Some(hash)) => { - (serial, uri, hash) + } + }) + }, + 100_000_000 + )?; + + let mut snapshot = None; + let mut deltas = Ok(vec![]); + while let Some(mut content) = outer.take_opt_element_with_limit( + &mut reader, + |element| { + match element.name() { + SNAPSHOT => { + if snapshot.is_some() { + return Err(XmlError::Malformed) } - _ => return Err(XmlError::Malformed) - }; - if let Some(limit) = delta_limit { - let len = deltas.as_ref().map(|deltas| { - deltas.len() - }).unwrap_or(0); - if len >= limit { - deltas = Err(DeltaListError::Oversized); + let mut uri = None; + let mut hash = None; + element.attributes(|name, value| match name { + b"uri" => { + uri = Some(value.ascii_into()?); + Ok(()) + } + b"hash" => { + hash = Some(value.ascii_into()?); + Ok(()) + } + _ => Err(XmlError::Malformed) + })?; + match (uri, hash) { + (Some(uri), Some(hash)) => { + snapshot = Some(UriAndHash::new(uri, hash)); + Ok(()) + } + _ => Err(XmlError::Malformed) } } - if let Ok(ref mut deltas) = deltas { - deltas.push(DeltaInfo::new(serial, uri, hash)) + DELTA => { + let mut serial = None; + let mut uri = None; + let mut hash = None; + element.attributes(|name, value| match name { + b"serial" => { + serial = Some(value.ascii_into()?); + Ok(()) + } + b"uri" => { + uri = Some(value.ascii_into()?); + Ok(()) + } + b"hash" => { + hash = Some(value.ascii_into()?); + Ok(()) + } + _ => Err(XmlError::Malformed) + })?; + let (serial, uri, hash) = match (serial, uri, hash) { + (Some(serial), Some(uri), Some(hash)) => { + (serial, uri, hash) + } + _ => return Err(XmlError::Malformed) + }; + if let Some(limit) = delta_limit { + let len = deltas.as_ref().map(|deltas| { + deltas.len() + }).unwrap_or(0); + if len >= limit { + deltas = Err(DeltaListError::Oversized); + } + } + if let Ok(ref mut deltas) = deltas { + deltas.push(DeltaInfo::new(serial, uri, hash)) + } + Ok(()) } - Ok(()) + _ => Err(XmlError::Malformed) } - _ => Err(XmlError::Malformed) - } - }, 100_000_000)? { + }, + 100_000_000 + )? { content.take_end(&mut reader)?; } @@ -776,33 +779,36 @@ pub trait ProcessSnapshot { let mut session_id = None; let mut serial = None; - let mut outer = reader.start_with_limit(|element| { - if element.name() != SNAPSHOT { - info!("Bad outer: not snapshot, but {:?}", element.name()); - return Err(XmlError::Malformed) - } - element.attributes(|name, value| match name { - b"version" => { - if value.ascii_into::()? != 1 { - info!("Bad version"); - return Err(XmlError::Malformed) - } - Ok(()) + let mut outer = reader.start_with_limit( + |element| { + if element.name() != SNAPSHOT { + info!("Bad outer: not snapshot, but {:?}", element.name()); + return Err(XmlError::Malformed) } - b"session_id" => { - session_id = Some(value.ascii_into()?); - Ok(()) - } - b"serial" => { - serial = Some(value.ascii_into()?); - Ok(()) - } - _ => { - info!("Bad attribute on snapshot."); - Err(XmlError::Malformed) - } - }) - }, 100_000_000).map_err(Into::into)?; + element.attributes(|name, value| match name { + b"version" => { + if value.ascii_into::()? != 1 { + info!("Bad version"); + return Err(XmlError::Malformed) + } + Ok(()) + } + b"session_id" => { + session_id = Some(value.ascii_into()?); + Ok(()) + } + b"serial" => { + serial = Some(value.ascii_into()?); + Ok(()) + } + _ => { + info!("Bad attribute on snapshot."); + Err(XmlError::Malformed) + } + }) + }, + 100_000_000 + ).map_err(Into::into)?; match (session_id, serial) { (Some(session_id), Some(serial)) => { @@ -816,22 +822,26 @@ pub trait ProcessSnapshot { loop { let mut uri = None; - let inner = outer.take_opt_element_with_limit(&mut reader, |element| { - if element.name() != PUBLISH { - info!("Bad inner: not publish"); - return Err(ProcessError::malformed()) - } - element.attributes(|name, value| match name { - b"uri" => { - uri = Some(value.ascii_into()?); - Ok(()) - } - _ => { - info!("Bad attribute on publish."); - Err(ProcessError::malformed()) + let inner = outer.take_opt_element_with_limit( + &mut reader, + |element| { + if element.name() != PUBLISH { + info!("Bad inner: not publish"); + return Err(ProcessError::malformed()) } - }) - },100_000_000)?; + element.attributes(|name, value| match name { + b"uri" => { + uri = Some(value.ascii_into()?); + Ok(()) + } + _ => { + info!("Bad attribute on publish."); + Err(ProcessError::malformed()) + } + }) + }, + 100_000_000 + )?; let mut inner = match inner { Some(inner) => inner, None => break diff --git a/src/xml/decode.rs b/src/xml/decode.rs index 4624348..4968fa4 100644 --- a/src/xml/decode.rs +++ b/src/xml/decode.rs @@ -8,64 +8,7 @@ use quick_xml::name::Namespace; use crate::util::base64; -/// A simple BufRead passthrough proxy that acts as a "trip computer" -/// -/// It keeps track of the amount of bytes read since it was last reset. -/// If a limit is set, it will return an IO error when attempting to read -/// past that limit. -struct BufReadCounter { - reader: R, - trip: u128, - limit: u128 -} - -impl BufReadCounter { - - /// Create a new trip computer (resetting counter) for a BufRead - /// Acts transparently to the implementation of a BufRead below - pub fn new(reader: R) -> Self { - BufReadCounter { - reader, - trip: 0, - limit: 0 - } - } - - /// Reset the amount of bytes read back to 0 - pub fn reset(&mut self) { - self.trip = 0; - } - - /// Set a limit or pass 0 to disable the limit to the maximum bytes to - /// read. This overrides the previous limit. - pub fn limit(&mut self, limit: u128) { - self.limit = limit; - } -} - -impl io::Read for BufReadCounter { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - self.reader.read(buf) - } -} - -impl io::BufRead for BufReadCounter { - fn fill_buf(&mut self) -> io::Result<&[u8]> { - if self.limit > 0 && self.trip > self.limit { - return Err( - io::Error::new(io::ErrorKind::Other, - format!("Trip is over limit ({:?}/{:?})", - &self.trip, &self.limit)) - ); - } - self.reader.fill_buf() - } - - fn consume(&mut self, amt: usize) { - self.trip += u128::try_from(amt).unwrap_or_default(); - self.reader.consume(amt) - } -} +//------------ Reader -------------------------------------------------------- /// An XML reader. /// @@ -568,6 +511,68 @@ impl Text<'_> { } +//------------ BufReadCounter ------------------------------------------------ + +/// A simple BufRead passthrough proxy that acts as a "trip computer" +/// +/// It keeps track of the amount of bytes read since it was last reset. +/// If a limit is set, it will return an IO error when attempting to read +/// past that limit. +struct BufReadCounter { + reader: R, + trip: u128, + limit: u128 +} + +impl BufReadCounter { + /// Create a new trip computer (resetting counter) for a BufRead. + /// + /// Acts transparently to the implementation of a BufRead below. + pub fn new(reader: R) -> Self { + BufReadCounter { + reader, + trip: 0, + limit: 0 + } + } + + /// Reset the amount of bytes read back to 0 + pub fn reset(&mut self) { + self.trip = 0; + } + + /// Set a limit or pass 0 to disable the limit to the maximum bytes to + /// read. This overrides the previous limit. + pub fn limit(&mut self, limit: u128) { + self.limit = limit; + } +} + +impl io::Read for BufReadCounter { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + self.reader.read(buf) + } +} + +impl io::BufRead for BufReadCounter { + fn fill_buf(&mut self) -> io::Result<&[u8]> { + if self.limit > 0 && self.trip > self.limit { + return Err( + io::Error::new(io::ErrorKind::Other, + format!("Trip is over limit ({:?}/{:?})", + &self.trip, &self.limit)) + ); + } + self.reader.fill_buf() + } + + fn consume(&mut self, amt: usize) { + self.trip += u128::try_from(amt).unwrap_or_default(); + self.reader.consume(amt) + } +} + + //------------ Error --------------------------------------------------------- #[derive(Debug)] diff --git a/tests/rrdp-resilience.rs b/tests/rrdp-resilience.rs index 2c2213d..c1c10ca 100644 --- a/tests/rrdp-resilience.rs +++ b/tests/rrdp-resilience.rs @@ -12,7 +12,10 @@ mod tests { use tokio::net::TcpListener; use rpki::rrdp::NotificationFile; - async fn serve(test_bytes: &'static[u8],_: Request) -> Result>, Infallible> { + async fn serve( + test_bytes: &'static[u8], + _: Request + ) -> Result>, Infallible> { let body = Full::new(Bytes::from_static(test_bytes)); Ok(Response::builder() @@ -65,7 +68,9 @@ mod tests { #[tokio::test] async fn test_snapshot_uri() { - let bytes = include_bytes!("../test-data/rrdp/bomb-snapshot-uri.xml.gz"); + let bytes = include_bytes!( + "../test-data/rrdp/bomb-snapshot-uri.xml.gz" + ); assert!(run(bytes).await.is_ok()); } @@ -74,4 +79,4 @@ mod tests { let bytes = include_bytes!("../test-data/rrdp/bomb-whitespace.xml.gz"); assert!(run(bytes).await.is_ok()); } -} \ No newline at end of file +}