Skip to content

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

:bug: Avoid concurrent bazaar README corruption

Daniel7ff6d3024d17
diff --git a/kernel/bazaar/readme.go b/kernel/bazaar/readme.go
index d2c9f13ad1..4261376071 100644
--- a/kernel/bazaar/readme.go
+++ b/kernel/bazaar/readme.go
@@ -107,7 +107,7 @@ func getInstalledPackageREADME(installPath, linkBase string, readme LocaleString
// renderPackageREADME 渲染 README Markdown 为 HTML。
func renderPackageREADME(linkBase string, mdData []byte) (ret string) {
- mdData = bytes.TrimPrefix(mdData, []byte("\xef\xbb\xbf")) // 移除文件开头的 BOM
+ mdData = bytes.Clone(bytes.TrimPrefix(mdData, []byte("\xef\xbb\xbf"))) // 移除文件开头的 BOM 并隔离解析缓冲区
luteEngine := lute.New()
luteEngine.SetSanitize(true)
luteEngine.SetSoftBreak2HardBreak(false)
diff --git a/kernel/bazaar/readme_test.go b/kernel/bazaar/readme_test.go
new file mode 100644
index 0000000000..e98e3b38be
--- /dev/null
+++ b/kernel/bazaar/readme_test.go
@@ -0,0 +1,85 @@
+// 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 bazaar
+
+import (
+ "bytes"
+ "sync"
+ "testing"
+)
+
+func TestRenderPackageREADMEDoesNotMutateInput(t *testing.T) {
+ source := []byte(`<div style="display: flex;">
+ <!-- 按钮:感谢您的支持 -->
+ <a href="https://example.com">❤️ 感谢您的支持</a>
+</div>
+
+</br>
+
+## 更新日志
+
+> 如果未检测到旧数据,请重新导入。
+`)
+ backing := bytes.Repeat([]byte{0xA5}, len(source)+1024)
+ markdown := backing[:len(source)]
+ copy(markdown, source)
+ original := bytes.Clone(backing)
+
+ renderPackageREADME("https://example.com/package", markdown)
+
+ if !bytes.Equal(original, backing) {
+ t.Fatal("rendering package README mutated the input backing array")
+ }
+}
+
+func TestRenderPackageREADMEConcurrently(t *testing.T) {
+ source := []byte(`<div style="display: flex;">
+ <!-- 按钮:感谢您的支持 -->
+ <a href="https://example.com">❤️ 感谢您的支持</a>
+</div>
+
+</br>
+
+## 更新日志
+
+> 如果未检测到旧数据,请重新导入。
+`)
+ backing := make([]byte, len(source), len(source)+1024)
+ copy(backing, source)
+ markdown := backing[:len(source)]
+ expected := renderPackageREADME("https://example.com/package", bytes.Clone(markdown))
+ const workers = 32
+ var waitGroup sync.WaitGroup
+ waitGroup.Add(workers)
+ errors := make(chan string, workers)
+ for range workers {
+ go func() {
+ defer waitGroup.Done()
+ for range 20 {
+ if actual := renderPackageREADME("https://example.com/package", markdown); actual != expected {
+ errors <- actual
+ return
+ }
+ }
+ }()
+ }
+ waitGroup.Wait()
+ close(errors)
+ if actual, ok := <-errors; ok {
+ t.Fatalf("concurrent README rendering returned corrupted HTML:\n%s", actual)
+ }
+}