diff --git a/site/src/components/tools/ShareBar.tsx b/site/src/components/tools/ShareBar.tsx index f926f860..bde0a4ca 100644 --- a/site/src/components/tools/ShareBar.tsx +++ b/site/src/components/tools/ShareBar.tsx @@ -4,7 +4,9 @@ import { useEffect, useState } from "react"; import { useTranslations } from "next-intl"; import { Link } from "@/lib/i18n/navigation"; import { useToolEvents } from "@/lib/analytics/useToolEvents"; -import { socialShareUrl, escapeEmbedHtml } from "@/lib/analytics/shareAttribution"; +import { socialShareUrl } from "@/lib/analytics/shareAttribution"; +import { buildToolEmbedSnippet } from "@/lib/tools/embed-html"; +import { isShareCancelled } from "@/lib/tools/share-errors"; interface Props { /** Canonical, absolute URL of the tool page (built server-side from siteConfig). */ @@ -121,8 +123,8 @@ export function ShareBar({ url, embedUrl, title, slug }: Props) { const enc = encodeURIComponent; // Keep optional embed attribution visible to readers without ranking-credit links. // Broadly distributed widget links are qualified per Google spam guidelines. - const safeTitle = escapeEmbedHtml(title); - const snippet = `\n
Free ${safeTitle} by Toolify365
`; + // Generate a validated, escaped and nofollow-qualified snippet. + const snippet = buildToolEmbedSnippet(title, embedUrl, url); // Attribute social shares without changing canonical, copy or embed URLs. const trackable = (source: string) => enc(socialShareUrl(url, source)); @@ -155,8 +157,10 @@ export function ShareBar({ url, embedUrl, title, slug }: Props) { // 成功時だけ送る。キャンセルは reject されるのでここには来ない。 share("native"); return; - } catch { - /* user cancelled — fall through */ + } catch (error) { + // Explicit cancellation is not a request to copy to the clipboard. + if (isShareCancelled(error)) return; + // Other share failures can still use the existing link-copy fallback. } } // フォールバックは copy() 側が copy_result("link") を送るため share は送らない diff --git a/site/src/lib/tools/embed-html.test.ts b/site/src/lib/tools/embed-html.test.ts new file mode 100644 index 00000000..89dbaad2 --- /dev/null +++ b/site/src/lib/tools/embed-html.test.ts @@ -0,0 +1,59 @@ +import { describe, expect, it } from "vitest"; +import { buildToolEmbedSnippet, escapeHtml } from "./embed-html"; + +describe("safe tool embed HTML", () => { + it("escapes all HTML-special characters in localized titles", () => { + expect(escapeHtml(`A&B <"C"> 'D'`)) + .toBe("A&B <"C"> 'D'"); + }); + it("escapes attribute values without double-escaping titles", () => { + const snippet = buildToolEmbedSnippet('A&B "Calculator"', 'https://toolify365.com/en/embed?a=1&b=2', 'https://toolify365.com/en/tools?a=1&b=2'); + expect(snippet).toContain('title="A&B "Calculator""'); + expect(snippet).toContain('src="https://toolify365.com/en/embed?a=1&b=2"'); + expect(snippet).toContain('href="https://toolify365.com/en/tools?a=1&b=2"'); + expect(snippet).toContain('>A&B "Calculator"'); + }); + it("qualifies distributed widget attribution links without affecting visible citations", () => { + const snippet = buildToolEmbedSnippet("My Tool", "https://toolify365.com/embed/en/test", "https://toolify365.com/en/tools/test"); + const links = [...snippet.matchAll(/]*>/g)].map((m) => m[0]); + expect(links).toHaveLength(2); + for (const link of links) { + expect(link).toContain('rel="noopener noreferrer nofollow"'); + expect(link).toContain('target="_blank"'); + } + expect(snippet).toContain('>My Tool'); + expect(snippet).toContain('>toolify365'); + }); + it("cannot inject markup through title or a quoted attribute", () => { + const snippet = buildToolEmbedSnippet('">', 'https://toolify365.com/embed" onload="alert(1)', "https://toolify365.com/tool' onclick='evil()"); + expect(snippet).not.toContain('", "/relative/embed", "http://evil.example/iframe"]) { + const snippet = buildToolEmbedSnippet("Safe", source, source); + expect(snippet).toContain('src="about:blank"'); + expect(snippet).toContain('href="https://toolify365.com"'); + expect(snippet).not.toContain('src="javascript:'); + expect(snippet).not.toContain('href="javascript:'); + } + expect(buildToolEmbedSnippet("Safe", "https://toolify365.com/en/embed", "https://toolify365.com/en/tools")) + .toContain('src="https://toolify365.com/en/embed"'); + expect(buildToolEmbedSnippet("Safe", "http://localhost:3000/en/embed", "http://127.0.0.1:3000/en/tools")) + .toContain('src="http://localhost:3000/en/embed"'); + for (const source of [ + "https://username:password@toolify365.com/en/embed", + "https://username@toolify365.com/en/embed", + "\nhttps://toolify365.com/en/embed", + "https://toolify365.com/en/embed\t", + " https://toolify365.com/en/embed", + ]) { + const snippet = buildToolEmbedSnippet("Safe", source, source); + expect(snippet).toContain('src="about:blank"'); + expect(snippet).toContain('href="https://toolify365.com"'); + } + }); + +}); diff --git a/site/src/lib/tools/embed-html.ts b/site/src/lib/tools/embed-html.ts new file mode 100644 index 00000000..4dcb4ae6 --- /dev/null +++ b/site/src/lib/tools/embed-html.ts @@ -0,0 +1,37 @@ +/** Escape text embedded into HTML markup, including quoted attributes. */ +export function escapeHtml(value: string): string { + return value.replace(/[&<>"']/g, (char) => { + switch (char) { + case "&": return "&"; + case "<": return "<"; + case ">": return ">"; + case '"': return """; + default: return "'"; + } + }); +} + +/** Reject active or opaque URL schemes even after HTML attribute escaping. */ +function safeEmbedUrl(value: string, fallback: string): string { + // WHATWG URL parsing trims some controls; reject them before parsing. + if (/[\u0000-\u001f\u007f]/.test(value) || value.trim() !== value) return fallback; + try { + const parsed = new URL(value); + // Userinfo in copied embed snippets risks leaking credentials. + if (parsed.username || parsed.password) return fallback; + const localDev = parsed.protocol === "http:" && + (parsed.hostname === "localhost" || parsed.hostname === "127.0.0.1"); + if (parsed.protocol === "https:" || localDev) return value; + } catch { + // Relative and malformed inputs must not be emitted as iframe/link URLs. + } + return fallback; +} + +/** Keep snippet assembly isolated and testable outside React. */ +export function buildToolEmbedSnippet(title: string, embedUrl: string, toolUrl: string): string { + const safeTitle = escapeHtml(title); + const safeEmbedSrc = escapeHtml(safeEmbedUrl(embedUrl, 'about:blank')); + const safeToolUrl = escapeHtml(safeEmbedUrl(toolUrl, 'https://toolify365.com')); + return `\nFree ${safeTitle} by toolify365
`; +} diff --git a/site/src/lib/tools/share-errors.test.ts b/site/src/lib/tools/share-errors.test.ts new file mode 100644 index 00000000..08f36a41 --- /dev/null +++ b/site/src/lib/tools/share-errors.test.ts @@ -0,0 +1,22 @@ +import { describe, expect, it } from "vitest"; +import { isShareCancelled } from "./share-errors"; + +describe("Web Share API cancellation", () => { + it("recognizes DOMException and Error-like AbortError rejections", () => { + expect(isShareCancelled(new DOMException("User cancelled", "AbortError"))).toBe(true); + const error = new Error("Share sheet closed"); + error.name = "AbortError"; + expect(isShareCancelled(error)).toBe(true); + expect(isShareCancelled({ name: "AbortError" })).toBe(true); + }); + + it("preserves clipboard fallback for other share failures", () => { + for (const error of [ + new Error("Unavailable"), + new DOMException("Not allowed", "NotAllowedError"), + null, undefined, "AbortError", { name: "aborterror" }, { message: "AbortError" }, + ]) { + expect(isShareCancelled(error)).toBe(false); + } + }); +}); diff --git a/site/src/lib/tools/share-errors.ts b/site/src/lib/tools/share-errors.ts new file mode 100644 index 00000000..c421961c --- /dev/null +++ b/site/src/lib/tools/share-errors.ts @@ -0,0 +1,13 @@ +/** + * The Web Share API rejects with AbortError when the user cancels the share + * sheet. Cancellation is not a clipboard-copy request. + * + * Some browsers expose a DOMException and others return an Error-like object. + * Checking the name avoids dependence on the DOMException constructor. + */ +export function isShareCancelled(error: unknown): boolean { + return error !== null + && typeof error === "object" + && "name" in error + && error.name === "AbortError"; +}