Skip to content

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

:lock: Prevent stored XSS via document titles in the backlink dock https://github.com/siyuan-note/siyuan/security/advisories/GHSA-c5h9-g2c6-fxjw

Daniel6f093ebe50af
diff --git a/app/src/types/api/index.d.ts b/app/src/types/api/index.d.ts
index 5f3916ac00..b2c1bbfa9d 100644
--- a/app/src/types/api/index.d.ts
+++ b/app/src/types/api/index.d.ts
@@ -554,7 +554,7 @@ export type BacklinkList = { "backlinks": Array<BacklinkPath | null> | null; "ba
export type BacklinkListRequestInput = { "containChildren"?: boolean | null; "id"?: string | null; "includeMentions"?: boolean | null; "k"?: string; "knownRevision"?: string | null; "mSort"?: string | null; "mk"?: string; "notebook"?: string | null; "refDefCandidates"?: boolean | null; "sort"?: string | null; "sourceFilter"?: BacklinkSourceFilterInput | null; };
-export type BacklinkPath = { "blocks"?: Array<SearchBlock | null>; "box": string; "children"?: Array<SearchPath | null>; "count": number; "created": string; "depth": number; "folded": boolean; "hPath": string; "id": string; "name": string; "nodeType": string; "number"?: string; "revision": string; "subType": string; "type": string; "updated": string; };
+export type BacklinkPath = { "blocks"?: Array<SearchBlock | null>; "box": string; "children"?: Array<SearchPath | null>; "count": number; "created": string; "depth": number; "folded": boolean; "hPath": string; "id": string; "name": string; "nameIsHTML"?: boolean; "nodeType": string; "number"?: string; "revision": string; "subType": string; "type": string; "updated": string; };
export type BacklinkRefDef = { "id": string; "path": string; "text": string; };
@@ -1612,7 +1612,7 @@ export type SearchListFilterInput = { "o"?: boolean | null; "t"?: boolean | null
export type SearchPageRequestInput = { "page"?: number | null; "pageSize"?: number | null; };
-export type SearchPath = { "blocks"?: Array<SearchBlock | null>; "box": string; "children"?: Array<SearchPath | null>; "count": number; "created": string; "depth": number; "folded": boolean; "hPath": string; "id": string; "name": string; "nodeType": string; "number"?: string; "subType": string; "type": string; "updated": string; };
+export type SearchPath = { "blocks"?: Array<SearchBlock | null>; "box": string; "children"?: Array<SearchPath | null>; "count": number; "created": string; "depth": number; "folded": boolean; "hPath": string; "id": string; "name": string; "nameIsHTML"?: boolean; "nodeType": string; "number"?: string; "subType": string; "type": string; "updated": string; };
export type SearchPathRequestInput = { "path": string; };
diff --git a/app/src/types/index.d.ts b/app/src/types/index.d.ts
index e4332f898c..dfd103173e 100644
--- a/app/src/types/index.d.ts
+++ b/app/src/types/index.d.ts
@@ -951,6 +951,7 @@ interface IBlockTree {
hPath?: string,
subType?: string,
name: string,
+ nameIsHTML?: boolean,
type: string,
depth: number,
url?: string,
diff --git a/app/src/util/Tree.test.ts b/app/src/util/Tree.test.ts
new file mode 100644
index 0000000000..bd9df00460
--- /dev/null
+++ b/app/src/util/Tree.test.ts
@@ -0,0 +1,103 @@
+import {describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+
+(globalThis as any).SIYUAN_VERSION = "test";
+(globalThis as any).NODE_ENV = "test";
+
+// 记录 createTopLevelItem 写入模板的 HTML,用于断言最终进入 innerHTML 的内容
+let capturedHTML = "";
+
+const fakeElement = () => ({
+ innerHTML: "",
+ querySelector: (): null => null,
+ querySelectorAll: (): unknown[] => [],
+ getAttribute: (): null => null,
+ setAttribute: (): void => undefined,
+ addEventListener: (): void => undefined,
+});
+
+(globalThis as any).document = {
+ getElementById: (): null => null,
+ querySelectorAll: (): unknown[] => [],
+ // 真实浏览器的 HTMLTemplateElement.innerHTML 会转发到 content,这里按同样语义模拟
+ createElement: () => {
+ const content = {
+ get innerHTML(): string {
+ return capturedHTML;
+ },
+ set innerHTML(value: string) {
+ capturedHTML = value;
+ },
+ // createTopLevelItem 会取回列表项节点交给 mathRender,这里返回一个可用的替身
+ querySelector: () => fakeElement(),
+ };
+ return {
+ content,
+ get innerHTML(): string {
+ return capturedHTML;
+ },
+ set innerHTML(value: string) {
+ capturedHTML = value;
+ },
+ };
+ },
+};
+
+// 空数据构造时会读取 window.siyuan.languages,这里仅提供必需的取词
+(globalThis as any).window = {
+ siyuan: {languages: {emptyContent: "empty"}},
+};
+
+// 大纲条目会调用全局 Lute 生成 aria-label,这里提供最小替身
+(globalThis as any).Lute = {
+ BlockDOM2Content: (value: string) => value,
+ EscapeHTMLStr: (value: string) => value,
+};
+
+type TreeInstance = { createTopLevelItem: (item: IBlockTree) => unknown };
+
+const renderItem = async (item: IBlockTree) => {
+ capturedHTML = "";
+ const {Tree} = await import("./Tree");
+ const tree = new Tree({element: fakeElement() as unknown as HTMLElement, data: []}) as unknown as TreeInstance;
+ tree.createTopLevelItem(item);
+ return capturedHTML;
+};
+
+describe("tree item name escaping", () => {
+ it("escapes backlink document titles so they cannot inject markup", async () => {
+ const html = await renderItem({
+ id: "20260101120000-abcdefg",
+ name: "<img src=x onerror=alert(document.domain)>",
+ type: "backlink",
+ depth: 0,
+ count: 0,
+ });
+ assert.ok(!html.includes("<img"), `raw img tag reached innerHTML: ${html}`);
+ // escapeHtml 只处理 & 和 <,转义 < 已足以让标签无法成立
+ assert.ok(html.includes("&lt;img src=x onerror=alert(document.domain)>"), `actual: ${JSON.stringify(html)}`);
+ });
+
+ it("escapes ampersands and angle brackets in plain names", async () => {
+ const html = await renderItem({
+ name: "a&b<c",
+ type: "backlink",
+ depth: 0,
+ count: 0,
+ });
+ assert.ok(html.includes("a&amp;b&lt;c"), html);
+ });
+
+ it("preserves intentional HTML when the producer marks the name as HTML", async () => {
+ const html = await renderItem({
+ id: "20260101120000-abcdefg",
+ name: "<span style=\"color: red;\">标题</span>",
+ nameIsHTML: true,
+ type: "outline",
+ depth: 0,
+ count: 0,
+ });
+ assert.ok(html.includes("<span style=\"color: red;\">标题</span>"), html);
+ assert.ok(!html.includes("&lt;span"), html);
+ });
+});
diff --git a/app/src/util/Tree.ts b/app/src/util/Tree.ts
index 1b9fbd1b2a..07f9051242 100644
--- a/app/src/util/Tree.ts
+++ b/app/src/util/Tree.ts
@@ -130,7 +130,7 @@ ${item.label !== undefined && item.label !== null ? `data-label='${item.label}'`
</span>
${iconHTML}
${numberHTML}
- <span class="b3-list-item__text ariaLabel" data-position="${this.titleTooltipPosition}"${titleTip}>${item.name}</span>
+ <span class="b3-list-item__text ariaLabel" data-position="${this.titleTooltipPosition}"${titleTip}>${item.nameIsHTML ? item.name : escapeHtml(item.name)}</span>
${getTreeItemTailHTML(countHTML, this.topExtHTML || "", isM)}
</li>`;
if (item.children && item.children.length > 0) {
diff --git a/kernel/api/contract_search_block.go b/kernel/api/contract_search_block.go
index 2ae665ea21..5a8ef432ee 100644
--- a/kernel/api/contract_search_block.go
+++ b/kernel/api/contract_search_block.go
@@ -42,7 +42,7 @@ func searchPathContracts(values []*model.Path) []*apicontract.SearchPath {
result := make([]*apicontract.SearchPath, len(values))
for i, value := range values {
if value != nil {
- result[i] = &apicontract.SearchPath{ID: value.ID, Box: value.Box, Name: value.Name, Number: value.Number, HPath: value.HPath, Type: value.Type, NodeType: value.NodeType, SubType: value.SubType, Blocks: searchBlockContracts(value.Blocks), Children: searchPathContracts(value.Children), Depth: value.Depth, Count: value.Count, Folded: value.Folded, Updated: value.Updated, Created: value.Created}
+ result[i] = &apicontract.SearchPath{ID: value.ID, Box: value.Box, Name: value.Name, NameIsHTML: value.NameIsHTML, Number: value.Number, HPath: value.HPath, Type: value.Type, NodeType: value.NodeType, SubType: value.SubType, Blocks: searchBlockContracts(value.Blocks), Children: searchPathContracts(value.Children), Depth: value.Depth, Count: value.Count, Folded: value.Folded, Updated: value.Updated, Created: value.Created}
}
}
return result
diff --git a/kernel/apicontract/outline.go b/kernel/apicontract/outline.go
index b421d5ab13..ca9c9a6aca 100644
--- a/kernel/apicontract/outline.go
+++ b/kernel/apicontract/outline.go
@@ -18,21 +18,22 @@ type HeadingNumbersRequest struct {
}
type SearchPath struct {
- ID string `json:"id"`
- Box string `json:"box"`
- Name string `json:"name"`
- Number string `json:"number,omitempty"`
- HPath string `json:"hPath"`
- Type string `json:"type"`
- NodeType string `json:"nodeType"`
- SubType string `json:"subType"`
- Blocks []*SearchBlock `json:"blocks,omitempty"`
- Children []*SearchPath `json:"children,omitempty"`
- Depth int `json:"depth"`
- Count int `json:"count"`
- Folded bool `json:"folded"`
- Updated string `json:"updated"`
- Created string `json:"created"`
+ ID string `json:"id"`
+ Box string `json:"box"`
+ Name string `json:"name"`
+ NameIsHTML bool `json:"nameIsHTML,omitempty"`
+ Number string `json:"number,omitempty"`
+ HPath string `json:"hPath"`
+ Type string `json:"type"`
+ NodeType string `json:"nodeType"`
+ SubType string `json:"subType"`
+ Blocks []*SearchBlock `json:"blocks,omitempty"`
+ Children []*SearchPath `json:"children,omitempty"`
+ Depth int `json:"depth"`
+ Count int `json:"count"`
+ Folded bool `json:"folded"`
+ Updated string `json:"updated"`
+ Created string `json:"created"`
}
func init() {
diff --git a/kernel/apicontract/schema.json b/kernel/apicontract/schema.json
index 99b309ac28..1c5d848e31 100644
--- a/kernel/apicontract/schema.json
+++ b/kernel/apicontract/schema.json
@@ -14161,6 +14161,9 @@
"name": {
"type": "string"
},
+ "nameIsHTML": {
+ "type": "boolean"
+ },
"nodeType": {
"type": "string"
},
@@ -35247,6 +35250,9 @@
"name": {
"type": "string"
},
+ "nameIsHTML": {
+ "type": "boolean"
+ },
"nodeType": {
"type": "string"
},
diff --git a/kernel/model/block.go b/kernel/model/block.go
index 1ab26fca42..9ae5bf443c 100644
--- a/kernel/model/block.go
+++ b/kernel/model/block.go
@@ -97,19 +97,20 @@ func (block *Block) IsDoc() bool {
}
type Path struct {
- ID string `json:"id"` // 块 ID
- Box string `json:"box"` // 块 Box
- Name string `json:"name"` // 当前路径
- Number string `json:"number,omitempty"` // 标题编号
- HPath string `json:"hPath"` // 人类可读路径
- Type string `json:"type"` // "path"
- NodeType string `json:"nodeType"` // 节点类型
- SubType string `json:"subType"` // 节点子类型
- Blocks []*Block `json:"blocks,omitempty"` // 子块节点
- Children []*Path `json:"children,omitempty"` // 子路径节点
- Depth int `json:"depth"` // 层级深度
- Count int `json:"count"` // 子块计数
- Folded bool `json:"folded"` // 是否折叠
+ ID string `json:"id"` // 块 ID
+ Box string `json:"box"` // 块 Box
+ Name string `json:"name"` // 当前路径
+ NameIsHTML bool `json:"nameIsHTML,omitempty"` // Name 是否为渲染后的 HTML,前端据此决定是否转义
+ Number string `json:"number,omitempty"` // 标题编号
+ HPath string `json:"hPath"` // 人类可读路径
+ Type string `json:"type"` // "path"
+ NodeType string `json:"nodeType"` // 节点类型
+ SubType string `json:"subType"` // 节点子类型
+ Blocks []*Block `json:"blocks,omitempty"` // 子块节点
+ Children []*Path `json:"children,omitempty"` // 子路径节点
+ Depth int `json:"depth"` // 层级深度
+ Count int `json:"count"` // 子块计数
+ Folded bool `json:"folded"` // 是否折叠
Updated string `json:"updated"` // 更新时间
Created string `json:"created"` // 创建时间
diff --git a/kernel/model/outline.go b/kernel/model/outline.go
index d4f82caddc..794b7c4bd1 100644
--- a/kernel/model/outline.go
+++ b/kernel/model/outline.go
@@ -362,16 +362,17 @@ func outline(tree *parse.Tree) (ret []*Path) {
for _, b := range children {
resetDepth(b, 0)
ret = append(ret, &Path{
- ID: b.ID,
- Box: b.Box,
- Name: b.Content,
- NodeType: b.Type,
- Type: "outline",
- SubType: b.SubType,
- Blocks: b.Children,
- Depth: 0,
- Count: b.Count,
- Folded: true,
+ ID: b.ID,
+ Box: b.Box,
+ Name: b.Content,
+ NameIsHTML: true,
+ NodeType: b.Type,
+ Type: "outline",
+ SubType: b.SubType,
+ Blocks: b.Children,
+ Depth: 0,
+ Count: b.Count,
+ Folded: true,
})
}
}