File tree Expand file tree Collapse file tree
chat-app/components/ai-elements Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -18,6 +18,19 @@ import { ChevronDownIcon } from 'lucide-react';
1818import type { ComponentProps , ReactNode } from 'react' ;
1919import { createContext , useContext , useEffect , useState } from 'react' ;
2020
21+ const sanitizeUrl = ( value : string | undefined | null ) : string | undefined => {
22+ if ( ! value ) return undefined ;
23+ try {
24+ const url = new URL ( value , typeof window !== 'undefined' ? window . location . origin : undefined ) ;
25+ if ( url . protocol === 'http:' || url . protocol === 'https:' ) {
26+ return url . toString ( ) ;
27+ }
28+ } catch {
29+ // Ignore invalid URLs
30+ }
31+ return undefined ;
32+ } ;
33+
2134export interface WebPreviewContextValue {
2235 url : string ;
2336 setUrl : ( url : string ) => void ;
@@ -176,13 +189,14 @@ export const WebPreviewBody = ({
176189 ...props
177190} : WebPreviewBodyProps ) => {
178191 const { url } = useWebPreview ( ) ;
192+ const safeSrc = sanitizeUrl ( src ?? url ) ;
179193
180194 return (
181195 < div className = "flex-1" >
182196 < iframe
183197 className = { cn ( 'size-full' , className ) }
184198 sandbox = "allow-scripts allow-same-origin allow-forms allow-popups allow-presentation"
185- src = { ( src ?? url ) || undefined }
199+ src = { safeSrc || undefined }
186200 title = "Preview"
187201 { ...props }
188202 />
You can’t perform that action at this time.
0 commit comments