feat: enforce input safety limits
This commit is contained in:
parent
648a4d50cb
commit
a9bfca053b
|
|
@ -26,6 +26,10 @@ Reusable consumers should call the library facade: `load_config`,
|
|||
pure and shares the exact resolved configuration used by analysis.
|
||||
Input iteration yields a separate result per document, so a bad directory entry
|
||||
does not discard successfully read siblings.
|
||||
Reads enforce the resource limits in
|
||||
[ADR 0002](docs/decisions/0002-input-policy-and-limits.md), including bounded
|
||||
file/member sizes, archive expansion and compression ratios, entry counts,
|
||||
directory depth, safe member paths, strict UTF-8, and NUL-byte rejection.
|
||||
|
||||
The implementation is being restructured into a reusable library plus detector,
|
||||
formatter, and LSP binaries. See [tasks.md](tasks.md) for the ordered backlog and
|
||||
|
|
|
|||
|
|
@ -16,6 +16,38 @@ pub struct ReplacementRule {
|
|||
pub struct Config {
|
||||
/// Enabled replacement rules in deterministic matching order.
|
||||
pub replacements: Vec<ReplacementRule>,
|
||||
/// Resource and traversal limits applied while reading inputs.
|
||||
pub input: InputLimits,
|
||||
}
|
||||
|
||||
/// Bounded resource policy for filesystem and archive inputs.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct InputLimits {
|
||||
/// Maximum bytes accepted for a regular file or one archive member.
|
||||
pub max_document_bytes: u64,
|
||||
/// Maximum bytes accepted for an outer archive.
|
||||
pub max_archive_bytes: u64,
|
||||
/// Maximum total declared expanded bytes across archive members.
|
||||
pub max_expanded_bytes: u64,
|
||||
/// Maximum number of members in one archive.
|
||||
pub max_archive_entries: usize,
|
||||
/// Maximum allowed expanded-to-compressed ratio.
|
||||
pub max_compression_ratio: u64,
|
||||
/// Maximum directory traversal depth, including the root at depth zero.
|
||||
pub max_walk_depth: usize,
|
||||
}
|
||||
|
||||
impl Default for InputLimits {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
max_document_bytes: 8 * 1024 * 1024,
|
||||
max_archive_bytes: 64 * 1024 * 1024,
|
||||
max_expanded_bytes: 256 * 1024 * 1024,
|
||||
max_archive_entries: 10_000,
|
||||
max_compression_ratio: 100,
|
||||
max_walk_depth: 64,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Config {
|
||||
|
|
@ -45,6 +77,7 @@ impl Config {
|
|||
replacement: "-",
|
||||
})
|
||||
.collect(),
|
||||
input: InputLimits::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,15 @@
|
|||
//! Streaming ZIP-based archive input.
|
||||
|
||||
use std::{fs::File, io::Read, path::PathBuf};
|
||||
use std::{
|
||||
fs::{self, File},
|
||||
io::Read,
|
||||
path::{Component, Path, PathBuf},
|
||||
};
|
||||
|
||||
use miette::{IntoDiagnostic, Result, WrapErr};
|
||||
use miette::{IntoDiagnostic, Result, WrapErr, miette};
|
||||
use zip::ZipArchive;
|
||||
|
||||
use crate::{SourceId, input::Document};
|
||||
use crate::{SourceId, config::InputLimits, input::Document};
|
||||
|
||||
/// Iterator over text members in one ZIP archive.
|
||||
pub(super) struct ArchiveDocuments {
|
||||
|
|
@ -15,6 +19,10 @@ pub(super) struct ArchiveDocuments {
|
|||
archive: ZipArchive<File>,
|
||||
/// Index of the next member to inspect.
|
||||
index: usize,
|
||||
/// Cumulative declared expanded bytes accepted so far.
|
||||
expanded_bytes: u64,
|
||||
/// Resource policy copied from resolved configuration.
|
||||
limits: InputLimits,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for ArchiveDocuments {
|
||||
|
|
@ -30,18 +38,40 @@ impl std::fmt::Debug for ArchiveDocuments {
|
|||
|
||||
impl ArchiveDocuments {
|
||||
/// Open an archive for streaming member reads.
|
||||
pub(super) fn open(outer: PathBuf) -> Result<Self> {
|
||||
pub(super) fn open(outer: PathBuf, limits: &InputLimits) -> Result<Self> {
|
||||
let archive_size = fs::metadata(&outer)
|
||||
.into_diagnostic()
|
||||
.wrap_err_with(|| format!("failed to inspect ZIP `{}`", outer.display()))?
|
||||
.len();
|
||||
if archive_size > limits.max_archive_bytes {
|
||||
return Err(miette!(
|
||||
"archive `{}` is {archive_size} bytes, exceeding the {} byte limit",
|
||||
outer.display(),
|
||||
limits.max_archive_bytes
|
||||
));
|
||||
}
|
||||
|
||||
let file = File::open(&outer)
|
||||
.into_diagnostic()
|
||||
.wrap_err_with(|| format!("failed to open ZIP `{}`", outer.display()))?;
|
||||
let archive = ZipArchive::new(file)
|
||||
.into_diagnostic()
|
||||
.wrap_err_with(|| format!("failed to parse ZIP `{}`", outer.display()))?;
|
||||
if archive.len() > limits.max_archive_entries {
|
||||
return Err(miette!(
|
||||
"archive `{}` has {} entries, exceeding the {} entry limit",
|
||||
outer.display(),
|
||||
archive.len(),
|
||||
limits.max_archive_entries
|
||||
));
|
||||
}
|
||||
|
||||
Ok(Self {
|
||||
outer,
|
||||
archive,
|
||||
index: 0,
|
||||
expanded_bytes: 0,
|
||||
limits: limits.clone(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -71,14 +101,57 @@ impl Iterator for ArchiveDocuments {
|
|||
continue;
|
||||
}
|
||||
|
||||
let name = PathBuf::from(member.name());
|
||||
let mut content = String::new();
|
||||
let name = match safe_member_path(member.name()) {
|
||||
Ok(name) => name,
|
||||
Err(error) => return Some(Err(error)),
|
||||
};
|
||||
if member.encrypted() {
|
||||
return Some(Err(miette!(
|
||||
"archive member `{}` in `{}` is encrypted",
|
||||
name.display(),
|
||||
self.outer.display()
|
||||
)));
|
||||
}
|
||||
|
||||
let expanded_size = member.size();
|
||||
if expanded_size > self.limits.max_document_bytes {
|
||||
return Some(Err(miette!(
|
||||
"archive member `{}` is {expanded_size} bytes, exceeding the {} byte document limit",
|
||||
name.display(),
|
||||
self.limits.max_document_bytes
|
||||
)));
|
||||
}
|
||||
let compressed_size = member.compressed_size();
|
||||
let allowed_expanded =
|
||||
compressed_size.saturating_mul(self.limits.max_compression_ratio);
|
||||
if expanded_size > 0 && expanded_size > allowed_expanded {
|
||||
return Some(Err(miette!(
|
||||
"archive member `{}` exceeds the {}:1 compression ratio limit",
|
||||
name.display(),
|
||||
self.limits.max_compression_ratio
|
||||
)));
|
||||
}
|
||||
self.expanded_bytes = match self.expanded_bytes.checked_add(expanded_size) {
|
||||
Some(total) if total <= self.limits.max_expanded_bytes => total,
|
||||
_ => {
|
||||
return Some(Err(miette!(
|
||||
"archive `{}` exceeds the {} byte total expansion limit",
|
||||
self.outer.display(),
|
||||
self.limits.max_expanded_bytes
|
||||
)));
|
||||
}
|
||||
};
|
||||
|
||||
let read_limit = self.limits.max_document_bytes.saturating_add(1);
|
||||
let mut bytes = Vec::new();
|
||||
if let Err(error) = member
|
||||
.read_to_string(&mut content)
|
||||
.by_ref()
|
||||
.take(read_limit)
|
||||
.read_to_end(&mut bytes)
|
||||
.into_diagnostic()
|
||||
.wrap_err_with(|| {
|
||||
format!(
|
||||
"failed to read member `{}` from `{}` as UTF-8 text",
|
||||
"failed to read member `{}` from `{}`",
|
||||
name.display(),
|
||||
self.outer.display()
|
||||
)
|
||||
|
|
@ -86,6 +159,31 @@ impl Iterator for ArchiveDocuments {
|
|||
{
|
||||
return Some(Err(error));
|
||||
}
|
||||
if u64::try_from(bytes.len()).unwrap_or(u64::MAX) > self.limits.max_document_bytes {
|
||||
return Some(Err(miette!(
|
||||
"archive member `{}` exceeded the {} byte document limit while reading",
|
||||
name.display(),
|
||||
self.limits.max_document_bytes
|
||||
)));
|
||||
}
|
||||
if bytes.contains(&0) {
|
||||
return Some(Err(miette!(
|
||||
"archive member `{}` contains a NUL byte",
|
||||
name.display()
|
||||
)));
|
||||
}
|
||||
let content = match String::from_utf8(bytes)
|
||||
.into_diagnostic()
|
||||
.wrap_err_with(|| {
|
||||
format!(
|
||||
"failed to decode member `{}` from `{}` as UTF-8 text",
|
||||
name.display(),
|
||||
self.outer.display()
|
||||
)
|
||||
}) {
|
||||
Ok(content) => content,
|
||||
Err(error) => return Some(Err(error)),
|
||||
};
|
||||
|
||||
return Some(Ok(Document {
|
||||
source: SourceId::ArchiveMember {
|
||||
|
|
@ -99,3 +197,39 @@ impl Iterator for ArchiveDocuments {
|
|||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse an archive name into a safe relative member identity.
|
||||
fn safe_member_path(name: &str) -> Result<PathBuf> {
|
||||
let normalized = name.replace('\\', "/");
|
||||
let path = Path::new(&normalized);
|
||||
if normalized.is_empty()
|
||||
|| path
|
||||
.components()
|
||||
.any(|component| !matches!(component, Component::Normal(_)))
|
||||
{
|
||||
return Err(miette!("unsafe archive member path `{name}`"));
|
||||
}
|
||||
|
||||
Ok(path.to_path_buf())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn rejects_absolute_and_traversing_member_paths() {
|
||||
for path in [
|
||||
"../escape.txt",
|
||||
"/absolute.txt",
|
||||
"a/../../escape",
|
||||
r"a\..\escape",
|
||||
] {
|
||||
assert!(safe_member_path(path).is_err(), "accepted {path}");
|
||||
}
|
||||
assert_eq!(
|
||||
safe_member_path("safe/member.txt").unwrap(),
|
||||
Path::new("safe/member.txt")
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,11 +2,31 @@
|
|||
|
||||
use std::{fs, path::Path};
|
||||
|
||||
use miette::{IntoDiagnostic, Result, WrapErr};
|
||||
use miette::{IntoDiagnostic, Result, WrapErr, miette};
|
||||
|
||||
use crate::config::InputLimits;
|
||||
|
||||
/// Read a regular file as strict UTF-8 text.
|
||||
pub(super) fn read_text(path: &Path) -> Result<String> {
|
||||
fs::read_to_string(path)
|
||||
pub(super) fn read_text(path: &Path, limits: &InputLimits) -> Result<String> {
|
||||
let metadata = fs::metadata(path)
|
||||
.into_diagnostic()
|
||||
.wrap_err_with(|| format!("failed to inspect `{}`", path.display()))?;
|
||||
if metadata.len() > limits.max_document_bytes {
|
||||
return Err(miette!(
|
||||
"input `{}` is {} bytes, exceeding the {} byte document limit",
|
||||
path.display(),
|
||||
metadata.len(),
|
||||
limits.max_document_bytes
|
||||
));
|
||||
}
|
||||
|
||||
let bytes = fs::read(path)
|
||||
.into_diagnostic()
|
||||
.wrap_err_with(|| format!("failed to read `{}`", path.display()))?;
|
||||
if bytes.contains(&0) {
|
||||
return Err(miette!("input `{}` contains a NUL byte", path.display()));
|
||||
}
|
||||
String::from_utf8(bytes)
|
||||
.into_diagnostic()
|
||||
.wrap_err_with(|| format!("failed to read `{}` as UTF-8 text", path.display()))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ use std::{path::Path, vec};
|
|||
|
||||
use miette::{Result, miette};
|
||||
|
||||
use crate::SourceId;
|
||||
use crate::{SourceId, config::InputLimits};
|
||||
|
||||
use self::{archive::ArchiveDocuments, walk::WalkDocuments};
|
||||
|
||||
|
|
@ -55,15 +55,15 @@ impl Iterator for Documents {
|
|||
}
|
||||
|
||||
/// Open a file, directory, or recognized ZIP-based container for streaming.
|
||||
pub fn read(path: &Path) -> Result<Documents> {
|
||||
pub fn read(path: &Path, limits: &InputLimits) -> Result<Documents> {
|
||||
if path.is_file() {
|
||||
if has_zip_extension(path) {
|
||||
return ArchiveDocuments::open(path.to_path_buf()).map(|archive| Documents {
|
||||
return ArchiveDocuments::open(path.to_path_buf(), limits).map(|archive| Documents {
|
||||
inner: DocumentsInner::Archive(archive),
|
||||
});
|
||||
}
|
||||
|
||||
let result = file::read_text(path).map(|content| Document {
|
||||
let result = file::read_text(path, limits).map(|content| Document {
|
||||
source: SourceId::File(path.to_path_buf()),
|
||||
content,
|
||||
});
|
||||
|
|
@ -74,7 +74,7 @@ pub fn read(path: &Path) -> Result<Documents> {
|
|||
|
||||
if path.is_dir() {
|
||||
return Ok(Documents {
|
||||
inner: DocumentsInner::Directory(WalkDocuments::new(path)),
|
||||
inner: DocumentsInner::Directory(WalkDocuments::new(path, limits)),
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -9,6 +9,10 @@ use zip::write::FileOptions;
|
|||
|
||||
use super::*;
|
||||
|
||||
fn limits() -> InputLimits {
|
||||
InputLimits::default()
|
||||
}
|
||||
|
||||
fn write_file(path: &Path, content: &str) {
|
||||
let mut file = File::create(path).expect("create fixture");
|
||||
file.write_all(content.as_bytes()).expect("write fixture");
|
||||
|
|
@ -35,7 +39,7 @@ fn reads_one_file() {
|
|||
let path = directory.path().join("test.txt");
|
||||
write_file(&path, "line1\nline2");
|
||||
|
||||
let documents = read(&path)
|
||||
let documents = read(&path, &limits())
|
||||
.expect("open input")
|
||||
.collect::<Result<Vec<_>>>()
|
||||
.expect("read input");
|
||||
|
|
@ -52,7 +56,7 @@ fn yields_a_directory_error_without_discarding_other_files() {
|
|||
fs::write(directory.path().join("b.txt"), [0xff]).expect("write invalid fixture");
|
||||
write_file(&directory.path().join("c.txt"), "c");
|
||||
|
||||
let results = read(directory.path())
|
||||
let results = read(directory.path(), &limits())
|
||||
.expect("open directory")
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
|
|
@ -67,7 +71,7 @@ fn streams_archive_members_with_outer_identity() {
|
|||
let path = directory.path().join("archive.zip");
|
||||
create_zip(&path).expect("create zip");
|
||||
|
||||
let documents = read(&path)
|
||||
let documents = read(&path, &limits())
|
||||
.expect("open archive")
|
||||
.collect::<Result<Vec<_>>>()
|
||||
.expect("read archive");
|
||||
|
|
@ -85,7 +89,7 @@ fn invalid_utf8_is_a_per_document_error() {
|
|||
let path = directory.path().join("invalid.txt");
|
||||
fs::write(&path, [0xff, 0xfe]).expect("write fixture");
|
||||
|
||||
let mut documents = read(&path).expect("open input");
|
||||
let mut documents = read(&path, &limits()).expect("open input");
|
||||
assert!(documents.next().expect("one result").is_err());
|
||||
assert!(documents.next().is_none());
|
||||
}
|
||||
|
|
@ -95,6 +99,23 @@ fn missing_path_is_rejected() {
|
|||
let directory = tempdir().expect("temp dir");
|
||||
let path = directory.path().join("missing.txt");
|
||||
|
||||
let error = read(&path).expect_err("missing input should fail");
|
||||
let error = read(&path, &limits()).expect_err("missing input should fail");
|
||||
assert!(error.to_string().contains("neither a file nor a directory"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn enforces_document_size_limit_before_reading() {
|
||||
let directory = tempdir().expect("temp dir");
|
||||
let path = directory.path().join("large.txt");
|
||||
write_file(&path, "too large");
|
||||
let mut limits = limits();
|
||||
limits.max_document_bytes = 3;
|
||||
|
||||
let mut documents = read(&path, &limits).expect("open input");
|
||||
let error = documents
|
||||
.next()
|
||||
.expect("one result")
|
||||
.expect_err("oversized input");
|
||||
|
||||
assert!(error.to_string().contains("document limit"));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,23 +2,29 @@
|
|||
|
||||
use std::path::Path;
|
||||
|
||||
use miette::{IntoDiagnostic, Result, WrapErr};
|
||||
use miette::{IntoDiagnostic, Result};
|
||||
use walkdir::{IntoIter, WalkDir};
|
||||
|
||||
use crate::{SourceId, input::Document};
|
||||
use crate::{SourceId, config::InputLimits, input::Document};
|
||||
|
||||
/// Iterator over readable regular files below a directory.
|
||||
#[derive(Debug)]
|
||||
pub(super) struct WalkDocuments {
|
||||
/// Underlying streaming directory iterator.
|
||||
entries: IntoIter,
|
||||
/// Limits applied to every yielded file.
|
||||
limits: InputLimits,
|
||||
}
|
||||
|
||||
impl WalkDocuments {
|
||||
/// Begin a recursive walk without following symbolic links.
|
||||
pub(super) fn new(root: &Path) -> Self {
|
||||
pub(super) fn new(root: &Path, limits: &InputLimits) -> Self {
|
||||
Self {
|
||||
entries: WalkDir::new(root).follow_links(false).into_iter(),
|
||||
entries: WalkDir::new(root)
|
||||
.follow_links(false)
|
||||
.max_depth(limits.max_walk_depth)
|
||||
.into_iter(),
|
||||
limits: limits.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -37,10 +43,7 @@ impl Iterator for WalkDocuments {
|
|||
}
|
||||
|
||||
let path = entry.into_path();
|
||||
let content = match std::fs::read_to_string(&path)
|
||||
.into_diagnostic()
|
||||
.wrap_err_with(|| format!("failed to read `{}` as UTF-8 text", path.display()))
|
||||
{
|
||||
let content = match super::file::read_text(&path, &self.limits) {
|
||||
Ok(content) => content,
|
||||
Err(error) => return Some(Err(error)),
|
||||
};
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ mod input;
|
|||
mod model;
|
||||
mod replacement;
|
||||
|
||||
pub use config::{Config, ReplacementRule};
|
||||
pub use config::{Config, InputLimits, ReplacementRule};
|
||||
pub use input::{Document, Documents};
|
||||
pub use model::{Analysis, Category, Confidence, Finding, RuleId, SourceId, TextEdit};
|
||||
|
||||
|
|
@ -34,8 +34,8 @@ pub fn plan_replacements(config: &Config, source: &str) -> Vec<TextEdit> {
|
|||
}
|
||||
|
||||
/// Read documents from a file, directory, or supported ZIP-based container.
|
||||
pub fn read_inputs(path: &Path) -> miette::Result<Documents> {
|
||||
input::read(path)
|
||||
pub fn read_inputs(config: &Config, path: &Path) -> miette::Result<Documents> {
|
||||
input::read(path, &config.input)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ fn main() -> Result<()> {
|
|||
.ok_or_else(|| miette!("usage: john_slop <path>"))?;
|
||||
|
||||
let config = load_config();
|
||||
for document in read_inputs(Path::new(&path))? {
|
||||
for document in read_inputs(&config, Path::new(&path))? {
|
||||
let document = document?;
|
||||
let analysis = analyze_source(&config, &document.content);
|
||||
println!(
|
||||
|
|
|
|||
2
tasks.md
2
tasks.md
|
|
@ -52,7 +52,7 @@ are written down, and a clean baseline passes all quality gates.
|
|||
- [x] Split `reader.rs` into `input/file.rs`, `input/walk.rs`, and
|
||||
`input/archive.rs`; yield one `Result<Document, InputError>` at a time rather
|
||||
than loading a directory into a `Vec`.
|
||||
- [ ] Preserve outer and inner archive identities. Prevent path traversal and
|
||||
- [x] Preserve outer and inner archive identities. Prevent path traversal and
|
||||
decompression bombs using the limits from Milestone 0.
|
||||
- [ ] Centralize error enums with `thiserror`; add `miette` context only at
|
||||
presentation boundaries. Remove placeholder `try_report` and sample reports.
|
||||
|
|
|
|||
Loading…
Reference in New Issue