fix(core): prevent iOS crash on invalid plugin response JSON (#8049)

This commit is contained in:
Lucas Fernandes Nogueira 2023-10-19 19:47:43 -03:00 committed by GitHub
parent d16206a086
commit 22f26882cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 15 deletions

View File

@ -0,0 +1,5 @@
---
"tauri": patch:bug
---
Prevent crash on iOS when the Swift plugin data is not a valid JSON string.

View File

@ -344,12 +344,19 @@ pub(crate) fn run_command<R: Runtime, C: AsRef<str>, F: FnOnce(PluginResponse) +
.unwrap() .unwrap()
.remove(&id) .remove(&id)
{ {
let payload = serde_json::from_str(payload.to_str().unwrap()).unwrap(); let json = payload.to_str().unwrap();
handler(if success == 1 { match serde_json::from_str(json) {
Ok(payload) Ok(payload) => {
} else { handler(if success == 1 {
Err(payload) Ok(payload)
}); } else {
Err(payload)
});
}
Err(err) => {
handler(Err(format!("{err}, data: {}", json).into()));
}
}
} }
} }

View File

@ -69,7 +69,7 @@ pub fn get<R: Runtime>(
fn get_response<R: Runtime>( fn get_response<R: Runtime>(
request: Request<Vec<u8>>, request: Request<Vec<u8>>,
manager: &WindowManager<R>, #[allow(unused_variables)] manager: &WindowManager<R>,
window_origin: &str, window_origin: &str,
web_resource_request_handler: Option<&WebResourceRequestHandler>, web_resource_request_handler: Option<&WebResourceRequestHandler>,
#[cfg(all(dev, mobile))] (url, response_cache): ( #[cfg(all(dev, mobile))] (url, response_cache): (

View File

@ -32,10 +32,7 @@ use crate::{
}, },
sealed::ManagerBase, sealed::ManagerBase,
sealed::RuntimeOrDispatch, sealed::RuntimeOrDispatch,
utils::{ utils::config::{WindowConfig, WindowEffectsConfig, WindowUrl},
config::{WindowConfig, WindowEffectsConfig, WindowUrl},
ProgressBarState,
},
EventLoopMessage, Manager, Runtime, Theme, WindowEvent, EventLoopMessage, Manager, Runtime, Theme, WindowEvent,
}; };
#[cfg(desktop)] #[cfg(desktop)]
@ -2071,7 +2068,10 @@ impl<R: Runtime> Window<R> {
/// - **Linux / macOS**: Progress bar is app-wide and not specific to this window. /// - **Linux / macOS**: Progress bar is app-wide and not specific to this window.
/// - **Linux**: Only supported desktop environments with `libunity` (e.g. GNOME). /// - **Linux**: Only supported desktop environments with `libunity` (e.g. GNOME).
/// - **iOS / Android:** Unsupported. /// - **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 self
.window .window
.dispatcher .dispatcher

View File

@ -314,13 +314,12 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
#[cfg(any(debug_assertions, feature = "devtools"))] #[cfg(any(debug_assertions, feature = "devtools"))]
desktop_commands::internal_toggle_devtools, desktop_commands::internal_toggle_devtools,
]); ]);
#[allow(clippy::needless_return)] handler(invoke)
return handler(invoke);
} }
#[cfg(mobile)] #[cfg(mobile)]
{ {
invoke.resolver.reject("Window API not available on mobile"); invoke.resolver.reject("Window API not available on mobile");
return true; true
} }
}) })
.build() .build()