Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion chat-app/components/ai-elements/web-preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛑 セキュリティ脆弱性: sanitizeUrl関数でサーバーサイドレンダリング時にベースURLが未定義になる可能性があります。window.location.originが利用できない場合、相対URLが予期しない動作を引き起こす可能性があります1

Suggested change
const url = new URL(value, typeof window !== 'undefined' ? window.location.origin : undefined);
const url = new URL(value);

Footnotes

  1. CWE-20: Improper Input Validation - https://cwe.mitre.org/data/definitions/20.html

if (url.protocol === 'http:' || url.protocol === 'https:') {
return url.toString();
}
Comment on lines +25 to +27

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

プロトコル検証が不完全です。`

Suggested change
if (url.protocol === 'http:' || url.protocol === 'https:') {
return url.toString();
}
if (url.protocol !== 'http:' && url.protocol !== 'https:') {
return undefined;
}

} catch {
// Ignore invalid URLs
}
return undefined;
};

export interface WebPreviewContextValue {
url: string;
setUrl: (url: string) => void;
Expand Down Expand Up @@ -176,13 +189,14 @@ export const WebPreviewBody = ({
...props
}: WebPreviewBodyProps) => {
const { url } = useWebPreview();
const safeSrc = sanitizeUrl(src ?? url);

return (
<div className="flex-1">
<iframe
className={cn('size-full', className)}
sandbox="allow-scripts allow-same-origin allow-forms allow-popups allow-presentation"
src={(src ?? url) || undefined}
src={safeSrc || undefined}
title="Preview"
{...props}
/>
Expand Down