2020-04-29 18:12:51 -07:00
|
|
|
use thiserror::Error;
|
|
|
|
|
|
|
|
#[derive(PartialEq, Debug, Error, Eq, Clone)]
|
2020-04-27 11:06:00 -07:00
|
|
|
pub enum SanitizeError {
|
2020-04-29 18:12:51 -07:00
|
|
|
#[error("index out of bounds")]
|
2020-04-27 11:06:00 -07:00
|
|
|
IndexOutOfBounds,
|
2020-04-29 18:12:51 -07:00
|
|
|
#[error("value out of bounds")]
|
|
|
|
ValueOutOfBounds,
|
|
|
|
#[error("invalid value")]
|
2020-04-27 20:22:30 -07:00
|
|
|
InvalidValue,
|
2020-04-27 11:06:00 -07:00
|
|
|
}
|
|
|
|
|
2020-04-29 18:12:51 -07:00
|
|
|
/// Trait for sanitizing values and members of over the wire messages.
|
|
|
|
/// Implementation should recursively decent through the data structure
|
|
|
|
/// and sanitize all struct members and enum clauses. Sanitize excludes
|
|
|
|
/// signature verification checks, those are handled by another pass.
|
|
|
|
/// Sanitize checks should include but are not limited too:
|
|
|
|
/// * All index values are in range
|
|
|
|
/// * All values are within their static max/min bounds
|
2020-04-27 11:06:00 -07:00
|
|
|
pub trait Sanitize {
|
|
|
|
fn sanitize(&self) -> Result<(), SanitizeError> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Sanitize> Sanitize for Vec<T> {
|
|
|
|
fn sanitize(&self) -> Result<(), SanitizeError> {
|
|
|
|
for x in self.iter() {
|
|
|
|
x.sanitize()?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|