Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: let browser pages open target=_blank links and show alerts (#14360)

Daniel Han18743904c324
diff --git a/studio/frontend/tests/browser-native-page-plugin-scripts.test.ts b/studio/frontend/tests/browser-native-page-plugin-scripts.test.ts
new file mode 100644
index 00000000000..bcaecc69f14
--- /dev/null
+++ b/studio/frontend/tests/browser-native-page-plugin-scripts.test.ts
@@ -0,0 +1,103 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import assert from "node:assert/strict";
+import { test } from "node:test";
+import { runInNewContext } from "node:vm";
+import { readText } from "./helpers/kit.ts";
+
+const rust = readText("../../src-tauri/src/browser_webview.rs");
+const guard = rust.split('const PAGE_GUARD_SCRIPT: &str = r#"')[1].split('"#;')[0];
+const restore = rust.split('const PAGE_RESTORE_SCRIPT: &str = "')[1].split('";')[0];
+
+// Init scripts of opener 2.5.4, dialog 2.7.2, notification 2.3.3 (Cargo.lock), wrapped as Tauri does.
+const PLUGINS = [
+ `window.addEventListener("click",function(e){if(e.defaultPrevented||0!==e.button||e.metaKey||e.altKey)return;const t=e.composedPath().find(e=>e instanceof Node&&"A"===e.nodeName.toUpperCase());if(!t||!t.href||"_blank"!==t.target&&!e.ctrlKey&&!e.shiftKey)return;const n=new URL(t.href);["http:","https:","mailto:","tel:"].every(e=>n.protocol!==e)||(e.preventDefault(),window.__TAURI_INTERNALS__.invoke("plugin:opener|open_url",{url:n}))})`,
+ `window.alert=function(i){window.__TAURI_INTERNALS__.invoke("plugin:dialog|message",{message:i.toString()})},window.confirm=async function(i){return await window.__TAURI_INTERNALS__.invoke("plugin:dialog|confirm",{message:i.toString()})}`,
+ `window.Notification=function(n,t){window.__TAURI_INTERNALS__.invoke("plugin:notification|notify",{options:{title:n}})}`,
+].map((script) => `(function () { ${script} })();`);
+
+function nativeFn(name: string) {
+ return { [name]() {} }[name];
+}
+
+function loadDocument(label: string, { guarded }: { guarded: boolean }) {
+ const target = new EventTarget();
+ const invoked: string[] = [];
+ class Node {}
+ const window: Record<string, unknown> = {
+ URL,
+ Node,
+ Event,
+ alert: nativeFn("alert"),
+ confirm: nativeFn("confirm"),
+ Notification: nativeFn("Notification"),
+ addEventListener: target.addEventListener.bind(target),
+ removeEventListener: target.removeEventListener.bind(target),
+ dispatchEvent: target.dispatchEvent.bind(target),
+ __TAURI_INTERNALS__: {
+ metadata: { currentWebview: { label } },
+ invoke: async (command: string) => void invoked.push(command),
+ },
+ };
+ window.window = window;
+ const natives = { alert: window.alert, confirm: window.confirm, Notification: window.Notification };
+ const run = (script: string) => runInNewContext(script, window);
+ if (guarded) run(guard);
+ PLUGINS.forEach(run);
+ if (guarded && label.startsWith("unsloth-browser-")) run(restore);
+ const click = (href: string, targetAttr: string) =>
+ run(`(() => {
+ const link = Object.assign(new Node(), { nodeName: "A", href: ${JSON.stringify(href)}, target: ${JSON.stringify(targetAttr)} });
+ const event = new Event("click", { cancelable: true });
+ Object.defineProperties(event, { button: { value: 0 }, composedPath: { value: () => [link] } });
+ window.dispatchEvent(event);
+ return event.defaultPrevented;
+ })()`) as boolean;
+ return { window, natives, invoked, click, run };
+}
+
+test("unguarded, a page's target=_blank clicks are cancelled and alert is a refused command", () => {
+ const page = loadDocument("unsloth-browser-tab1", { guarded: false });
+ assert.equal(page.click("https://example.org/", "_blank"), true);
+ assert.notEqual(page.window.alert, page.natives.alert);
+ (page.window.alert as (message: string) => void)("hi");
+ assert.deepEqual(page.invoked, ["plugin:opener|open_url", "plugin:dialog|message"]);
+});
+
+test("a browser page keeps its own target=_blank clicks, alert, confirm and Notification", () => {
+ const page = loadDocument("unsloth-browser-tab1", { guarded: true });
+ assert.equal(page.click("https://example.org/", "_blank"), false);
+ assert.equal(page.window.alert, page.natives.alert);
+ assert.equal(page.window.confirm, page.natives.confirm);
+ assert.equal(page.window.Notification, page.natives.Notification);
+ assert.equal("__unslothPageGuard" in page.window, false);
+ assert.deepEqual(page.invoked, []);
+ page.run(`window.alert = () => { window.ownAlert = true; }; alert("x");
+ window.addEventListener("click", () => { window.ownClick = true; });`);
+ page.click("https://example.org/", "");
+ assert.equal(page.window.ownAlert, true);
+ assert.equal(page.window.ownClick, true);
+});
+
+test("the app's own webview keeps the plugins", () => {
+ const main = loadDocument("main", { guarded: true });
+ assert.equal(main.click("https://example.org/", "_blank"), true);
+ assert.notEqual(main.window.alert, main.natives.alert);
+ assert.equal("__unslothPageGuard" in main.window, false);
+ assert.deepEqual(main.invoked, ["plugin:opener|open_url"]);
+});
+
+test("the guard plugin is registered before every plugin that injects page scripts", () => {
+ const main = readText("../../src-tauri/src/main.rs");
+ const at = (needle: string) => {
+ const index = main.indexOf(needle);
+ assert.ok(index >= 0, needle);
+ return index;
+ };
+ const guardAt = at(".plugin(browser_webview::page_guard_plugin())");
+ for (const plugin of ["tauri_plugin_opener::", "tauri_plugin_dialog::", "tauri_plugin_notification::"]) {
+ assert.ok(guardAt < at(`.plugin(${plugin}`), plugin);
+ }
+ assert.match(rust, /\.initialization_script\(PAGE_RESTORE_SCRIPT\)/);
+});
diff --git a/studio/src-tauri/src/browser_webview.rs b/studio/src-tauri/src/browser_webview.rs
index 34e32d44c00..710fd4338da 100644
--- a/studio/src-tauri/src/browser_webview.rs
+++ b/studio/src-tauri/src/browser_webview.rs
@@ -92,6 +92,38 @@ const MUTE_SCRIPT: &str = r#"((muted) => {
window[key](muted);
})"#;
+/// Pages get every plugin's init script too, but their commands are refused there (opener eats `_blank`
+/// clicks, dialog/notification stub `alert`/`confirm`/`Notification`); `PAGE_RESTORE_SCRIPT` undoes them.
+const PAGE_GUARD_SCRIPT: &str = r#"(() => {
+ const label = window.__TAURI_INTERNALS__?.metadata?.currentWebview?.label;
+ if (typeof label !== "string" || !label.startsWith("unsloth-browser-")) return;
+ const kept = ["alert", "confirm", "Notification", "addEventListener"]
+ .map((name) => [name, Object.getOwnPropertyDescriptor(window, name)]);
+ const added = [];
+ const add = window.addEventListener;
+ window.addEventListener = function (...args) {
+ added.push(args);
+ return add.apply(this, args);
+ };
+ Object.defineProperty(window, "__unslothPageGuard", {
+ configurable: true,
+ value: () => {
+ delete window.__unslothPageGuard;
+ for (const [name, own] of kept) own ? Object.defineProperty(window, name, own) : delete window[name];
+ for (const [type, listener, options] of added) window.removeEventListener(type, listener, options);
+ },
+ });
+})();"#;
+
+const PAGE_RESTORE_SCRIPT: &str = "window.__unslothPageGuard?.();";
+
+/// Registered before the opener, dialog and notification plugins (`PAGE_GUARD_SCRIPT`).
+pub fn page_guard_plugin<R: Runtime>() -> tauri::plugin::TauriPlugin<R> {
+ tauri::plugin::Builder::new("unsloth-page-guard")
+ .js_init_script(PAGE_GUARD_SCRIPT.to_string())
+ .build()
+}
+
/// Read after a load or title change. Pages can spoof it; it only feeds the panel's buttons.
const STATE_SCRIPT: &str = r#"(() => {
try {
@@ -1222,6 +1254,7 @@ async fn create_view<R: Runtime>(
}
_ => false,
})
+ .initialization_script(PAGE_RESTORE_SCRIPT)
.zoom_hotkeys_enabled(false)
.devtools(cfg!(debug_assertions))
.focused(false);
diff --git a/studio/src-tauri/src/main.rs b/studio/src-tauri/src/main.rs
index 3ba50139424..b3663233b75 100644
--- a/studio/src-tauri/src/main.rs
+++ b/studio/src-tauri/src/main.rs
@@ -2349,6 +2349,7 @@ fn main() {
show_main_window(app);
}))
.plugin(webview_cache_plugin())
+ .plugin(browser_webview::page_guard_plugin())
.plugin(tauri_plugin_deep_link::init())
.plugin(tauri_plugin_autostart::init(
tauri_plugin_autostart::MacosLauncher::LaunchAgent,