Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Preserve SiYuan links in marketplace READMEs https://github.com/siyuan-note/siyuan/issues/19041

Daniel8d48ed6754e4
diff --git a/app/src/config/bazaar.ts b/app/src/config/bazaar.ts
index a031fbb5ec..c20417f9da 100644
--- a/app/src/config/bazaar.ts
+++ b/app/src/config/bazaar.ts
@@ -51,6 +51,7 @@ import {
} from "../util/bazaarPackage";
import {Dialog} from "../dialog";
import {previewImages} from "../protyle/preview/image";
+import {BAZAAR_README_SANITIZE_OPTIONS} from "./bazaarReadmeSanitize";
interface IBazaarMountSnapshot {
element: HTMLElement;
@@ -1334,7 +1335,7 @@ type="checkbox">
const isInstalledReadme = from === "downloaded";
if (isInstalledReadme) {
const mdElement = readmeElement.querySelector(".item__readme");
- mdElement.innerHTML = window.DOMPurify.sanitize(displayData.preferredReadme || "", {FORBID_TAGS: ["iframe", "frame", "frameset"]});
+ mdElement.innerHTML = window.DOMPurify.sanitize(displayData.preferredReadme || "", BAZAAR_README_SANITIZE_OPTIONS);
highlightRender(mdElement);
} else {
fetchPost("/api/bazaar/getBazaarPackageREADME", {
@@ -1352,7 +1353,7 @@ type="checkbox">
return;
}
const mdElement = readmeElement.querySelector(".item__readme");
- mdElement.innerHTML = window.DOMPurify.sanitize(response.data.html, {FORBID_TAGS: ["iframe", "frame", "frameset"]});
+ mdElement.innerHTML = window.DOMPurify.sanitize(response.data.html, BAZAAR_README_SANITIZE_OPTIONS);
highlightRender(mdElement);
});
}
diff --git a/app/src/config/bazaarReadmeSanitize.test.ts b/app/src/config/bazaarReadmeSanitize.test.ts
new file mode 100644
index 0000000000..9606405b89
--- /dev/null
+++ b/app/src/config/bazaarReadmeSanitize.test.ts
@@ -0,0 +1,30 @@
+import {describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+import {BAZAAR_README_ALLOWED_URI_REGEXP, BAZAAR_README_SANITIZE_OPTIONS} from "./bazaarReadmeSanitize";
+
+describe("bazaarReadmeSanitize", () => {
+ it("allows supported URI protocols", () => {
+ [
+ "https://example.com",
+ "mailto:[email protected]",
+ "siyuan://bazaar/themes/Whisper/readme",
+ "web+siyuan://bazaar/themes/Whisper/readme",
+ "/relative/path",
+ "#heading",
+ ].forEach((uri) => assert.equal(BAZAAR_README_ALLOWED_URI_REGEXP.test(uri), true, uri));
+ });
+
+ it("rejects dangerous and unsupported URI protocols", () => {
+ [
+ "javascript:alert(1)",
+ "data:text/html,<script>alert(1)</script>",
+ "vbscript:msgbox(1)",
+ "unknown://example.com",
+ ].forEach((uri) => assert.equal(BAZAAR_README_ALLOWED_URI_REGEXP.test(uri), false, uri));
+ });
+
+ it("keeps embedded frames forbidden", () => {
+ assert.deepEqual(BAZAAR_README_SANITIZE_OPTIONS.FORBID_TAGS, ["iframe", "frame", "frameset"]);
+ assert.equal("ALLOW_UNKNOWN_PROTOCOLS" in BAZAAR_README_SANITIZE_OPTIONS, false);
+ });
+});
diff --git a/app/src/config/bazaarReadmeSanitize.ts b/app/src/config/bazaarReadmeSanitize.ts
new file mode 100644
index 0000000000..0134770b25
--- /dev/null
+++ b/app/src/config/bazaarReadmeSanitize.ts
@@ -0,0 +1,6 @@
+export const BAZAAR_README_ALLOWED_URI_REGEXP = /^(?:(?:(?:f|ht)tps?|mailto|tel|callto|sms|cid|xmpp|matrix|siyuan|web\+siyuan):|[^a-z]|[a-z+.\-]+(?:[^a-z+.\-:]|$))/i;
+
+export const BAZAAR_README_SANITIZE_OPTIONS = {
+ FORBID_TAGS: ["iframe", "frame", "frameset"],
+ ALLOWED_URI_REGEXP: BAZAAR_README_ALLOWED_URI_REGEXP,
+};