Skip to content

Commit c081082

Browse files
committed
feat: Hard-limit original image byte size to 2 * max_bytes if recoding fails (#8296)
`max_bytes` isn't a strict limit for non-avatars (note: it's 500 kB by default), still, we don't want to send a gigabyte if recoding fails.
1 parent 50e83f2 commit c081082

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

src/blob.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -329,15 +329,17 @@ impl<'a> BlobObject<'a> {
329329
) -> Result<String> {
330330
// Add white background only to avatars to spare the CPU.
331331
let mut add_white_bg = is_avatar;
332-
let mut no_exif = false;
333-
let no_exif_ref = &mut no_exif;
332+
let mut can_use_original = false;
333+
let can_use_original_ref = &mut can_use_original;
334334
let mut name = name.unwrap_or_else(|| self.name.clone());
335335
let original_name = name.clone();
336336
let vt = &mut *viewtype;
337337
let res: Result<String> = tokio::task::block_in_place(move || {
338338
let mut file = std::fs::File::open(self.to_abs_path())?;
339339
let (nr_bytes, exif) = image_metadata(&file)?;
340-
*no_exif_ref = exif.is_none();
340+
// `max_bytes` isn't a strict limit for non-avatars, still, we don't want to send a
341+
// gigabyte if recoding fails.
342+
*can_use_original_ref = exif.is_none() && nr_bytes <= u64::try_from(max_bytes)? * 2;
341343
// It's strange that BufReader modifies a file position while it takes a non-mut
342344
// reference. Ok, just rewind it.
343345
file.rewind()?;
@@ -390,7 +392,7 @@ impl<'a> BlobObject<'a> {
390392
} else {
391393
max(img.width(), img.height())
392394
};
393-
let exceeds_max_bytes = nr_bytes > max_bytes as u64;
395+
let exceeds_max_bytes = nr_bytes > u64::try_from(max_bytes)?;
394396

395397
let jpeg_quality = 75;
396398
let ofmt = match fmt {
@@ -502,7 +504,7 @@ impl<'a> BlobObject<'a> {
502504
match res {
503505
Ok(_) => res,
504506
Err(err) => {
505-
if !is_avatar && no_exif {
507+
if !is_avatar && can_use_original {
506508
error!(
507509
context,
508510
"Cannot check/recode image, using original data: {err:#}.",

0 commit comments

Comments
 (0)