tauri icon indicating copy to clipboard operation
tauri copied to clipboard

[bug] Isolation pattern: the isolation iframe gets an opaque origin, so crypto.subtle is undefined and ALL IPC silently hangs (Windows/WebView2)

Open asmarmomand opened this issue 1 month ago • 1 comments

Describe the bug

With app.security.pattern.use = "isolation", every invoke() hangs forever on Windows. No error is thrown, no promise rejects, and nothing is logged on either the JS or the Rust side.

The cause is in crates/tauri/scripts/isolation.js. The isolation iframe is created with allow-scripts and nothing else:

let iframe = document.createElement('iframe')
iframe.id = '__tauri_isolation__'
iframe.sandbox.add('allow-scripts')     // <- no allow-same-origin
iframe.src = __TEMPLATE_isolation_src__

A sandboxed iframe without allow-same-origin is assigned an opaque origin. On Windows (and Android) the isolation frame is navigated to http://<schema>.localhost (crates/tauri/src/pattern.rs, format_real_schema), and the resulting frame is not a secure context, so window.crypto.subtle is undefined.

That breaks the isolation payload script, which needs SubtleCrypto to import the AES-GCM key:

// crates/tauri-utils/src/pattern/isolation.js
const aesGcmKey = await window.crypto.subtle.importKey(
  'raw', aesGcmKeyRaw, 'AES-GCM', false, ['encrypt']
)

Why it fails silently — this is the part that makes it expensive to debug

Three things compound:

  1. crates/tauri-utils/src/pattern/isolation.js is an async IIFE with no try/catch. The importKey above rejects, the IIFE's promise rejects as an unhandled rejection inside a sandboxed iframe, and the setTimeout(waitUntilReady, readyIntervalMs) at the bottom of the file is never reached — so __TAURI_ISOLATION_READY__ is never posted to the parent.
  2. In crates/tauri/scripts/ipc.js, the case 'isolation' branch does if (isolation.ready) sendIsolationMessage(message) else isolation.queue.push(message), and isolation.ready is only ever set by the __TAURI_ISOLATION_READY__ listener. So isolation.queue grows without bound and is never flushed.
  3. Therefore every invoke() promise is pending forever. There is nothing to catch, no Rust log line (nothing reaches Rust), and no error boundary fires. Only a timeout can observe it.

Downstream this presents as unrelated-looking failures: OAuth never starts (shell:open is IPC), the SQL plugin never creates its database, keychain commands never return — all one root cause.

Reproduction

Minimal, no Tauri required

This isolates the web-platform mechanism. Serve two files over plain http and open parent.html:

<!-- parent.html -->
<script>
  const i = document.createElement('iframe')
  i.sandbox.add('allow-scripts')           // flip: also add 'allow-same-origin'
  i.src = 'http://localhost:8791/child.html'
  document.body.appendChild(i)
</script>
<!-- child.html -->
<script>
  parent.postMessage({
    origin: location.origin,               // "null" == opaque
    isSecureContext: window.isSecureContext,
    subtle: typeof (window.crypto && window.crypto.subtle)
  }, '*')
</script>

With allow-scripts only the child reports an opaque origin; adding allow-same-origin gives it its real origin, isSecureContext: true and a working crypto.subtle.

In Tauri

  1. tauri.conf.json → "app": { "security": { "pattern": { "use": "isolation" } } }.
  2. Build and run a release build on Windows.
  3. Launch with WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS=--remote-debugging-port=9222, attach over CDP, and read isSecureContext + typeof crypto.subtle per frame.
  4. Call any invoke() from the frontend — the promise never settles.

Measured

Frame Origin isSecureContext crypto.subtle
main http://tauri.localhost true object
isolation http://isolation-<uuid>.localhost false undefined

…with a live pageerror from the isolation frame: TypeError: Cannot read properties of undefined (reading 'importKey').

Expected behavior

The isolation frame should be a secure context so its AES-GCM key bootstrap succeeds and IPC works; failing that, the key bootstrap should fail loudly rather than leaving every invoke() pending.

Platform

  • OS: Windows 10 Pro 19045, WebView2
  • tauri: 2.11.6 (line is identical in 2.11.5 and unchanged on dev)
  • Pattern: isolation
  • macOS/Linux are not affected: format_real_schema gives those platforms a Tauri-registered custom scheme (<schema>://localhost) rather than http://<schema>.localhost.

Related

  • #10956 — "Refused to execute inline script in isolation mode", Windows/WebView2, open since 2024-09-11, status: needs triage. A second Windows-specific isolation defect.
  • tauri-apps/wry #1710 / #1709 — the same bug class already accepted for Android: Android WebView excludes http://tauri.localhost from secure contexts, blocking secure-context APIs.

Suggested fix

Add allow-same-origin to the isolation iframe's sandbox (PR below). The frame keeps a different origin from the main frame (<schema>.localhost vs tauri.localhost), so the two remain cross-origin and postMessage stays the only channel.

Two secondary issues worth fixing alongside

  1. The payload script has no error handling. Wrapping the IIFE in try/catch and posting a failure message to the parent would have turned this from a silent hang into a one-line diagnosis. Worth doing independently of the sandbox fix.
  2. frame-src is never widened for the isolation origin. For the main document, crates/tauri/src/manager/mod.rs pushes the isolation origin into default-src only. An app whose configured CSP sets an explicit frame-src (e.g. frame-src 'none', which is otherwise good hygiene) will have the isolation iframe blocked outright, because an explicit frame-src suppresses the default-src fallback. Either inject into frame-src as well, or document that frame-src must not be set when using the isolation pattern.

asmarmomand avatar Oct 06 '26 21:10 asmarmomand

I tried this on Windows 11 and couldn't reproduce the hang.

Setup: Windows 11 build 26300, WebView2 157.0.4341.46, tauri dev at c42354b (the core crates are identical to the 2.12.6 release), examples/isolation built with cargo build --example isolation --features isolation, both debug and --release. I attached over CDP (WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS=--remote-debugging-port=...) and read the values in each frame:

frame self.origin isSecureContext typeof crypto.subtle
main (http://tauri.localhost) http://tauri.localhost true object
isolation iframe (sandbox="allow-scripts") null true object

So the iframe does get an opaque origin, but here it is still a secure context, __TAURI_ISOLATION_READY__ arrives, and invoke("ping") resolves.

When I force crypto.subtle to be undefined inside the isolation iframe (injected over CDP before the document runs), I get the failure mode described above: the iframe throws, the ready message never arrives, and invoke stays pending with nothing on the Rust side.

Output with crypto.subtle forced to undefined

Injected into the isolation iframe:

Object.defineProperty(Crypto.prototype, 'subtle', { get() { return undefined } })
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'importKey')
    at http://isolation-b4a40420-d40f-4a09-b4f8-b158a088e9af.localhost/:33:48
    at http://isolation-b4a40420-d40f-4a09-b4f8-b158a088e9af.localhost/:198:3
...
invoke("ping"): still pending after 5000 ms

So the part that differs is whether the opaque-origin iframe counts as a secure context in your environment. @asmarmomand could you post the full tauri info output (mainly the WebView2 version), and check whether examples/isolation from this repo hangs for you as well?

Assertive-xxh avatar Nov 18 '26 05:11 Assertive-xxh