Skip to content

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

:bug: Decode IAL attribute values once when building the block index

Danielf124590a6a91
diff --git a/kernel/sql/database.go b/kernel/sql/database.go
index 80da24810f..6e72df5d66 100644
--- a/kernel/sql/database.go
+++ b/kernel/sql/database.go
@@ -955,9 +955,10 @@ func buildBlockFromNode(n *ast.Node, tree *parse.Tree) (block *Block, attributes
boxID := tree.Box
p := tree.Path
rootID := tree.Root.ID
- name := html.UnescapeString(n.IALAttr("name"))
- alias := html.UnescapeString(n.IALAttr("alias"))
- memo := html.UnescapeString(n.IALAttr("memo"))
+ // IALAttr 已解码过一次属性值转义,此处不再解码,否则落盘时正确转义的值会被还原成原始 HTML 进入索引
+ name := n.IALAttr("name")
+ alias := n.IALAttr("alias")
+ memo := n.IALAttr("memo")
tag := tagFromNode(n)
var content, fcontent, markdown, parentID string
@@ -1059,7 +1060,8 @@ func tagFromNode(node *ast.Node) (ret string) {
tagBuilder := bytes.Buffer{}
if ast.NodeDocument == node.Type {
- tagIAL := html.UnescapeString(node.IALAttr("tags"))
+ // 与 buildBlockFromNode 一致,IALAttr 已解码过一次属性值转义
+ tagIAL := node.IALAttr("tags")
tags := strings.SplitSeq(tagIAL, ",")
for t := range tags {
t = strings.TrimSpace(t)
diff --git a/kernel/sql/ial_decode_test.go b/kernel/sql/ial_decode_test.go
new file mode 100644
index 0000000000..a5e93c5bc7
--- /dev/null
+++ b/kernel/sql/ial_decode_test.go
@@ -0,0 +1,60 @@
+package sql
+
+import (
+ "strings"
+ "testing"
+
+ "github.com/88250/lute/ast"
+ "github.com/88250/lute/parse"
+ "github.com/siyuan-note/siyuan/kernel/util"
+)
+
+// IAL 属性值在 .sy 中以转义形态存储,IALAttr 读取时已解码一次,
+// 索引构建不得再解码,否则用户输入的字面量实体文本会在索引中变成原始 HTML
+func TestBuildBlockFromNodeDecodesIALAttrOnce(t *testing.T) {
+ engine := util.NewLute()
+ for _, tc := range []struct{ name, typed, want string }{
+ {"raw html typed by user", "<b>", "<b>"},
+ {"literal entity typed by user", "&lt;b&gt;", "&lt;b&gt;"},
+ {"plain text", "R&D", "R&D"},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ tree := parse.Parse("", []byte("hello"), engine.ParseOptions)
+ tree.Box = "20260919000000-box0000"
+ tree.Path = "/20260919000000-root000.sy"
+ tree.HPath = "/doc"
+ tree.Root.ID = "20260919000000-root000"
+
+ node := tree.Root.FirstChild
+ if nil == node || ast.NodeParagraph != node.Type {
+ t.Fatalf("unexpected first child: %v", node)
+ }
+ node.ID = "20260919000000-para000"
+ node.SetIALAttr("name", tc.typed)
+ node.SetIALAttr("alias", tc.typed)
+ node.SetIALAttr("memo", tc.typed)
+
+ block, _ := buildBlockFromNode(node, tree)
+ for field, got := range map[string]string{"name": block.Name, "alias": block.Alias, "memo": block.Memo} {
+ if got != tc.want {
+ t.Errorf("%s: want %q, got %q", field, tc.want, got)
+ }
+ }
+ })
+ }
+}
+
+// 文档级 tags 属性同样只能解码一次,标签索引不得出现原始 HTML
+func TestTagFromNodeDecodesTagsOnce(t *testing.T) {
+ node := &ast.Node{Type: ast.NodeDocument}
+ node.SetIALAttr("tags", "&lt;b&gt;,plain")
+
+ got := tagFromNode(node)
+ want := "#&lt;b&gt;# #plain#"
+ if got != want {
+ t.Fatalf("want %q, got %q", want, got)
+ }
+ if strings.ContainsAny(got, "<>") {
+ t.Fatalf("tag index contains raw HTML: %q", got)
+ }
+}