Skip to content

Commit

Permalink
fix(core): convert js Map to object before serialization, closes #6078
Browse files Browse the repository at this point in the history
 (#6099)

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
  • Loading branch information
amrbashir and lucasfernog authored Jan 19, 2023
1 parent a822a6a commit d4d6a98
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 83 deletions.
5 changes: 5 additions & 0 deletions .changes/core-js-Map.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": "patch"
---

Fix serialization of js `Map` when used in `invoke`.
2 changes: 1 addition & 1 deletion core/tauri-runtime-wry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ exclude = [ "CHANGELOG.md", "/target" ]
readme = "README.md"

[dependencies]
wry = { git = "https://github.com/tauri-apps/wry", default-features = false, features = [ "file-drop", "protocol" ] }
wry = { version = "0.24.1", default-features = false, features = [ "file-drop", "protocol" ] }
tauri-runtime = { version = "0.12.1", path = "../tauri-runtime" }
tauri-utils = { version = "1.2.1", path = "../tauri-utils" }
uuid = { version = "1", features = [ "v4" ] }
Expand Down
4 changes: 2 additions & 2 deletions core/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -990,10 +990,10 @@ fn decode_path(path: PathBuf) -> PathBuf {
impl From<FileDropEventWrapper> for FileDropEvent {
fn from(event: FileDropEventWrapper) -> Self {
match event.0 {
WryFileDropEvent::Hovered { paths, position: _ } => {
WryFileDropEvent::Hovered(paths) => {
FileDropEvent::Hovered(paths.into_iter().map(decode_path).collect())
}
WryFileDropEvent::Dropped { paths, position: _ } => {
WryFileDropEvent::Dropped(paths) => {
FileDropEvent::Dropped(paths.into_iter().map(decode_path).collect())
}
// default to cancelled
Expand Down
2 changes: 1 addition & 1 deletion core/tauri-utils/src/pattern/isolation.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
algorithm.iv = window.crypto.getRandomValues(new Uint8Array(12))

let encoder = new TextEncoder()
let payloadRaw = encoder.encode(JSON.stringify(data))
let payloadRaw = encoder.encode(__RAW_stringify_ipc_message_fn__(data))

return window.crypto.subtle
.encrypt(algorithm, aesGcmKey, payloadRaw)
Expand Down
3 changes: 3 additions & 0 deletions core/tauri-utils/src/pattern/isolation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ pub struct IsolationJavascriptCodegen {
pub struct IsolationJavascriptRuntime<'a> {
/// The key used on the Rust backend and the Isolation Javascript
pub runtime_aes_gcm_key: &'a [u8; 32],
/// The function that stringifies a IPC message.
#[raw]
pub stringify_ipc_message_fn: &'a str,
}

#[cfg(test)]
Expand Down
11 changes: 11 additions & 0 deletions core/tauri/scripts/stringify-ipc-message-fn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(function (message) {
return JSON.stringify(message, (_k, val) => {
if (val instanceof Map) {
let o = {};
val.forEach((v, k) => o[k] = v);
return o;
} else {
return val;
}
})
})
2 changes: 1 addition & 1 deletion core/tauri/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,7 @@ impl<R: Runtime> Builder<R> {
invoke_handler: Box::new(|_| ()),
invoke_responder: Arc::new(window_invoke_responder),
invoke_initialization_script:
"Object.defineProperty(window, '__TAURI_POST_MESSAGE__', { value: (message) => window.ipc.postMessage(JSON.stringify(message)) })".into(),
format!("Object.defineProperty(window, '__TAURI_POST_MESSAGE__', {{ value: (message) => window.ipc.postMessage({}(message)) }})", crate::manager::STRINGIFY_IPC_MESSAGE_FN),
on_page_load: Box::new(|_, _| ()),
pending_windows: Default::default(),
plugins: PluginStore::default(),
Expand Down
4 changes: 4 additions & 0 deletions core/tauri/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ const WINDOW_FILE_DROP_HOVER_EVENT: &str = "tauri://file-drop-hover";
const WINDOW_FILE_DROP_CANCELLED_EVENT: &str = "tauri://file-drop-cancelled";
const MENU_EVENT: &str = "tauri://menu";

pub(crate) const STRINGIFY_IPC_MESSAGE_FN: &str =
include_str!("../scripts/stringify-ipc-message-fn.js");

#[derive(Default)]
/// Spaced and quoted Content-Security-Policy hash values.
struct CspHashStrings {
Expand Down Expand Up @@ -724,6 +727,7 @@ impl<R: Runtime> WindowManager<R> {
let asset = String::from_utf8_lossy(asset.as_ref());
let template = tauri_utils::pattern::isolation::IsolationJavascriptRuntime {
runtime_aes_gcm_key: &aes_gcm_key,
stringify_ipc_message_fn: STRINGIFY_IPC_MESSAGE_FN,
};
match template.render(asset.as_ref(), &Default::default()) {
Ok(asset) => HttpResponseBuilder::new()
Expand Down
Loading

0 comments on commit d4d6a98

Please sign in to comment.