Skip to content

Commit

Permalink
fix(core): prevent iOS crash on invalid plugin response JSON (#8049)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog authored Oct 19, 2023
1 parent d16206a commit 22f2688
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changes/prevent-ios-crash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": patch:bug
---

Prevent crash on iOS when the Swift plugin data is not a valid JSON string.
19 changes: 13 additions & 6 deletions core/tauri/src/plugin/mobile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,12 +344,19 @@ pub(crate) fn run_command<R: Runtime, C: AsRef<str>, F: FnOnce(PluginResponse) +
.unwrap()
.remove(&id)
{
let payload = serde_json::from_str(payload.to_str().unwrap()).unwrap();
handler(if success == 1 {
Ok(payload)
} else {
Err(payload)
});
let json = payload.to_str().unwrap();
match serde_json::from_str(json) {
Ok(payload) => {
handler(if success == 1 {
Ok(payload)
} else {
Err(payload)
});
}
Err(err) => {
handler(Err(format!("{err}, data: {}", json).into()));
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion core/tauri/src/protocol/tauri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub fn get<R: Runtime>(

fn get_response<R: Runtime>(
request: Request<Vec<u8>>,
manager: &WindowManager<R>,
#[allow(unused_variables)] manager: &WindowManager<R>,
window_origin: &str,
web_resource_request_handler: Option<&WebResourceRequestHandler>,
#[cfg(all(dev, mobile))] (url, response_cache): (
Expand Down
10 changes: 5 additions & 5 deletions core/tauri/src/window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ use crate::{
},
sealed::ManagerBase,
sealed::RuntimeOrDispatch,
utils::{
config::{WindowConfig, WindowEffectsConfig, WindowUrl},
ProgressBarState,
},
utils::config::{WindowConfig, WindowEffectsConfig, WindowUrl},
EventLoopMessage, Manager, Runtime, Theme, WindowEvent,
};
#[cfg(desktop)]
Expand Down Expand Up @@ -2071,7 +2068,10 @@ impl<R: Runtime> Window<R> {
/// - **Linux / macOS**: Progress bar is app-wide and not specific to this window.
/// - **Linux**: Only supported desktop environments with `libunity` (e.g. GNOME).
/// - **iOS / Android:** Unsupported.
pub fn set_progress_bar(&self, progress_state: ProgressBarState) -> crate::Result<()> {
pub fn set_progress_bar(
&self,
progress_state: crate::utils::ProgressBarState,
) -> crate::Result<()> {
self
.window
.dispatcher
Expand Down
5 changes: 2 additions & 3 deletions core/tauri/src/window/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,13 +314,12 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
#[cfg(any(debug_assertions, feature = "devtools"))]
desktop_commands::internal_toggle_devtools,
]);
#[allow(clippy::needless_return)]
return handler(invoke);
handler(invoke)
}
#[cfg(mobile)]
{
invoke.resolver.reject("Window API not available on mobile");
return true;
true
}
})
.build()
Expand Down

0 comments on commit 22f2688

Please sign in to comment.