fix: trust iframe urls

This commit is contained in:
ajnart committed 2026-08-26 17:35:39 +02:00
1 parent c34629d39e
commit 55b62a6e2b
5 files changed
+15 -69

No files matched your search

-2
View File
@@ -66,8 +66,6 @@ add_header Content-Security-Policy "frame-ancestors 'self' https://trusted-domai
- **Wikipedia homepage:** https://en.wikipedia.org/wiki/Main_Page - **Wikipedia homepage:** https://en.wikipedia.org/wiki/Main_Page
- **Any YouTube video or stream:** https://www.youtube.com/embed/dQw4w9WgXcQ?si=MWUbbG66UNTla_Ni - **Any YouTube video or stream:** https://www.youtube.com/embed/dQw4w9WgXcQ?si=MWUbbG66UNTla_Ni
- **Same-origin page:** `/path/to/page` resolves from the current Homarr origin. Root-relative URLs are useful when Homarr and
the embedded page share a reverse-proxy host.
### Configuration ### Configuration
@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest"; import { describe, expect, it } from "vitest";
import { getAllowedPermissions, getFrameTitle, getSandboxFlags, isSupportedProtocol } from "./component"; import { getAllowedPermissions, getFrameTitle, getSandboxFlags } from "./component";
const permissions = { const permissions = {
allowFullScreen: true, allowFullScreen: true,
@@ -18,12 +18,6 @@ describe("iframe policy", () => {
expect(getSandboxFlags(permissions)).toContain("allow-modals"); expect(getSandboxFlags(permissions)).toContain("allow-modals");
}); });
it("accepts only HTTP protocols", () => {
expect(isSupportedProtocol("https://example.com")).toBe(true);
expect(isSupportedProtocol("javascript:alert(1)")).toBe(false);
expect(isSupportedProtocol("not a url")).toBe(false);
});
it("creates a display title without credentials, paths, or query parameters", () => { it("creates a display title without credentials, paths, or query parameters", () => {
expect(getFrameTitle("https://user:secret@example.com/private?token=sensitive")).toBe("example.com"); expect(getFrameTitle("https://user:secret@example.com/private?token=sensitive")).toBe("example.com");
}); });
+3 -29
View File
@@ -1,26 +1,21 @@
"use client"; "use client";
import { Box, Stack, Text, Title } from "@mantine/core"; import { Box, Stack, Text, Title } from "@mantine/core";
import { IconBrowserOff, IconProtocol } from "@tabler/icons-react"; import { IconBrowserOff } from "@tabler/icons-react";
import { objectEntries } from "@homarr/common"; import { objectEntries } from "@homarr/common";
import { useI18n } from "@homarr/translation/client"; import { useI18n } from "@homarr/translation/client";
import type { WidgetComponentProps } from "../definition"; import type { WidgetComponentProps } from "../definition";
import classes from "./component.module.css"; import classes from "./component.module.css";
import { getSafeIframeUrl } from "./url";
export default function IFrameWidget({ options, isEditMode }: WidgetComponentProps<"iframe">) { export default function IFrameWidget({ options, isEditMode }: WidgetComponentProps<"iframe">) {
const t = useI18n("widget.iframe"); const t = useI18n("widget.iframe");
const { embedUrl, allowScrolling, ...permissions } = options; const { embedUrl, allowScrolling, ...permissions } = options;
const safeEmbedUrl = getSafeIframeUrl(embedUrl);
const allowedPermissions = getAllowedPermissions(permissions); const allowedPermissions = getAllowedPermissions(permissions);
const sandboxFlags = getSandboxFlags(permissions); const sandboxFlags = getSandboxFlags(permissions);
if (embedUrl.trim() === "") return <NoUrl />; if (embedUrl.trim() === "") return <NoUrl />;
if (!safeEmbedUrl) {
return <UnsupportedProtocol />;
}
return ( return (
<Stack h="100%" w="100%" gap={0}> <Stack h="100%" w="100%" gap={0}>
@@ -29,8 +24,8 @@ export default function IFrameWidget({ options, isEditMode }: WidgetComponentPro
loading="lazy" loading="lazy"
style={isEditMode ? { userSelect: "none", pointerEvents: "none" } : undefined} style={isEditMode ? { userSelect: "none", pointerEvents: "none" } : undefined}
className={classes.iframe} className={classes.iframe}
src={safeEmbedUrl} src={embedUrl}
title={getFrameTitle(safeEmbedUrl)} title={getFrameTitle(embedUrl)}
allow={allowedPermissions} allow={allowedPermissions}
scrolling={allowScrolling ? "yes" : "no"} scrolling={allowScrolling ? "yes" : "no"}
sandbox={sandboxFlags.join(" ")} sandbox={sandboxFlags.join(" ")}
@@ -42,12 +37,6 @@ export default function IFrameWidget({ options, isEditMode }: WidgetComponentPro
); );
} }
const supportedProtocols = ["http", "https"];
export const isSupportedProtocol = (url: string) => {
return getSafeIframeUrl(url) !== undefined;
};
const NoUrl = () => { const NoUrl = () => {
const t = useI18n("widget.iframe"); const t = useI18n("widget.iframe");
@@ -59,21 +48,6 @@ const NoUrl = () => {
); );
}; };
const UnsupportedProtocol = () => {
const t = useI18n("widget.iframe");
return (
<Stack align="center" justify="center" h="100%">
<IconProtocol />
<Title order={4} ta="center">
{t("error.unsupportedProtocol", {
supportedProtocols: supportedProtocols.map((protocol) => protocol).join(", "),
})}
</Title>
</Stack>
);
};
export const getAllowedPermissions = ( export const getAllowedPermissions = (
permissions: Omit<WidgetComponentProps<"iframe">["options"], "embedUrl" | "allowScrolling">, permissions: Omit<WidgetComponentProps<"iframe">["options"], "embedUrl" | "allowScrolling">,
) => { ) => {
+11 -12
View File
@@ -2,7 +2,6 @@ import { IconBrowser, IconExternalLink } from "@tabler/icons-react";
import { createWidgetDefinition } from "../definition"; import { createWidgetDefinition } from "../definition";
import { optionsBuilder } from "../options"; import { optionsBuilder } from "../options";
import { getSafeIframeUrl } from "./url";
const createOptions = () => const createOptions = () =>
optionsBuilder.from((factory) => ({ optionsBuilder.from((factory) => ({
@@ -23,17 +22,17 @@ export const { definition, componentLoader } = createWidgetDefinition("iframe",
supportsAdvancedFocus: false, supportsAdvancedFocus: false,
icon: IconBrowser, icon: IconBrowser,
contextActions: ({ options }) => { contextActions: ({ options }) => {
const embedUrl = getSafeIframeUrl(options.embedUrl); const embedUrl = options.embedUrl;
return embedUrl if (typeof embedUrl !== "string" || embedUrl.trim() === "") return [];
? [
{ return [
key: "open-iframe", {
label: "widget.common.openInNewTab.label", key: "open-iframe",
icon: IconExternalLink, label: "widget.common.openInNewTab.label",
onClick: () => window.open(embedUrl, "_blank", "noopener,noreferrer"), icon: IconExternalLink,
}, onClick: () => window.open(embedUrl, "_blank", "noopener,noreferrer"),
] },
: []; ];
}, },
createOptions, createOptions,
}).withDynamicImport(() => import("./component")); }).withDynamicImport(() => import("./component"));
-19
View File
@@ -1,19 +0,0 @@
import { getSafeApplicationUrl } from "../common/application-url";
const relativeUrlBase = "https://homarr.invalid";
export const getSafeIframeUrl = (value: unknown): string | undefined => {
const absoluteUrl = getSafeApplicationUrl(value);
if (absoluteUrl) return absoluteUrl;
if (typeof value !== "string") return undefined;
const path = value.trim();
if (!path.startsWith("/")) return undefined;
const resolvedUrl = getSafeApplicationUrl(path, { baseUrl: relativeUrlBase });
if (!resolvedUrl) return undefined;
const parsedUrl = new URL(resolvedUrl);
if (parsedUrl.origin !== relativeUrlBase) return undefined;
return `${parsedUrl.pathname}${parsedUrl.search}${parsedUrl.hash}`;
};