Skip to content
Merged
Show file tree
Hide file tree
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
14 changes: 9 additions & 5 deletions site/src/components/tools/ShareBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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). */
Expand Down Expand Up @@ -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 = `<iframe src="${embedUrl}" width="100%" height="520" style="border:1px solid #e2e8f0;border-radius:12px" loading="lazy" title="${safeTitle}"></iframe>\n<p style="font:13px/1.6 system-ui,-apple-system,sans-serif;text-align:center;margin:6px 0;color:#64748b">Free <a href="${url}" target="_blank" rel="nofollow noopener">${safeTitle}</a> by <a href="https://toolify365.com" target="_blank" rel="nofollow noopener">Toolify365</a></p>`;
// 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));
Expand Down Expand Up @@ -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 は送らない
Expand Down
59 changes: 59 additions & 0 deletions site/src/lib/tools/embed-html.test.ts
Original file line number Diff line number Diff line change
@@ -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&amp;B &lt;&quot;C&quot;&gt; &#39;D&#39;");
});
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&amp;B &quot;Calculator&quot;"');
expect(snippet).toContain('src="https://toolify365.com/en/embed?a=1&amp;b=2"');
expect(snippet).toContain('href="https://toolify365.com/en/tools?a=1&amp;b=2"');
expect(snippet).toContain('>A&amp;B &quot;Calculator&quot;</a>');
});
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(/<a\b[^>]*>/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</a>');
expect(snippet).toContain('>toolify365</a>');
});
it("cannot inject markup through title or a quoted attribute", () => {
const snippet = buildToolEmbedSnippet('"><script>alert(1)</script>', 'https://toolify365.com/embed" onload="alert(1)', "https://toolify365.com/tool' onclick='evil()");
expect(snippet).not.toContain('<script>');
expect(snippet).not.toContain('src="https://toolify365.com/embed" onload=');
expect(snippet).not.toContain("href=\"https://toolify365.com/tool' onclick=");
expect(snippet).toContain('&lt;script&gt;');
});
it("blocks active schemes and relative embed URLs but permits HTTPS and local development", () => {
for (const source of ["javascript:alert(1)", "data:text/html,<script>alert(1)</script>", "/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"');
}
});

});
37 changes: 37 additions & 0 deletions site/src/lib/tools/embed-html.ts
Original file line number Diff line number Diff line change
@@ -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 "&amp;";
case "<": return "&lt;";
case ">": return "&gt;";
case '"': return "&quot;";
default: return "&#39;";
}
});
}

/** 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 `<iframe src="${safeEmbedSrc}" width="100%" height="520" style="border:1px solid #e2e8f0;border-radius:12px" loading="lazy" title="${safeTitle}"></iframe>\n<p style="font:13px/1.6 system-ui,-apple-system,sans-serif;text-align:center;margin:6px 0;color:#64748b">Free <a href="${safeToolUrl}" target="_blank" rel="noopener noreferrer nofollow">${safeTitle}</a> by <a href="https://toolify365.com" target="_blank" rel="noopener noreferrer nofollow">toolify365</a></p>`;
}
22 changes: 22 additions & 0 deletions site/src/lib/tools/share-errors.test.ts
Original file line number Diff line number Diff line change
@@ -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);
}
});
});
13 changes: 13 additions & 0 deletions site/src/lib/tools/share-errors.ts
Original file line number Diff line number Diff line change
@@ -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";
}
Loading