From a8c0d384fc51b12d2436c11d10fd8c2dfdcd9d4a Mon Sep 17 00:00:00 2001 From: "Ngo Iok Ui (Wu Yu Wei)" Date: Tue, 28 Nov 2023 17:27:15 +0900 Subject: [PATCH] Fix IPC crash on wkwebview if receiving invalid types (#1097) --- .changes/ipc-fix.md | 6 ++++++ src/wkwebview/mod.rs | 16 +++++++++------- 2 files changed, 15 insertions(+), 7 deletions(-) create mode 100644 .changes/ipc-fix.md diff --git a/.changes/ipc-fix.md b/.changes/ipc-fix.md new file mode 100644 index 000000000..089969472 --- /dev/null +++ b/.changes/ipc-fix.md @@ -0,0 +1,6 @@ +--- +"wry": patch +--- + +Fix IPC crash on wkwebview if receiving invalid types. + diff --git a/src/wkwebview/mod.rs b/src/wkwebview/mod.rs index cb41c7c6f..49d8f62e4 100644 --- a/src/wkwebview/mod.rs +++ b/src/wkwebview/mod.rs @@ -140,15 +140,17 @@ impl InnerWebView { if !function.is_null() { let function = &mut *(*function as *mut Box); let body: id = msg_send![msg, body]; - let utf8: *const c_char = msg_send![body, UTF8String]; - if let Ok(js) = CStr::from_ptr(utf8).to_str() { - (function)(js.to_string()); - } else { - log::warn!("WebView received invalid UTF8 string from IPC."); + let is_string: bool = msg_send![body, isKindOfClass: class!(NSString)]; + if is_string { + let utf8: *const c_char = msg_send![body, UTF8String]; + if let Ok(js) = CStr::from_ptr(utf8).to_str() { + (function)(js.to_string()); + return; + } } - } else { - log::warn!("WebView instance is dropped! This handler shouldn't be called."); } + + log::warn!("WebView received invalid IPC call."); } }