Skip to content

Commit afcdd78

Browse files
Potential fix for code scanning alert no. 3: DOM text reinterpreted as HTML (#153)
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
1 parent be919a4 commit afcdd78

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

chat-app/components/ai-elements/web-preview.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@ import { ChevronDownIcon } from 'lucide-react';
1818
import type { ComponentProps, ReactNode } from 'react';
1919
import { 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+
2134
export 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
/>

0 commit comments

Comments
 (0)