tauri: Trying to check if an object is an instance of SharedArrayBuffer throws error.

Describe the bug During runtime Tauri interprets SharedArrayBuffer as a variable when checking instance types using instanceof. For example this code:

console.log({} instanceof SharedArrayBuffer);

would throw :

Unhandled Promise Rejection: ReferenceError: Can't find variable: SharedArrayBuffer

instead of printing false to the console.

To Reproduce

  1. Run the code console.log({} instanceof SharedArrayBuffer);
  2. Throws error

Expected behavior Should just print false

Platform and Versions (please complete the following information):

OS: Ubuntu 20.10 Node: v15.3.0 NPM: 7.0.14 Rustc: 1.50.0

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Reactions: 6
  • Comments: 21 (15 by maintainers)

Most upvoted comments

I just tested on Ubuntu 22.04 with libwebkit2gtk-4.0-dev/jammy,now 2.36.0-2ubuntu1. Using the localhost plugin (https://github.com/tauri-apps/tauri/issues/3917#issuecomment-1104433755), I can get window.crossOriginIsolated to be true, but SharedArrayBuffer is still not available.

I tried to set the headers on macOS and window.crossOriginIsolated is still false.

WindowBuilder::new(
        app,
        "main",
        tauri::WindowUrl::App("index.html".into()),
      )
      .title("Tauri")
      .on_web_resource_request(|req, resp| {
        if req.uri().starts_with("tauri://") {
          resp.headers_mut().insert(
            "Cross-Origin-Opener-Policy",
            "same-origin".try_into().unwrap(),
          );
          resp.headers_mut().insert(
            "Cross-Origin-Embedder-Policy",
            "require-corp".try_into().unwrap(),
          );
          println!("done");
        }
      })
      .build()?;

This might not be supported on webviews at all.

This solution can be used on Windows though.

This is the app I’m using for testing: https://github.com/pwespi/tauri-app-shared-array-buffer (uses the localhost plugin).

The frontend part of it is deployed to https://tauri-app-shared-array-buffer.vercel.app/, such that it can also be used to test browsers.

Notable results:

Test SharedArrayBuffer available
Tauri App on Ubuntu 22.04.3, WebKitGTK 2.42.3 false
Epiphany Browser on Ubuntu 22.04.3, WebKitGTK 2.42.3 false
Tauri App on macOS 14.2.1 true
Safari on macOS 14.2.1 true
Safari on iOS 17.2.1 true

WebKit/WebKit#8578 (comment) describes the implementation and platform support surface attached to the merged changes that enable access to SharedArrayBuffer Objects docs using Atomics.waitAsync() docs on the main thread running JavaScriptCore.

Might be worth re-running tests on latest release of iOS, OSX and a linux build with updated WebKitGTK

I still see false printed to the console. I am on windows though. I will have to test it on my linux machine later.

Current state:

  • I can not make window.crossOriginIsolated to be true despite the headers being correctly set in production (using the on_web_resource_request approach). I’m on MacOS 14.5.
image
  • Running the app with __XPC_JSC_useSharedArrayBuffer=1 force-enables SAB, but window.crossOriginIsolated stays false. The option is not viable for mass-production apps (users won’t run the app via CLI).

  • Using the localhost plugin works, but it emits this ugly “Allow incoming connections?” window.

after setting COOP/COEP headers, start app with env: __XPC_JSC_useSharedArrayBuffer=1 on MacOS will do the trick.

Supporting SharedArrayBuffer would unblock access to packages like:

Does anyone have any updates or workarounds?

I guess that it was removed due to Spectre (note the deprecation of high-resolution timers too). We should probably nudge the webkit devs to look into re-enabling.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer#security_requirements

This is definitely upstream and not anything either Tauri nor WRY can do anything about. Sorry