Skip to content

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

chore(markdown-ui): disable image previews on public traces; fix dom nesting error for markdown img (#2948)

marliessophie2950a480f185
diff --git a/web/src/components/ui/MarkdownViewer.tsx b/web/src/components/ui/MarkdownViewer.tsx
index 07812efcec..669f481631 100644
--- a/web/src/components/ui/MarkdownViewer.tsx
+++ b/web/src/components/ui/MarkdownViewer.tsx
@@ -32,6 +32,14 @@ import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePos
import { Skeleton } from "@/src/components/ui/skeleton";
import { useMarkdownContext } from "@/src/features/theming/useMarkdownContext";
import { captureException } from "@sentry/nextjs";
+import { useSession } from "next-auth/react";
+import { type ExtraProps as ReactMarkdownExtraProps } from "react-markdown";
+
+type ReactMarkdownNode = ReactMarkdownExtraProps["node"];
+type ReactMarkdownNodeChildren = Exclude<
+ ReactMarkdownNode,
+ undefined
+>["children"];
// ReactMarkdown does not render raw HTML by default for security reasons, to prevent XSS (Cross-Site Scripting) attacks.
// html is rendered as plain text by default.
@@ -42,10 +50,6 @@ const MemoizedReactMarkdown: FC<Options> = memo(
prevProps.className === nextProps.className,
);
-const isChecklist = (children: ReactNode) =>
- Array.isArray(children) &&
- children.some((child: any) => child?.props?.className === "task-list-item");
-
/**
* Implemented customLoader as we cannot whitelist user provided image domains
* Security risks are taken care of by a validation in api.utilities.validateImgUrl
@@ -65,13 +69,13 @@ const customLoader = ({
const ImageErrorDisplay = ({
src,
- errorDescription,
+ displayError,
}: {
src: string;
- errorDescription: string;
+ displayError: string;
}) => (
<div className="flex flex-row items-center gap-2">
- <span title={errorDescription} className="h-4 w-4">
+ <span title={displayError} className="h-4 w-4">
<ImageOff className="h-4 w-4" />
</span>
<Link href={src} className="underline" target="_blank">
@@ -84,10 +88,23 @@ const MarkdownImage: Components["img"] = ({ src, alt }) => {
const [isZoomedIn, setIsZoomedIn] = useState(true);
const [hasFetchError, setHasFetchError] = useState(false);
const [isImageVisible, setIsImageVisible] = useState(false);
+ const session = useSession();
if (!isPresent(src)) return null;
- const isValidImage = api.utilities.validateImgUrl.useQuery(src);
+ const isValidImage = api.utilities.validateImgUrl.useQuery(src, {
+ enabled: session.status === "authenticated",
+ });
+
+ if (session.status !== "authenticated") {
+ return (
+ <ImageErrorDisplay
+ src={src}
+ displayError="Images not rendered on public traces and observations"
+ />
+ );
+ }
+
if (isValidImage.isLoading) {
return (
<Skeleton className="h-8 w-1/2 items-center p-2 text-xs">
@@ -96,13 +113,13 @@ const MarkdownImage: Components["img"] = ({ src, alt }) => {
);
}
- const errorDescription = `Cannot load image. ${src.includes("http") ? "Http images are not rendered in Langfuse for security reasons" : "Invalid image URL"}`;
+ const displayError = `Cannot load image. ${src.includes("http") ? "Http images are not rendered in Langfuse for security reasons" : "Invalid image URL"}`;
if (isValidImage.data?.isValid) {
return (
<div>
{hasFetchError ? (
- <ImageErrorDisplay src={src} errorDescription={errorDescription} />
+ <ImageErrorDisplay src={src} displayError={displayError} />
) : (
<div
className={cn(
@@ -171,7 +188,7 @@ const MarkdownImage: Components["img"] = ({ src, alt }) => {
);
}
- return <ImageErrorDisplay src={src} errorDescription={errorDescription} />;
+ return <ImageErrorDisplay src={src} displayError={displayError} />;
};
const isTextElement = (child: ReactNode): child is ReactElement =>
@@ -179,6 +196,10 @@ const isTextElement = (child: ReactNode): child is ReactElement =>
typeof child.type !== "string" &&
["p", "h1", "h2", "h3", "h4", "h5", "h6"].includes(child.type.name);
+const isChecklist = (children: ReactNode) =>
+ Array.isArray(children) &&
+ children.some((child: any) => child?.props?.className === "task-list-item");
+
const transformListItemChildren = (children: ReactNode) =>
Children.map(children, (child) =>
isTextElement(child) ? (
@@ -190,6 +211,14 @@ const transformListItemChildren = (children: ReactNode) =>
),
);
+const isImageNode = (node?: ReactMarkdownNode): boolean =>
+ !!node &&
+ Array.isArray(node.children) &&
+ node.children.some(
+ (child: ReactMarkdownNodeChildren[number]) =>
+ "tagName" in child && child.tagName === "img",
+ );
+
export function MarkdownView({
markdown,
title,
@@ -267,7 +296,10 @@ export function MarkdownView({
)}
remarkPlugins={[remarkGfm, remarkMath]}
components={{
- p({ children }) {
+ p({ children, node }) {
+ if (isImageNode(node)) {
+ return <>{children}</>;
+ }
return (
<p className="mb-2 whitespace-pre-wrap last:mb-0">{children}</p>
);