Skip to content

Commit 22f2688

Browse files
authored
fix(core): prevent iOS crash on invalid plugin response JSON (#8049)
1 parent d16206a commit 22f2688

File tree

5 files changed

+26
-15
lines changed

5 files changed

+26
-15
lines changed

.changes/prevent-ios-crash.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch:bug
3+
---
4+
5+
Prevent crash on iOS when the Swift plugin data is not a valid JSON string.

core/tauri/src/plugin/mobile.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -344,12 +344,19 @@ pub(crate) fn run_command<R: Runtime, C: AsRef<str>, F: FnOnce(PluginResponse) +
344344
.unwrap()
345345
.remove(&id)
346346
{
347-
let payload = serde_json::from_str(payload.to_str().unwrap()).unwrap();
348-
handler(if success == 1 {
349-
Ok(payload)
350-
} else {
351-
Err(payload)
352-
});
347+
let json = payload.to_str().unwrap();
348+
match serde_json::from_str(json) {
349+
Ok(payload) => {
350+
handler(if success == 1 {
351+
Ok(payload)
352+
} else {
353+
Err(payload)
354+
});
355+
}
356+
Err(err) => {
357+
handler(Err(format!("{err}, data: {}", json).into()));
358+
}
359+
}
353360
}
354361
}
355362

core/tauri/src/protocol/tauri.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub fn get<R: Runtime>(
6969

7070
fn get_response<R: Runtime>(
7171
request: Request<Vec<u8>>,
72-
manager: &WindowManager<R>,
72+
#[allow(unused_variables)] manager: &WindowManager<R>,
7373
window_origin: &str,
7474
web_resource_request_handler: Option<&WebResourceRequestHandler>,
7575
#[cfg(all(dev, mobile))] (url, response_cache): (

core/tauri/src/window/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@ use crate::{
3232
},
3333
sealed::ManagerBase,
3434
sealed::RuntimeOrDispatch,
35-
utils::{
36-
config::{WindowConfig, WindowEffectsConfig, WindowUrl},
37-
ProgressBarState,
38-
},
35+
utils::config::{WindowConfig, WindowEffectsConfig, WindowUrl},
3936
EventLoopMessage, Manager, Runtime, Theme, WindowEvent,
4037
};
4138
#[cfg(desktop)]
@@ -2071,7 +2068,10 @@ impl<R: Runtime> Window<R> {
20712068
/// - **Linux / macOS**: Progress bar is app-wide and not specific to this window.
20722069
/// - **Linux**: Only supported desktop environments with `libunity` (e.g. GNOME).
20732070
/// - **iOS / Android:** Unsupported.
2074-
pub fn set_progress_bar(&self, progress_state: ProgressBarState) -> crate::Result<()> {
2071+
pub fn set_progress_bar(
2072+
&self,
2073+
progress_state: crate::utils::ProgressBarState,
2074+
) -> crate::Result<()> {
20752075
self
20762076
.window
20772077
.dispatcher

core/tauri/src/window/plugin.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,13 +314,12 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
314314
#[cfg(any(debug_assertions, feature = "devtools"))]
315315
desktop_commands::internal_toggle_devtools,
316316
]);
317-
#[allow(clippy::needless_return)]
318-
return handler(invoke);
317+
handler(invoke)
319318
}
320319
#[cfg(mobile)]
321320
{
322321
invoke.resolver.reject("Window API not available on mobile");
323-
return true;
322+
true
324323
}
325324
})
326325
.build()

0 commit comments

Comments
 (0)