Skip to content

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

:bug: Avoid leaking inline attributes from pasted HTML https://github.com/siyuan-note/siyuan/issues/19155

Daniel50e0bb479acb
diff --git a/kernel/api/lute.go b/kernel/api/lute.go
index 2d51fb4f68..cde7112fdf 100644
--- a/kernel/api/lute.go
+++ b/kernel/api/lute.go
@@ -269,6 +269,7 @@ func html2BlockDOM(c *gin.Context) {
parse.TextMarks2Inlines(tree) // 先将 TextMark 转换为 Inlines https://github.com/siyuan-note/siyuan/issues/13056
parse.NestedInlines2FlattedSpansHybrid(tree, false)
+ removeWhitespaceTextMarkStyles(tree)
md, err := lute.FormatNodeSync(tree.Root, luteEngine.ParseOptions, luteEngine.RenderOptions)
if nil != err {
@@ -296,6 +297,23 @@ func html2BlockDOM(c *gin.Context) {
}
}
+func removeWhitespaceTextMarkStyles(tree *parse.Tree) {
+ var unlinks []*ast.Node
+ ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
+ if entering && n.Type == ast.NodeTextMark && strings.TrimSpace(n.TextMarkTextContent) == "" {
+ n.RemoveIALAttr("style")
+ // 空白文本无法绑定 Kramdown 行内属性,移除样式属性节点以免渲染为正文。
+ if n.Next != nil && n.Next.Type == ast.NodeKramdownSpanIAL && parse.IALVal(n.Next, "style") != "" {
+ unlinks = append(unlinks, n.Next)
+ }
+ }
+ return ast.WalkContinue
+ })
+ for _, n := range unlinks {
+ n.Unlink()
+ }
+}
+
func normalizeIFramePosition(node *ast.Node) {
// iframe 的绝对或固定定位依赖原网页容器,粘贴后需让块保留在文档流中。
if isOutOfFlowPosition(node.IALAttr("style")) {
diff --git a/kernel/api/lute_text_style_test.go b/kernel/api/lute_text_style_test.go
index f450e09317..1d79c0a835 100644
--- a/kernel/api/lute_text_style_test.go
+++ b/kernel/api/lute_text_style_test.go
@@ -68,6 +68,9 @@ func TestHTML2BlockDOMQuotedFontFamily(t *testing.T) {
styled, bold, link := 0, false, false
ast.Walk(engine.BlockDOM2Tree(response.Data).Root, func(n *ast.Node, entering bool) ast.WalkStatus {
if entering && n.Type == ast.NodeTextMark {
+ if strings.TrimSpace(n.TextMarkTextContent) == "" {
+ return ast.WalkContinue
+ }
style := n.IALAttr("style")
for _, want := range []string{`font-family: "Mona Sans VF", "Segoe UI", Arial;`, "color: rgb(31, 35, 40);", "background-color: white;", "font-size: 14.000000px;"} {
if !strings.Contains(style, want) {
@@ -124,3 +127,42 @@ func TestHTML2BlockDOMMatchesElementsByDefault(t *testing.T) {
t.Fatalf("source appearance survived: %s", response.Data)
}
}
+
+func TestHTML2BlockDOMDoesNotLeakWhitespaceTextMarkStyles(t *testing.T) {
+ originalConf := model.Conf
+ model.Conf = model.NewAppConf()
+ model.Conf.System = &conf.System{}
+ t.Cleanup(func() { model.Conf = originalConf })
+ gin.SetMode(gin.TestMode)
+ input := `<h3 style="color: rgb(31, 35, 40); font-family: Arial; font-size: 20px; font-weight: 600">` +
+ `author<span>&nbsp;</span>commented<span>&nbsp;</span><relative-time><span>now</span></relative-time></h3>`
+ body, err := json.Marshal(map[string]any{
+ "dom": input, "skipBase64Assets": true, "skipInlineSVGAssets": true, "preserveSourceFormat": true,
+ })
+ if err != nil {
+ t.Fatal(err)
+ }
+ recorder := httptest.NewRecorder()
+ context, _ := gin.CreateTestContext(recorder)
+ context.Request = httptest.NewRequest(http.MethodPost, "/api/lute/html2BlockDOM", strings.NewReader(string(body)))
+ context.Request.Header.Set("Content-Type", "application/json")
+ html2BlockDOM(context)
+ var response struct {
+ Code int
+ Data string
+ }
+ if err = json.Unmarshal(recorder.Body.Bytes(), &response); err != nil || response.Code != 0 {
+ t.Fatalf("conversion failed: %s, %v", recorder.Body.String(), err)
+ }
+ if strings.Contains(response.Data, "{: style=") {
+ t.Fatalf("leaked attributes: %s", response.Data)
+ }
+ if !strings.Contains(response.Data, `style="color: rgb(31, 35, 40);font-family: Arial;font-size: 20.000000px;"`) {
+ t.Fatalf("lost visible source formatting: %s", response.Data)
+ }
+ for _, text := range []string{"author", "commented", "now"} {
+ if !strings.Contains(response.Data, text) {
+ t.Fatalf("lost %q: %s", text, response.Data)
+ }
+ }
+}