Skip to content

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

:lock: Reject traversal notebook IDs in setNotebookIcon https://github.com/siyuan-note/siyuan/security/advisories/GHSA-6h8x-6mph-7438

Daniel962695351f2f
diff --git a/kernel/api/notebook.go b/kernel/api/notebook.go
index 766720298a..89f0c56192 100644
--- a/kernel/api/notebook.go
+++ b/kernel/api/notebook.go
@@ -87,6 +87,9 @@ func setNotebookIcon(c *gin.Context) {
) {
return
}
+ if util.InvalidIDPattern(boxID, ret) {
+ return
+ }
if err := holdEncryptedBoxRequest(c, boxID); err != nil {
ret.Code = -1
ret.Msg = model.Conf.Language(314)
diff --git a/kernel/api/notebook_security_test.go b/kernel/api/notebook_security_test.go
index a94ea99b48..bdc3adbc5d 100644
--- a/kernel/api/notebook_security_test.go
+++ b/kernel/api/notebook_security_test.go
@@ -17,6 +17,7 @@
package api
import (
+ "bytes"
"encoding/json"
"net/http"
"net/http/httptest"
@@ -187,6 +188,49 @@ func TestGetNotebookInfoHidesInvisibleNotebookFromReader(t *testing.T) {
}
}
+func TestSetNotebookIconRejectsPathTraversal(t *testing.T) {
+ gin.SetMode(gin.TestMode)
+
+ oldConf, oldDataDir := model.Conf, util.DataDir
+ util.DataDir = t.TempDir()
+ model.Conf = model.NewAppConf()
+ model.Conf.FileTree = conf.NewFileTree()
+ t.Cleanup(func() {
+ model.Conf, util.DataDir = oldConf, oldDataDir
+ })
+
+ escapedDir := filepath.Join(filepath.Dir(util.DataDir), "escaped")
+ body, err := json.Marshal(map[string]string{
+ "notebook": "../" + filepath.Base(escapedDir),
+ "icon": "1f600",
+ })
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ engine := gin.New()
+ engine.POST("/api/notebook/setNotebookIcon", setNotebookIcon)
+
+ recorder := httptest.NewRecorder()
+ request := httptest.NewRequest(http.MethodPost, "/api/notebook/setNotebookIcon", bytes.NewReader(body))
+ request.Header.Set("Content-Type", "application/json")
+ engine.ServeHTTP(recorder, request)
+
+ response := &struct {
+ Code int `json:"code"`
+ Msg string `json:"msg"`
+ }{}
+ if err := json.Unmarshal(recorder.Body.Bytes(), response); err != nil {
+ t.Fatalf("unmarshal response failed: %v", err)
+ }
+ if response.Code != -1 {
+ t.Fatalf("path traversal notebook ID was accepted: %s", recorder.Body.String())
+ }
+ if _, err := os.Stat(filepath.Join(escapedDir, ".siyuan", "conf.json")); !os.IsNotExist(err) {
+ t.Fatalf("conf.json was written outside the workspace: %v", err)
+ }
+}
+
func TestGetNotebookConfHidesEncryptedNotebookFromReader(t *testing.T) {
gin.SetMode(gin.TestMode)
diff --git a/kernel/model/box.go b/kernel/model/box.go
index 746c20c250..81737bc21f 100644
--- a/kernel/model/box.go
+++ b/kernel/model/box.go
@@ -371,6 +371,10 @@ func syncBoxConfCryptoBackup(boxID string, boxConf *conf.BoxConf) error {
}
func (box *Box) saveConf0(data []byte) error {
+ if !ast.IsNodeIDPattern(box.ID) {
+ return fmt.Errorf("invalid box ID [%s]", box.ID)
+ }
+
confPath := filepath.Join(util.DataDir, box.ID, ".siyuan/conf.json")
if err := os.MkdirAll(filepath.Join(util.DataDir, box.ID, ".siyuan"), 0755); err != nil {
return fmt.Errorf("mkdir box conf dir failed: %w", err)
@@ -1144,6 +1148,11 @@ func pushNotebookSortChanged() {
}
func SetBoxIcon(boxID, icon string) {
+ if !ast.IsNodeIDPattern(boxID) {
+ logging.LogErrorf("invalid box ID [%s]", boxID)
+ return
+ }
+
icon = filterBoxIcon(icon)
box := &Box{ID: boxID}
diff --git a/kernel/model/box_icon_test.go b/kernel/model/box_icon_test.go
index 3a01566319..cb60981068 100644
--- a/kernel/model/box_icon_test.go
+++ b/kernel/model/box_icon_test.go
@@ -16,7 +16,15 @@
package model
-import "testing"
+import (
+ "errors"
+ "os"
+ "path/filepath"
+ "testing"
+
+ "github.com/siyuan-note/siyuan/kernel/conf"
+ "github.com/siyuan-note/siyuan/kernel/util"
+)
func TestFilterBoxIconPreservesNetworkURL(t *testing.T) {
for _, icon := range []string{
@@ -65,3 +73,30 @@ func TestFilterBoxIconSanitizesCustomIconFilename(t *testing.T) {
t.Fatalf("unsafe custom icon filename was preserved [%q]", icon)
}
}
+
+func TestSetBoxIconRejectsInvalidBoxID(t *testing.T) {
+ oldDataDir := util.DataDir
+ util.DataDir = t.TempDir()
+ t.Cleanup(func() { util.DataDir = oldDataDir })
+
+ escapedDir := filepath.Join(filepath.Dir(util.DataDir), "escaped")
+ SetBoxIcon("../"+filepath.Base(escapedDir), "1f600")
+ if _, err := os.Stat(filepath.Join(escapedDir, ".siyuan", "conf.json")); !errors.Is(err, os.ErrNotExist) {
+ t.Fatalf("conf.json was written outside the workspace: %v", err)
+ }
+}
+
+func TestSaveConfRejectsInvalidBoxID(t *testing.T) {
+ oldDataDir := util.DataDir
+ util.DataDir = t.TempDir()
+ t.Cleanup(func() { util.DataDir = oldDataDir })
+
+ escapedDir := filepath.Join(filepath.Dir(util.DataDir), "escaped")
+ box := &Box{ID: "../" + filepath.Base(escapedDir)}
+ if err := box.SaveConf(conf.NewBoxConf()); err == nil {
+ t.Fatal("SaveConf accepted a traversal box ID")
+ }
+ if _, err := os.Stat(filepath.Join(escapedDir, ".siyuan", "conf.json")); !errors.Is(err, os.ErrNotExist) {
+ t.Fatalf("conf.json was written outside the workspace: %v", err)
+ }
+}