mirror of
https://github.com/storytold/lightcraft.git
synced 2026-12-02 10:49:56 -06:00
security: O(width) box-blur priming, case-insensitive download names, stats walk skips directory links (#3729)
* security: box blur window sum in O(width), case-insensitive download name uniqueness, stats walk does not follow directory links * address review: skip the last-pixel term when the radius does not reach it
This commit is contained in:
@@ -242,6 +242,7 @@ fn validate_files(files: &[FileSpec<'_>]) -> Result<u64, DownloadError> {
|
||||
if files.len() > 256 {
|
||||
return Err(DownloadError::Invalid("too many files".into()));
|
||||
}
|
||||
// (names that differ only in case are one file on a case-insensitive file system)
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
let mut total = 0u64;
|
||||
for f in files {
|
||||
@@ -259,7 +260,7 @@ fn validate_files(files: &[FileSpec<'_>]) -> Result<u64, DownloadError> {
|
||||
|| f.name.starts_with('.')
|
||||
|| f.name.contains(['/', '\\', ':'])
|
||||
|| f.name.chars().any(char::is_control)
|
||||
|| !seen.insert(f.name)
|
||||
|| !seen.insert(f.name.to_lowercase())
|
||||
{
|
||||
return Err(DownloadError::Invalid("a filename must be a unique, ordinary filename".into()));
|
||||
}
|
||||
|
||||
@@ -425,3 +425,10 @@ fn hostile_file_specs_are_rejected_before_touching_disk() {
|
||||
assert!(!dir.exists());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn names_differing_only_in_case_are_one_file() {
|
||||
let spec = |name| FileSpec { name, size: Some(1), sha256: None, max: 1 };
|
||||
assert!(super::validate_files(&[spec("Model.bin")]).is_ok());
|
||||
assert!(super::validate_files(&[spec("Model.bin"), spec("model.BIN")]).is_err());
|
||||
}
|
||||
|
||||
@@ -49,8 +49,15 @@ fn box_row<T: Pixel>(row: &mut [T], tmp: &mut Vec<T>, r: usize) {
|
||||
let last = w - 1;
|
||||
let inv = 1.0 / (2 * r + 1) as f32;
|
||||
let mut acc = T::zero();
|
||||
for i in 0..=2 * r {
|
||||
acc = acc.madd(s[i.saturating_sub(r).min(last)], 1.0);
|
||||
// the window [-r, r] with clamped edges: r copies of the first pixel, the pixels 0..=min(r, last), then the
|
||||
// last pixel for each offset past it (O(w), not O(r), however large the radius)
|
||||
acc = acc.madd(s[0], r as f32);
|
||||
for v in &s[..=r.min(last)] {
|
||||
acc = acc.madd(*v, 1.0);
|
||||
}
|
||||
// only when the window reaches past the last pixel: a NaN there must not poison a row it isn't in
|
||||
if r > last {
|
||||
acc = acc.madd(s[last], (r - last) as f32);
|
||||
}
|
||||
for x in 0..w {
|
||||
row[x] = T::zero().madd(acc, inv);
|
||||
@@ -177,6 +184,14 @@ pub fn gaussian_taps<T: Pixel>(img: &Image<T>, taps: &[f32]) -> Image<T> {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn a_non_finite_last_pixel_stays_out_of_windows_that_do_not_reach_it() {
|
||||
let mut row = [1.0f32; 8];
|
||||
row[7] = f32::NAN;
|
||||
box_row(&mut row, &mut Vec::new(), 2);
|
||||
assert!(row[..5].iter().all(|v| v.is_finite()), "{row:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fir_gaussian_has_bounded_finite_taps_for_hostile_sigmas() {
|
||||
for sigma in [f32::NAN, f32::NEG_INFINITY, f32::INFINITY, f32::MAX, -5.0, 0.0, 0.1, 4.0] {
|
||||
@@ -277,4 +292,18 @@ mod tests {
|
||||
let var: f32 = b.data.iter().enumerate().map(|(i, v)| v * ((i as f32 - 100.0).powi(2))).sum();
|
||||
assert!((var.sqrt() - sigma).abs() / sigma < 0.1, "{}", var.sqrt());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn box_row_matches_the_clamped_window_sum_for_radii_past_the_row() {
|
||||
for (w, r) in [(1usize, 1usize), (5, 1), (5, 4), (5, 5), (5, 9), (7, 3)] {
|
||||
let mut row: Vec<f32> = (0..w).map(|i| (i * i + 1) as f32).collect();
|
||||
let want: Vec<f32> = (0..w as isize)
|
||||
.map(|x| (-(r as isize)..=r as isize).map(|j| row[(x + j).clamp(0, w as isize - 1) as usize]).sum::<f32>() / (2 * r + 1) as f32)
|
||||
.collect();
|
||||
box_row(&mut row, &mut Vec::new(), r);
|
||||
for (a, b) in row.iter().zip(&want) {
|
||||
assert!((a - b).abs() < 1e-3, "w {w} r {r}: {a} vs {b}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+2
-1
@@ -18,7 +18,8 @@ fn walk(dir: &Path, c: &mut Counts) {
|
||||
let p = e.path();
|
||||
let name = e.file_name();
|
||||
let name = name.to_string_lossy();
|
||||
if p.is_dir() {
|
||||
// (a link to a directory is not followed: a cycle of links would be walked without end)
|
||||
if e.file_type().is_ok_and(|t| t.is_dir()) {
|
||||
// Nested fuzz workspaces and build output are not part of the crate.
|
||||
if name != "target" && name != "fuzz" && !name.starts_with('.') {
|
||||
walk(&p, c);
|
||||
|
||||
Reference in New Issue
Block a user