@@ -23,6 +23,8 @@ use tauri::{
2323use tokio:: sync:: { RwLock , watch} ;
2424use tokio_util:: sync:: CancellationToken ;
2525
26+ const MAX_DIMENSION : u32 = 16_384 ;
27+
2628pub 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