Turn any website into a lightweight desktop app with one command, on macOS, Windows and Linux.
Rust chatgptclaudeclidesktopdesktop-appelectron-alternativegeminilinuxmacosnativefierno-electronrusttauriweb-to-desktopwebsite-to-appwebviewwindowsyoutube
fix: open a sign-in popup at its address so it keeps its opener
Tw93d95ac6f4a0cf
diff --git a/src-tauri/src/app/window.rs b/src-tauri/src/app/window.rsindex eaf42be8..2cb3bb59 100644--- a/src-tauri/src/app/window.rs+++ b/src-tauri/src/app/window.rs@@ -244,11 +244,61 @@ fn is_blank_popup_url(url: &Url) -> bool {url.scheme() == "about" && url.path() == "blank"}+/// The address a popup's web view is built with. WKWebView and WebView2 load+/// a popup's address themselves into the web view they are handed, as the+/// popup's first navigation; given the address as well, Wry loads it a second+/// time as the app's own navigation, which moves the page to another process+/// and drops `window.opener` (measured on macOS 26.7: a sign-in popup opened+/// at its address reported no opener). `about:blank` is the one address+/// Tauri does not load. Linux keeps its address: not measured there.+fn popup_build_url(target_url: &Url) -> Url {+ if cfg!(any(target_os = "macos", target_os = "windows")) {+ Url::parse("about:blank").expect("about:blank must be a valid URL")+ } else {+ target_url.clone()+ }+}++/// Whether a page's new-window request gets a window of its own: always with+/// `--new-window`, and without it for a blank popup and for one that asks for+/// a size.+fn builds_requested_popup(allow_new_window: bool, target_url: &Url, sized: bool) -> bool {+ allow_new_window || is_blank_popup_url(target_url) || sized+}+#[cfg(test)]mod popup_tests {- use super::is_blank_popup_url;+ use super::{builds_requested_popup, is_blank_popup_url, popup_build_url};use tauri::Url;+ #[test]+ fn a_popup_web_view_is_built_blank_so_the_engine_loads_its_address_once() {+ let google = Url::parse("https://accounts.google.com/gsi/select?client_id=1").unwrap();+ let built = popup_build_url(&google);+ if cfg!(any(target_os = "macos", target_os = "windows")) {+ // A second, app-made load of the address drops window.opener.+ assert_eq!(built.as_str(), "about:blank");+ } else {+ assert_eq!(built, google);+ }+ }++ #[test]+ fn a_sized_popup_is_built_at_its_address_without_new_window() {+ // X signs in with Google through a sized popup. Built blank and sent+ // to its address afterwards, it lost its opener to X's+ // Cross-Origin-Opener-Policy and ended on a blank page.+ let google = Url::parse("https://accounts.google.com/gsi/select?client_id=1").unwrap();+ assert!(builds_requested_popup(false, &google, true));+ assert!(!builds_requested_popup(false, &google, false));+ assert!(builds_requested_popup(true, &google, false));+ assert!(builds_requested_popup(+ false,+ &Url::parse("about:blank").unwrap(),+ false+ ));+ }+#[test]fn only_blank_documents_get_the_default_popup_exception() {for input in ["about:blank", "about:blank#download", "about:blank?pending"] {@@ -280,7 +330,7 @@ fn open_requested_window(tauri_config,WindowBuildOptions {label: &label,- url: WebviewUrl::External(target_url.clone()),+ url: WebviewUrl::External(popup_build_url(&target_url)),visible: true,new_window_features: Some(features),},@@ -685,7 +735,11 @@ fn build_window(window_builder = window_builder.on_new_window(move |target_url, features| {// Even without --new-window, two-stage popups need a real webview// with our download delegate, never a proxy for the main window.- if !allow_new_window && !is_blank_popup_url(&target_url) {+ // So does a popup that asks for a size: the injected scripts send+ // only a sign-in popup or one of the app's own site here sized,+ // and it must start at its address to keep its opener (WebKitGTK+ // reports no size, so Linux is unchanged).+ if !builds_requested_popup(allow_new_window, &target_url, features.size().is_some()) {return NewWindowResponse::Deny;}match open_requested_window(diff --git a/src-tauri/src/inject/event.js b/src-tauri/src/inject/event.jsindex 07146608..651e807e 100644--- a/src-tauri/src/inject/event.js+++ b/src-tauri/src/inject/event.js@@ -1044,13 +1044,12 @@ document.addEventListener("DOMContentLoaded", () => {(window.isAuthPopup(popupUrl, name) ||(!window.pakeConfig?.new_window && ownSite))) {- const popup = openPopupThroughBlank(- originalWindowOpen,- window,- popupUrl,- name,- specs,- );+ // Straight to its address, as a browser opens it: the native handler+ // builds a sized popup under every setting. Opening it blank first+ // and then sending it there lost the opener on sites that set+ // Cross-Origin-Opener-Policy (X does): WebKit keeps a popup with its+ // opener across that policy only on the popup's first navigation.+ const popup = originalWindowOpen.call(window, popupUrl, name, specs);if (popup) return popup;}}diff --git a/src-tauri/src/inject/frame_links.js b/src-tauri/src/inject/frame_links.jsindex abc7d391..4521cc31 100644--- a/src-tauri/src/inject/frame_links.js+++ b/src-tauri/src/inject/frame_links.js@@ -54,17 +54,18 @@url = normalizePopupUrl(url);const popupUrl = popupDestination(url, name, specs);if (popupUrl) {- const popup = openPopupThroughBlank(- originalOpen,- window,- popupUrl,- name,- specs,- );+ // Straight to its address: see the main page's route in event.js.+ const popup = originalOpen.call(window, popupUrl, name, specs);if (popup) return popup;}// Named targets can navigate an existing frame and must retain its proxy.- if (name && String(name).toLowerCase() !== "_blank") {+ // A sized one is a popup instead, and the native handler now builds every+ // sized popup, so one to another site takes the external route below.+ if (+ name &&+ String(name).toLowerCase() !== "_blank" &&+ !(opensSignInPopups() && requestsPopupWindow(name, specs))+ ) {return originalOpen.call(window, url, name, specs);}const external = externalDestination(url, name);diff --git a/src-tauri/src/inject/link_policy.js b/src-tauri/src/inject/link_policy.jsindex 0bfe08c2..0fb802f0 100644--- a/src-tauri/src/inject/link_policy.js+++ b/src-tauri/src/inject/link_policy.js@@ -49,27 +49,6 @@ function opensSignInPopups() {return !/linux/i.test(platform);}-// The native handler builds a blank popup under every setting and on every-// platform, with its opener and Pake's download delegate; a popup that starts-// at its address exists only with --new-window. So a sign-in popup starts-// blank and is sent to its address, the two-stage form sites use themselves.-function openPopupThroughBlank(nativeOpen, opener, url, name, specs) {- const popup = nativeOpen.call(opener, "about:blank", name, specs);- if (!popup) return null;- try {- popup.location.href = url;- } catch (error) {- console.error("[Pake] Failed to send a popup to its address:", error);- try {- popup.close();- } catch (closeError) {- // The caller falls back to its previous route either way.- }- return null;- }- return popup;-}-// This list intentionally preserves Pake's existing domain routing policy.const MULTI_PART_PUBLIC_SUFFIXES = ["co.uk",diff --git a/tests/integration/signin-popup-server.mjs b/tests/integration/signin-popup-server.mjsindex 8b1f8f14..2b956d9d 100644--- a/tests/integration/signin-popup-server.mjs+++ b/tests/integration/signin-popup-server.mjs@@ -66,6 +66,13 @@ f.onclick=()=>{const p=window.open('/o/oauth2/v2/auth?mode=frame','g_frame_windoif(p){const t=setInterval(()=>{if(p.closed){parent.postMessage({frameClosed:true},'*');clearInterval(t)}},300)}};</script>`;+// The app's page carries the opener policy x.com sends. A popup keeps its+// opener across it only when its first navigation is the popup's address, so+// a runtime that opens the popup blank and sends it there afterwards, or that+// loads the address a second time itself, ends G and F with "provider+// opener=no" and no answer. The callback page sends no policy: a popup that+// comes back from the provider to a page with one loses its opener in any+// browser, so sites that sign in this way do not send it there.function send(response, body, status = 200, headers = {}) {response.writeHead(status, {"Content-Type": "text/html; charset=utf-8",@@ -74,6 +81,9 @@ function send(response, body, status = 200, headers = {}) {});response.end(body);}+const appHeaders = {+ "Cross-Origin-Opener-Policy": "same-origin-allow-popups",+};const selfClosePage = `<!doctype html><meta charset=utf-8><title>S closing</title><script>window.close();@@ -82,7 +92,7 @@ setTimeout(()=>{document.title='S still here'},1500);const routes = {[APP]: {- "/": (response) => send(response, appPage),+ "/": (response) => send(response, appPage, 200, appHeaders),"/popup-start": (response) =>send(response, "", 302, {Location: `http://127.0.0.1:${PROVIDER}/o/oauth2/v2/auth?redirect_uri=${encodeURIComponent(`http://localhost:${APP}/callback?via=n`)}`,diff --git a/tests/unit/event-link-guard.test.js b/tests/unit/event-link-guard.test.jsindex ed503401..eff09225 100644--- a/tests/unit/event-link-guard.test.js+++ b/tests/unit/event-link-guard.test.js@@ -321,10 +321,11 @@ describe("event link guard", () => {GOOGLE_SPECS,);expect(result).toBe(popup);+ // Straight to its address: a popup opened blank and sent there+ // afterwards loses its opener on a site with an opener policy.expect(nativeOpen.mock.calls).toEqual([- ["about:blank", "g_auth_token_window", GOOGLE_SPECS],+ [GOOGLE_POPUP, "g_auth_token_window", GOOGLE_SPECS],]);- expect(popup.location.href).toBe(GOOGLE_POPUP);expect(context.window.location.href).toBe("https://example.com/app");},);@@ -360,11 +361,7 @@ describe("event link guard", () => {"share","width=600,height=400",);- expect(widget.nativeOpen).not.toHaveBeenCalledWith(- "about:blank",- "share",- "width=600,height=400",- );+ expect(widget.nativeOpen).not.toHaveBeenCalled();const app = load();expect(@@ -375,9 +372,8 @@ describe("event link guard", () => {),).toBe(app.popup);expect(app.nativeOpen.mock.calls).toEqual([- ["about:blank", "flow", "width=600,height=400"],+ ["https://app.example.net/popup", "flow", "width=600,height=400"],]);- expect(app.popup.location.href).toBe("https://app.example.net/popup");});it("gives a sized popup of the app's own site its own window", () => {@@ -390,10 +386,13 @@ describe("event link guard", () => {specs,);expect(result).toBe(popup);- expect(nativeOpen.mock.calls).toEqual([["about:blank", " Google", specs]]);- expect(popup.location.href).toBe(- "https://example.com/verifyNoPopupBlockerHtmlAndRedirect?redirectUri=x",- );+ expect(nativeOpen.mock.calls).toEqual([+ [+ "https://example.com/verifyNoPopupBlockerHtmlAndRedirect?redirectUri=x",+ " Google",+ specs,+ ],+ ]);expect(context.window.location.href).toBe("https://example.com/app");});@@ -426,14 +425,14 @@ describe("event link guard", () => {},);- it("falls back to the current window when the blank popup is refused", () => {+ it("falls back to the current window when the popup is refused", () => {const { context, nativeOpen } = loadPopupPage(MAC);nativeOpen.mockReturnValue(null);expect(context.window.open(GOOGLE_POPUP, "oauth", GOOGLE_SPECS)).toBe(context.window,);expect(nativeOpen.mock.calls).toEqual([- ["about:blank", "oauth", GOOGLE_SPECS],+ [GOOGLE_POPUP, "oauth", GOOGLE_SPECS],]);expect(context.window.location.href).toBe(GOOGLE_POPUP);});diff --git a/tests/unit/frame-links.test.js b/tests/unit/frame-links.test.jsindex 2bfa1657..e1fa0573 100644--- a/tests/unit/frame-links.test.js+++ b/tests/unit/frame-links.test.js@@ -90,7 +90,6 @@ describe("subframe external links", () => {);it.each([- ["https://outside.example/article", "details", {}],["https://outside.example/article", "_self", {}],["https://outside.example/article", "_parent", {}],["https://outside.example/article", "_top", {}],@@ -144,15 +143,14 @@ describe("subframe external links", () => {{ new_window: true },],])(- "opens the sized popup %s (%s) blank and sends it to its address",+ "opens the sized popup %s (%s) at its address",(url, name, specs, config) => {const frame = loadFrame(config);expect(frame.window.open(url, name, specs)).toBe(frame.nativeWindow);- expect(frame.nativeOpen).toHaveBeenCalledOnce();- expect(frame.nativeOpen).toHaveBeenCalledWith("about:blank", name, specs);- expect(frame.nativeWindow.location.href).toBe(- new URL(url, "https://mail.example.com/app").href,- );+ // Not blank first: that lost the opener on a site with an opener policy.+ expect(frame.nativeOpen.mock.calls).toEqual([+ [new URL(url, "https://mail.example.com/app").href, name, specs],+ ]);expect(frame.messages).toEqual([]);},);@@ -193,9 +191,8 @@ describe("subframe external links", () => {frame.nativeWindow,);expect(frame.nativeOpen.mock.calls).toEqual([- ["about:blank", "oauth", "width = 400"],+ [url.href, "oauth", "width = 400"],]);- expect(frame.nativeWindow.location.href).toBe(url.href);});it("keeps a Linux frame on the native route", () => {@@ -205,17 +202,32 @@ describe("subframe external links", () => {expect(frame.nativeOpen.mock.calls).toEqual([[url, "oauth", "width=400"]]);});- it("falls back to the native route when the blank popup is refused", () => {+ it("sends a sized named popup of another site to the system browser", () => {+ // The native handler builds every sized popup, so this one must not reach it.const frame = loadFrame();- frame.nativeOpen.mockReturnValueOnce(null);- const url = "https://accounts.google.com/o/oauth2/auth";- expect(frame.window.open(url, "oauth", "width=400")).toBe(- frame.nativeWindow,- );+ expect(+ frame.window.open(+ "https://outside.example/article",+ "details",+ "width=400",+ ),+ ).toBeNull();+ expect(frame.nativeOpen).not.toHaveBeenCalled();+ expect(frame.messages).toEqual([+ {+ type: "pake:frame-external-link",+ url: "https://outside.example/article",+ },+ ]);+ });++ it("keeps an unsized named target on the native route", () => {+ const frame = loadFrame();+ frame.window.open("https://outside.example/article", "details");expect(frame.nativeOpen.mock.calls).toEqual([- ["about:blank", "oauth", "width=400"],- [url, "oauth", "width=400"],+ ["https://outside.example/article", "details", undefined],]);+ expect(frame.messages).toEqual([]);});it("forwards external anchors while preserving downloads and named frames", () => {diff --git a/tests/unit/new-window-macos.test.js b/tests/unit/new-window-macos.test.jsindex fde16258..a2ba3d64 100644--- a/tests/unit/new-window-macos.test.js+++ b/tests/unit/new-window-macos.test.js@@ -25,7 +25,10 @@ describe("macOS new-window handling (regression: #1194)", () => {// WKWebView that does not have Pake's runtime state.expect(newWindowBlock).toContain("open_requested_window");expect(newWindowBlock).toContain(- "!allow_new_window && !is_blank_popup_url(&target_url)",+ "!builds_requested_popup(allow_new_window, &target_url, features.size().is_some())",+ );+ expect(source).toContain(+ "allow_new_window || is_blank_popup_url(target_url) || sized",);expect(newWindowBlock).toContain("NewWindowResponse::Create");expect(newWindowBlock).not.toMatch(/NewWindowResponse::Allow\b/);