diff --git a/chat-app/components/ai-elements/web-preview.tsx b/chat-app/components/ai-elements/web-preview.tsx index 0f14ad67..21679f54 100644 --- a/chat-app/components/ai-elements/web-preview.tsx +++ b/chat-app/components/ai-elements/web-preview.tsx @@ -18,6 +18,19 @@ import { ChevronDownIcon } from 'lucide-react'; import type { ComponentProps, ReactNode } from 'react'; import { createContext, useContext, useEffect, useState } from 'react'; +const sanitizeUrl = (value: string | undefined | null): string | undefined => { + if (!value) return undefined; + try { + const url = new URL(value, typeof window !== 'undefined' ? window.location.origin : undefined); + if (url.protocol === 'http:' || url.protocol === 'https:') { + return url.toString(); + } + } catch { + // Ignore invalid URLs + } + return undefined; +}; + export interface WebPreviewContextValue { url: string; setUrl: (url: string) => void; @@ -176,13 +189,14 @@ export const WebPreviewBody = ({ ...props }: WebPreviewBodyProps) => { const { url } = useWebPreview(); + const safeSrc = sanitizeUrl(src ?? url); return (