Skip to content

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

:bug: Tag names are double escaped and bookmarks cannot be renamed or removed

Daniel4c8ebb8db1a3
diff --git a/kernel/api/tag.go b/kernel/api/tag.go
index b36428c8ac..5bd6886882 100644
--- a/kernel/api/tag.go
+++ b/kernel/api/tag.go
@@ -20,6 +20,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/siyuan-note/siyuan/kernel/apicontract"
"github.com/siyuan-note/siyuan/kernel/model"
+ "github.com/siyuan-note/siyuan/kernel/util"
)
func tagContracts(tags model.Tags) []*apicontract.TagData {
@@ -29,7 +30,8 @@ func tagContracts(tags model.Tags) []*apicontract.TagData {
ret := make([]*apicontract.TagData, len(tags))
for i, tag := range tags {
if tag != nil {
- ret[i] = &apicontract.TagData{Name: tag.Name, Label: tag.Label, Children: tagContracts(tag.Children),
+ // 模型层的 Name/Label 为 HTML 转义形态,前端负责按上下文转义,这里还原为纯文本
+ ret[i] = &apicontract.TagData{Name: util.UnescapeHTML(tag.Name), Label: tag.Label, Children: tagContracts(tag.Children),
Type: tag.Type, Depth: tag.Depth, Count: tag.Count}
}
}
diff --git a/kernel/model/bookmark.go b/kernel/model/bookmark.go
index 948a6fc007..197546cba2 100644
--- a/kernel/model/bookmark.go
+++ b/kernel/model/bookmark.go
@@ -19,6 +19,7 @@ package model
import (
"errors"
"fmt"
+ "html"
"path/filepath"
"sort"
"strings"
@@ -67,7 +68,8 @@ func RemoveBookmark(bookmark string) (err error) {
continue
}
- if bookmarkAttrVal := node.IALAttr("bookmark"); bookmarkAttrVal == bookmark {
+ // 前端按纯文本回传标签,存储态为转义形态,比较前统一还原
+ if bookmarkAttrVal := node.IALAttr("bookmark"); bookmarkAttrVal == html.UnescapeString(bookmark) {
node.RemoveIALAttr("bookmark")
cache.PutBlockIALInBox(node.ID, tree.Box, parse.IAL2Map(node.KramdownIAL))
changed = true
@@ -139,7 +141,8 @@ func RenameBookmark(oldBookmark, newBookmark string) (err error) {
continue
}
- if bookmarkAttrVal := node.IALAttr("bookmark"); bookmarkAttrVal == oldBookmark {
+ // 前端按纯文本回传旧标签,存储态为转义形态,比较前统一还原
+ if bookmarkAttrVal := node.IALAttr("bookmark"); bookmarkAttrVal == html.UnescapeString(oldBookmark) {
node.SetIALAttr("bookmark", newBookmark)
cache.PutBlockIALInBox(node.ID, tree.Box, parse.IAL2Map(node.KramdownIAL))
changed = true
@@ -245,7 +248,9 @@ func BuildBookmark() (ret *Bookmarks) {
}
}
- label := BookmarkLabel(block.IAL["bookmark"])
+ // 存储态为 HTML 转义形态,统一还原为纯文本:前端按上下文转义展示,
+ // 重命名/删除也按纯文本回传,保证比较一致
+ label := BookmarkLabel(html.UnescapeString(block.IAL["bookmark"]))
if bs, ok := labelBlocks[label]; ok {
bs = append(bs, block)
labelBlocks[label] = bs
diff --git a/kernel/model/escape_contract_test.go b/kernel/model/escape_contract_test.go
new file mode 100644
index 0000000000..3b61bafa51
--- /dev/null
+++ b/kernel/model/escape_contract_test.go
@@ -0,0 +1,71 @@
+// SiYuan - From thought to insight, with agents
+// Copyright (c) 2020-present, b3log.org
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+package model
+
+import (
+ "html"
+ "testing"
+
+ "github.com/88250/lute/ast"
+ "github.com/siyuan-note/siyuan/kernel/util"
+)
+
+// 标签面板的展示文本必须是纯文本:模型层转义存储、API 层还原,前端负责按上下文转义,
+// 否则前端会对已转义文本再转义一次,含 & 或 < 的标签会显示为 a&amp;b
+func TestTagNameIsPlainTextForFrontend(t *testing.T) {
+ for _, label := range []string{"a&b", "a<b", "it's", `a"b`, "plain", "标题"} {
+ tags := buildTags(Tags{}, []string{label}, 0)
+ if 1 > len(tags) {
+ t.Fatalf("buildTags(%q) returned nothing", label)
+ }
+
+ // 模型层保持转义形态
+ if util.EscapeHTML(label) != tags[0].Name {
+ t.Errorf("label=%q modelName=%q, want escaped form", label, tags[0].Name)
+ }
+ // API 层还原为纯文本,前端据此再转义一次
+ if plain := util.UnescapeHTML(tags[0].Name); plain != label {
+ t.Errorf("label=%q plainName=%q, want plain text", label, plain)
+ }
+ }
+}
+
+// 书签面板的展示文本必须是纯文本,且重命名/删除的比较值要与之相等,
+// 否则标签含 & 或 < 的书签无法重命名和删除,且没有任何报错
+func TestBookmarkLabelComparisonIsConsistent(t *testing.T) {
+ for _, label := range []string{"R&D", "a<b", `a"b`, "it's", "plain"} {
+ node := &ast.Node{Type: ast.NodeParagraph}
+ node.SetIALAttr("bookmark", label)
+
+ // BuildBookmark 取 blocks.ial 的原样存储值并还原为纯文本
+ var stored string
+ for _, kv := range node.KramdownIAL {
+ if "bookmark" == kv[0] {
+ stored = kv[1]
+ }
+ }
+ displayed := html.UnescapeString(stored)
+ if displayed != label {
+ t.Errorf("label=%q displayed=%q, want plain text", label, displayed)
+ }
+
+ // 前端按纯文本回传,内核比较前同样还原
+ if got := node.IALAttr("bookmark"); got != html.UnescapeString(displayed) {
+ t.Errorf("label=%q compare=%q displayed=%q, mismatched", label, got, displayed)
+ }
+ }
+}