Skip to content

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

:sparkles: Support custom protocol links in database text fields https://github.com/siyuan-note/siyuan/issues/20649

Daniela9eb9b45287b
diff --git a/app/src/protyle/render/av/action.ts b/app/src/protyle/render/av/action.ts
index 0fca27c806..054283e514 100644
--- a/app/src/protyle/render/av/action.ts
+++ b/app/src/protyle/render/av/action.ts
@@ -344,7 +344,7 @@ export const avClick = (protyle: IProtyle, event: MouseEvent & { target: HTMLEle
event.stopPropagation();
return true;
} else if (linkElement && richTextElement.contains(linkElement)) {
- href = getAVRichTextSafeURL(linkElement.dataset.href || linkElement.getAttribute("href"));
+ href = getAVRichTextSafeURL(linkElement.dataset.href || linkElement.getAttribute("href"), false, true);
}
if (href) {
openLink(protyle.app, href, event, event.ctrlKey || event.metaKey);
diff --git a/app/src/protyle/render/av/blockAttr.ts b/app/src/protyle/render/av/blockAttr.ts
index db9d8ab634..f5a194491d 100644
--- a/app/src/protyle/render/av/blockAttr.ts
+++ b/app/src/protyle/render/av/blockAttr.ts
@@ -113,7 +113,7 @@ const handleRichTextInteraction = (protyle: IProtyle, event: MouseEvent) => {
});
/// #endif
} else if (linkElement && richTextElement.contains(linkElement)) {
- const link = getAVRichTextSafeURL(linkElement.dataset.href || linkElement.getAttribute("href"));
+ const link = getAVRichTextSafeURL(linkElement.dataset.href || linkElement.getAttribute("href"), false, true);
if (link) {
openLink(protyle.app, link, event, event.ctrlKey || event.metaKey);
}
diff --git a/app/src/protyle/render/av/richText.test.ts b/app/src/protyle/render/av/richText.test.ts
index 20ab760b70..64c317961b 100644
--- a/app/src/protyle/render/av/richText.test.ts
+++ b/app/src/protyle/render/av/richText.test.ts
@@ -415,6 +415,27 @@ describe("attribute view text source compatibility", () => {
}
});
+ it("allows custom protocols only for explicit links and keeps dangerous schemes blocked", () => {
+ for (const target of [
+ "hook://file/EqhAm6xHS?p=YXJjaGl2ZXMvcHJvdmVuYW5jZQ==&n=2026%2D10%2D05",
+ "obsidian://open?vault=notes&file=book", "custom+app://action/path", "CUSTOM://action",
+ "ftp://example.com/file",
+ ]) {
+ assert.equal(getAVRichTextSafeURL(target, false, true), target);
+ assert.equal(getAVRichTextSafeURL(target), "", "automatic resource policy must remain restricted");
+ }
+ for (const target of [
+ "javascript://host/payload", "JaVaScRiPt://host/payload", "vbscript://host/payload",
+ "data://host/payload", "blob://host/payload", "file://host/payload", "filesystem://host/payload",
+ "about://host/payload", "chrome://settings", "chrome-extension://id/page", "devtools://host/page",
+ "view-source://host/page", "resource://host/page", "moz-extension://id/page",
+ "java%73cript://host/payload", "java\u200Bscript://host/payload", "custom:payload",
+ "hook:/file/path", "hook://", "hook:payload", "hook://host/path%0a", "folder:name",
+ ]) {
+ assert.equal(getAVRichTextSafeURL(target, false, true), "", target);
+ }
+ });
+
it("keeps only supported inline color declarations", () => {
for (const id of ["error", "warning", "info", "success"]) {
const style = `color: var(--b3-card-${id}-color); background-color: var(--b3-card-${id}-background);`;
diff --git a/app/src/protyle/render/av/richText.ts b/app/src/protyle/render/av/richText.ts
index 4d250c3126..b8f2add6bf 100644
--- a/app/src/protyle/render/av/richText.ts
+++ b/app/src/protyle/render/av/richText.ts
@@ -248,7 +248,7 @@ export const sanitizeAVRichTextBlockDOM = (blockDOM: string, images = false, tab
});
});
template.content.querySelectorAll<HTMLElement>('[data-type~="a"][data-href]').forEach((element) => {
- const href = getAVRichTextSafeURL(element.dataset.href, tableCellCustomInline);
+ const href = getAVRichTextSafeURL(element.dataset.href, tableCellCustomInline, true);
if (href) {
element.dataset.href = href;
} else {
@@ -454,7 +454,7 @@ const prepareAVRichTextPreviewHTML = (html: string) => {
element.disabled = true;
});
template.content.querySelectorAll<HTMLElement>("[data-href], a[href]").forEach((element) => {
- const href = getAVRichTextSafeURL(element.getAttribute("href") || element.dataset.href);
+ const href = getAVRichTextSafeURL(element.getAttribute("href") || element.dataset.href, false, true);
if (!href) {
element.removeAttribute("href");
element.removeAttribute("data-href");
diff --git a/app/src/protyle/render/av/richTextValue.ts b/app/src/protyle/render/av/richTextValue.ts
index ab5d9ccdca..09dcf2495f 100644
--- a/app/src/protyle/render/av/richTextValue.ts
+++ b/app/src/protyle/render/av/richTextValue.ts
@@ -20,6 +20,7 @@ export const AV_RICH_TEXT_EDITOR_ALLOWED_ATTRIBUTES = [
"data-marker", "data-node-id", "data-node-index", "data-position", "data-subtype", "data-type", "draggable",
"spellcheck", "spin", "style", "updated", "xlink:href",
];
+const AV_RICH_TEXT_CUSTOM_PROTOCOL = /^(?!(?:javascript|vbscript|data|blob|file|filesystem|about|chrome|chrome-extension|devtools|view-source|resource|moz-extension):)[a-z][a-z0-9+.-]*:\/\//i;
export const AV_RICH_TEXT_PREVIEW_SANITIZE_OPTIONS = {
ALLOWED_TAGS: AV_RICH_TEXT_PREVIEW_ALLOWED_TAGS,
ALLOWED_ATTR: AV_RICH_TEXT_PREVIEW_ALLOWED_ATTRIBUTES,
@@ -27,7 +28,8 @@ export const AV_RICH_TEXT_PREVIEW_SANITIZE_OPTIONS = {
ALLOW_DATA_ATTR: false,
// 公式源码是纯文本,不按链接地址过滤;链接属性仍使用独立的地址校验。
ADD_URI_SAFE_ATTR: ["data-content"],
- ALLOWED_URI_REGEXP: /^(?:(?:https?|mailto|siyuan|tel|web\+siyuan):|[#/?]|\.\.?\/|[^a-z]|[a-z0-9._~-]+(?:[/?#]|$))/i,
+ ALLOWED_URI_REGEXP: new RegExp(AV_RICH_TEXT_CUSTOM_PROTOCOL.source +
+ "|" + /^(?:(?:https?|mailto|siyuan|tel|web\+siyuan):|[#/?]|\.\.?\/|[^a-z]|[a-z0-9._~-]+(?:[/?#]|$))/.source, "i"),
};
const BUILTIN_INLINE_COLOR_COUNT = 13;
const MAX_INLINE_FONT_FAMILY_LENGTH = 256;
@@ -190,7 +192,7 @@ const isCleanAVRichTextAssetPath = (value: string) => {
return cleanPath === path;
};
-export const getAVRichTextSafeURL = (value?: string | null, localLinks = false) => {
+export const getAVRichTextSafeURL = (value?: string | null, localLinks = false, customProtocols = false) => {
const original = value || "";
if (!original) {
return "";
@@ -237,7 +239,8 @@ export const getAVRichTextSafeURL = (value?: string | null, localLinks = false)
const opaque = url.slice(scheme.length + 1);
return opaque && !opaque.startsWith("/") ? url : "";
}
- if (scheme !== "http" && scheme !== "https" && scheme !== "siyuan" && scheme !== "web+siyuan") {
+ if (scheme !== "http" && scheme !== "https" && scheme !== "siyuan" && scheme !== "web+siyuan" &&
+ !(customProtocols && AV_RICH_TEXT_CUSTOM_PROTOCOL.test(url))) {
return "";
}
if (!url.toLowerCase().startsWith(`${scheme}://`)) {
diff --git a/app/src/protyle/util/paste.ts b/app/src/protyle/util/paste.ts
index fd42132b0f..491028a62f 100644
--- a/app/src/protyle/util/paste.ts
+++ b/app/src/protyle/util/paste.ts
@@ -119,7 +119,7 @@ const pastePlainTextLink = (protyle: IProtyle, range: Range, text: string) => {
return true;
}
const linkDest = resolveLinkDest(text, protyle.lute);
- if (linkDest && (!restricted || getAVRichTextSafeURL(linkDest, !!getTableCellRichContext(protyle)))) {
+ if (linkDest && (!restricted || getAVRichTextSafeURL(linkDest, !!getTableCellRichContext(protyle), true))) {
protyle.toolbar.range = range;
protyle.toolbar.setInlineMark(protyle, "a", "range", {type: "a", color: linkDest});
return true;
diff --git a/app/tests/avCustomProtocol.test.js b/app/tests/avCustomProtocol.test.js
new file mode 100644
index 0000000000..9d204e488d
--- /dev/null
+++ b/app/tests/avCustomProtocol.test.js
@@ -0,0 +1,147 @@
+const assert = require("node:assert/strict");
+const {readFileSync, mkdtempSync, rmSync} = require("node:fs");
+const path = require("node:path");
+const os = require("node:os");
+
+const runCases = (sources) => {
+ const check = require("node:assert/strict");
+ new Function("module", "exports", sources.lute)(undefined, undefined);
+ new Function("module", "exports", sources.html)(undefined, undefined);
+ window.siyuan = {languages: {}};
+ const opened = [];
+ const dependencies = {
+ "../../toolbar/fontFamilyCore": {},
+ "../../../util/escape": {escapeHtml: window.Lute.EscapeHTMLStr},
+ "../../../constants": {Constants: {}},
+ "../../wysiwyg/codeBlockUtil": {CODE_TAB_SPACE_VALUES: []},
+ "../../util/inlineElementBoundary": {restoreInlineElementBoundaries() {}},
+ "../setLute": {getAgentLute: () => {
+ const lute = window.Lute.New();
+ lute.SetTextMark(true);
+ lute.SetHTMLTag2TextMark(true);
+ lute.SetKramdownIAL(true);
+ lute.SetSpin(true);
+ lute.SetProtyleWYSIWYG(true);
+ lute.SetSanitize(true);
+ return lute;
+ }},
+ "../../../editor/openLink": {openLink: (_app, href) => opened.push(href)},
+ "./attributeValue": {getAVTemplateInteractiveElement: () => undefined},
+ };
+ const load = name => {
+ if (!dependencies[name]) {
+ const exports = {};
+ if (sources[name]) {
+ new Function("require", "exports", sources[name])(load, exports);
+ }
+ dependencies[name] = exports;
+ }
+ return dependencies[name];
+ };
+ const richText = load("./richText");
+ const {avClick} = load("./action");
+ const {handleRichTextInteraction} = load("./blockAttr");
+ for (const target of [
+ "hook://file/EqhAm6xHS?p=YXJjaGl2ZXMvcHJvdmVuYW5jZQ==&n=2026%2D10%2D05",
+ "obsidian://open?vault=notes&file=book", "custom+app://action/path",
+ ]) {
+ let markdown = `[Book](${target})`;
+ for (let round = 0; round < 3; round++) {
+ const blockDOM = richText.getAVRichTextBlockDOM(markdown);
+ const serialized = richText.serializeAVRichTextBlockDOM(blockDOM);
+ check.equal(serialized.plainText, "Book");
+ markdown = serialized.markdown;
+ const host = document.createElement("div");
+ host.className = "av__celltext--rich";
+ host.innerHTML = richText.getAVRichTextPreviewHTML(markdown);
+ document.body.replaceChildren(host);
+ const link = host.querySelector("a[href]");
+ check.ok(link, `link was removed: ${markdown}`);
+ check.equal(link.textContent, "Book");
+ check.equal(link.getAttribute("href"), target);
+ for (const handler of [avClick, handleRichTextInteraction]) {
+ host.onclick = event => handler({app: {}}, event);
+ const count = opened.length;
+ const event = new MouseEvent("click", {bubbles: true, cancelable: true});
+ link.dispatchEvent(event);
+ check.equal(opened.length, count + 1);
+ check.equal(opened.at(-1), target);
+ check.equal(event.defaultPrevented, true);
+ }
+ }
+ }
+ for (const target of [
+ "javascript://host/payload", "vbscript://host/payload", "data://host/payload",
+ "file://host/path", "chrome://settings", "java%73cript://host/payload",
+ ]) {
+ const host = document.createElement("div");
+ host.innerHTML = richText.getAVRichTextPreviewHTML(`[Blocked](${target})`);
+ check.equal(host.querySelector("a[href], [data-href], script, iframe"), null, target);
+ }
+ const imageDOM = '<div data-type="NodeParagraph"><div contenteditable="true">' +
+ '<span class="img"><img data-src="hook://file/example" src="hook://file/example"></span></div></div>';
+ const image = document.createElement("div");
+ image.innerHTML = richText.sanitizeAVRichTextBlockDOM(imageDOM, true, true);
+ check.equal(image.querySelector("img[src], img[data-src]"), null);
+};
+
+const runElectron = async () => {
+ const {app, BrowserWindow} = require("electron");
+ app.setPath("userData", process.argv[2]);
+ app.commandLine.appendSwitch("disable-gpu");
+ await app.whenReady();
+ const win = new BrowserWindow({show: false, webPreferences: {nodeIntegration: true, contextIsolation: false}});
+ let exitCode = 0;
+ try {
+ const ts = require("typescript");
+ const {parse} = require("ifdef-loader/preprocessor");
+ for (const mobile of [false, true]) {
+ const sources = Object.fromEntries(["richText", "richTextValue", "action", "blockAttr"].map(name => ["./" + name,
+ ts.transpileModule(parse(readFileSync(path.join(__dirname, `../src/protyle/render/av/${name}.ts`), "utf8"),
+ {MOBILE: mobile, BROWSER: true}, false, true), {
+ compilerOptions: {module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2021},
+ }).outputText]));
+ sources["./blockAttr"] += "\nexports.handleRichTextInteraction = handleRichTextInteraction;";
+ sources.lute = readFileSync(path.join(__dirname, "../stage/protyle/js/lute/lute.min.js"), "utf8");
+ sources.html = readFileSync(path.join(__dirname, "../stage/protyle/js/protyle-html.js"), "utf8");
+ await win.loadURL("data:text/html,<html><body></body></html>");
+ const error = await win.webContents.executeJavaScript(`(() => {
+ try { (${runCases.toString()})(${JSON.stringify(sources)}); return ""; }
+ catch (error) { return error.stack; }
+ })()`);
+ assert.equal(error, "");
+ }
+ console.log("AV custom protocol cases passed");
+ } catch (error) {
+ console.error(error);
+ exitCode = 1;
+ } finally {
+ win.destroy();
+ app.exit(exitCode);
+ }
+};
+
+if (process.versions.electron && process.type === "browser") {
+ runElectron().catch(error => {
+ console.error(error);
+ require("electron").app.exit(1);
+ });
+} else {
+ require("node:test").it("preserves and opens custom database text links on desktop and mobile", {
+ skip: process.platform === "linux" && !process.env.DISPLAY && !process.env.WAYLAND_DISPLAY,
+ timeout: 45000,
+ }, async () => {
+ const env = {...process.env};
+ delete env.ELECTRON_RUN_AS_NODE;
+ const profile = mkdtempSync(path.join(os.tmpdir(), "siyuan-av-custom-protocol-"));
+ try {
+ const {stdout} = await require("node:util").promisify(require("node:child_process").execFile)(
+ require("electron"), [__filename, profile], {env, windowsHide: true, timeout: 40000});
+ assert.match(stdout, /AV custom protocol cases passed/);
+ } finally {
+ assert.equal(path.dirname(path.resolve(profile)), path.resolve(os.tmpdir()));
+ assert.ok(path.basename(profile).startsWith("siyuan-av-custom-protocol-"));
+ rmSync(profile, {recursive: true, force: true, maxRetries: 5, retryDelay: 100});
+ }
+ });
+}
diff --git a/kernel/av/value.go b/kernel/av/value.go
index 179dd9449a..181a73835e 100644
--- a/kernel/av/value.go
+++ b/kernel/av/value.go
@@ -715,7 +715,7 @@ func isAllowedValueTextRichNode(node *ast.Node, localLinks bool) bool {
ast.NodeFileAnnotationRefText:
return true
case ast.NodeLinkDest:
- return isAllowedValueTextRichLinkTarget(node.TokensStr()) ||
+ return isAllowedValueTextRichLinkTarget(node.TokensStr(), !node.ParentIs(ast.NodeImage)) ||
localLinks && !node.ParentIs(ast.NodeImage) && isAllowedTableCellRichLocalLinkTarget(node.TokensStr())
case ast.NodeCodeBlock, ast.NodeCodeBlockFenceInfoMarker:
return !isValueTextRichExecutableCodeFence(node.CodeBlockInfo)
@@ -791,7 +791,7 @@ func isAllowedValueTextMarkReferenceData(node *ast.Node, localLinks bool) bool {
}
if node.IsTextMarkType("a") {
- if !isAllowedValueTextRichLinkTarget(node.TextMarkAHref) &&
+ if !isAllowedValueTextRichLinkTarget(node.TextMarkAHref, true) &&
!(localLinks && isAllowedTableCellRichLocalLinkTarget(node.TextMarkAHref)) {
return false
}
@@ -809,7 +809,7 @@ func isAllowedValueTextMarkReferenceData(node *ast.Node, localLinks bool) bool {
return true
}
-func isAllowedValueTextRichLinkTarget(target string) bool {
+func isAllowedValueTextRichLinkTarget(target string, customProtocols bool) bool {
if "" == target {
return true
}
@@ -840,8 +840,13 @@ func isAllowedValueTextRichLinkTarget(target string) bool {
strings.HasPrefix(strings.ToLower(target), scheme+"://")
case "":
return isAllowedValueTextRichRelativeLinkTarget(target, decodedTarget, parsed)
- default:
+ case "javascript", "vbscript", "data", "blob", "file", "filesystem", "about", "chrome", "chrome-extension",
+ "devtools", "view-source", "resource", "moz-extension":
return false
+ default:
+ // 自定义协议仅用于主动点击的链接,不能作为图片等自动加载的资源地址。
+ return customProtocols && parsed.Host != "" && parsed.Opaque == "" &&
+ strings.HasPrefix(strings.ToLower(target), scheme+"://")
}
}
diff --git a/kernel/av/value_text_test.go b/kernel/av/value_text_test.go
index 90eb2ba036..c2e4ec38d5 100644
--- a/kernel/av/value_text_test.go
+++ b/kernel/av/value_text_test.go
@@ -18,6 +18,7 @@ package av
import (
"encoding/json"
+ "html"
"strings"
"testing"
@@ -952,6 +953,11 @@ func TestValueTextRichLinkTargetPolicy(t *testing.T) {
"tel:+8613800138000",
"siyuan://blocks/20240101000000-abcdefg?focus=1",
"web+siyuan://bazaar/plugins/example/readme",
+ "hook://file/EqhAm6xHS?p=YXJjaGl2ZXMvcHJvdmVuYW5jZQ==&n=2026%2D10%2D05",
+ "obsidian://open?vault=notes&file=book",
+ "custom+app://action/path",
+ "CUSTOM://action",
+ "ftp://example.com/file",
"../relative/note.md",
"/absolute/path",
"#heading",
@@ -974,8 +980,26 @@ func TestValueTextRichLinkTargetPolicy(t *testing.T) {
"data:text/html,<script>alert(1)</script>",
"file:///tmp/document.txt",
"blob:https://example.com/id",
- "ftp://example.com/file",
- "custom://action",
+ "javascript://host/payload",
+ "JaVaScRiPt://host/payload",
+ "vbscript://host/payload",
+ "data://host/payload",
+ "blob://host/payload",
+ "file://host/payload",
+ "filesystem://host/payload",
+ "about://host/payload",
+ "chrome://settings",
+ "chrome-extension://id/page",
+ "devtools://host/page",
+ "view-source://host/page",
+ "resource://host/page",
+ "moz-extension://id/page",
+ "java%73cript://host/payload",
+ "hook:/file/path",
+ "hook://",
+ "hook:payload",
+ "hook://host/path%0a",
+ "folder:name",
"https:example.com",
"http:///missing-host",
"tel:",
@@ -1010,6 +1034,37 @@ func TestValueTextRichLinkTargetPolicy(t *testing.T) {
}
}
+func TestValueTextRichCustomProtocolRoundTrip(t *testing.T) {
+ const target = "hook://file/EqhAm6xHS?p=YXJjaGl2ZXMvcHJvdmVuYW5jZQ==&n=2026%2D10%2D05"
+ rich := &ValueTextRich{Spec: ValueTextRichSpec, Format: ValueTextRichFormatKramdown,
+ Content: "[Book](" + target + ")"}
+ for range 3 {
+ tree, err := NormalizeValueTextRich(rich)
+ if err != nil {
+ t.Fatal(err)
+ }
+ found := false
+ ast.Walk(tree.Root, func(node *ast.Node, entering bool) ast.WalkStatus {
+ if entering && node.Type == ast.NodeTextMark && node.IsTextMarkType("a") {
+ found = true
+ if html.UnescapeString(node.TextMarkAHref) != target || node.TextMarkTextContent != "Book" {
+ t.Fatalf("custom link changed: %+v", node)
+ }
+ }
+ return ast.WalkContinue
+ })
+ if !found {
+ t.Fatal("custom protocol link was removed")
+ }
+ }
+ image := &ast.Node{Type: ast.NodeImage}
+ destination := &ast.Node{Type: ast.NodeLinkDest, Tokens: []byte(target)}
+ image.AppendChild(destination)
+ if isAllowedValueTextRichNode(destination, true) {
+ t.Fatal("custom protocols must not be loaded as images")
+ }
+}
+
func TestValueTextRichRejectsObfuscatedLinkTargets(t *testing.T) {
contents := []string{
"[classic](<vbscript:msgbox(1)>)",