use std::io::Read;
use std::path::Path;
use alloc::string::String;
use alloc::vec::Vec;
const PROBE_BYTES: u64 = 1;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Limit {
name: &'static str,
bytes: u64,
}
impl Limit {
pub const fn new(name: &'static str, bytes: u64) -> Self {
Self { name, bytes }
}
pub const fn name(&self) -> &'static str {
self.name
}
pub const fn bytes(&self) -> u64 {
self.bytes
}
}
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum BoundedReadError {
#[error("{limit_name} exceeds the configured limit of {limit_bytes} bytes")]
#[non_exhaustive]
TooLarge {
limit_name: &'static str,
limit_bytes: u64,
},
#[error(transparent)]
Io(#[from] std::io::Error),
#[error("the document is not valid UTF-8")]
NotUtf8,
}
pub fn read_bounded<R: Read>(reader: R, limit: Limit) -> Result<Vec<u8>, BoundedReadError> {
let probe = limit.bytes.saturating_add(PROBE_BYTES);
let mut out = Vec::new();
reader.take(probe).read_to_end(&mut out)?;
let read = u64::try_from(out.len()).unwrap_or(u64::MAX);
if read > limit.bytes {
return Err(BoundedReadError::TooLarge {
limit_name: limit.name,
limit_bytes: limit.bytes,
});
}
Ok(out)
}
pub fn read_file_bounded(path: &Path, limit: Limit) -> Result<Vec<u8>, BoundedReadError> {
read_bounded(std::fs::File::open(path)?, limit)
}
pub fn read_file_bounded_string(path: &Path, limit: Limit) -> Result<String, BoundedReadError> {
let bytes = read_file_bounded(path, limit)?;
String::from_utf8(bytes).map_err(|_| BoundedReadError::NotUtf8)
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Cursor;
const LIMIT: Limit = Limit::new("fixture", 4);
#[test]
fn a_short_document_is_returned_unchanged() {
let mut input = Cursor::new(alloc::vec![1u8, 2, 3]);
assert_eq!(
read_bounded(&mut input, LIMIT).ok(),
Some(alloc::vec![1u8, 2, 3])
);
}
#[test]
fn exactly_at_the_limit_succeeds() {
let mut input = Cursor::new(alloc::vec![7u8; 4]);
assert_eq!(
read_bounded(&mut input, LIMIT).ok(),
Some(alloc::vec![7u8; 4])
);
}
#[test]
fn one_byte_over_the_limit_is_too_large() {
let mut input = Cursor::new(alloc::vec![7u8; 5]);
assert!(matches!(
read_bounded(&mut input, LIMIT),
Err(BoundedReadError::TooLarge {
limit_name: "fixture",
limit_bytes: 4,
})
));
}
struct Endless {
remaining: u64,
}
impl Read for Endless {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
let want = u64::try_from(buf.len()).unwrap_or(u64::MAX);
let count = usize::try_from(self.remaining.min(want)).unwrap_or(usize::MAX);
buf[..count].fill(0);
self.remaining = self.remaining.saturating_sub(count as u64);
Ok(count)
}
}
#[test]
fn a_lying_length_does_not_grow_the_buffer_past_the_limit() {
let mut input = Endless {
remaining: 4 * 1024 * 1024 * 1024,
};
assert!(matches!(
read_bounded(&mut input, LIMIT),
Err(BoundedReadError::TooLarge {
limit_name: "fixture",
limit_bytes: 4,
})
));
}
#[test]
fn invalid_utf8_is_reported() {
let scratch = test_fixtures::process::scratch_dir("broadcast-common-bounded-utf8");
let path = scratch.join("fixture");
std::fs::write(&path, [0xFFu8, 0xFE]).expect("write fixture");
let result = read_file_bounded_string(&path, LIMIT);
assert!(matches!(result, Err(BoundedReadError::NotUtf8)));
}
#[test]
fn the_file_variant_uses_the_same_cap() {
let scratch = test_fixtures::process::scratch_dir("broadcast-common-bounded");
let path = scratch.join("fixture");
std::fs::write(&path, [0u8; 8]).expect("write fixture");
let short = read_file_bounded(&path, Limit::new("file", 16));
let long = read_file_bounded(&path, Limit::new("file", 4));
assert_eq!(short.ok().map(|bytes| bytes.len()), Some(8));
assert!(matches!(
long,
Err(BoundedReadError::TooLarge {
limit_name: "file",
limit_bytes: 4,
})
));
}
#[test]
fn the_file_string_variant_reads_text() {
let scratch = test_fixtures::process::scratch_dir("broadcast-common-bounded-text");
let path = scratch.join("fixture");
std::fs::write(&path, "hello").expect("write fixture");
let text = read_file_bounded_string(&path, Limit::new("text", 16));
assert_eq!(text.ok().as_deref(), Some("hello"));
}
}