Skip to content

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

:lock: Escape block attributes rendered in search result rows

Daniel1c5782603119
diff --git a/app/src/search/attrs.test.ts b/app/src/search/attrs.test.ts
new file mode 100644
index 0000000000..760405deb4
--- /dev/null
+++ b/app/src/search/attrs.test.ts
@@ -0,0 +1,30 @@
+import {describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+import {getAttr} from "./attrs";
+
+const payload = "<img src=x onerror=alert(1)>";
+
+describe("getAttr", () => {
+ it("escapes raw HTML in name, alias and memo", () => {
+ const html = getAttr({name: payload, alias: payload, memo: payload});
+ assert.equal(html.includes("<img"), false);
+ // escapeSearchHighlight 只转义 <,裸 > 在文本上下文中无害
+ assert.equal(html.match(/&lt;img src=x onerror=alert\(1\)>/g)?.length, 3);
+ });
+
+ it("preserves the search highlight inserted by the kernel", () => {
+ const html = getAttr({name: "", alias: "&lt;b&gt;<mark>xw</mark>", memo: ""});
+ assert.equal(html.includes("<mark>xw</mark>"), true);
+ assert.equal(html.includes("&lt;b&gt;"), true);
+ });
+
+ it("does not escape the text already escaped by the kernel twice", () => {
+ const html = getAttr({name: "a&amp;b", alias: "", memo: ""});
+ assert.equal(html.includes("a&amp;b"), true);
+ assert.equal(html.includes("&amp;amp;"), false);
+ });
+
+ it("omits empty attributes", () => {
+ assert.equal(getAttr({name: "", alias: "", memo: ""}), "");
+ });
+});
diff --git a/app/src/search/attrs.ts b/app/src/search/attrs.ts
index 413bb659a8..cb7bd3f1e0 100644
--- a/app/src/search/attrs.ts
+++ b/app/src/search/attrs.ts
@@ -1,13 +1,15 @@
+import {escapeSearchHighlight} from "../util/escape";
+
export const getAttr = (block: Pick<IBlock, "name" | "alias" | "memo">) => {
let attrHTML = "";
if (block.name) {
- attrHTML += `<span class="b3-list-item__meta fn__flex" style="max-width: 30%"><svg class="b3-list-item__hinticon"><use xlink:href="#iconN"></use></svg><span class="b3-list-item__hinttext">${block.name}</span></span>`;
+ attrHTML += `<span class="b3-list-item__meta fn__flex" style="max-width: 30%"><svg class="b3-list-item__hinticon"><use xlink:href="#iconN"></use></svg><span class="b3-list-item__hinttext">${escapeSearchHighlight(block.name)}</span></span>`;
}
if (block.alias) {
- attrHTML += `<span class="b3-list-item__meta fn__flex" style="max-width: 30%"><svg class="b3-list-item__hinticon"><use xlink:href="#iconA"></use></svg><span class="b3-list-item__hinttext">${block.alias}</span></span>`;
+ attrHTML += `<span class="b3-list-item__meta fn__flex" style="max-width: 30%"><svg class="b3-list-item__hinticon"><use xlink:href="#iconA"></use></svg><span class="b3-list-item__hinttext">${escapeSearchHighlight(block.alias)}</span></span>`;
}
if (block.memo) {
- attrHTML += `<span class="b3-list-item__meta fn__flex" style="max-width: 30%"><svg class="b3-list-item__hinticon"><use xlink:href="#iconM"></use></svg><span class="b3-list-item__hinttext">${block.memo}</span></span>`;
+ attrHTML += `<span class="b3-list-item__meta fn__flex" style="max-width: 30%"><svg class="b3-list-item__hinticon"><use xlink:href="#iconM"></use></svg><span class="b3-list-item__hinttext">${escapeSearchHighlight(block.memo)}</span></span>`;
}
return attrHTML;
};