Переглянути джерело

fix(core): nested isolation iframes, closes #4015 (#4020)

Lucas Fernandes Nogueira 3 роки тому
батько
коміт
022eed4667

+ 5 - 0
.changes/fix-nested-isolation-iframe.md

@@ -0,0 +1,5 @@
+---
+"tauri": patch
+---
+
+Fixes nested isolation iframe injection.

+ 10 - 8
core/tauri/scripts/isolation.js

@@ -3,13 +3,15 @@
 // SPDX-License-Identifier: MIT
 
 window.addEventListener('DOMContentLoaded', () => {
-  let style = document.createElement('style')
-  style.textContent = __TEMPLATE_style__
-  document.head.append(style)
+  if (window.location.origin.startsWith(__TEMPLATE_origin__)) {
+    let style = document.createElement('style')
+    style.textContent = __TEMPLATE_style__
+    document.head.append(style)
 
-  let iframe = document.createElement('iframe')
-  iframe.id = '__tauri_isolation__'
-  iframe.sandbox.add('allow-scripts')
-  iframe.src = __TEMPLATE_isolation_src__
-  document.body.append(iframe)
+    let iframe = document.createElement('iframe')
+    iframe.id = '__tauri_isolation__'
+    iframe.sandbox.add('allow-scripts')
+    iframe.src = __TEMPLATE_isolation_src__
+    document.body.append(iframe)
+  }
 })

+ 1 - 0
core/tauri/src/hooks.rs

@@ -39,6 +39,7 @@ pub(crate) struct IpcJavascript<'a> {
 #[derive(Template)]
 #[default_template("../scripts/isolation.js")]
 pub(crate) struct IsolationJavascript<'a> {
+  pub(crate) origin: &'a str,
   pub(crate) isolation_src: &'a str,
   pub(crate) style: &'a str,
 }

+ 1 - 0
core/tauri/src/manager.rs

@@ -438,6 +438,7 @@ impl<R: Runtime> WindowManager<R> {
     if let Pattern::Isolation { schema, .. } = self.pattern() {
       webview_attributes = webview_attributes.initialization_script(
         &IsolationJavascript {
+          origin: &self.get_browser_origin(),
           isolation_src: &crate::pattern::format_real_schema(schema),
           style: tauri_utils::pattern::isolation::IFRAME_STYLE,
         }