An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:lock: Hide encrypted notebook key material from readers https://github.com/siyuan-note/siyuan/security/advisories/GHSA-8x84-r2ff-h8pq
Daniel3bc014c7dc32
diff --git a/kernel/api/notebook.go b/kernel/api/notebook.goindex 24cf2f484c..f26361eb33 100644--- a/kernel/api/notebook.go+++ b/kernel/api/notebook.go@@ -330,10 +330,14 @@ func getNotebookConf(c *gin.Context) {return}+ boxConf := box.GetConf()+ if !model.IsAdminRoleContext(c) {+ model.HideBoxConfSecret(boxConf)+ }ret.Data = map[string]any{"box": box.ID,"name": box.Name,- "conf": box.GetConf(),+ "conf": boxConf,}}diff --git a/kernel/api/notebook_security_test.go b/kernel/api/notebook_security_test.gonew file mode 100644index 0000000000..2528d33a76--- /dev/null+++ b/kernel/api/notebook_security_test.go@@ -0,0 +1,115 @@+// SiYuan - Refactor your thinking+// 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 api++import (+ "encoding/json"+ "net/http"+ "net/http/httptest"+ "os"+ "path/filepath"+ "strings"+ "testing"++ "github.com/gin-gonic/gin"+ "github.com/siyuan-note/siyuan/kernel/conf"+ "github.com/siyuan-note/siyuan/kernel/model"+ "github.com/siyuan-note/siyuan/kernel/util"+)++func TestGetNotebookConfHidesBoxCryptFromReader(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+ })++ const boxID = "20260724000000-abcdefg"+ boxConf := conf.NewBoxConf()+ boxConf.Name = "Encrypted notebook"+ boxConf.Encrypted = true+ boxConf.BoxCrypt = &conf.BoxEncryption{+ Spec: 1,+ WrappedDEK: []byte("wrapped-dek"),+ WrapNonce: []byte("wrap-nonce"),+ CreatedAt: 123,+ }+ boxConfPath := filepath.Join(util.DataDir, boxID, ".siyuan", "conf.json")+ if err := os.MkdirAll(filepath.Dir(boxConfPath), 0755); err != nil {+ t.Fatal(err)+ }+ data, err := json.Marshal(boxConf)+ if err != nil {+ t.Fatal(err)+ }+ if err = os.WriteFile(boxConfPath, data, 0644); err != nil {+ t.Fatal(err)+ }++ tests := []struct {+ name string+ role model.Role+ expectBoxCrypt bool+ }{+ {name: "reader", role: model.RoleReader, expectBoxCrypt: false},+ {name: "administrator", role: model.RoleAdministrator, expectBoxCrypt: true},+ }+ for _, test := range tests {+ t.Run(test.name, func(t *testing.T) {+ engine := gin.New()+ engine.Use(func(c *gin.Context) {+ c.Set(model.RoleContextKey, test.role)+ c.Next()+ })+ engine.POST("/api/notebook/getNotebookConf", getNotebookConf)++ recorder := httptest.NewRecorder()+ request := httptest.NewRequest(+ http.MethodPost,+ "/api/notebook/getNotebookConf",+ strings.NewReader(`{"notebook":"`+boxID+`"}`),+ )+ request.Header.Set("Content-Type", "application/json")+ engine.ServeHTTP(recorder, request)++ response := &struct {+ Code int `json:"code"`+ Data struct {+ Conf *conf.BoxConf `json:"conf"`+ } `json:"data"`+ }{}+ if err := json.Unmarshal(recorder.Body.Bytes(), response); err != nil {+ t.Fatalf("unmarshal response failed: %v", err)+ }+ if 0 != response.Code || nil == response.Data.Conf {+ t.Fatalf("unexpected response: %s", recorder.Body.String())+ }+ if response.Data.Conf.Encrypted != boxConf.Encrypted ||+ response.Data.Conf.Name != boxConf.Name ||+ response.Data.Conf.SortMode != boxConf.SortMode {+ t.Fatalf("functional notebook settings were changed: %#v", response.Data.Conf)+ }+ if test.expectBoxCrypt != (nil != response.Data.Conf.BoxCrypt) {+ t.Fatalf("unexpected box crypt visibility: %#v", response.Data.Conf.BoxCrypt)+ }+ })+ }+}diff --git a/kernel/model/conf.go b/kernel/model/conf.goindex 0362ed3bc9..4cca11d32e 100644--- a/kernel/model/conf.go+++ b/kernel/model/conf.go@@ -1255,6 +1255,12 @@ func HideConfSecret(c *AppConf) {c.Sync = &conf.Sync{}c.Secrets = &conf.Secrets{}c.Variables = &conf.Variables{}+ if nil != c.NotebookCrypto {+ c.NotebookCrypto = &conf.NotebookCrypto{+ Enabled: c.NotebookCrypto.Enabled,+ AutoLockMinutes: c.NotebookCrypto.AutoLockMinutes,+ }+ }c.System.AppDir = ""c.System.ConfDir = ""c.System.DataDir = ""@@ -1263,6 +1269,13 @@ func HideConfSecret(c *AppConf) {c.System.NetworkProxy = &conf.NetworkProxy{}}+// HideBoxConfSecret 隐藏笔记本配置中的密钥材料。+func HideBoxConfSecret(c *conf.BoxConf) {+ if nil != c {+ c.BoxCrypt = nil+ }+}+func clearPortJSON() {pid := fmt.Sprintf("%d", os.Getpid())portJSON := filepath.Join(util.HomeDir, ".config", "siyuan", "port.json")diff --git a/kernel/model/conf_secret_test.go b/kernel/model/conf_secret_test.gonew file mode 100644index 0000000000..d9ea1ac2db--- /dev/null+++ b/kernel/model/conf_secret_test.go@@ -0,0 +1,94 @@+// SiYuan - Refactor your thinking+// 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 (+ "testing"++ "github.com/siyuan-note/siyuan/kernel/conf"+ "github.com/siyuan-note/siyuan/kernel/util"+)++func TestHideConfSecretPreservesNotebookCryptoSettings(t *testing.T) {+ appConf := NewAppConf()+ appConf.System = &conf.System{}+ appConf.NotebookCrypto = &conf.NotebookCrypto{+ Enabled: true,+ MasterSalt: []byte("master-salt"),+ KDFParams: util.DefaultArgon2Params(),+ KEKVerifier: []byte("kek-verifier"),+ VerifierNonce: []byte("verifier-nonce"),+ AutoLockMinutes: 17,+ Spec: conf.CurrentNotebookCryptoSpec,+ BackupID: "backup-id",+ CreatedAt: 123,+ Checksum: "checksum",+ KEKMAC: []byte("kek-mac"),+ }++ HideConfSecret(appConf)++ notebookCrypto := appConf.NotebookCrypto+ if nil == notebookCrypto {+ t.Fatal("notebook crypto settings should be preserved")+ }+ if !notebookCrypto.Enabled || 17 != notebookCrypto.AutoLockMinutes {+ t.Fatalf("functional notebook crypto settings were changed: %#v", notebookCrypto)+ }+ if 0 < len(notebookCrypto.MasterSalt) ||+ (util.Argon2Params{} != notebookCrypto.KDFParams) ||+ 0 < len(notebookCrypto.KEKVerifier) ||+ 0 < len(notebookCrypto.VerifierNonce) ||+ 0 != notebookCrypto.Spec ||+ "" != notebookCrypto.BackupID ||+ 0 != notebookCrypto.CreatedAt ||+ "" != notebookCrypto.Checksum ||+ 0 < len(notebookCrypto.KEKMAC) {+ t.Fatalf("notebook crypto key material was not hidden: %#v", notebookCrypto)+ }+}++func TestHideBoxConfSecretPreservesNotebookSettings(t *testing.T) {+ boxConf := &conf.BoxConf{+ Name: "Encrypted notebook",+ Sort: 7,+ Icon: "1f512",+ Closed: true,+ SortMode: util.SortModeCustom,+ Encrypted: true,+ BoxCrypt: &conf.BoxEncryption{+ Spec: 1,+ WrappedDEK: []byte("wrapped-dek"),+ WrapNonce: []byte("wrap-nonce"),+ CreatedAt: 123,+ },+ }++ HideBoxConfSecret(boxConf)++ if nil != boxConf.BoxCrypt {+ t.Fatalf("wrapped notebook key was not hidden: %#v", boxConf.BoxCrypt)+ }+ if "Encrypted notebook" != boxConf.Name ||+ 7 != boxConf.Sort ||+ "1f512" != boxConf.Icon ||+ !boxConf.Closed ||+ util.SortModeCustom != boxConf.SortMode ||+ !boxConf.Encrypted {+ t.Fatalf("functional notebook settings were changed: %#v", boxConf)+ }+}