fix(webui): gate website preview actions by browser capability

This commit is contained in:
Xubin Ren committed 2026-10-04 10:26:47 +08:00
1 parent 4b4b5cfa9f
commit bcd0423ce3
7 files changed
+68 -10

No files matched your search

+8
View File
@@ -180,6 +180,14 @@ File previews follow the active topic's access mode. Restricted workspace access
previews only files under the selected workspace. Full Access can preview files
outside the workspace when that access mode is allowed by the gateway.
Website links normally open in a new browser tab. Right-click a link (or press
**Shift+F10** while it is focused) for link actions. On mobile, use the reply's
**Message actions → View links** menu. **Preview website** appears only when the
current browser and URL allow credential-isolated embedding; otherwise the menu
explains the restriction and keeps **Open in browser** and **Copy link** available.
Sites can still refuse embedding through their own security headers. nanobot
does not proxy pages or bypass those restrictions; use **Open in browser** instead.
Open the context indicator beside the composer model badge to see how much of
the model's context window is in use. The **Recent rounds** chart shows input
tokens for each logical model round, including tool-call rounds. Hover or focus
+4 -2
View File
@@ -5,7 +5,7 @@ import { useTranslation } from "react-i18next";
import { WebPreviewContext } from "@/components/WebLink";
import { floatingItemClassName, floatingItemFocusClassName } from "@/components/ui/floating-surface";
import { copyTextToClipboard } from "@/lib/clipboard";
import { parseWebLink } from "@/lib/web-preview";
import { currentWebPreviewRestriction, parseWebLink } from "@/lib/web-preview";
interface MessageWebLink { href: string; title: string }
@@ -72,6 +72,7 @@ function MessageLinkActions({ link, onClose, onPreview }: {
onPreview?: () => void;
}) {
const { t } = useTranslation();
const restriction = onPreview ? currentWebPreviewRestriction(parseWebLink(link.href)) : null;
const [feedback, setFeedback] = useState<"copied" | "copyFailed" | null>(null);
const mounted = useRef(false);
useLayoutEffect(() => {
@@ -84,9 +85,10 @@ function MessageLinkActions({ link, onClose, onPreview }: {
};
return <div className="border-t border-border/45 pt-1">
<p className="max-h-24 overflow-y-auto px-2.5 py-2 text-xs text-muted-foreground [overflow-wrap:anywhere]" dir="auto">{link.href}</p>
{onPreview ? <button type="button" className={itemClassName} onClick={onPreview}>{t("webPreview.open")}</button> : null}
{onPreview && !restriction ? <button type="button" className={itemClassName} onClick={onPreview}>{t("webPreview.open")}</button> : null}
<a className={itemClassName} href={link.href} target="_blank" rel="noreferrer noopener" onClick={onClose}>{t("webPreview.external")}</a>
<button type="button" className={itemClassName} onClick={() => { void copy(); }}>{t("webPreview.copy")}</button>
{restriction ? <p className="px-2.5 py-1.5 text-xs leading-relaxed text-muted-foreground">{t(`webPreview.${restriction}`)}</p> : null}
{feedback ? <p role="status" className="px-2.5 py-1.5 text-xs text-muted-foreground">{t(`webPreview.${feedback}`)}</p> : null}
</div>;
}
+4 -2
View File
@@ -5,7 +5,7 @@ import { useTranslation } from "react-i18next";
import { floatingItemClassName, floatingItemFocusClassName, floatingSurfaceClassName, floatingSurfaceMotionClassName } from "@/components/ui/floating-surface";
import { useFloatingPortal } from "@/components/ui/floating-portal";
import { copyTextToClipboard } from "@/lib/clipboard";
import { parseWebLink } from "@/lib/web-preview";
import { currentWebPreviewRestriction, parseWebLink } from "@/lib/web-preview";
import { cn } from "@/lib/utils";
export const WebPreviewContext = createContext<((url: string) => void) | undefined>(undefined);
@@ -28,6 +28,7 @@ function WebsiteLink({ href, children, className, ...props }: ComponentPropsWith
const openPreview = useContext(WebPreviewContext);
const portal = useFloatingPortal();
const [open, setOpen] = useState(false);
const restriction = open && openPreview ? currentWebPreviewRestriction(parseWebLink(href)) : null;
const [feedback, setFeedback] = useState<"copied" | "copyFailed" | null>(null);
const link = useRef<HTMLAnchorElement>(null);
const point = useRef({ x: 0, y: 0 });
@@ -94,11 +95,12 @@ function WebsiteLink({ href, children, className, ...props }: ComponentPropsWith
event.preventDefault();
if (!interactedOutside.current) link.current?.focus();
}}>
{openPreview ? <Menu.Item className={itemClassName} onSelect={() => openPreview(href)}>{t("webPreview.open")}</Menu.Item> : null}
{openPreview && !restriction ? <Menu.Item className={itemClassName} onSelect={() => openPreview(href)}>{t("webPreview.open")}</Menu.Item> : null}
<Menu.Item asChild className={itemClassName}>
<a href={href} target="_blank" rel="noreferrer noopener">{t("webPreview.external")}</a>
</Menu.Item>
<Menu.Item className={itemClassName} onSelect={(event) => { event.preventDefault(); void copy(); }}>{t("webPreview.copy")}</Menu.Item>
{restriction ? <p className="max-w-64 px-2.5 py-1.5 text-xs leading-relaxed text-muted-foreground">{t(`webPreview.${restriction}`)}</p> : null}
{feedback === "copyFailed" ? <div role="status" className="max-w-64 px-2.5 py-1.5 text-xs text-muted-foreground">{t("webPreview.copyFailed")}</div> : null}
</Menu.Content>
</Menu.Portal>
+2 -4
View File
@@ -4,8 +4,7 @@ import { useTranslation } from "react-i18next";
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
import { isLoopbackHost } from "@/lib/network";
import { isNativeRuntime } from "@/lib/runtime";
import { parseWebLink, webPreviewRestriction } from "@/lib/web-preview";
import { currentWebPreviewRestriction, parseWebLink } from "@/lib/web-preview";
interface WebPreviewPanelProps {
url: string;
@@ -15,8 +14,7 @@ export function WebPreviewPanel({ url: value }: WebPreviewPanelProps) {
const { t } = useTranslation();
const [revision, setRevision] = useState(0);
const url = parseWebLink(value);
const restriction = url ? webPreviewRestriction(url, new URL(window.location.href), isNativeRuntime(),
"credentialless" in HTMLIFrameElement.prototype) : "invalid";
const restriction = currentWebPreviewRestriction(url);
const buttonClass = "inline-flex h-8 w-8 shrink-0 items-center justify-center rounded-md text-muted-foreground hover:bg-muted hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring";
return (
<section aria-label={t("webPreview.title")} data-testid="web-preview-panel" className="flex min-h-0 flex-1 flex-col">
+8
View File
@@ -1,3 +1,5 @@
import { isNativeRuntime } from "@/lib/runtime";
/** Only explicit web URLs belong in the web-link menu; file/session links have other owners. */
export function parseWebLink(value: string): URL | null {
if (value.length > 8192 || value.includes("\\")
@@ -22,3 +24,9 @@ export function webPreviewRestriction(
if (pageUrl.protocol === "https:" && url.protocol === "http:") return "mixedContent";
return null;
}
/** Menus and restored preview tabs must agree on the current browser's boundary. */
export function currentWebPreviewRestriction(url: URL | null) {
return url ? webPreviewRestriction(url, new URL(window.location.href), isNativeRuntime(),
"credentialless" in HTMLIFrameElement.prototype) : "invalid";
}
+27 -2
View File
@@ -1,13 +1,21 @@
import { act, cleanup, fireEvent, render, screen, waitFor, within } from "@testing-library/react";
import { useRef } from "react";
import { afterEach, describe, expect, it, vi } from "vitest";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { ThreadMessages } from "@/components/thread/ThreadMessages";
import { WebLink, WebPreviewContext } from "@/components/WebLink";
import { useMessageWebLinks } from "@/components/MessageLinksMenu";
import { copyTextToClipboard } from "@/lib/clipboard";
vi.mock("@/lib/clipboard", () => ({ copyTextToClipboard: vi.fn(async () => true) }));
afterEach(() => { cleanup(); vi.unstubAllGlobals(); vi.mocked(copyTextToClipboard).mockReset().mockResolvedValue(true); });
const credentialless = Object.getOwnPropertyDescriptor(HTMLIFrameElement.prototype, "credentialless");
beforeEach(() => {
Object.defineProperty(HTMLIFrameElement.prototype, "credentialless", { configurable: true, value: false });
});
afterEach(() => {
cleanup(); vi.unstubAllGlobals(); vi.mocked(copyTextToClipboard).mockReset().mockResolvedValue(true);
if (credentialless) Object.defineProperty(HTMLIFrameElement.prototype, "credentialless", credentialless);
else Reflect.deleteProperty(HTMLIFrameElement.prototype, "credentialless");
});
function view(content: string, openPreview?: (url: string) => void) {
return <WebPreviewContext.Provider value={openPreview}>
@@ -73,6 +81,23 @@ describe("message link actions", () => {
if (!screen.queryByRole("dialog")) fireEvent.click(screen.getByRole("button", { name: "Message actions" }));
expect(screen.getAllByRole("dialog")).toHaveLength(1);
});
it("keeps mobile users in the link sheet when isolated previews are unsupported", async () => {
mobileViewport();
Reflect.deleteProperty(HTMLIFrameElement.prototype, "credentialless");
const preview = vi.fn();
render(view("[Website](https://example.com/demo)", preview));
await screen.findByRole("link", { name: /Website/ });
const menu = await openLinks();
expect(within(menu).queryByRole("button", { name: "Preview website" })).not.toBeInTheDocument();
expect(within(menu).getByText(/does not support credential-isolated/)).toBeVisible();
const external = within(menu).getByRole("link", { name: "Open in browser" });
expect(external).toHaveAttribute("href", "https://example.com/demo");
expect(external).toHaveAttribute("rel", "noreferrer noopener");
fireEvent.click(within(menu).getByRole("button", { name: "Copy link" }));
await waitFor(() => expect(copyTextToClipboard).toHaveBeenLastCalledWith("https://example.com/demo"));
expect(preview).not.toHaveBeenCalled();
expect(screen.getByRole("dialog")).toBe(menu);
});
it("keeps user actions outside the bubble and exposes a labelled mobile fork action", async () => {
mobileViewport();
+15
View File
@@ -116,6 +116,21 @@ describe("web link actions", () => {
fireEvent.click(screen.getByRole("menuitem", { name: "Copy link" }));
await waitFor(() => expect(screen.queryByRole("menu")).not.toBeInTheDocument());
});
it.each(["unsupported", "native", "sameOrigin"])("omits unusable preview actions for %s links while keeping external open and copy", async (reason) => {
if (reason === "unsupported") Reflect.deleteProperty(HTMLIFrameElement.prototype, "credentialless");
if (reason === "native") vi.mocked(isNativeRuntime).mockReturnValue(true);
const href = reason === "sameOrigin" ? `${window.location.origin}/settings` : "https://example.com/demo";
const open = vi.fn();
render(<WebPreviewContext.Provider value={open}><WebLink href={href}>Example</WebLink></WebPreviewContext.Provider>);
fireEvent.contextMenu(screen.getByRole("link", { name: "Example" }));
expect(screen.queryByRole("menuitem", { name: "Preview website" })).not.toBeInTheDocument();
expect(screen.getByRole("menuitem", { name: "Open in browser" })).toHaveAttribute("href", href);
const explanation = reason === "unsupported" ? /does not support credential-isolated/ : reason === "native" ? /native host/ : /application itself cannot be embedded/;
expect(screen.getByText(explanation)).toBeVisible();
fireEvent.click(screen.getByRole("menuitem", { name: "Copy link" }));
await waitFor(() => expect(copyTextToClipboard).toHaveBeenLastCalledWith(href));
expect(open).not.toHaveBeenCalled();
});
it("ignores a late copy completion after the user reopens the menu", async () => {
let finish!: (result: boolean) => void;
vi.mocked(copyTextToClipboard).mockImplementationOnce(() => new Promise(resolve => { finish = resolve; }));