Skip to content

Commit 4f8707e

Browse files
Potential fix for code scanning alert no. 12: DOM text reinterpreted as HTML
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
1 parent 236aecf commit 4f8707e

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/components/Browser.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ChevronLeft, ChevronRight, Home, Lock, Maximize2, Menu, MoreVertical, Plus, RotateCw, Star, X } from "lucide-react";
22
import { useEffect, useMemo, useRef, useState } from "react";
3-
import { actionBarClass, addressInputClass, classNames, closeButtonClass, encodeProxyUrl, formatUrl, getActualUrl, getDefaultUrl, type Tab, tabButtonClass } from "@/lib/tabs";
3+
import { actionBarClass, addressInputClass, classNames, closeButtonClass, encodeProxyUrl, formatUrl, getActualUrl, getDefaultUrl, type Tab, tabButtonClass, sanitizeUrl } from "@/lib/tabs";
44

55
const IconButton = ({ onClick, icon: Icon, className = "", disabled = false, title = "" }: { onClick?: () => void; icon: React.ComponentType<{ className?: string }>; className?: string; disabled?: boolean; title?: string }) => (
66
<button
@@ -450,7 +450,7 @@ export default function Browser() {
450450
iframeRefs.current[tab.id] = el;
451451
}}
452452
title={tab.title}
453-
src={encodeProxyUrl(tab.url)}
453+
src={encodeProxyUrl(sanitizeUrl(tab.url))}
454454
className={classNames("absolute inset-0 h-full w-full border-0", tab.active ? "block" : "hidden")}
455455
sandbox="allow-scripts allow-same-origin allow-forms allow-popups allow-popups-to-escape-sandbox"
456456
/>

src/lib/tabs.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,21 @@ export const getDefaultUrl = () => {
4646
}
4747
};
4848

49+
// Only allow 'about:blank' and HTTP(S) URLs. All others are replaced with 'about:blank'.
50+
export function sanitizeUrl(url: string): string {
51+
if (!url || url === "about:blank") return "about:blank";
52+
try {
53+
// This throws for invalid URLs, so fallback to 'about:blank'
54+
const parsed = new URL(url, "https://example.com");
55+
if (parsed.protocol === "http:" || parsed.protocol === "https:") {
56+
return parsed.href;
57+
}
58+
} catch {
59+
// Malformed URL, fallback
60+
}
61+
return "about:blank";
62+
}
63+
4964
export const encodeProxyUrl = (url: string): string => {
5065
if (!url || url === "about:blank") return "about:blank";
5166
if (typeof window === "undefined") return url;

0 commit comments

Comments
 (0)