tauri-apps / tauri

Build smaller, faster, and more secure desktop and mobile applications with a web frontend.
https://tauri.app
Apache License 2.0
84.1k stars 2.53k forks source link

[bug] Inconsistent `register_uri_scheme_protocol` behaviour on Windows. Connection refused for requests from iframe. #11505

Open HuakunShen opened 1 day ago

HuakunShen commented 1 day ago

Describe the bug

Note: The error only happens on Windows.

What I was trying to do

I was trying to load a static website from local file system into a tauri app using iframe (for a plugin system).

To achieve this, I implemented a custom protocol with register_uri_scheme_protocol. What I did was basically implementing a file server to serve static html, css, js files (so I don't need to spawn another http file server).

How It Works

It works like this

import { convertFileSrc } from "@tauri-apps/api/core";
const iframeSrc = convertFileSrc("", "ext");
<iframe src={iframeSrc} title="iframe"></iframe>

This will try to load index.html with url

The index.html loads a main.js and a style.css. iframe automatically sends requests to fetch the 2 files

style.css sets text color to red. main.js changes text from External JavaScript File Not Loaded to External JavaScript Loaded.

Expected Behavior

On Mac, everything works perfectly

image

Problem

On Windows, only index.html is loaded. style.css and main.js fail to load.

image

In the console, there are 2 errors image

The requests to fetch style.css and main.js got connection refused. image

The 2 requests timed out after 2 seconds, while the first request to index.html resolves in 1ms.

Here are more info about the failed request

image image

What a successful request look like

image image

My Hypothesis

I thought there was something wrong with the implementation of the custom file server I implemented with Rust and tauri::http.

However, fetching main.js from Tauri app works perfectly.

fetch("http://ext.localhost/main.js")
      .then((res) => res.text())
      .then((text) => {
        console.log(text);
      });

And from the log message in my custom file server rust code, I can see that, there was only request to index.html, no request for style.css or main.js at all.

So tauri core basically denies the connection from iframe, but not from the window.

I don't know what the difference is between request from window and iframe, but this behavior is completely different from MacOS.

Reproduction

https://github.com/[HuakunShen/tauri-windows-uri-scheme-bug

To run it,

pnpm install
pnpm tauri dev

[!CAUTION] Error only happens on Windows. It should work on Mac.

Expected behavior

No response

Full tauri info output

[✔] Environment
    - OS: Windows 10.0.22631 x86_64 (X64)
    ✔ WebView2: 129.0.2792.89
    ✔ MSVC: 
        - Visual Studio Build Tools 2019
        - Visual Studio Community 2022
    ✔ rustc: 1.80.1 (3f5fd8dd4 2024-08-06)
    ✔ cargo: 1.80.1 (376290515 2024-07-16)
    ✔ rustup: 1.27.1 (54dd3d00f 2024-04-24)
    ✔ Rust toolchain: stable-x86_64-pc-windows-msvc (default)
    - node: 20.17.0
    - pnpm: 9.9.0
    - npm: 10.8.2
    - bun: 1.1.33
    - deno: deno 2.0.0

[-] Packages
    - tauri 🦀: 2.0.6
    - tauri-build 🦀: 2.0.2
    - wry 🦀: 0.46.3
    - tao 🦀: 0.30.3
    - @tauri-apps/api : 2.0.3
    - @tauri-apps/cli : 2.0.5

[-] Plugins
    - tauri-plugin-shell 🦀: 2.0.2
    - @tauri-apps/plugin-shell : 2.0.1

[-] App
    - build-type: bundle
    - CSP: unset
    - frontendDist: ../build
    - devUrl: http://localhost:1420/
    - framework: Svelte
    - bundler: Vite

Stack trace

No response

Additional context

This is very critical to my app, please help!

Thanks!

HuakunShen commented 1 day ago

Related issue and discusstion:

Seems like real custom protocol on Windows won't arrive in Tauri v2.

Tauri's custom protocol is implemented differently on Windows, it's not a real custom protocol. I don't know how ext.localhost is handled under the hood. I am guessing, requests from regular webview window is sent to Tauri core, while requests from iframe doesn't know where http://ext.localhost/ is.

Requests from iframe should be somehow redirected to Tauri core.