Skip to content

armgit / memos

Public Imported from GitHub
0

Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.

feat(markdown): allow Bilibili player iframes (#6490)

tongjin901bf9f4cc89
diff --git a/web/src/lib/html-sanitize-schema.ts b/web/src/lib/html-sanitize-schema.ts
index 6af27561d..6061b7618 100644
--- a/web/src/lib/html-sanitize-schema.ts
+++ b/web/src/lib/html-sanitize-schema.ts
@@ -4,6 +4,7 @@ const TRUSTED_IFRAME_SRC_PATTERNS = [
/^https:\/\/www\.youtube\.com\/embed\/[^?#]+(?:\?.*)?$/i,
/^https:\/\/www\.youtube-nocookie\.com\/embed\/[^?#]+(?:\?.*)?$/i,
/^https:\/\/player\.vimeo\.com\/video\/[^?#]+(?:\?.*)?$/i,
+ /^https:\/\/player\.bilibili\.com\/player\.html(?:\?.*)?$/i,
/^https:\/\/open\.spotify\.com\/embed\/[^?#]+(?:\?.*)?$/i,
/^https:\/\/w\.soundcloud\.com\/player\/?(?:\?.*)?$/i,
/^https:\/\/www\.loom\.com\/embed\/[^?#]+(?:\?.*)?$/i,
diff --git a/web/src/lib/sanitized-html.ts b/web/src/lib/sanitized-html.ts
index 34755b842..ef44f0788 100644
--- a/web/src/lib/sanitized-html.ts
+++ b/web/src/lib/sanitized-html.ts
@@ -6,10 +6,35 @@ import remarkGfm from "remark-gfm";
import remarkParse from "remark-parse";
import remarkRehype from "remark-rehype";
import { unified } from "unified";
+import { visit } from "unist-util-visit";
import { isTrustedIframeSrc, SANITIZE_SCHEMA } from "@/lib/html-sanitize-schema";
import { markdownStyles } from "@/lib/markdownStyles";
import { cn } from "@/lib/utils";
+const BILIBILI_PLAYER_SRC = /^https:\/\/player\.bilibili\.com\/player\.html(?:\?|$)/i;
+
+/**
+ * Normalizes every `src` to a lowercase `https:` URL before sanitization. The sanitizer's
+ * protocol allowlist is case-sensitive, so `HTTPS://…` would be dropped, and it treats a
+ * scheme-relative `//host/…` as a relative URL, which an HTTP deployment would load over
+ * plain HTTP. Bilibili's share dialog emits a scheme-relative iframe, and its player starts
+ * playing on load unless the URL says `autoplay=0`.
+ */
+const normalizeSources = (tree: Root): void => {
+ visit(tree, "element", (node) => {
+ const src = node.properties.src;
+ if (typeof src !== "string") return;
+ const trimmed = src.trim();
+ let normalized = trimmed.startsWith("//") ? `https:${trimmed}` : trimmed.replace(/^https:/i, "https:");
+ if (node.tagName === "iframe" && BILIBILI_PLAYER_SRC.test(normalized)) {
+ const url = new URL(normalized);
+ if (!url.searchParams.has("autoplay")) url.searchParams.set("autoplay", "0");
+ normalized = url.href;
+ }
+ node.properties.src = normalized;
+ });
+};
+
/**
* Raw HTML in a memo, such as a `<details>` section or a trusted video embed,
* as the memo view shows it. Loaded on demand: the editor keeps an HTML block
@@ -22,6 +47,7 @@ const processor = unified()
.use(remarkBreaks)
.use(remarkRehype, { allowDangerousHtml: true })
.use(rehypeRaw)
+ .use(() => normalizeSources)
.use([[rehypeSanitize, SANITIZE_SCHEMA]]);
const ATTRIBUTE_NAMES: Record<string, string> = {
@@ -66,7 +92,12 @@ const finishElement = (element: HTMLElement, node: Element): HTMLElement | null
break;
case "iframe":
if (!isTrustedIframeSrc(element.getAttribute("src") ?? "")) return null;
- element.className = cn("max-w-full rounded-lg border border-border", element.className);
+ // Share snippets without a size (Bilibili's) would otherwise render at the browser's 300×150.
+ element.className = cn(
+ "max-w-full rounded-lg border border-border",
+ !node.properties.width && !node.properties.height && "w-full aspect-video",
+ element.className,
+ );
break;
case "details":
element.className = cn("my-0 mb-2", element.className);
diff --git a/web/tests/memo-content-security.test.ts b/web/tests/memo-content-security.test.ts
index a6e726b46..08ee9e9cd 100644
--- a/web/tests/memo-content-security.test.ts
+++ b/web/tests/memo-content-security.test.ts
@@ -51,6 +51,34 @@ describe("raw HTML sanitization", () => {
expect(renderHtml('<iframe src="https://evil.example/embed/abc123" title="demo"></iframe>')).not.toMatch(/<iframe/);
});
+ it("upgrades scheme-relative and uppercase-scheme sources to https", () => {
+ const youtube = /<iframe[^>]+src="https:\/\/www\.youtube\.com\/embed\/abc123"/;
+ expect(renderHtml('<iframe src="//www.youtube.com/embed/abc123"></iframe>')).toMatch(youtube);
+ expect(renderHtml('<iframe src=" HTTPS://www.youtube.com/embed/abc123"></iframe>')).toMatch(youtube);
+ expect(renderHtml('<img src="HTTPS://img.example/a.png">')).toContain('src="https://img.example/a.png"');
+ expect(renderHtml('<img src="//img.example/a.png">')).toContain('src="https://img.example/a.png"');
+ expect(renderHtml('<iframe src="http://www.youtube.com/embed/abc123"></iframe>')).not.toMatch(/<iframe/);
+ expect(renderHtml('<iframe src="//evil.example/embed/abc123"></iframe>')).not.toMatch(/<iframe/);
+ });
+
+ it("renders Bilibili's share snippet as a non-autoplaying, full-width player", () => {
+ const html = renderHtml(
+ '<iframe src="//player.bilibili.com/player.html?isOutside=true&bvid=BV1xx411c7mD&p=1" scrolling="no" border="0" frameborder="no" framespacing="0" allowfullscreen="true"></iframe>',
+ );
+ expect(html).toMatch(
+ /<iframe[^>]+src="https:\/\/player\.bilibili\.com\/player\.html\?isOutside=true&amp;bvid=BV1xx411c7mD&amp;p=1&amp;autoplay=0"/,
+ );
+ expect(html).toMatch(/class="[^"]*w-full[^"]*aspect-video/);
+ expect(renderHtml('<iframe src="https://player.bilibili.com/player.html?bvid=BV1xx411c7mD&autoplay=1"></iframe>')).toMatch(
+ /autoplay=1"/,
+ );
+ expect(
+ renderHtml('<iframe src="https://player.bilibili.com/player.html?bvid=BV1xx411c7mD" width="640" height="360"></iframe>'),
+ ).not.toMatch(/aspect-video/);
+ expect(renderHtml('<iframe src="//player.bilibili.com.evil.example/player.html?bvid=BV1xx411c7mD"></iframe>')).not.toMatch(/<iframe/);
+ expect(renderHtml('<iframe src="http://player.bilibili.com/player.html?bvid=BV1xx411c7mD"></iframe>')).not.toMatch(/<iframe/);
+ });
+
it("renders Markdown between disclosure tags and keeps the authored open state", () => {
const html = renderHtml("<details open>\n<summary>More <b>info</b></summary>\n\nSome **bold** text\n\n</details>");
expect(html).toMatch(/<details[^>]* open=""/);
@@ -70,6 +98,12 @@ describe("trusted iframe providers", () => {
expect(isTrustedIframeSrc("https://www.google.com/maps/embed?pb=test")).toBe(true);
expect(isTrustedIframeSrc("https://app.diagrams.net/?embed=1")).toBe(true);
expect(isTrustedIframeSrc("https://www.draw.io/?embed=1")).toBe(true);
+ expect(isTrustedIframeSrc("https://player.bilibili.com/player.html?bvid=BV1xx411c7mD&page=1")).toBe(true);
+ expect(isTrustedIframeSrc("//player.bilibili.com/player.html?bvid=BV1xx411c7mD")).toBe(false);
+ expect(isTrustedIframeSrc("HTTPS://player.bilibili.com/player.html?bvid=BV1xx411c7mD")).toBe(true);
+ expect(isTrustedIframeSrc("https://player.bilibili.com.evil.example/player.html?bvid=BV1xx411c7mD")).toBe(false);
+ expect(isTrustedIframeSrc("https://player.bilibili.com/video/BV1xx411c7mD")).toBe(false);
+ expect(isTrustedIframeSrc("http://player.bilibili.com/player.html?bvid=BV1xx411c7mD")).toBe(false);
expect(isTrustedIframeSrc("https://evil.example/embed/abc123")).toBe(false);
});
});