Skip to content

Commit 980720e

Browse files
committed
Add image dimension and data validation to editor
1 parent 860ccd7 commit 980720e

1 file changed

Lines changed: 42 additions & 3 deletions

File tree

apps/desktop/src-tauri/src/screenshot_editor.rs

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ use tauri::{
2323
use tokio::sync::{RwLock, watch};
2424
use tokio_util::sync::CancellationToken;
2525

26+
const MAX_DIMENSION: u32 = 16_384;
27+
2628
pub struct ScreenshotEditorInstance {
2729
pub ws_port: u16,
2830
pub ws_shutdown_token: CancellationToken,
@@ -105,14 +107,51 @@ impl ScreenshotEditorInstances {
105107
let pending_frame = pending.and_then(|p| p.remove(&key));
106108

107109
if let Some(frame) = pending_frame {
108-
let rgb_img =
109-
RgbImage::from_raw(frame.width, frame.height, frame.data).unwrap();
110+
let width = frame.width;
111+
let height = frame.height;
112+
113+
if width > MAX_DIMENSION || height > MAX_DIMENSION {
114+
return Err(format!(
115+
"Image dimensions exceed maximum: {width}x{height}"
116+
));
117+
}
118+
119+
let expected_len = width
120+
.checked_mul(height)
121+
.and_then(|p| p.checked_mul(3))
122+
.ok_or_else(|| {
123+
format!("Image dimensions overflow: {width}x{height}")
124+
})?;
125+
let expected_len = usize::try_from(expected_len)
126+
.map_err(|_| format!("Image size too large: {width}x{height}"))?;
127+
128+
let data = frame.data;
129+
130+
if data.len() != expected_len {
131+
return Err(format!(
132+
"Image data length mismatch: expected {expected_len} bytes for {width}x{height} frame, got {}",
133+
data.len()
134+
));
135+
}
136+
137+
let rgb_img = RgbImage::from_raw(width, height, data).ok_or_else(|| {
138+
format!("Invalid RGB data for {width}x{height} frame")
139+
})?;
110140
let rgba_img: image::RgbaImage = rgb_img.convert();
111-
(rgba_img.into_raw(), frame.width, frame.height)
141+
(rgba_img.into_raw(), width, height)
112142
} else {
113143
let img =
114144
image::open(&path).map_err(|e| format!("Failed to open image: {e}"))?;
115145
let (w, h) = img.dimensions();
146+
147+
if w > MAX_DIMENSION || h > MAX_DIMENSION {
148+
return Err(format!("Image dimensions exceed maximum: {w}x{h}"));
149+
}
150+
151+
w.checked_mul(h)
152+
.and_then(|p| p.checked_mul(4))
153+
.ok_or_else(|| format!("Image dimensions overflow: {w}x{h}"))?;
154+
116155
(img.to_rgba8().into_raw(), w, h)
117156
}
118157
};

0 commit comments

Comments
 (0)