Skip to content

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

:lock: Escape block names and widget info in hint, backlink and breadcrumb rendering https://github.com/siyuan-note/siyuan/security/advisories/GHSA-hf87-qh3j-3p88

Daniel7776e0212584
diff --git a/app/src/protyle/breadcrumb/index.ts b/app/src/protyle/breadcrumb/index.ts
index aa09011db2..71f730c957 100644
--- a/app/src/protyle/breadcrumb/index.ts
+++ b/app/src/protyle/breadcrumb/index.ts
@@ -35,7 +35,7 @@ import {resize} from "../util/resize";
import {listIndent, listOutdent} from "../wysiwyg/list";
import {improveBreadcrumbAppearance} from "../wysiwyg/renderBacklink";
import {getCloudURL} from "../../config/util/about";
-import {escapeAriaLabel} from "../../util/escape";
+import {decodeHTML, escapeAttr, escapeAriaLabel, escapeHtml, escapeSearchHighlight} from "../../util/escape";
import {refreshUndoButtons} from "../undo/globalUndo";
import {getAllEditor} from "../../layout/getAll";
import {genEmbedStatTip, type IBlockStat, type IEmbedStat} from "../../layout/status";
@@ -1138,7 +1138,7 @@ ${padHTML}
} else {
html += `<span class="protyle-breadcrumb__item${isCurrent ? " protyle-breadcrumb__item--active" : ""}" data-node-id="${item.id}" role="button" tabindex="-1" aria-label="${escapeAriaLabel(item.name || window.siyuan.languages.untitled)}"${(response.data.length === 1 || index === 0) ? ' style="max-width:none"' : ""}>
<svg class="popover__block" data-id="${item.id}"><use xlink:href="#${getIconByType(item.type, item.subType)}"></use></svg>
- ${item.name ? `<span class="protyle-breadcrumb__text" title="${item.name}">${item.name}</span>` : ""}
+ ${item.name ? `<span class="protyle-breadcrumb__text" title="${escapeAttr(escapeHtml(decodeHTML(item.name)))}">${escapeSearchHighlight(item.name)}</span>` : ""}
</span>`;
}
if (index !== response.data.length - 1) {
diff --git a/app/src/protyle/hint/extend.ts b/app/src/protyle/hint/extend.ts
index 0b71576c3e..76cc2b797d 100644
--- a/app/src/protyle/hint/extend.ts
+++ b/app/src/protyle/hint/extend.ts
@@ -14,7 +14,7 @@ import {transaction} from "../wysiwyg/transaction";
import {getAssetExtension, getAssetName, getDisplayName, isEncryptedBox} from "../../util/pathName";
import {genEmptyElement} from "../../block/util";
import {getOrderedListStart, updateListOrder} from "../wysiwyg/list";
-import {escapeHtml, stripSearchMark} from "../../util/escape";
+import {escapeHtml, escapeSearchHighlight, stripSearchMark} from "../../util/escape";
import {zoomOut} from "../../menus/protyle";
import {hideElements} from "../ui/hideElements";
import {genAssetHTML} from "../../asset/renderAssets";
@@ -456,13 +456,13 @@ export const genHintItemHTML = (item: IBlock) => {
}
let attrHTML = "";
if (item.name) {
- attrHTML += `<span class="fn__flex"><svg class="b3-list-item__hinticon"><use xlink:href="#iconN"></use></svg><span>${item.name}</span></span><span class="fn__space"></span>`;
+ attrHTML += `<span class="fn__flex"><svg class="b3-list-item__hinticon"><use xlink:href="#iconN"></use></svg><span>${escapeSearchHighlight(item.name)}</span></span><span class="fn__space"></span>`;
}
if (item.alias) {
- attrHTML += `<span class="fn__flex"><svg class="b3-list-item__hinticon"><use xlink:href="#iconA"></use></svg><span>${item.alias}</span></span><span class="fn__space"></span>`;
+ attrHTML += `<span class="fn__flex"><svg class="b3-list-item__hinticon"><use xlink:href="#iconA"></use></svg><span>${escapeSearchHighlight(item.alias)}</span></span><span class="fn__space"></span>`;
}
if (item.memo) {
- attrHTML += `<span class="fn__flex"><svg class="b3-list-item__hinticon"><use xlink:href="#iconM"></use></svg><span>${item.memo}</span></span>`;
+ attrHTML += `<span class="fn__flex"><svg class="b3-list-item__hinticon"><use xlink:href="#iconM"></use></svg><span>${escapeSearchHighlight(item.memo)}</span></span>`;
}
if (attrHTML) {
attrHTML = `<div class="fn__flex b3-list-item__meta b3-list-item__showall">${attrHTML}</div>`;
@@ -527,14 +527,14 @@ export const hintRef = (key: string, protyle: IProtyle, source: THintSource): IH
createItemCount++;
}
response.data.blocks.forEach((item: IBlock) => {
- const name = item.name ? stripSearchMark(item.name) : item.refText.replace(new RegExp(Constants.ZWSP, "g"), "");
+ const name = item.name ? stripSearchMark(escapeSearchHighlight(item.name)) : item.refText.replace(new RegExp(Constants.ZWSP, "g"), "");
let value = `<span data-type="block-ref" data-id="${item.id}" data-subtype="d">${name}</span>`;
if (source === "search") {
value = `<span data-type="block-ref" data-id="${item.id}" data-subtype="s">${key}${Constants.ZWSP}${name}</span>`;
} else if (source === "av") {
let refText = name;
if (nodeElement) {
- refText = item.ial["custom-sy-av-s-text-" + nodeElement.getAttribute("data-av-id")] || refText;
+ refText = escapeHtml(item.ial["custom-sy-av-s-text-" + nodeElement.getAttribute("data-av-id")] || "") || refText;
}
value = `<span data-type="block-ref" data-id="${item.id}" data-subtype="s">${refText}</span>`;
}
diff --git a/app/src/protyle/hint/index.ts b/app/src/protyle/hint/index.ts
index b2c3160ce0..b1a0f437ef 100644
--- a/app/src/protyle/hint/index.ts
+++ b/app/src/protyle/hint/index.ts
@@ -34,7 +34,7 @@ import {highlightRender} from "../render/highlightRender";
import {assetMenu, imgMenu} from "../../menus/protyle";
import {hideElements} from "../ui/hideElements";
import {fetchPost} from "../../util/fetch";
-import {stripSearchMark} from "../../util/escape";
+import {escapeHtml, escapeSearchHighlight, stripSearchMark} from "../../util/escape";
import {getDisplayName, isEncryptedBox, pathPosix} from "../../util/pathName";
import {
addEmoji,
@@ -515,9 +515,9 @@ export class Hint {
let blockRefHTML;
if (source === "av") {
// av 搜索时需要获取值 https://github.com/siyuan-note/siyuan/issues/12020
- let refText = item.name ? stripSearchMark(item.name) : item.refText.replace(new RegExp(Constants.ZWSP, "g"), "");
+ let refText = item.name ? stripSearchMark(escapeSearchHighlight(item.name)) : item.refText.replace(new RegExp(Constants.ZWSP, "g"), "");
if (nodeElement) {
- refText = item.ial["custom-sy-av-s-text-" + nodeElement.getAttribute("data-av-id")] || refText;
+ refText = escapeHtml(item.ial["custom-sy-av-s-text-" + nodeElement.getAttribute("data-av-id")] || "") || refText;
}
blockRefHTML = `<span data-type="block-ref" data-id="${item.id}" data-subtype="s">${refText}</span>`;
} else {
diff --git a/app/src/protyle/toolbar/index.ts b/app/src/protyle/toolbar/index.ts
index 3b72193d2d..1143fe687f 100644
--- a/app/src/protyle/toolbar/index.ts
+++ b/app/src/protyle/toolbar/index.ts
@@ -53,7 +53,7 @@ import {linkMenu} from "../../menus/protyle";
import {addScript} from "../util/addScript";
import {confirmDialog} from "../../dialog/confirmDialog";
import {paste, pasteAsPlainText, pasteEscaped} from "../util/paste";
-import {escapeHtml} from "../../util/escape";
+import {escapeAttr, escapeHtml} from "../../util/escape";
import {resizeSide} from "../../history/resizeSide";
import {activeBlur, updateMobilePluginToolbar} from "../../mobile/util/keyboardToolbar";
import {FormatPainter} from "./FormatPainter";
@@ -2077,9 +2077,9 @@ export class Toolbar {
content: string,
name: string
}, index: number) => {
- searchHTML += `<div data-value="${item.path}" data-content="${item.content}" class="b3-list-item${index === 0 ? " b3-list-item--focus" : ""}">
- ${item.name}
- <span class="b3-list-item__meta">${item.content}</span>
+ searchHTML += `<div data-value="${escapeAttr(item.path)}" data-content="${escapeAttr(item.content)}" class="b3-list-item${index === 0 ? " b3-list-item--focus" : ""}">
+ ${escapeHtml(item.name)}
+ <span class="b3-list-item__meta">${escapeHtml(item.content)}</span>
</div>`;
});
listElement.innerHTML = searchHTML;
diff --git a/app/src/protyle/wysiwyg/renderBacklink.ts b/app/src/protyle/wysiwyg/renderBacklink.ts
index 0fb3ef4ddd..da59075423 100644
--- a/app/src/protyle/wysiwyg/renderBacklink.ts
+++ b/app/src/protyle/wysiwyg/renderBacklink.ts
@@ -9,6 +9,7 @@ import {disabledForeverProtyle, disabledProtyle} from "../util/onGet";
import {avRender} from "../render/av/render";
import {hasClosestByAttribute} from "../util/hasClosest";
import {isEncryptedBox} from "../../util/pathName";
+import {decodeHTML, escapeAttr, escapeHtml, escapeSearchHighlight} from "../../util/escape";
interface IBacklinkData {
id?: string,
@@ -216,7 +217,7 @@ export const genBreadcrumb = (blockPaths: IBreadcrumb[], renderFirst: boolean, p
}
html += `<span class="protyle-breadcrumb__item${index === blockPaths.length - 1 ? " protyle-breadcrumb__item--active" : ""}" data-id="${item.id}">
<svg class="popover__block" data-id="${item.id}"><use xlink:href="#${getIconByType(item.type, item.subType)}"></use></svg>
- ${item.name ? `<span class="protyle-breadcrumb__text" title="${item.name}">${item.name}</span>` : ""}
+ ${item.name ? `<span class="protyle-breadcrumb__text" title="${escapeAttr(escapeHtml(decodeHTML(item.name)))}">${escapeSearchHighlight(item.name)}</span>` : ""}
</span>`;
if (index !== blockPaths.length - 1) {
html += '<svg class="protyle-breadcrumb__arrow"><use xlink:href="#iconRight"></use></svg>';
diff --git a/app/src/util/escape.test.ts b/app/src/util/escape.test.ts
index 09f37be26b..bd660b518d 100644
--- a/app/src/util/escape.test.ts
+++ b/app/src/util/escape.test.ts
@@ -1,6 +1,6 @@
import {describe, it} from "node:test";
import * as assert from "node:assert/strict";
-import {stripSearchMark} from "./escape";
+import {escapeSearchHighlight, stripSearchMark} from "./escape";
describe("strip search mark", () => {
it("removes search highlights without decoding escaped block names", () => {
@@ -8,3 +8,15 @@ describe("strip search mark", () => {
assert.equal(stripSearchMark("&lt;mark&gt;<mark>xw</mark>&lt;/mark&gt;"), "&lt;mark&gt;xw&lt;/mark&gt;");
});
});
+
+describe("escape search highlight", () => {
+ it("preserves search highlight tags", () => {
+ assert.equal(escapeSearchHighlight("&lt;mark&gt;<mark>xw</mark>&lt;/mark&gt;"), "&lt;mark&gt;<mark>xw</mark>&lt;/mark&gt;");
+ });
+
+ it("escapes raw HTML tags except mark", () => {
+ assert.equal(escapeSearchHighlight('<img src="x" onerror="alert(1)">'), '&lt;img src="x" onerror="alert(1)">');
+ assert.equal(escapeSearchHighlight("<script>alert(1)</script>"), "&lt;script>alert(1)&lt;/script>");
+ assert.equal(escapeSearchHighlight('<mark onclick="alert(1)">x</mark>'), '&lt;mark onclick="alert(1)">x</mark>');
+ });
+});
diff --git a/app/src/util/escape.ts b/app/src/util/escape.ts
index 179df56307..fb6d8ba38d 100644
--- a/app/src/util/escape.ts
+++ b/app/src/util/escape.ts
@@ -9,6 +9,11 @@ export const stripSearchMark = (html: string) => {
return html.replace(/<\/?mark>/g, "");
};
+// 仅转义非搜索高亮的 < 字符,保留内核插入的 <mark> 高亮标签
+export const escapeSearchHighlight = (html: string) => {
+ return html.replace(/<(?!\/?mark>)/g, "&lt;");
+};
+
export const escapeLessThans = (html: string) => {
return html.replace(/</g, "&lt;");
};