Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

[Metabot] Treat protocol-relative links as external in `AIMarkdown` (#83814)

Sloan Sparger63b52d2066dc
diff --git a/frontend/src/metabase/metabot/components/AIMarkdown/AIMarkdown.tsx b/frontend/src/metabase/metabot/components/AIMarkdown/AIMarkdown.tsx
index 123c9f8d7e7..3c51fe32c05 100644
--- a/frontend/src/metabase/metabot/components/AIMarkdown/AIMarkdown.tsx
+++ b/frontend/src/metabase/metabot/components/AIMarkdown/AIMarkdown.tsx
@@ -101,6 +101,10 @@ const parseEntityLink = (
return path && { ...path, href };
};
+// Excludes `//host`, which browsers resolve to another origin
+const isRelativePath = (href?: string): href is string =>
+ href != null && /^\/(?!\/)/.test(href);
+
const getComponents = ({
onInternalLinkClick,
}: Pick<AIMarkdownProps, "onInternalLinkClick">) => ({
@@ -126,7 +130,7 @@ const getComponents = ({
);
}
- if (href?.startsWith("/")) {
+ if (isRelativePath(href)) {
return (
<InternalLink
onInternalLinkClick={onInternalLinkClick}
diff --git a/frontend/src/metabase/metabot/components/AIMarkdown/AIMarkdown.unit.spec.tsx b/frontend/src/metabase/metabot/components/AIMarkdown/AIMarkdown.unit.spec.tsx
index 6bafaa790c2..e4dcdb9ab1c 100644
--- a/frontend/src/metabase/metabot/components/AIMarkdown/AIMarkdown.unit.spec.tsx
+++ b/frontend/src/metabase/metabot/components/AIMarkdown/AIMarkdown.unit.spec.tsx
@@ -133,6 +133,14 @@ describe("AIMarkdown", () => {
expect(screen.queryByTestId("smart-link")).not.toBeInTheDocument();
});
+ it("should render protocol-relative links as external", async () => {
+ setup({ children: "See [Evil](//evil.com)" });
+
+ const link = await screen.findByRole("link", { name: "Evil" });
+ expect(link).toHaveAttribute("target", "_blank");
+ expect(link).toHaveAttribute("rel", "noopener noreferrer");
+ });
+
it("shows a hover card with the entity details for a Library token", async () => {
setup({ children: "[Orders](/table/5)" });