Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(observability): render markdown links as native anchors (LFE-10977) (#15245)

Nikita Kabardine988c28c714c
diff --git a/web/src/components/ui/MarkdownViewer.clienttest.tsx b/web/src/components/ui/MarkdownViewer.clienttest.tsx
new file mode 100644
index 0000000000..1277fc97a9
--- /dev/null
+++ b/web/src/components/ui/MarkdownViewer.clienttest.tsx
@@ -0,0 +1,96 @@
+import { render } from "@testing-library/react";
+import {
+ MarkdownView,
+ prependBasePathToInternalHref,
+} from "@/src/components/ui/MarkdownViewer";
+import { MarkdownContextProvider } from "@/src/features/theming/useMarkdownContext";
+
+vi.mock("next/router", () => ({
+ useRouter: () => ({ query: {} }),
+}));
+
+vi.mock("next-themes", () => ({
+ useTheme: () => ({ resolvedTheme: "light" }),
+}));
+
+vi.mock("@/src/features/posthog-analytics/usePostHogClientCapture", () => ({
+ usePostHogClientCapture: () => vi.fn(),
+}));
+
+const renderMarkdown = (markdown: string) =>
+ render(
+ <MarkdownContextProvider>
+ <MarkdownView markdown={markdown} />
+ </MarkdownContextProvider>,
+ );
+
+describe("MarkdownView link rendering", () => {
+ it("renders an external link as a native anchor opening in a new tab", () => {
+ const { container } = renderMarkdown("[example](https://example.com/page)");
+
+ const anchor = container.querySelector("a");
+ expect(anchor).not.toBeNull();
+ expect(anchor?.getAttribute("href")).toBe("https://example.com/page");
+ expect(anchor?.getAttribute("target")).toBe("_blank");
+ expect(anchor?.getAttribute("rel")).toBe("noopener noreferrer");
+ });
+
+ // Regression guard for the Sentry "Invalid href '…' passed to next/router"
+ // noise family (LANGFUSE-5DZ / 5EA / 5ER, ~40k lifetime events): user-content
+ // markdown embeds URLs that a Next.js <Link> rejects (repeated `//` from an
+ // embedded second `https://`, etc.). A native <a> never runs the router's
+ // href validation, so such a link must render without throwing — a
+ // regression to <Link> would throw here.
+ it("renders a malformed user URL as an anchor without throwing", () => {
+ const malformed =
+ "https://www.example.com/a%22,%22thumb%22:%22https://assets.example.com/b";
+
+ expect(() => renderMarkdown(`[bad](${malformed})`)).not.toThrow();
+
+ const { container } = renderMarkdown(`[bad](${malformed})`);
+ const anchor = container.querySelector("a");
+ expect(anchor).not.toBeNull();
+ expect(anchor?.getAttribute("target")).toBe("_blank");
+ });
+
+ it("renders an unsafe-protocol link as plain text, not an anchor", () => {
+ const { container } = renderMarkdown("[x](javascript:alert(1))");
+
+ expect(container.querySelector("a")).toBeNull();
+ expect(container.textContent).toContain("x");
+ });
+});
+
+describe("prependBasePathToInternalHref", () => {
+ // A native <a> loses the NEXT_PUBLIC_BASE_PATH that <Link> used to prepend to
+ // root-relative internal hrefs; this helper restores it for subpath deploys.
+ it("prepends the base path to a root-relative internal href", () => {
+ expect(
+ prependBasePathToInternalHref("/project/abc/traces/def", "/lf"),
+ ).toBe("/lf/project/abc/traces/def");
+ });
+
+ it("is a no-op when no base path is configured", () => {
+ expect(prependBasePathToInternalHref("/project/abc", "")).toBe(
+ "/project/abc",
+ );
+ });
+
+ it("leaves absolute URLs untouched", () => {
+ expect(
+ prependBasePathToInternalHref("https://example.com/page", "/lf"),
+ ).toBe("https://example.com/page");
+ expect(prependBasePathToInternalHref("mailto:[email protected]", "/lf")).toBe(
+ "mailto:[email protected]",
+ );
+ });
+
+ it("leaves protocol-relative, hash, search and dot-relative refs untouched", () => {
+ expect(prependBasePathToInternalHref("//evil.example.com", "/lf")).toBe(
+ "//evil.example.com",
+ );
+ expect(prependBasePathToInternalHref("#section", "/lf")).toBe("#section");
+ expect(prependBasePathToInternalHref("?tab=io", "/lf")).toBe("?tab=io");
+ expect(prependBasePathToInternalHref("./sibling", "/lf")).toBe("./sibling");
+ });
+});
diff --git a/web/src/components/ui/MarkdownViewer.tsx b/web/src/components/ui/MarkdownViewer.tsx
index 7a4cc7d537..2c67223897 100644
--- a/web/src/components/ui/MarkdownViewer.tsx
+++ b/web/src/components/ui/MarkdownViewer.tsx
@@ -10,7 +10,6 @@ import {
createElement,
} from "react";
import ReactMarkdown, { type Options } from "react-markdown";
-import Link from "next/link";
import remarkGfm from "remark-gfm";
import { CodeBlock } from "@/src/components/ui/Codeblock";
import { useTheme } from "next-themes";
@@ -40,6 +39,7 @@ import { MENTION_USER_PREFIX } from "@/src/features/comments/lib/mentionParser";
import { useCollapsibleSystemPrompt } from "@/src/hooks/useCollapsibleSystemPrompt";
import { Button } from "@/src/components/ui/button";
import { getSafeImageUrl, getSafeLinkUrl } from "@/src/components/ui/safe-url";
+import { env } from "@/src/env.mjs";
import {
getPromptReferenceMarkdownHref,
getPromptReferenceMarkdownLabel,
@@ -90,6 +90,23 @@ const transformListItemChildren = (children: ReactNode) =>
: child,
);
+/**
+ * A Next.js `<Link>` auto-prepends the configured `NEXT_PUBLIC_BASE_PATH` to
+ * root-relative internal hrefs (`/project/...`); a native `<a>` does not. Since
+ * markdown links now render as native anchors, replicate that so a hand-authored
+ * internal link still resolves under the base path on subpath deployments.
+ * Only root-relative paths are rewritten — absolute URLs (with a scheme),
+ * protocol-relative (`//`), hash (`#`), search (`?`), and `./`/`../` refs are
+ * left untouched (Next's `<Link>` did not prepend the base path to those either).
+ */
+export const prependBasePathToInternalHref = (
+ href: string,
+ basePath: string,
+): string =>
+ basePath && href.startsWith("/") && !href.startsWith("//")
+ ? `${basePath}${href}`
+ : href;
+
const isImageNode = (node?: ReactMarkdownNode): boolean =>
!!node &&
Array.isArray(node.children) &&
@@ -283,18 +300,31 @@ function MarkdownRenderer({
);
}
- // Handle regular links
+ // Handle regular links. These are user-content URLs opened in a
+ // new tab (target="_blank"), so a native <a> is correct: a Next.js
+ // <Link> gives no client-routing benefit for an external new-tab
+ // navigation, but it DOES run the router's href validation, which
+ // throws "Invalid href '…' passed to next/router" for the many
+ // malformed URLs embedded in trace content (e.g. a URL containing
+ // a second `https://`). That was a top Sentry noise family
+ // (LANGFUSE-5DZ / 5EA / 5ER, ~40k lifetime events). getSafeLinkUrl
+ // already gates the protocol/shape; a native <a> never validates.
+ // Re-apply NEXT_PUBLIC_BASE_PATH for root-relative internal hrefs,
+ // which <Link> used to prepend automatically (subpath deploys).
const safeHref = getSafeLinkUrl(href);
if (safeHref) {
return (
- <Link
- href={safeHref}
+ <a
+ href={prependBasePathToInternalHref(
+ safeHref,
+ env.NEXT_PUBLIC_BASE_PATH ?? "",
+ )}
className="underline"
target="_blank"
rel="noopener noreferrer"
>
{children}
- </Link>
+ </a>
);
}
return (